python-soundfile — python-soundfile 0.13.1 documentation (original) (raw)

version python status license

contributors downloads

The soundfile module is an audio library based on libsndfile, CFFI and NumPy. Full documentation is available on https://python-soundfile.readthedocs.io/.

The soundfile module can read and write sound files. File reading/writing is supported through libsndfile, which is a free, cross-platform, open-source (LGPL) library for reading and writing many different sampled sound file formats that runs on many platforms including Windows, OS X, and Unix. It is accessed throughCFFI, which is a foreign function interface for Python calling C code. CFFI is supported for CPython 2.6+, 3.x and PyPy 2.0+. The soundfile module represents audio data as NumPy arrays.

python-soundfile is BSD licensed (BSD 3-Clause License).

(c) 2013, Bastian Bechtold

open-issues closed-issues open-prs closed-prs

Breaking Changes

The soundfile module has evolved rapidly in the past. Most notably, we changed the import name from import pysoundfile toimport soundfile in 0.7. In 0.6, we cleaned up many small inconsistencies, particularly in the the ordering and naming of function arguments and the removal of the indexing interface.

In 0.8.0, we changed the default value of always_2d from Trueto False. Also, the order of arguments of the write function changed from write(data, file, ...) to write(file, data, ...).

In 0.9.0, we changed the ctype arguments of the buffer_*methods to dtype, using the Numpy dtype notation. The oldctype arguments still work, but are now officially deprecated.

In 0.12.0, we changed the load order of the libsndfile library. Now, the packaged libsndfile in the platform-specific wheels is tried before falling back to any system-provided libsndfile. If you would prefer using the system-provided libsndfile, install the source package or source wheel instead of the platform-specific wheels.

Installation

The soundfile module depends on the Python packages CFFI and NumPy, and the library libsndfile.

In a modern Python, you can use pip install soundfile to download and install the latest release of the soundfile module and its dependencies. On Windows (64/32) and OS X (Intel/ARM) and Linux 64, this will also install a current version of the library libsndfile. If you install the source module, you need to install libsndfile using your distribution’s package manager, for example sudo apt install libsndfile1.

If you are running on an unusual platform or if you are using an older version of Python, you might need to install NumPy and CFFI separately, for example using the Anaconda package manager.

Building

Soundfile itself does not contain any compiled code and can be bundled into a wheel with the usual python setup.py bdist_wheel. However, soundfile relies on libsndfile, and optionally ships its own copy of libsndfile in the wheel.

To build a binary wheel that contains libsndfile, make sure to checkout and update the _soundfile_data submodule, then runpython setup.py bdist_wheel as usual. If the resulting file size of the wheel is around one megabyte, a matching libsndfile has been bundled (without libsndfile, it’s around 25 KB).

To build binary wheels for all supported platforms, run python build_wheels.py, which will python setup.py bdist_wheel for each of the platforms we have precompiled libsndfiles for.

Error Reporting

In case of API usage errors the soundfile module raises the usual ValueError or TypeError.

For other errors SoundFileError is raised (used to be RuntimeError). Particularly, a LibsndfileError subclass of this exception is raised on errors reported by the libsndfile library. In that case the exception object provides the libsndfile internal error code in the LibsndfileError.code attribute and the raw libsndfile error message in the LibsndfileError.error_string attribute.

Read/Write Functions

Data can be written to the file using soundfile.write(), or read from the file using soundfile.read(). The soundfile module can open all file formats that libsndfile supports, for example WAV, FLAC, OGG and MAT files (see Known Issues below about writing OGG files).

Here is an example for a program that reads a wave file and copies it into an FLAC file:

import soundfile as sf

data, samplerate = sf.read('existing_file.wav') sf.write('new_file.flac', data, samplerate)

Block Processing

Sound files can also be read in short, optionally overlapping blocks with soundfile.blocks(). For example, this calculates the signal level for each block of a long file:

import numpy as np import soundfile as sf

rms = [np.sqrt(np.mean(block**2)) for block in sf.blocks('myfile.wav', blocksize=1024, overlap=512)]

SoundFile Objects

Sound files can also be opened as SoundFile objects. EverySoundFile has a specific sample rate, data format and a set number of channels.

If a file is opened, it is kept open for as long as the SoundFileobject exists. The file closes when the object is garbage collected, but you should use the SoundFile.close() method or the context manager to close the file explicitly:

import soundfile as sf

with sf.SoundFile('myfile.wav', 'r+') as f: while f.tell() < f.frames: pos = f.tell() data = f.read(1024) f.seek(pos) f.write(data*2)

All data access uses frames as index. A frame is one discrete time-step in the sound file. Every frame contains as many samples as there are channels in the file.

RAW Files

soundfile.read() can usually auto-detect the file type of sound files. This is not possible for RAW files, though:

import soundfile as sf

data, samplerate = sf.read('myfile.raw', channels=1, samplerate=44100, subtype='FLOAT')

Note that on x86, this defaults to endian='LITTLE'. If you are reading big endian data (mostly old PowerPC/6800-based files), you have to set endian='BIG' accordingly.

You can write RAW files in a similar way, but be advised that in most cases, a more expressive format is better and should be used instead.

Virtual IO

If you have an open file-like object, soundfile.read() can open it just like regular files:

import soundfile as sf with open('filename.flac', 'rb') as f: data, samplerate = sf.read(f)

Here is an example using an HTTP request:

import io import soundfile as sf from urllib.request import urlopen

url = "http://tinyurl.com/shepard-risset" data, samplerate = sf.read(io.BytesIO(urlopen(url).read()))

Note that the above example only works with Python 3.x. For Python 2.x support, replace the third line with:

from urllib2 import urlopen

In-memory files

Chunks of audio, i.e. bytes, can also be read and written without touching the filesystem. In the following example OGG is converted to WAV entirely in memory (without writing files to the disk):

import io import soundfile as sf

def ogg2wav(ogg: bytes): ogg_buf = io.BytesIO(ogg) ogg_buf.name = 'file.ogg' data, samplerate = sf.read(ogg_buf) wav_buf = io.BytesIO() wav_buf.name = 'file.wav' sf.write(wav_buf, data, samplerate) wav_buf.seek(0) # Necessary for .read() to return all bytes return wav_buf.read()

Controlling bitrate mode and compression level

For some audio formats, you can control the bitrate and compression level.

compression_level is a float between 0 and 1, with 1 being the highest compression, and bitrate_mode is ‘VARIABLE’, ‘CONSTANT’, or ‘AVERAGE’.

import soundfile as sf

for example, this uncompressed 5 minute wav file with 32 kHz sample rate is 18 Mb

data, samplerate = sf.read('5min_32kHz.wav')

maximum mp3 compression results in 1.1 Mb file, with either CONSTANT or VARIABLE bit rate

sf.write('max_compression_vbr.mp3', data, samplerate, bitrate_mode='VARIABLE', compression_level=.99) sf.write('max_compression_cbr.mp3', data, samplerate, bitrate_mode='CONSTANT', compression_level=.99)

minimum mp3 compression results in 3.5 Mb file

sf.write('min_compression_vbr.mp3', data, samplerate, bitrate_mode='VARIABLE', compression_level=0)

Known Issues

Writing to OGG files can result in empty files with certain versions of libsndfile. See #130 for news on this issue.

If using a Buildroot style system, Python has trouble locating libsndfile.so file, which causes python-soundfile to not be loaded. This is apparently a bug in python. For the time being, in soundfile.py, you can remove the call to _find_library and hardcode the location of the libsndfile.so in _ffi.dlopen. See #258 for discussion on this issue.

News

2013-08-27 V0.1.0 Bastian Bechtold:

Initial prototype. A simple wrapper for libsndfile in Python

2013-08-30 V0.2.0 Bastian Bechtold:

Bugfixes and more consistency with PySoundCard

2013-08-30 V0.2.1 Bastian Bechtold:

Bugfixes

2013-09-27 V0.3.0 Bastian Bechtold:

Added binary installer for Windows, and context manager

2013-11-06 V0.3.1 Bastian Bechtold:

Switched from distutils to setuptools for easier installation

2013-11-29 V0.4.0 Bastian Bechtold:

Thanks to David Blewett, now with Virtual IO!

2013-12-08 V0.4.1 Bastian Bechtold:

Thanks to Xidorn Quan, FLAC files are not float32 any more.

2014-02-26 V0.5.0 Bastian Bechtold:

Thanks to Matthias Geier, improved seeking and a flush() method.

2015-01-19 V0.6.0 Bastian Bechtold:

A big, big thank you to Matthias Geier, who did most of the work!

2015-04-12 V0.7.0 Bastian Bechtold:

Again, thanks to Matthias Geier for all of his hard work, but also Nils Werner and Whistler7 for their many suggestions and help.

2015-10-20 V0.8.0 Bastian Bechtold:

Again, Matthias Geier contributed a whole lot of hard work to this release.

And many more minor bug fixes.

2017-02-02 V0.9.0 Bastian Bechtold:

Thank you, Matthias Geier, Tomas Garcia, and Todd, for contributions for this release.

And some minor bug fixes.

2017-11-12 V0.10.0 Bastian Bechtold:

Thank you, Matthias Geier, Toni Barth, Jon Peirce, Till Hoffmann, and Tomas Garcia, for contributions to this release.

2022-06-02 V0.11.0 Bastian Bechtold:

Thank you, tennies, Hannes Helmholz, Christoph Boeddeker, Matt Vollrath, Matthias Geier, Jacek Konieczny, Boris Verkhovskiy, Jonas Haag, Eduardo Moguillansky, Panos Laganakos, Jarvy Jarvison, Domingo Ramirez, Tim Chagnon, Kyle Benesch, Fabian-Robert Stöter, Joe Todd

2023-02-02 V0.12.0 Bastian Bechtold

Thank you, Barabazs, Andrew Murray, Jon Peirce, for contributions to this release.

2023-02-15 V0.12.1 Bastian Bechtold

Thank you, funnypig, for the bug report

2025-01-02 V0.13.0 Bastian Bechtold

Thank you, Zhong Jianxin, mcclure, jneuendorf-i4h, aoirint, endolith, Guy Illes, ytya, Sam Lapp, Benjamin Moody

2025-01-25 V0.13.1 Bastian Bechtold

Thank you, Brian McFee and Guy Illes

Contributing

If you find bugs, errors, omissions or other things that need improvement, please create an issue or a pull request athttps://github.com/bastibe/python-soundfile/. Contributions are always welcome!

Testing

If you fix a bug, you should add a test that exposes the bug (to avoid future regressions), if you add a feature, you should add tests for it as well.

Set up local environment with the following commands:

pip install numpy pytest "cffi>=1.0" python soundfile_build.py

To run the tests, use:

This uses pytest;

Note

There is a known problem that prohibits the use of file descriptors on Windows if the libsndfile DLL was compiled with a different compiler than the Python interpreter. Unfortunately, this is typically the case if the packaged DLLs are used. To skip the tests which utilize file descriptors, use:

python setup.py test --pytest-args="-knot\ fd"

Coverage

If you want to measure code coverage, you can use coverage.py. Just install it with:

pip install coverage --user

… and run it with:

coverage run --source soundfile -m pytest coverage html

The resulting HTML files will be written to the htmlcov/ directory.

You can even check branch coverage:

coverage run --branch --source soundfile -m pytest coverage html

Documentation

If you make changes to the documentation, you can re-create the HTML pages on your local system using Sphinx.

You can install it and a few other necessary packages with:

pip install -r doc/requirements.txt --user

To create the HTML pages, use:

python setup.py build_sphinx

The generated files will be available in the directory build/sphinx/html/.

API Documentation

python-soundfile is an audio library based on libsndfile, CFFI and NumPy.

Sound files can be read or written directly using the functionsread() and write(). To read a sound file in a block-wise fashion, use blocks(). Alternatively, sound files can be opened as SoundFile objects.

For further information, see https://python-soundfile.readthedocs.io/.

soundfile.read(file, frames=-1, start=0, stop=None, dtype='float64', always_2d=False, fill_value=None, out=None, samplerate=None, channels=None, format=None, subtype=None, endian=None, closefd=True)[source]

Provide audio data from a sound file as NumPy array.

By default, the whole file is read from the beginning, but the position to start reading can be specified with start and the number of frames to read can be specified with frames. Alternatively, a range can be specified with start and stop.

If there is less data left in the file than requested, the rest of the frames are filled with fill_value. If no fill_value is specified, a smaller array is returned.

Parameters:

Returns:

Examples

import soundfile as sf data, samplerate = sf.read('stereo_file.wav') data array([[ 0.71329652, 0.06294799], [-0.26450912, -0.38874483], ... [ 0.67398441, -0.11516333]]) samplerate 44100

soundfile.write(file, data, samplerate, subtype=None, endian=None, format=None, closefd=True, compression_level=None, bitrate_mode=None)[source]

Write data to a sound file.

Note

If file exists, it will be truncated and overwritten!

Parameters:

Examples

Write 10 frames of random data to a new file:

import numpy as np import soundfile as sf sf.write('stereo_file.wav', np.random.randn(10, 2), 44100, 'PCM_24')

soundfile.blocks(file, blocksize=None, overlap=0, frames=-1, start=0, stop=None, dtype='float64', always_2d=False, fill_value=None, out=None, samplerate=None, channels=None, format=None, subtype=None, endian=None, closefd=True)[source]

Return a generator for block-wise reading.

By default, iteration starts at the beginning and stops at the end of the file. Use start to start at a later position and _frames_or stop to stop earlier.

If you stop iterating over the generator before it’s exhausted, the sound file is not closed. This is normally not a problem because the file is opened in read-only mode. To close the file properly, the generator’s close() method can be called.

Parameters:

Yields:

numpy.ndarray or type(out) – Blocks of audio data. If out was given, and the requested frames are not an integer multiple of the length of out, and no fill_value was given, the last block will be a smaller view into out.

Examples

import soundfile as sf for block in sf.blocks('stereo_file.wav', blocksize=1024): pass # do something with 'block'

soundfile.info(file, verbose=False)[source]

Returns an object with information about a SoundFile.

Parameters:

verbose (bool) – Whether to print additional information.

soundfile.available_formats()[source]

Return a dictionary of available major formats.

Examples

import soundfile as sf sf.available_formats() {'FLAC': 'FLAC (FLAC Lossless Audio Codec)', 'OGG': 'OGG (OGG Container format)', 'WAV': 'WAV (Microsoft)', 'AIFF': 'AIFF (Apple/SGI)', ... 'WAVEX': 'WAVEX (Microsoft)', 'RAW': 'RAW (header-less)', 'MAT5': 'MAT5 (GNU Octave 2.1 / Matlab 5.0)'}

soundfile.available_subtypes(format=None)[source]

Return a dictionary of available subtypes.

Parameters:

format (str) – If given, only compatible subtypes are returned.

Examples

import soundfile as sf sf.available_subtypes('FLAC') {'PCM_24': 'Signed 24 bit PCM', 'PCM_16': 'Signed 16 bit PCM', 'PCM_S8': 'Signed 8 bit PCM'}

soundfile.check_format(format, subtype=None, endian=None)[source]

Check if the combination of format/subtype/endian is valid.

Examples

import soundfile as sf sf.check_format('WAV', 'PCM_24') True sf.check_format('FLAC', 'VORBIS') False

soundfile.default_subtype(format)[source]

Return the default subtype for a given format.

Examples

import soundfile as sf sf.default_subtype('WAV') 'PCM_16' sf.default_subtype('MAT5') 'DOUBLE'

class soundfile.SoundFile(file, mode='r', samplerate=None, channels=None, subtype=None, endian=None, format=None, closefd=True, compression_level=None, bitrate_mode=None)[source]

Open a sound file.

If a file is opened with mode 'r' (the default) or'r+', no sample rate, channels or file format need to be given because the information is obtained from the file. An exception is the 'RAW' data format, which always requires these data points.

File formats consist of three case-insensitive strings:

A SoundFile object is a context manager, which means if used in a “with” statement, close() is automatically called when reaching the end of the code block inside the “with” statement.

Parameters:

Examples

from soundfile import SoundFile

Open an existing file for reading:

myfile = SoundFile('existing_file.wav')

do something with myfile

myfile.close()

Create a new sound file for reading and writing using a with statement:

with SoundFile('new_file.wav', 'x+', 44100, 2) as myfile: # do something with myfile # ... assert not myfile.closed # myfile.close() is called automatically at the end assert myfile.closed

property name

The file name of the sound file.

property mode

The open mode the sound file was opened with.

property samplerate

The sample rate of the sound file.

property frames

The number of frames in the sound file.

property channels

The number of channels in the sound file.

property format

The major format of the sound file.

property subtype

The subtype of data in the the sound file.

property endian

The endian-ness of the data in the sound file.

property format_info

A description of the major format of the sound file.

property subtype_info

A description of the subtype of the sound file.

property sections

The number of sections of the sound file.

property closed

Whether the sound file is closed or not.

property compression_level

The compression level on ‘write()’

property bitrate_mode

The bitrate mode on ‘write()’

Retrieve the log string generated when opening the file.

seekable()[source]

Return True if the file supports seeking.

seek(frames, whence=0)[source]

Set the read/write position.

Parameters:

Returns:

int – The new absolute read/write position in frames.

Examples

from soundfile import SoundFile, SEEK_END myfile = SoundFile('stereo_file.wav')

Seek to the beginning of the file:

Seek to the end of the file:

myfile.seek(0, SEEK_END) 44100 # this is the file length

tell()[source]

Return the current read/write position.

read(frames=-1, dtype='float64', always_2d=False, fill_value=None, out=None)[source]

Read from the file and return data as NumPy array.

Reads the given number of frames in the given data format starting at the current read/write position. This advances the read/write position by the same number of frames. By default, all frames from the current read/write position to the end of the file are returned. Use seek() to move the current read/write position.

Parameters:

Returns:

audiodata (numpy.ndarray or type(out)) – A two-dimensional NumPy (frames x channels) array is returned. If the sound file has only one channel, a one-dimensional array is returned. Use always_2d=Trueto return a two-dimensional array anyway.

If out was specified, it is returned. If out has more frames than available in the file (or if frames is smaller than the length of out) and no fill_value is given, then only a part of out is overwritten and a view containing all valid frames is returned.

Examples

from soundfile import SoundFile myfile = SoundFile('stereo_file.wav')

Reading 3 frames from a stereo file:

myfile.read(3) array([[ 0.71329652, 0.06294799], [-0.26450912, -0.38874483], [ 0.67398441, -0.11516333]]) myfile.close()

See also

buffer_read, write

buffer_read(frames=-1, dtype=None)[source]

Read from the file and return data as buffer object.

Reads the given number of frames in the given data format starting at the current read/write position. This advances the read/write position by the same number of frames. By default, all frames from the current read/write position to the end of the file are returned. Use seek() to move the current read/write position.

Parameters:

Returns:

buffer – A buffer containing the read data.

buffer_read_into(buffer, dtype)[source]

Read from the file into a given buffer object.

Fills the given buffer with frames in the given data format starting at the current read/write position (which can be changed with seek()) until the buffer is full or the end of the file is reached. This advances the read/write position by the number of frames that were read.

Parameters:

Returns:

int – The number of frames that were read from the file. This can be less than the size of buffer. The rest of the buffer is not filled with meaningful data.

See also

buffer_read, read

write(data)[source]

Write audio data from a NumPy array to the file.

Writes a number of frames at the read/write position to the file. This also advances the read/write position by the same number of frames and enlarges the file if necessary.

Note that writing int values to a float file will not scale the values to [-1.0, 1.0). If you write the valuenp.array([42], dtype='int32'), to a subtype='FLOAT'file, the file will then contain np.array([42.], dtype='float32').

Parameters:

data (array_like) –

The data to write. Usually two-dimensional (frames x channels), but one-dimensional data can be used for mono files. Only the data types 'float64', 'float32','int32' and 'int16' are supported.

Note

The data type of data does not select the data type of the written file. Audio data will be converted to the given subtype. Writing int values to a float file will not scale the values to [-1.0, 1.0). If you write the value np.array([42], dtype='int32'), to a subtype='FLOAT' file, the file will then contain np.array([42.], dtype='float32').

Examples

import numpy as np from soundfile import SoundFile myfile = SoundFile('stereo_file.wav')

Write 10 frames of random data to a new file:

with SoundFile('stereo_file.wav', 'w', 44100, 2, 'PCM_24') as f: f.write(np.random.randn(10, 2))

See also

buffer_write, read

buffer_write(data, dtype)[source]

Write audio data from a buffer/bytes object to the file.

Writes the contents of data to the file at the current read/write position. This also advances the read/write position by the number of frames that were written and enlarges the file if necessary.

Parameters:

See also

write, buffer_read

blocks(blocksize=None, overlap=0, frames=-1, dtype='float64', always_2d=False, fill_value=None, out=None)[source]

Return a generator for block-wise reading.

By default, the generator yields blocks of the given_blocksize_ (using a given overlap) until the end of the file is reached; frames can be used to stop earlier.

Parameters:

Yields:

numpy.ndarray or type(out) – Blocks of audio data. If out was given, and the requested frames are not an integer multiple of the length of out, and no_fill_value_ was given, the last block will be a smaller view into out.

Examples

from soundfile import SoundFile with SoundFile('stereo_file.wav') as f: for block in f.blocks(blocksize=1024): pass # do something with 'block'

truncate(frames=None)[source]

Truncate the file to a given number of frames.

After this command, the read/write position will be at the new end of the file.

Parameters:

frames (int, optional) – Only the data before frames is kept, the rest is deleted. If not specified, the current read/write position is used.

flush()[source]

Write unwritten data to the file system.

Data written with write() is not immediately written to the file system but buffered in memory to be written at a later time. Calling flush() makes sure that all changes are actually written to the file system.

This has no effect on files opened in read-only mode.

close()[source]

Close the file. Can be called multiple times.

copy_metadata()[source]

Get all metadata present in this SoundFile

Returns:

metadata (dict[str, str]) – A dict with all metadata. Possible keys are: ‘title’, ‘copyright’, ‘software’, ‘artist’, ‘comment’, ‘date’, ‘album’, ‘license’, ‘tracknumber’ and ‘genre’.

exception soundfile.SoundFileError[source]

Base class for all soundfile-specific errors.

exception soundfile.SoundFileRuntimeError[source]

soundfile module runtime error.

Errors that used to be RuntimeError.

exception soundfile.LibsndfileError(code, prefix='')[source]

libsndfile errors.

code

libsndfile internal error number.

property error_string

Raw libsndfile error message.

Index