question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is `blocksize` and callback's `frames` param?

See original GitHub issue

I need a little help to understand what is blocksize and callback’s frames param.

At first I thought len(outdata) equals frames, and so it is with play-a-sine-signal example (on my machine), but no with play-a-very-long-sound-file

I read the api section in doc:https://python-sounddevice.readthedocs.io/en/0.3.15/api/streams.html, but I still don’t understand what is the relationship between blocksize, frames, and len(outdata). could you please explain more? Thank you

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
hyansupercommented, Apr 26, 2020

Thank you, that answers my question

0reactions
mgeiercommented, Apr 26, 2020

I was surprised for a moment, but then I saw the reason for the confusion:

The play_long_file.py example uses a RawOutputStream. This means that the outdata argument in the callback function is not a NumPy array but a Python buffer object. When you use len() on such an object, you get the size of the data in bytes.

In this case, you should look at stream.samplesize, which is the number of bytes per sample. Since the example uses dtype='float32', you’ll see that the sample size is 4. You’ll also have to check the number of channels (which in your file alert.wav seems to be 1).

The length in bytes of the output buffer is the number of frames times the number of channels times the sample size. This is why in your case len(outdata) is 4 times the number of frames, because the sample size is 4 and the number of channels is 1.

So I was wrong to say that above:

Within the callback function, this is always True:

frames == len(outdata)

This is only true when you use NumPy, i.e. when you use Stream and OutputStream.

When you use RawOutputStream, this is more accurate:

frames * stream.samplesize * stream.channels  == len(outdata)

When you use RawStream, there are pairs of values for samplesize and channels, the second one is the value corresponding to the output:

frames * stream.samplesize[1] * stream.channels[1]  == len(outdata)

Does that make sense?

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - In InputStream, what does blocksize do? (sounddevice)
The callback function is called at a rate determined by blocksize . If you make blocksize larger, the callback will be called less...
Read more >
API Documentation — python-sounddevice, version 0.3.12
blocksize (int, optional) – The number of frames passed to the stream callback function, or the preferred block granularity for a blocking read/write...
Read more >
Issue changing the callback "frames" #351 - GitHub
Hello, I am using a sounddevice.Stream with a callback function and want to change the "frames". I can't seem to find a way...
Read more >
Common Block Properties - MATLAB & Simulink - MathWorks
Property Description Values BackgroundColor Block background color. BlockType Block type (read‑only). character array CloseFcn Function called when close_system is run on block. function | character v......
Read more >
39.3. cd — CD-ROM access on SGI systems — Python 2.7.9 ...
Data from the CD is passed to the parser, which parses the frames, and calls any callback functions that have previously been added....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found