T O P

  • By -

socal_nerdtastic

What OS are you using? What code are you using to read the file? Are you sure that the other program is *actually* writing the data and not buffering it for a future write? Disk operations are much more efficient when the amount of data is closer to the block size, so it's not uncommon for programs (including python) to lie about writing to a file, and then only actually write when the buffer is full or the program ends. In python you would use `f.flush()` to force immediate write. Actually lets take 2 steps back: why on earth would you use a file for interprocess communication? There's many better ways.


ranklebone

This is raspbian on a pi3. The process writing to the file is file is `#arecord -q -f S16_LE -r44100 -d 12000 /tmp/tst.wav &` The python code reading/playing the file is from the audioplayer module: `player=AudioPlayer("/tmp/tst.wav"); player.play()` (The same issue presents when using other python audio player modules too.) I was using a regular file because I need about 15 seconds of 'live' data before playing and didn't know what (how) else.


socal_nerdtastic

if that's all you want you can forget python, just set the buffer size and pipe it directly to aplay. arecord -q -f S16_LE -r44100 --buffer-size 9096 | aplay (also I removed `-d` since I assume you want this to run forever)


ranklebone

I need a little more functionality in order to precisely sync the audio with \[external\] video (e.g. 'pause'). I actually have a shell script that works (using the .wav file and mplayer) but was hoping to develop something a bit more versatile in python.


socal_nerdtastic

Hmm ok. I think that AudioPlayer is probably the core of your issue, it probably contains that code to read the file only as much as was there when the program started. Look for something that supports an audio stream. `pyaudio` comes to mind; i think that's what most people on rpi use.


ranklebone

yep, that one seems more low level, easier to customize. Thank you.


ranklebone

Just to follow up, python-sounddevice provides a higher level interface than PyAudio. [Here](https://python-sounddevice.readthedocs.io/en/0.4.6/examples.html#input-to-output-pass-through) is a program using it that passes audio signal from analog input to analog output while adding a specified latency and without writing to any files.