Comments (4)
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.
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.
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.
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)
- numpy is a dependency, but this is not declared HOT 3
- buffer_read doesn't work in basic test HOT 2
- Soundfile read/write wav is not symmetric with default arguments HOT 3
- module 'soundfile' has no attribute 'SoundFileRuntimeError' HOT 5
- Value Error: array is too big when loading .flac file
- soundfile crashes when trying to export long .ogg file HOT 1
- soundfile.LibsndfileError: Error opening <_io.BytesIO object at 0x0000022B81C21850>: Format not recognised. HOT 2
- Cannot suppress warnings when decoding mp3
- Cannot open Ogg FLAC file: unknown error in flac decoder HOT 1
- miniforge3/lib/python3.9/site-packages/soundfile.py", line 1229, in _init_virtual_io def vio_get_filelen(user_data): MemoryError: Cannot allocate write+execute memory for ffi.callback(). You might be running on a system that prevents this. For more information, see https://cffi.readthedocs.io/en/latest/using.html#callbacks
- Cannot write Ogg files over 96 seconds long at 44.1 kHz HOT 4
- soundfile.info does not work on mp3 HOT 2
- soundfile.LibsndfileError: <exception str() failed> HOT 6
- How to save left and right channel in separate files? HOT 1
- soundfile.LibsndfileError: Error : unknown error in flac decoder. HOT 2
- "Format not recognised." for m4a files HOT 2
- soundfile.LibsndfileError: Error opening <tempfile._TemporaryFileWrapper object at 0x7feadae59070>: Format not recognised. HOT 2
- Part of mp3 is loaded. HOT 2
- unknown error in flac decoder HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-soundfile.