I’d like to write a python program, that is loading a internet-radio mp3 stream, doing some processing to the data and plays the audio stream from the computer.
I wasn’t able to find a way how to do that properly. So far I got to using this code to read stream data in blocks:
for block in r.iter_content(1024):
... process block
Then I can process those blocks by soundfile library and it is read properly as mp3:
stream_bytes = io.BytesIO()
stream_bytes.write(block)
...
# after writing some blocks convert mp3 data to audio data and samplerate:
data, samplerate = soundfile.read(stream_bytes)
print(f'{samplerate} {len(data)}')
Then, I know that I can play back audio with library pyaudio by initializing it with the same detected samplerate, number of channels, and send in the audio data.
Though, here I don’t know how to:
- How to process the stream itself as it goes. I mean I can’t just add up to the stream_bytes file and grow it forever, I have to truncate it sometime, but how to manage file ‘headers’ to still be recognizable as mp3? How to cut it, or how to make soundfile accept new ‘bytes’? I don’t see any examples for streaming in data.
- Convert
soundfile
data into byte-data acceptable bypyaudio
(but I will manage that somehow)