Codec Zero sound card
This commit is contained in:
26
.asoundrc
26
.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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
[Unit]
|
||||
Description=Dog Trainer
|
||||
After=sound.target
|
||||
After=sound-init.service
|
||||
Requires=sound-init.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
|
||||
BIN
recording_17.wav
BIN
recording_17.wav
Binary file not shown.
BIN
recording_22.wav
Normal file
BIN
recording_22.wav
Normal file
Binary file not shown.
BIN
recording_23.wav
BIN
recording_23.wav
Binary file not shown.
BIN
recording_27.wav
Normal file
BIN
recording_27.wav
Normal file
Binary file not shown.
14
sound-init.service
Normal file
14
sound-init.service
Normal 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
|
||||
Reference in New Issue
Block a user