Compare commits

...

2 Commits

Author SHA1 Message Date
cpu
74aea45d73 Merge remote-tracking branch 'refs/remotes/origin/main' 2023-12-09 02:08:59 +00:00
cpu
42f9c8f94d Codec Zero sound card 2023-12-09 01:35:25 +00:00
9 changed files with 41 additions and 37 deletions

View File

@@ -1,17 +1,13 @@
pcm.my_sound_device { #pcm.!default {
type hw # type hw
card USB # card Zero
# card PCH ## card USB
# card Headphones #}
device 0
pcm.hw0mix {
type dmix
ipc_key 1939 # must be unique
slave { pcm "hw:Zero" }
} }
pcm.my_sound_device_with_converter { pcm.!default hw0mix
type plug
slave {
pcm my_sound_device
format S16_LE
channels 2
rate 48000
}
}

BIN
beep.wav

Binary file not shown.

View File

@@ -4,16 +4,13 @@ from datetime import datetime
import subprocess import subprocess
import sys import sys
# PLAY_DEVICE = "default" # Playback/recording through the PulseAudio sound server PLAY_DEVICE = "default" # as defined in '~/.asoundrc'
# REC_DEVICE = "default" # Playback/recording through the PulseAudio sound server REC_DEVICE = "plughw:Zero" # as listed by 'arecord -L'
PLAY_DEVICE = "my_sound_device_with_converter" # as defined in '~/.asoundrc'
REC_DEVICE = "my_sound_device" # as defined in '~/.asoundrc'
MAX_RECORDING_TIME = 30 # limit recordings to only e.g. 30 seconds long 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 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.setmode(GPIO.BCM) # use BCM pin layout
GPIO.setwarnings(False) GPIO.setwarnings(False)
@@ -40,8 +37,7 @@ def terminate_recording(proc, pin):
"-of", "-of",
"default=noprint_wrappers=1:nokey=1", "default=noprint_wrappers=1:nokey=1",
f"recording_temp_{pin}.wav", f"recording_temp_{pin}.wav",
], ], check=True, stdout=subprocess.PIPE,
stdout=subprocess.PIPE,
).stdout.decode("utf-8") ).stdout.decode("utf-8")
subprocess.call( subprocess.call(
[ [
@@ -66,7 +62,8 @@ def terminate_recording(proc, pin):
def play(file): 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): def play_sound(pin):
@@ -86,20 +83,17 @@ def record_sound(pin):
proc = subprocess.Popen( proc = subprocess.Popen(
[ [
"arecord", "arecord",
"-D", f"-D{REC_DEVICE}",
REC_DEVICE, "-fS16_LE",
"-f", "-r48000",
"S16_LE", "-c2",
"-r", f"-d{MAX_RECORDING_TIME}",
"16000",
"-c",
"1",
"-d",
f"{MAX_RECORDING_TIME}",
f"recording_temp_{pin}.wav", f"recording_temp_{pin}.wav",
] ]
) )
print(f"Recording in progress (max {MAX_RECORDING_TIME} seconds)...") 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) wait_for_button_release_or_timeout(pin, MAX_RECORDING_TIME)
print("GPIO went HIGH - terminating recording") print("GPIO went HIGH - terminating recording")
play_beep() play_beep()
@@ -121,7 +115,6 @@ def handle_pin(pin):
if time.time() - timeout_start >= RECORD_BUTTON_PRESS_TIME: if time.time() - timeout_start >= RECORD_BUTTON_PRESS_TIME:
# Pin was LOW for longer time; recording # Pin was LOW for longer time; recording
play_beep()
record_sound(pin) record_sound(pin)
else: else:
# Pin was LOW for shorter time; playing # Pin was LOW for shorter time; playing

View File

@@ -1,6 +1,7 @@
[Unit] [Unit]
Description=Dog Trainer Description=Dog Trainer
After=sound.target After=sound-init.service
Requires=sound-init.service
[Service] [Service]
Type=simple Type=simple

Binary file not shown.

BIN
recording_22.wav Normal file

Binary file not shown.

Binary file not shown.

BIN
recording_27.wav Normal file

Binary file not shown.

14
sound-init.service Normal file
View File

@@ -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