diff --git a/piano.py b/piano.py index 42ed849..28047cb 100644 --- a/piano.py +++ b/piano.py @@ -1,26 +1,26 @@ from time import sleep from telemetrix_rpi_pico import telemetrix_rpi_pico from siwatlib.debounce import debounce -from siwatlib.sound import beep +from siwatlib.sound import piano_key import winsound index_shift = 2 -freqs = [261.6256,293.6648,329.6276,349.2282,391.9954,440.0000,493.8833,523.2511] - debouncers = [] def handle_button_press(data): - freq = freqs[data[1]-index_shift] - print(freq) - recieved_state = not data[2] - if debouncers[data[1]-index_shift].set_state(state=recieved_state): - beep(frequency=freq,duration=0.25) + try: + print(str(data[1])+' Pressed') + recieved_state = not data[2] + if debouncers[data[1]-index_shift].set_state(state=recieved_state): + piano_key((data[1]-1)*2-1) + except KeyError: + pass mcu = telemetrix_rpi_pico.TelemetrixRpiPico(com_port='COM10') for i in range(2,10): - debouncers.append(debounce(state=False,debounce_time=0.15)) + debouncers.append(debounce(state=False,debounce_time=0.2)) mcu.set_pin_mode_digital_input(i,callback=handle_button_press) while True: sleep(100) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index fca2e79..a0b5a4c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -telemetrix-rpi-pico \ No newline at end of file +telemetrix-rpi-pico +playsound==1.2.2 \ No newline at end of file diff --git a/siwatlib/piano_sample/key01.mp3 b/siwatlib/piano_sample/key01.mp3 new file mode 100644 index 0000000..03185fa Binary files /dev/null and b/siwatlib/piano_sample/key01.mp3 differ diff --git a/siwatlib/piano_sample/key02.mp3 b/siwatlib/piano_sample/key02.mp3 new file mode 100644 index 0000000..b5fa0a0 Binary files /dev/null and b/siwatlib/piano_sample/key02.mp3 differ diff --git a/siwatlib/piano_sample/key03.mp3 b/siwatlib/piano_sample/key03.mp3 new file mode 100644 index 0000000..09fe479 Binary files /dev/null and b/siwatlib/piano_sample/key03.mp3 differ diff --git a/siwatlib/piano_sample/key04.mp3 b/siwatlib/piano_sample/key04.mp3 new file mode 100644 index 0000000..28b0911 Binary files /dev/null and b/siwatlib/piano_sample/key04.mp3 differ diff --git a/siwatlib/piano_sample/key05.mp3 b/siwatlib/piano_sample/key05.mp3 new file mode 100644 index 0000000..ec54d1d Binary files /dev/null and b/siwatlib/piano_sample/key05.mp3 differ diff --git a/siwatlib/piano_sample/key06.mp3 b/siwatlib/piano_sample/key06.mp3 new file mode 100644 index 0000000..599fef1 Binary files /dev/null and b/siwatlib/piano_sample/key06.mp3 differ diff --git a/siwatlib/piano_sample/key07.mp3 b/siwatlib/piano_sample/key07.mp3 new file mode 100644 index 0000000..5bb3be0 Binary files /dev/null and b/siwatlib/piano_sample/key07.mp3 differ diff --git a/siwatlib/piano_sample/key08.mp3 b/siwatlib/piano_sample/key08.mp3 new file mode 100644 index 0000000..b135161 Binary files /dev/null and b/siwatlib/piano_sample/key08.mp3 differ diff --git a/siwatlib/piano_sample/key09.mp3 b/siwatlib/piano_sample/key09.mp3 new file mode 100644 index 0000000..5cb72ca Binary files /dev/null and b/siwatlib/piano_sample/key09.mp3 differ diff --git a/siwatlib/piano_sample/key10.mp3 b/siwatlib/piano_sample/key10.mp3 new file mode 100644 index 0000000..a3624f8 Binary files /dev/null and b/siwatlib/piano_sample/key10.mp3 differ diff --git a/siwatlib/piano_sample/key11.mp3 b/siwatlib/piano_sample/key11.mp3 new file mode 100644 index 0000000..8c828d2 Binary files /dev/null and b/siwatlib/piano_sample/key11.mp3 differ diff --git a/siwatlib/piano_sample/key12.mp3 b/siwatlib/piano_sample/key12.mp3 new file mode 100644 index 0000000..9fb18fd Binary files /dev/null and b/siwatlib/piano_sample/key12.mp3 differ diff --git a/siwatlib/piano_sample/key13.mp3 b/siwatlib/piano_sample/key13.mp3 new file mode 100644 index 0000000..b74d6b5 Binary files /dev/null and b/siwatlib/piano_sample/key13.mp3 differ diff --git a/siwatlib/piano_sample/key14.mp3 b/siwatlib/piano_sample/key14.mp3 new file mode 100644 index 0000000..10ab2ed Binary files /dev/null and b/siwatlib/piano_sample/key14.mp3 differ diff --git a/siwatlib/piano_sample/key15.mp3 b/siwatlib/piano_sample/key15.mp3 new file mode 100644 index 0000000..9b69134 Binary files /dev/null and b/siwatlib/piano_sample/key15.mp3 differ diff --git a/siwatlib/piano_sample/key16.mp3 b/siwatlib/piano_sample/key16.mp3 new file mode 100644 index 0000000..f829168 Binary files /dev/null and b/siwatlib/piano_sample/key16.mp3 differ diff --git a/siwatlib/piano_sample/key17.mp3 b/siwatlib/piano_sample/key17.mp3 new file mode 100644 index 0000000..225a7ba Binary files /dev/null and b/siwatlib/piano_sample/key17.mp3 differ diff --git a/siwatlib/piano_sample/key18.mp3 b/siwatlib/piano_sample/key18.mp3 new file mode 100644 index 0000000..568ae9a Binary files /dev/null and b/siwatlib/piano_sample/key18.mp3 differ diff --git a/siwatlib/piano_sample/key19.mp3 b/siwatlib/piano_sample/key19.mp3 new file mode 100644 index 0000000..55cf15d Binary files /dev/null and b/siwatlib/piano_sample/key19.mp3 differ diff --git a/siwatlib/piano_sample/key20.mp3 b/siwatlib/piano_sample/key20.mp3 new file mode 100644 index 0000000..5f18bf4 Binary files /dev/null and b/siwatlib/piano_sample/key20.mp3 differ diff --git a/siwatlib/piano_sample/key21.mp3 b/siwatlib/piano_sample/key21.mp3 new file mode 100644 index 0000000..c0b4b64 Binary files /dev/null and b/siwatlib/piano_sample/key21.mp3 differ diff --git a/siwatlib/piano_sample/key22.mp3 b/siwatlib/piano_sample/key22.mp3 new file mode 100644 index 0000000..c419f88 Binary files /dev/null and b/siwatlib/piano_sample/key22.mp3 differ diff --git a/siwatlib/piano_sample/key23.mp3 b/siwatlib/piano_sample/key23.mp3 new file mode 100644 index 0000000..9e5680a Binary files /dev/null and b/siwatlib/piano_sample/key23.mp3 differ diff --git a/siwatlib/piano_sample/key24.mp3 b/siwatlib/piano_sample/key24.mp3 new file mode 100644 index 0000000..2bb7fa2 Binary files /dev/null and b/siwatlib/piano_sample/key24.mp3 differ diff --git a/siwatlib/sound.py b/siwatlib/sound.py index 71ae48a..796163f 100644 --- a/siwatlib/sound.py +++ b/siwatlib/sound.py @@ -1,6 +1,8 @@ from pysine import pysine import winsound import threading +from playsound import playsound +import os MODE = 'pysine' @@ -23,5 +25,17 @@ def beep(frequency: float, duration: float): kwargs={'frequency': frequency, 'duration': duration} ) __beeper__.start() - - + +#TODO Tune the piano + +def __piano_key_thread__(note: str): + note = format(note,'02') + soundfile = os.path.dirname(__file__)+'\\piano_sample\\key'+note+'.mp3' + print(soundfile) + playsound(soundfile) + +def piano_key(note: str): + threading.Thread(target=__piano_key_thread__, + kwargs={'note': note} + ).start() + diff --git a/test.py b/test.py new file mode 100644 index 0000000..0c02b22 --- /dev/null +++ b/test.py @@ -0,0 +1,2 @@ +from siwatlib.sound import piano_key +piano_key(4) \ No newline at end of file