Try to understand how minimodem output redirection work:
man minimodem|minimodem --tx -8 1200|tee /tmp/datas.wav
du -h /tmp/datas.wav
0 /tmp/datas.wav
man minimodem|minimodem --tx -8 1200 -f /tmp/datas.wav
du -h /tmp/datas.wav
6.4M /tmp/datas.wav
Try to understand how aplay output redirection work:
aplay /tmp/datas.wav|tee /tmp/aplay_stdout.datas
du -h /tmp/datas.wav
0 /tmp/datas.wav
Also return an output buffer of 0 byte empty file, it's bad.
You must use fifo or another tricks, and understand that: minimodem need WAV file ,yes it can be done with a specific command and tool capable to encode raw to wav and using pipe, for example with this tool :
SoX - Sound eXchange, the Swiss Army knife of audio manipulation
and using FIFO file.
And please , specify HOW you send your datas (device type, modulation etc...), thank you.
AUTOMATIC-TASK SCRIPT
#!/bin/bash
# rtl_sox_minimodem.sh
# Automatic FM/FSK demodulation task
# with rtl_fm,sox,aplay and minimodem
# by Ephemeral (2019)
# YOU CAN CONFIGURE THIS VARIABLES
FREQUENCY=;
AUDIO_FREQUENCY=48000;
BAUD=1200;
INTERACT=0 # 0 or 1, 0 for interactive questions...
# USE_GUI=1, open aplay and minimodem in new terminal
USE_GUI=1;
# All 20 secs check for pidof rtl_fm, aplay,
# minimodem (INTERACT variable must be 0 and FORCE_RUN must be 1)
FORCE_RUN=1
TIME_TO_CHECK_ALWAYS_RUNNING=20;
MINIMODEM_LOG=/tmp/minimodem.$(date +"%Y-%m-%d_%H-%M-%S").log
FIFO=/tmp/fifo;
MINIMODEM_CMD="minimodem --rx -8 ${BAUD} -f ${FIFO}";
APLAY_CMD="aplay ${FIFO}";
RTL_FM_SOX_CMD="rtl_fm -f ${FREQUENCY} -s ${AUDIO_FREQUENCY} -|sox -t raw -esigned-integer -b16 -r${AUDIO_FREQUENCY} - -t wav -esigned-integer -b16 -r${AUDIO_FREQUENCY} ${FIFO}";
function ERROR(){
echo -e "\t$(tput setaf 1)${1}$(tput sgr 0)"
}
function INFO(){
echo -e "\t${1}"
}
function WARN(){
echo -e "\t$(tput setaf 3)${1}$(tput sgr 0)"
}
function CHECK(){
echo -e "\n\n[$(date)]"
if [ ! -e "${FIFO}" ];then
INFO "No FIFO file, create this...";
mkfifo /tmp/fifo
if [ "${?}" -eq "0" ];then
INFO "FIFO file created with success."
else
ERROR "An error was occured when create FIFO, please check the error.";
exit 1;
fi;
else
INFO "FIFO file already exists...";
fi;
if [ -z "$(pidof rtl_fm)" ];then
INFO "rtl_fm is not running, start that...";
if [ ! -z "$(pidof minimodem)" ]&&[ ! -z "$(pidof aplay)" ];then
WARN "rtl_fm require aplay and minimodem not running";
WARN "cannot start rtl_fm."
if [ "${INTERACT}" -eq "0" ];then
INFO "Force Kill pid=$(pidof minimodem aplay)";
kill -9 $(pidof minimodem aplay);
else
CHOICE=
while [ -z "${CHOICE}" ]:
do
INFO -en "Would you like to close aplay and minimodem pid ? >> "
read CHOICE
case "${CHOICE}" in
y|Y) INFO "Kill pid=$(pidof aplay minimodem)";kill -9 $(pidof aplay minimodem); break;;
n|N) echo "Bye";exit 0;;
*) ERROR "Please enter y or Y (Yes), n or N (No)!"; CHOICE="";;
esac
done;
fi;
else
INFO "Aplay and minimodem is not running, ok to start rtl_fm...";
fi;
INFO "${RTL_FM_SOX_CMD}";
#
if [ "${USE_GUI}" -eq "1" ];then
# cannot use pipe in --command , then use current shell and open inly minimodem and aplay in new term.
#lxterminal --title="Minimodem" --command="${MINIMODEM_CMD}";
rtl_fm -f ${FREQUENCY} -s ${AUDIO_FREQUENCY} -|sox -t raw -esigned-integer -b16 -r${AUDIO_FREQUENCY} - -t wav -esigned-integer -b16 -r${AUDIO_FREQUENCY} ${FIFO} &
else
rtl_fm -f ${FREQUENCY} -s ${AUDIO_FREQUENCY} -|sox -t raw -esigned-integer -b16 -r${AUDIO_FREQUENCY} - -t wav -esigned-integer -b16 -r${AUDIO_FREQUENCY} ${FIFO} &
fi;
INFO "rtl_fm started: $?, continue in 10 secs...";
sleep 5;
else
INFO "rtl_fm already started, noting to done...";
fi;
if [ -z "$(pidof minimodem)" ];then
INFO "Minimodem is not running, start that...";
if [ "${USE_GUI}" -eq "1" ];then
echo "Start at $(date)" >> "${MINIMODEM_LOG}";
lxterminal --title="Minimodem" --command="bash -c \"${MINIMODEM_CMD}\"";
else
minimodem --rx -8 ${BAUD} -f ${FIFO} &>"${MINIMODEM_LOG}" &
fi;
sleep 3;
if [ -z "$(pidof minimodem)" ];then
WARN "ERROR: unable to run minimodem, re-trying..."
kill -9 $(pidof rtl_fm aplay sox);
CHECK;
return
fi;
else
INFO "Minimodem already started, nothing to done...";
fi;
if [ -z "$(pidof aplay)" ];then
INFO "Aplay is not running, start that...";
if [ "${USE_GUI}" -eq "1" ];then
lxterminal --title="Aplay" --command="${APLAY_CMD}";
else
aplay "${FIFO}" &
fi;
else
INFO "Aplay already started, nothing to done...";
fi;
}
function MAIN(){
if [ -z "${FREQUENCY}" ];then
ERROR "Please configure FREQUENCY variable before using this script.";
exit 1;
fi;
kill -9 $(pidof sox minimodem aplay rtl_fm);
if [ "${FORCE_RUN}" -eq "1" ];then
if [ "${INTERACT}" -eq 1 ];then
ERROR "ERROR: You cannot use INTERACT=1 when you use FORCE_RUN=1";
exit 1;
else
if [ ! -f "${MINIMODEM_LOG}" ];then
echo "[+] Creating minimodem log file ${MINIMODEM_LOG}";
touch "${MINIMODEM_LOG}";
fi;
while :
do
CHECK
INFO "Waiting ${TIME_TO_CHECK_ALWAYS_RUNNING} secs before verification...";
sleep ${TIME_TO_CHECK_ALWAYS_RUNNING};
done;
fi;
else
CHECK;
fi;
}
MAIN
HOW TO USE THIS SCRIPT ?
1/ REAL-TIME+LXTERMINAL
configure FREQUENCY variable in the script header and then:
sudo chmod ug+x rtl_sox_minimodem.sh
bash rtl_sox_minimodem.sh
As you can see , rtl_fm use current terminal where the bash rtl_fm command as running, minimodem is opened in a new terminal for real-time display demodulated datas and then anoter terminal is opened for display the sound for the user.
The script is configured by default for checking if he is already ALIVE every 20 secondes, you can adjust this value for your needs.
Try to close the aplay or minimodem terminal (or the two term.) manually when all is running, wait 20 secs and normally all is automaticly restarted correctly without any errors (for me for sure) and you can see the demodulated datas in the minimodem terminal.
If you see one error:
ERROR: unable to run minimodem, re-trying...
Please wait for another iteration of the loop, and check if minimodem terminal is finally open, he must be.
2/ BACKGROUND-TASK + POSSIBLE REAL-TIME FROM ANOTHER TERM.
configure USE_GUI=0 in the script header and then run:
sudo chmod ug+x rtl_sox_minimodem.sh
bash rtl_sox_minimodem.sh
With this mode all is done in background and minimodem datas are written into a specified file /tmp/minimodem.${date}.log, then you can
open a new terminal (manually) and use (the filename is specified in this script output):
tail -f /tmp/minimodem.${date}.log
You can also filtered any specific datas in real-time:
tail -f /tmp/minimodem.${date}.log | grep "ONLY KNOWN DATAS"
for showing the datas in real-time (because with USE_GUI=0 minimodem redirect output in this file). Or read this file at any time.
MORE
You can put this script at-startup by creating an systemd unit file and configure it with this script.
This script was written relatively quickly, it can and must be improved.