python-soundfile icon indicating copy to clipboard operation
python-soundfile copied to clipboard

read from stream with blocks?!

Open aamarioneta opened this issue 4 years ago • 1 comments
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.

aamarioneta avatar Jan 19 '21 20:01 aamarioneta

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.

bastibe avatar Jan 20 '21 19:01 bastibe