Compare commits

...

5 Commits

Author SHA1 Message Date
cpu
5016ca5567 added sudo 2023-12-30 13:23:13 +01:00
cpu
a22b2158e1 fixed typos 2023-12-10 00:04:44 +01:00
cpu
00e3d27774 updated with Codec Zero 2023-12-09 03:22:35 +01:00
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
10 changed files with 62 additions and 48 deletions

View File

@@ -1,17 +1,7 @@
pcm.my_sound_device {
type hw
card USB
# card PCH
# card Headphones
device 0
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

View File

@@ -12,7 +12,12 @@ Hardware
- Buttons connected to the Raspberry pi GPIO as shown in the schema to debounce noisy contacts
![dog-trainer-schema](assets/dog-trainer-schema.png)
- USB speaker with a microphone e.g. [Jabra Speak 410](https://www.jabra.com/_/media/Jabra_VXi_Product-Documentation/Jabra-SPEAK-410-Series/technical-specifications/RevB/Jabra-Speak-410-Techsheet-A4-290420-Web.pdf)
- USB speaker with a microphone e.g. [Jabra Speak 410](https://www.jabra.com/_/media/Jabra_VXi_Product-Documentation/Jabra-SPEAK-410-Series/technical-specifications/RevB/Jabra-Speak-410-Techsheet-A4-290420-Web.pdf) or [Codec Zero](https://www.raspberrypi.com/products/codec-zero/)
- for the `Codec Zero` make follwoing changes
> `sudo nano /boot/config.txt`
`#dtparam=audio=on`
`dtoverlay=rpi-codeczero`
- microSD card 8GB
@@ -22,7 +27,7 @@ Operating system
----------------
Install [Raspberry Pi Imager](https://www.raspberrypi.com/news/raspberry-pi-imager-imaging-utility/) into your notebook. Launch it and choose `Raspberry Pi OS Lite` to write into the microSD card
Power on the Raspberry pi and finish the installation process. Log into its console using either:
Power on the `Raspberry Pi` and finish the installation process. Log into its console using either:
- ethernet/wifi from your notebook: `ssh pi@raspberrypi`
- connected keyboard and monitor
- serial port
@@ -33,7 +38,7 @@ Once you are logged into the `Raspberry pi` console update your system
> `sudo apt update`
`sudo apt upgrade`
and install `git`. It will be used to download the `dog-trainer`
Install `git`. It will be used to download the `dog-trainer`
> `sudo apt install -y git`
Recordings must be edited a little bit therefore some audio processing tool is needed. Install `ffmpeg` which will do the job
@@ -52,13 +57,17 @@ Go into the newly created directory
Configuration
-------------
First try using default audio device for playback and recording. Edit `dog-trainer.py` and activate `PLAY_DEVICE="default"` and `REC_DEVICE="default"`
Check your Raspberry pi board pin layout and make sure your buttons GPIOs match `PINS = [17, 22, 27]` from `dog-trainer.py`
> `pinout`
Try using default audio device for playback and recording. Edit `dog-trainer.py` and activate `PLAY_DEVICE="default"` and `REC_DEVICE="default"`
> `nano dog-trainer.py`
Skip to the next section `Run it!`. If either playback or recording does not work check your sound cards
> `aplay -L | grep ^hw:CARD=`
> `aplay -L | grep ^hw:CARD=`
`arecord -L | grep ^hw:CARD=`
If your sound device is other then `USB` then change the line `card USB` in ALSA config
Put your sound device name `"hw:Zero"` or `"hw:USB"` into the ALSA config file
> `nano .asoundrc`
Install the ALSA config file
@@ -82,14 +91,18 @@ If your username and group is other then `pi` and `pi` then also run
> `sed -i "s#User=pi#User=$USER#" dog-trainer.service`
`sed -i "s#Group=pi#Group=$(id -gn)#" dog-trainer.service`
Install the systemd service file
> `sudo mv dog-trainer.service /etc/systemd/system/`
> `sed -i "s#User=pi#User=$USER#" sound-init.service`
`sed -i "s#Group=pi#Group=$(id -gn)#" sound-init.service`
Install the systemd service files
> `sudo mv dog-trainer.service /etc/systemd/system/`
`sudo mv sound-init.service /etc/systemd/system/`
Reload systemd manager configuration
> `systemctl daemon-reload`
> `sudo systemctl daemon-reload`
Enable the `dog-trainer` service at boot time and also run it
> `systemctl enable --now dog-trainer`
> `sudo systemctl enable --now dog-trainer`
If it does not work check logs
> `journalctl -f -u dog-trainer`
@@ -100,4 +113,7 @@ Set volume for playback and recording
Launch soundcard mixer
> `alsamixer`
Press F6 to select a soundcard. Find your USB sound device in the list e.g. `Jabra Speak 410 USB`. Press F4 and use up/down arrow keys to set recording volume and repeat also for the playback (F3). Make sure it is not muted. Press (m) to toggle mute. Press ESC to exit.
Press F6 to select a soundcard. Find your USB sound device in the list e.g. `Jabra Speak 410 USB`. Press F4 and use up/down arrow keys to set recording volume and repeat also for the playback (F3). Make sure it is not muted. Press (m) to toggle mute. Press ESC to exit.
To preserve audio setting after the boot run
> `sudo alsactl store`

BIN
beep.wav

Binary file not shown.

View File

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

View File

@@ -1,6 +1,7 @@
[Unit]
Description=Dog Trainer
After=sound.target
After=sound-init.service
Requires=sound-init.service
[Service]
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