Created
February 9, 2024 15:23
-
-
Save JnBrymn/6c07cbc778a2b534a74f62c1f2565270 to your computer and use it in GitHub Desktop.
This is CircuitPython for the CircuitPlaygroundExpress that allows you to record music by pushing the A button and then touching the capacitive sensors. And then you can play the song by pushing the B button.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("STARTED") | |
import time | |
from adafruit_circuitplayground import cp | |
notes = { | |
"C": { | |
"frequency": 523.25, | |
"color": (0, 0, 3) | |
}, | |
"D": { | |
"frequency": 587.33, | |
"color": (0, 3, 0) | |
}, | |
"E": { | |
"frequency": 659.25, | |
"color": (3, 0, 0) | |
}, | |
"F": { | |
"frequency": 698.46, | |
"color": (3, 3, 0) | |
}, | |
"G": { | |
"frequency": 783.99, | |
"color": (3, 0, 3) | |
}, | |
"A": { | |
"frequency": 880.00, | |
"color": (0, 3, 3) | |
}, | |
"B": { | |
"frequency": 987.77, | |
"color": (3, 3, 3) | |
}, | |
} | |
duration = 0.1 # seconds | |
class Recorder: | |
def __init__(self): | |
self.notes = [] | |
self.start_times = [] | |
self.end_times = [] | |
self.rec_start_time = None | |
self.note_pushed = False | |
def start_recording(self): | |
self.notes = [] | |
self.start_times = [] | |
self.end_times = [] | |
self.rec_start_time = time.monotonic() | |
def start_note(self, note): | |
if not self.rec_start_time: | |
return | |
if self.note_pushed: | |
return | |
self.note_pushed = True | |
self.notes.append(note) | |
self.start_times.append(time.monotonic()) | |
cp.start_tone(note["frequency"]) | |
cp.pixels.fill(note["color"]) | |
def stop_note(self): | |
if not self.rec_start_time: | |
return | |
if not self.note_pushed: | |
return | |
self.note_pushed = False | |
self.end_times.append(time.monotonic()) | |
cp.stop_tone() | |
cp.pixels.fill((0, 0, 0)) | |
def play_song(self): | |
self.rec_start_time = None | |
print("Playing song") | |
last_end = self.start_times[0] | |
for note, start, end in zip(self.notes, self.start_times, self.end_times): | |
time.sleep(start-last_end) | |
cp.start_tone(note["frequency"]) | |
cp.pixels.fill(note["color"]) | |
time.sleep(end - start) | |
cp.stop_tone() | |
cp.pixels.fill((0, 0, 0)) | |
last_end = end | |
rec = Recorder() | |
while True: | |
if cp.button_a: | |
rec.start_recording() | |
elif cp.button_b: | |
rec.play_song() | |
elif cp.touch_A1: | |
rec.start_note(notes["C"]) | |
elif cp.touch_A2: | |
rec.start_note(notes["D"]) | |
elif cp.touch_A3: | |
rec.start_note(notes["E"]) | |
elif cp.touch_A4: | |
rec.start_note(notes["F"]) | |
elif cp.touch_A5: | |
rec.start_note(notes["G"]) | |
elif cp.touch_A6: | |
rec.start_note(notes["A"]) | |
elif cp.touch_A7: | |
rec.start_note(notes["B"]) | |
else: | |
rec.stop_note() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment