Giter VIP home page Giter VIP logo

Comments (4)

mgeier avatar mgeier commented on July 20, 2024 1

The situation is admittedly a bit complicated, so it is easy to be confused.

The parametersformat and subtype (and endian as well) only matter for the storage of the audio data in the file.

The data that you are handling in your Python code is entirely independent of that. In the buffer_*() methods, the dtype argument specifies the data type that you are handling.

Here's a hopefully illustrative example:

>>> import soundfile as sf
>>> sf.write('myfile.aiff', [1.0], subtype='PCM_16', samplerate=48000)

This creates a 16-bit file containing the largest possible signal value.
The given float value is automatically converted to a 16-bit integer.

>>> f = sf.SoundFile('myfile.aiff')
>>> bytes(f.buffer_read(dtype='int16'))
b'\xff\x7f'

As you can see, you still have to specify the dtype when reading the data. In this case, we are reading the same data type that's stored in the file, but that is not required.

And to come back to the question about endianness: If you look at the file contents (e.g. with xxd myfile.aiff), you see the contents at the very last two bytes: 7fff.

AIFF files are stored in big-endian format, but as you can see above, we got the bytes in little-endian format. We are getting native endianness. If you run this on a big-endian system, you should get b'\x7f\xff' (but I didn't try this because I don't have a big-endian system).

We can continue exploring:

>>> f.seek(0)
0
>>> bytes(f.buffer_read(dtype='int32'))
b'\x00\x00\xff\x7f'

We can read the value as 32-bit integer, even though it is stored as 16-bit integer in the file.
And the important thing is that libsndfile scales the value to be the largest 32-bit integer!

In summary:

What endianness properties should I expect buffer_read/buffer_write to have?

Native endianness.

Will it depend on format?

Nope.

from python-soundfile.

bastibe avatar bastibe commented on July 20, 2024

There are the buffer_read, buffer_read_into, and buffer_write functions which do essentially that. And anyways, a C-order numpy array is internally in interleaved order as well.

from python-soundfile.

mcclure avatar mcclure commented on July 20, 2024

That's very useful, thanks. Can you clarify—
https://python-soundfile.readthedocs.io/en/0.11.0/#soundfile.SoundFile.buffer_write
What endianness properties should I expect buffer_read/buffer_write to have? Will it depend on format?

from python-soundfile.

bastibe avatar bastibe commented on July 20, 2024

Yes, it entirely depends on the format. Have a look at https://libsndfile.github.io/libsndfile/api.html#raw for more information. I'd expect this to only work reasonably for uncompressed PCM-like formats.

from python-soundfile.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.