Play an MP3 file as it's being written - linux

I'm saving an fm station to an mp3 file using rtl_fm and sox.
rtl_fm to capture the signal and sox to transcode it to mp3.
rtl_fm -M wbfm -f 88.1M -d 0 -s 22050k -l 310 | sox -traw -r8k -es -b16 -c1 -V1 - -tmp3 - | sox -tmp3 - some_file.mp3
Then I'm trying to play that file in a second terminal, as the mp3 is being written using:
play -t mp3 some_file.mp3
The problem is that it only plays up until the time the mp3 had at the time the play command was invoked.
How do I get it to play the appended mp3 over time, while it's being written?
EDIT:
Running on Raspberry Pi 3 (Raspian Jessie), NooElec R820T SDR

There are a couple of things here. I don't think sox supports "tailing" a file, but I know mplayer does. However, in order to have better control over the pipeline, using gstreamer might be the way to go, as it has a parallel event stream built into its effects pipeline.
If you want to stick with sox, I would first get rid of the redundant second invocation of sox, e.g.:
rtl_fm -M wbfm -f 88.1M -d 0 -s 22050k -l 310 |
sox -ts16 -r8k -c1 -V1 - some_file.mp3
And in order to play the stream while transcoding it, you could multiplex it with tee, e.g.:
rtl_fm -M wbfm -f 88.1M -d 0 -s 22050k -l 310 |
tee >(sox -ts16 -r8k -c1 -V1 - some_file.mp3) |
play -ts16 -r8k -c1 -
Or if you want them to be separate processes:
# Save stream to a file
rtl_fm -M wbfm -f 88.1M -d 0 -s 22050k -l 310 > some_file.s16
# Encode stream
sox -ts16 -r8k -c1 -V1 some_file.s16 some_file.mp3
# Start playing the file at 10 seconds in
tail -c+$((8000 * 10)) -f some_file.s16 |
play -ts16 -r8k -c1 -

Related

How to play youtube song in terminal with ffplay and youtube-dl

Is there any you to stream any music the terminal with youtube-dl and ffplay
I know that ffplay can play audio with shell
$ audio stram | ffplay -i -
You can try this:
youtube-dl -f bestaudio ytsearch:"SONG NAME" -o - 2>/dev/null | ffplay -nodisp -autoexit -i - &>/dev/null
Or:
youtube-dl -f bestaudio VIDEO_URL -o - 2>/dev/null | ffplay -nodisp -autoexit -i - &>/dev/null
..and if you mordernize the command a little you can play YT-videos from the terminal without ads.
youtube-dl -f mp4 YOUTUBE_VIDEO_URL -o - 2>/dev/null | ffplay -autoexit -i - &>/dev/null
Due to YouTube at the moment are throttlening youtube-dl I'm now using yt-dlp instead. Same codebase but no throttlening :)
yt-dlp -f mp4 YOUTUBE_VIDEO_URL -o - 2>/dev/null | ffplay -autoexit -i - &>/dev/null

Sox Is Throwing A Random Error: Option A Isn't Recognized

I am running this command:
sox -t mp3 $(ls -1 /home/pi/Desktop/Music/*.mp3|sort -R) -t wav -r 44100 - | sudo ./pi_fm_rds -freq 104.5 -audio - -ps ZSFM -rt ZSFM
This is the result:
sox WARN getopt: option `A' not recognized
sox FAIL sox: invalid option
When I was running this command sox worked fine:
sox -t mp3 /home/pi/Desktop/Music/*.mp3 -t wav -r 44100 - | sudo ./pi_fm_rds -freq 104.5 -audio - -ps ZSFM -rt ZSFM
The new command is to have sox shuffle through the files in a directory randomly, but its giving me this wierd error. How can I make it work? I've done tons of research, but I can't find anything about 'option A' anywhere.
The problem was the whitespace in file names - all I had to do was change, for example, 'dasa berta.mp3' to 'dasaberta.mp3'. One of the file names had 'A' in it, and the terminal took it as a separate command. It wasn't a sox problem, it was a me-using-terminal-problem.

Merge multiple .raw file into single wav file using Sox

Does any one know how to merge multiple raw audio file into single wav file. I am using Sox but any other tools also fine.
I am trying below commands, but i know something wrong here
sox -r 16000 -b 16 -c 1 -e signed-integer file1.raw file2.raw out.wav
I am missing something ?
You need to specify all input file parameters per file (as RAW obviously does not have a header). Your initial guess was same as mine: that -r 16000 -b 16 -c 1 -e signed-integer applies to both file1.raw and file2.raw. It doesn't.
Here's how it should be done:
sox -r 16000 -b 16 -c 1 -e signed-integer file1.raw -r 16000 -b 16 -c 1 -e signed-integer file2.raw out.wav
You might want to add option -m for merge.

Recording with arecord stops after 1h 33m Under Fedora 23

I'm using this command to record audio in Linux Fedora 23
/usr/bin/arecord -d 11400 -D hw:1,0 -f S32_LE -c2 -r48000 -t wav | lame -b 192 - longrec.mp3 >> output.txt 2>&1 & echo $!
Basically I want an mp3 record of 3 hours and 10 minutes (11400 seconds) from the input soundcard. Everything works fine when started, but it always stops after 1h33m12s. File output.txt shows nothing of any interest:
LAME 3.99.5 64bits (http://lame.sf.net)
Using polyphase lowpass filter, transition band: 18774 Hz - 19355 Hz
Encoding <stdin> to longrec.mp3
Encoding as 48 kHz j-stereo MPEG-1 Layer III (8x) 192 kbps qval=3
Any clue of what the problem is?
[SOLVED] instead of using arecord I have switched to ffmpeg, command:
ffmpeg -f pulse -i alsa_input.usb-Focusrite_Scarlett_Solo_USB-00.analog-stereo -t 11400 -c:a libmp3lame -b:a 192k longrec.mp3
Has the same effect as arecord one and it also doesn't block sound-card resource (I can run multiple ffmpeg record instance from the same source, while with arecord I can do only one).

Streaming a file off a server

I am trying to stream the recorded audio from my raspberry pis to my desktop computer which handles pocketsphinx phenomenally. I can pipe the audio using
arecord -D plughw:1,0 -r 16000 -f S16_LE | ssh -C user#192.168.86.101 sox - test.wav
And then run it using
pocketsphinx_continuous -dict ~/4568.dic -lm ~/4568.lm -infile ~/test.wav
But once it reaches the end of the file, it stops, even though the file is still writing. Is there a way to keep it open?
Use named pipe instead of a regular file. Also you can file an issue at github.com/cmusphinx/pocketsphinx requesting that pocketsphinx_continious should be able to read from stdin. And of course you're welcome to submit such a patch.
To anyone else finding this,
arecord -D plughw:1,0 -r 16000 -f S16_LE | ssh -C user#192.168.86.101 pocketsphinx_continuous -infile /dev/stdin
is how to do it

Resources