0

I'm trying to play back an audio stream with the sounddevice module, ive tried many ways but they all don't make much sense and don't seem to work for me at all, the data 100% is valid and if saved to a file it plays back perfectly so i know the data isnt corrupted in any way.

import requests
import numpy as np
import sounddevice as sd

url = 'https://audio10.broadcastify.com/dbhq0jtm96yrs78.mp3?nocache=7847116&xan=xtf9912b'
#creates a varable for the url

chunk_size = 256 *4
#chunk size

r = requests.get(url, stream = True)
#uses requests.get to get data from link

for chunk in r.iter_content(chunk_size=chunk_size):
#grabs the audio content of the web page with the r varable and putting it into the chunk varable as byte data
    chunk = np.frombuffer(chunk, dtype=np.int8)
    #turns the chunk byte data into an array
    print(chunk)
    #prints the chunk data to show the result

    #somehow use the soundevice module to play back the audio stream? but i dont know how?
    #the only clue that i have is that i cant use sd.play(chunk) because the play function isnt
    #able to play stream data like that
  • Before being able to play the signal with the `sounddevice` module, you'll have to decompress the MP3 data. – Matthias Jun 08 '20 at 09:24
  • @Matthias how do you decompress data exactly..? – MINIGUNLORD Jun 09 '20 at 09:12
  • Here's an answer that uses `ffmpeg` for decoding the stream (and PyOpenAL for playback): https://stackoverflow.com/a/57454174/. Based on that, I've just created an example using the `sounddevice` module: https://github.com/spatialaudio/python-sounddevice/pull/246. – Matthias Jun 09 '20 at 17:58
  • @Matthias oh wow! thank you so much, this helps quite a bit – MINIGUNLORD Jun 12 '20 at 04:50
  • I'm glad I could help! BTW, I've merged the previously mentioned pull request and the example is now available here: [play_stream.py](https://github.com/spatialaudio/python-sounddevice/blob/master/examples/play_stream.py). – Matthias Jun 12 '20 at 06:32

0 Answers0