diff --git a/.asoundrc b/.asoundrc index c4afcdb..4532266 100644 --- a/.asoundrc +++ b/.asoundrc @@ -1,17 +1,13 @@ -pcm.my_sound_device { - type hw - card USB -# card PCH -# card Headphones - device 0 +#pcm.!default { +# type hw +# card Zero +## card USB +#} + +pcm.hw0mix { + type dmix + ipc_key 1939 # must be unique + slave { pcm "hw:Zero" } } -pcm.my_sound_device_with_converter { - type plug - slave { - pcm my_sound_device - format S16_LE - channels 2 - rate 48000 - } -} +pcm.!default hw0mix diff --git a/beep.wav b/beep.wav index d1b259e..92fd3d9 100644 Binary files a/beep.wav and b/beep.wav differ diff --git a/dog-trainer.py b/dog-trainer.py index 4b6bcfc..7e1def2 100755 --- a/dog-trainer.py +++ b/dog-trainer.py @@ -4,16 +4,13 @@ from datetime import datetime import subprocess import sys -# PLAY_DEVICE = "default" # Playback/recording through the PulseAudio sound server -# REC_DEVICE = "default" # Playback/recording through the PulseAudio sound server - -PLAY_DEVICE = "my_sound_device_with_converter" # as defined in '~/.asoundrc' -REC_DEVICE = "my_sound_device" # as defined in '~/.asoundrc' +PLAY_DEVICE = "default" # as defined in '~/.asoundrc' +REC_DEVICE = "plughw:Zero" # as listed by 'arecord -L' MAX_RECORDING_TIME = 30 # limit recordings to only e.g. 30 seconds long -TRIM_RECORDING_END = 0.8 # remove the clicking noise from the end of recordings +TRIM_RECORDING_END = 0.6 # remove the clicking noise from the end of recordings RECORD_BUTTON_PRESS_TIME = 2 # pressing and holding a button longer then e.g. 2 seconds triggers recording otherwise playing -PINS = [17, 23] # listen to changes on this GPIOs e.g. [4, 17, 23, 0, 5, 6] +PINS = [17, 22, 27] # listen to changes on this GPIOs e.g. [4, 17, 23, 0, 5, 6] GPIO.setmode(GPIO.BCM) # use BCM pin layout GPIO.setwarnings(False) @@ -40,8 +37,7 @@ def terminate_recording(proc, pin): "-of", "default=noprint_wrappers=1:nokey=1", f"recording_temp_{pin}.wav", - ], - stdout=subprocess.PIPE, + ], check=True, stdout=subprocess.PIPE, ).stdout.decode("utf-8") subprocess.call( [ @@ -66,7 +62,8 @@ def terminate_recording(proc, pin): def play(file): - subprocess.call(["aplay", "-D", PLAY_DEVICE, file]) + print(f"Playing {file}") + subprocess.run(["aplay", f"-D{PLAY_DEVICE}", file], check=True) def play_sound(pin): @@ -86,20 +83,17 @@ def record_sound(pin): proc = subprocess.Popen( [ "arecord", - "-D", - REC_DEVICE, - "-f", - "S16_LE", - "-r", - "16000", - "-c", - "1", - "-d", - f"{MAX_RECORDING_TIME}", + f"-D{REC_DEVICE}", + "-fS16_LE", + "-r48000", + "-c2", + f"-d{MAX_RECORDING_TIME}", f"recording_temp_{pin}.wav", ] ) print(f"Recording in progress (max {MAX_RECORDING_TIME} seconds)...") + time.sleep(0.5) + play_beep() wait_for_button_release_or_timeout(pin, MAX_RECORDING_TIME) print("GPIO went HIGH - terminating recording") play_beep() @@ -121,7 +115,6 @@ def handle_pin(pin): if time.time() - timeout_start >= RECORD_BUTTON_PRESS_TIME: # Pin was LOW for longer time; recording - play_beep() record_sound(pin) else: # Pin was LOW for shorter time; playing diff --git a/dog-trainer.service b/dog-trainer.service index 90c1040..4de0adb 100644 --- a/dog-trainer.service +++ b/dog-trainer.service @@ -1,6 +1,7 @@ [Unit] Description=Dog Trainer -After=sound.target +After=sound-init.service +Requires=sound-init.service [Service] Type=simple diff --git a/recording_17.wav b/recording_17.wav index c95d75a..f5a5af9 100644 Binary files a/recording_17.wav and b/recording_17.wav differ diff --git a/recording_22.wav b/recording_22.wav new file mode 100644 index 0000000..3784ea2 Binary files /dev/null and b/recording_22.wav differ diff --git a/recording_23.wav b/recording_23.wav deleted file mode 100644 index a1eddc3..0000000 Binary files a/recording_23.wav and /dev/null differ diff --git a/recording_27.wav b/recording_27.wav new file mode 100644 index 0000000..bd7bf33 Binary files /dev/null and b/recording_27.wav differ diff --git a/sound-init.service b/sound-init.service new file mode 100644 index 0000000..540de20 --- /dev/null +++ b/sound-init.service @@ -0,0 +1,14 @@ +[Unit] +Description=Sound Initialization Service +PartOf=dog-trainer.service + +[Service] +Type=simple +User=pi +Group=pi +ExecStart=/bin/sh -c 'cat /dev/zero | aplay -Ddefault -fS16_LE -c2 -r 48000' +Restart=always +RestartSec=3 + +[Install] +WantedBy=multi-user.target