#!/bin/bash APLAY="aplay -Dhw:0,0 -c 2 -r 48000 -d 15 " AREC="arecord -Dhw:0,6 -f s32_LE -c 4 -r 48000 -d 10 -vvv" PLAY_FILE="$1" REC_FILE="rec.wav" do_play() { local id=$1 echo "** $i Starting playback $PLAY_FILE" $APLAY $PLAY_FILE if [ $? -ne 0 ]; then # If aplay fails, send a SIGUSR1 signal to the parent process echo -e "\n**$i $PLAY_FILE playback error" kill -SIGUSR1 $$ fi echo -e "\n**$i $PLAY_FILE playback completed $aplay_pid" } cleanup() { jobs -p # Kill all background jobs for job in $(jobs -p); do kill -0 $job 2>/dev/null done jobs -p } handle_sigint() { echo "Caught SIGINT signal. Cleaning up..." cleanup exit 0 } handle_sigterm() { echo "Caught SIGTERM signal. Exiting gracefully..." cleanup exit 0 } handle_siguser1() { echo "playback error Exiting" cleanup exit 0 } trap 'handle_sigint' SIGINT trap 'handle_sigterm' SIGTERM trap 'handle_siguser1' SIGUSR1 # Main script logic for i in {0..30}; do echo "***$i Audio play and record parallel***" do_play $i & sleep 2 echo -e "\n**$i Starting recording " $AREC "test.wav" if [ $? -ne 0 ]; then echo -e "\n**$i recording error " cleanup exit 0 fi echo -e "\n** $i recording completed" cp "rec_full.wav" $REC_FILE if [ $i -eq 0 ]; then cp "test.wav" "rec_full.wav" else sox $REC_FILE "test.wav" "rec_full.wav" fi sleep 5 done