python-soundfile
python-soundfile copied to clipboard
read from stream with blocks?!
trafficstars
Hi. I'm trying to read from a stream because downloading the whole file is not really optimal. This is what i came up with but it doesn't work.
import io
from urllib.request import urlopen
import soundfile as sf
url = "http://tinyurl.com/shepard-risset"
read_from_stream = urlopen(url)
chunk_size = 1024
while True:
chunk = read_from_stream.read(chunk_size)
if not chunk:
break
read_bytes = io.BytesIO(chunk)
blocks, samplerate = sf.blocks(read_bytes, blocksize=chunk_size)
I'm getting an error:
raise RuntimeError(prefix + _ffi.string(err_str).decode('utf-8', 'replace'))
RuntimeError: Error opening <_io.BytesIO object at 0x7fee4b4c8950>: Supported file format but file is malformed.
What am i doing wrong? Sorry, python noob here, don't know where to ask.
If you have an object that behaves like a stream, you can pass it to soundfile directly. In your case, you should be able to open read_from_stream with soundfile directly, with no need for the while True loop.