1

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

spearna
  • 25
  • 9

1 Answers1

1

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 -
Thor
  • 45,082
  • 11
  • 119
  • 130
  • thanks! I tried the suggestions and removed the redundant sox commands. – spearna Aug 09 '17 at 03:25
  • Your second suggestion works, but it doesn't meet the function I'm attempting as it pairs the play and record commands. I am recording the radio fm signal to an mp3 file using the rtl_fm/sox command pipe. Then, using a separate command, I'm attempting to play the (still writing) mp3 file at a user determined start point of the file. This command could be called multiple times, independent of the rtl_fm/sox record command to get the start point desired. RECAP: 1 recording command; n play command(s) – spearna Aug 09 '17 at 03:30
  • Some other important info I neglected in original post: Running on Raspberry Pi 3 (Raspian Jessie), NooElec R820T SDR – spearna Aug 09 '17 at 03:33
  • @spearna: I'm not sure you can do this with sox without saving the stream to a temporary file – Thor Aug 09 '17 at 08:37
  • I've tried your suggestion for separate processes (note that I have updated the parameters here to use what I've been testing in other attempts): `rtl_fm -Mfm -f92.5M -s170k -Afast -l0 -Edeemp > some_file.s16 | sox -ts16 -r170k -es -b16 -c1 -V1 some_file.s16 some_file.mp3` In a second terminal, I run the other command: `tail -c+$((8000 * 10)) -f some_file.s16 | play -ts16 -r8k -c1 -` It runs without error, but all I hear is static. I know the station 92.5M works, so it has to be a config setting mismatch somewhere. If not sox, what would be a different tool? – spearna Aug 10 '17 at 03:17
  • @spearna: I don't know and I don't have a way of testing a solution – Thor Aug 10 '17 at 13:17
  • I'll keep exploring. Your pointers have given me some trails to pursue. – spearna Aug 10 '17 at 13:52
  • Thor could you elaborate more on `gstreamer`? I've never heard of it and wouldn't know where to begin. – spearna Sep 07 '17 at 03:37
  • @spearna: I have too little experience with `gstreamer` to give any specific details. I know it works similar to `sox` in that you can apply a plugin-chain to the input stream – Thor Sep 07 '17 at 15:54
  • @spearna did you get this fixed? I can do this to an .ogg file but not an .mp3 – Droid Chris Jun 12 '23 at 06:39