python-soundfile
================
|version| |python| |status| |license|
|contributors| |downloads|
The `soundfile <https://github.com/bastibe/python-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 <http://www.mega-nerd.com/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 through
`CFFI <https://cffi.readthedocs.io/>`__, 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|
.. |contributors| image:: https://img.shields.io/github/contributors/bastibe/python-soundfile.svg
.. |version| image:: https://img.shields.io/pypi/v/soundfile.svg
.. |python| image:: https://img.shields.io/pypi/pyversions/soundfile.svg
.. |license| image:: https://img.shields.io/github/license/bastibe/python-soundfile.svg
.. |downloads| image:: https://img.shields.io/pypi/dm/soundfile.svg
.. |open-issues| image:: https://img.shields.io/github/issues/bastibe/python-soundfile.svg
.. |closed-issues| image:: https://img.shields.io/github/issues-closed/bastibe/python-soundfile.svg
.. |open-prs| image:: https://img.shields.io/github/issues-pr/bastibe/python-soundfile.svg
.. |closed-prs| image:: https://img.shields.io/github/issues-pr-closed/bastibe/python-soundfile.svg
.. |status| image:: https://img.shields.io/pypi/status/soundfile.svg
Breaking Changes
----------------
The ``soundfile`` module has evolved rapidly in the past. Most
notably, we changed the import name from ``import pysoundfile`` to
``import 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 ``True``
to ``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 old
``ctype`` 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.
.. _Anaconda: https://www.continuum.io/downloads
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 run
``python 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
<http://www.mega-nerd.com/libsndfile/#Features>`__, for example WAV,
FLAC, OGG and MAT files (see `Known Issues <https://github.com/bastibe/python-soundfile#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:
.. code:: python
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:
.. code:: python
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. Every
`SoundFile` 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 `SoundFile`
object 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:
.. code:: python
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:
.. code:: python
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:
.. code:: python
import soundfile as sf
with open('filename.flac', 'rb') as f:
data, samplerate = sf.read(f)
Here is an example using an HTTP request:
.. code:: python
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:
.. code:: python
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):
.. code:: python
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'.
.. code:: python
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 <https://github.com/bastibe/python-soundfile/issues/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 <https://bugs.python.org/issue13508>`__. 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 <https://github.com/bastibe/python-soundfile/issues/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!
- Switched to ``float64`` as default data type.
- Function arguments changed for consistency.
- Added unit tests.
- Added global `read()`, `write()`, `blocks()` convenience
functions.
- Documentation overhaul and hosting on readthedocs.
- Added ``'x'`` open mode.
- Added `tell()` method.
- Added ``__repr__()`` method.
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.
- Renamed ``import pysoundfile`` to ``import soundfile``.
- Installation through pip wheels that contain the necessary
libraries for OS X and Windows.
- Removed ``exclusive_creation`` argument to `write()`.
- Added `truncate()` method.
2015-10-20 V0.8.0 Bastian Bechtold:
Again, Matthias Geier contributed a whole lot of hard work to this
release.
- Changed the default value of ``always_2d`` from ``True`` to
``False``.
- Numpy is now optional, and only loaded for ``read`` and
``write``.
- Added `SoundFile.buffer_read()` and
`SoundFile.buffer_read_into()` and `SoundFile.buffer_write()`,
which read/write raw data without involving Numpy.
- Added `info()` function that returns metadata of a sound file.
- Changed the argument order of the `write()` function from
``write(data, file, ...)`` to ``write(file, data, ...)``
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.
- Adds support for ALAC files.
- Adds new member ``__libsndfile_version__``
- Adds number of frames to ``info`` class
- Adds ``dtype`` argument to ``buffer_*`` methods
- Deprecates ``ctype`` argument to ``buffer_*`` methods
- Adds official support for Python 3.6
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.
- Should now work with cx_freeze.
- Several documentation fixes in the README.
- Removes deprecated ``ctype`` argument in favor of ``dtype`` in ``buffer_*()``.
- Adds `SoundFile.frames` in favor of now-deprecated ``__len__()``.
- Improves performance of `blocks()` and `SoundFile.blocks()`.
- Improves import time by using CFFI's out of line mode.
- Adds a build script for building distributions.
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
- MP3 support
- Adds binary wheels for macOS M1
- Improves compatibility with macOS, specifically for M1 machines
- Fixes file descriptor open for binary wheels on Windows and Python 3.5+
- Updates libsndfile to v1.1.0
- Adds get_strings method for retrieving all metadata at once
- Improves documentation, error messages and tests
- Displays length of very short files in samples
- Supports the file system path protocol (pathlib et al)
2023-02-02 V0.12.0 Bastian Bechtold
Thank you, Barabazs, Andrew Murray, Jon Peirce, for contributions
to this release.
- Updated libsndfile to v1.2.0
- Improves precompiled library location, especially with py2app or cx-freeze.
- Now provide binary wheels for Linux x86_64
- Now prefers packaged libsndfile over system-installed libsndfile
2023-02-15 V0.12.1 Bastian Bechtold
Thank you, funnypig, for the bug report
- Fixed typo on library location detection if no packaged lib and
no system lib was found
2025-01-02 V0.13.0 Bastian Bechtold
Thank you, Zhong Jianxin, mcclure, jneuendorf-i4h, aoirint, endolith, Guy Illes, ytya, Sam Lapp, Benjamin Moody
- Linux arm64 builds added
- Numpy is now a dependency
- Fixed error in blocks, if file is very short
- Compression level and bitrate controls added for compressed files
- Various README improvements
- Various build system improvements
- Various improvements to error messages
Raw data
{
"_id": null,
"home_page": "https://github.com/bastibe/python-soundfile",
"name": "soundfile",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "audio, libsndfile",
"author": "Bastian Bechtold",
"author_email": "basti@bastibe.de",
"download_url": "https://files.pythonhosted.org/packages/76/7c/4054082696ee09397eb88947f91cd2429bb7d20db0d4e82bfeb64c234589/soundfile-0.13.0.tar.gz",
"platform": "any",
"description": "python-soundfile\n================\n\n|version| |python| |status| |license|\n\n|contributors| |downloads|\n\nThe `soundfile <https://github.com/bastibe/python-soundfile>`__ module is an audio\nlibrary based on libsndfile, CFFI and NumPy. Full documentation is\navailable on https://python-soundfile.readthedocs.io/.\n\nThe ``soundfile`` module can read and write sound files. File reading/writing is\nsupported through `libsndfile <http://www.mega-nerd.com/libsndfile/>`__,\nwhich is a free, cross-platform, open-source (LGPL) library for reading\nand writing many different sampled sound file formats that runs on many\nplatforms including Windows, OS X, and Unix. It is accessed through\n`CFFI <https://cffi.readthedocs.io/>`__, which is a foreign function\ninterface for Python calling C code. CFFI is supported for CPython 2.6+,\n3.x and PyPy 2.0+. The ``soundfile`` module represents audio data as NumPy arrays.\n\n| python-soundfile is BSD licensed (BSD 3-Clause License).\n| (c) 2013, Bastian Bechtold\n\n\n|open-issues| |closed-issues| |open-prs| |closed-prs|\n\n.. |contributors| image:: https://img.shields.io/github/contributors/bastibe/python-soundfile.svg\n.. |version| image:: https://img.shields.io/pypi/v/soundfile.svg\n.. |python| image:: https://img.shields.io/pypi/pyversions/soundfile.svg\n.. |license| image:: https://img.shields.io/github/license/bastibe/python-soundfile.svg\n.. |downloads| image:: https://img.shields.io/pypi/dm/soundfile.svg\n.. |open-issues| image:: https://img.shields.io/github/issues/bastibe/python-soundfile.svg\n.. |closed-issues| image:: https://img.shields.io/github/issues-closed/bastibe/python-soundfile.svg\n.. |open-prs| image:: https://img.shields.io/github/issues-pr/bastibe/python-soundfile.svg\n.. |closed-prs| image:: https://img.shields.io/github/issues-pr-closed/bastibe/python-soundfile.svg\n.. |status| image:: https://img.shields.io/pypi/status/soundfile.svg\n\nBreaking Changes\n----------------\n\nThe ``soundfile`` module has evolved rapidly in the past. Most\nnotably, we changed the import name from ``import pysoundfile`` to\n``import soundfile`` in 0.7. In 0.6, we cleaned up many small\ninconsistencies, particularly in the the ordering and naming of\nfunction arguments and the removal of the indexing interface.\n\nIn 0.8.0, we changed the default value of ``always_2d`` from ``True``\nto ``False``. Also, the order of arguments of the ``write`` function\nchanged from ``write(data, file, ...)`` to ``write(file, data, ...)``.\n\nIn 0.9.0, we changed the ``ctype`` arguments of the ``buffer_*``\nmethods to ``dtype``, using the Numpy ``dtype`` notation. The old\n``ctype`` arguments still work, but are now officially deprecated.\n\nIn 0.12.0, we changed the load order of the libsndfile library. Now,\nthe packaged libsndfile in the platform-specific wheels is tried\nbefore falling back to any system-provided libsndfile. If you would\nprefer using the system-provided libsndfile, install the source\npackage or source wheel instead of the platform-specific wheels.\n\nInstallation\n------------\n\nThe ``soundfile`` module depends on the Python packages CFFI and NumPy, and the\nlibrary libsndfile.\n\nIn a modern Python, you can use ``pip install soundfile`` to download\nand install the latest release of the ``soundfile`` module and its\ndependencies. On Windows (64/32) and OS X (Intel/ARM) and Linux 64,\nthis will also install a current version of the library libsndfile. If\nyou install the source module, you need to install libsndfile using\nyour distribution's package manager, for example ``sudo apt install\nlibsndfile1``.\n\nIf you are running on an unusual platform or if you are using an older\nversion of Python, you might need to install NumPy and CFFI separately,\nfor example using the Anaconda_ package manager.\n\n.. _Anaconda: https://www.continuum.io/downloads\n\nBuilding\n--------\n\n``Soundfile`` itself does not contain any compiled code and can be\nbundled into a wheel with the usual ``python setup.py bdist_wheel``.\nHowever, ``soundfile`` relies on libsndfile, and optionally ships its\nown copy of libsndfile in the wheel.\n\nTo build a binary wheel that contains libsndfile, make sure to\ncheckout and update the ``_soundfile_data`` submodule, then run\n``python setup.py bdist_wheel`` as usual. If the resulting file size\nof the wheel is around one megabyte, a matching libsndfile has been\nbundled (without libsndfile, it's around 25 KB).\n\nTo build binary wheels for all supported platforms, run ``python\nbuild_wheels.py``, which will ``python setup.py bdist_wheel`` for each\nof the platforms we have precompiled libsndfiles for.\n\nError Reporting\n---------------\n\nIn case of API usage errors the ``soundfile`` module raises the usual `ValueError` or `TypeError`.\n\nFor other errors `SoundFileError` is raised (used to be `RuntimeError`).\nParticularly, a `LibsndfileError` subclass of this exception is raised on\nerrors reported by the libsndfile library. In that case the exception object\nprovides the libsndfile internal error code in the `LibsndfileError.code` attribute and the raw\nlibsndfile error message in the `LibsndfileError.error_string` attribute.\n\nRead/Write Functions\n--------------------\n\nData can be written to the file using `soundfile.write()`, or read from\nthe file using `soundfile.read()`. The ``soundfile`` module can open all file formats\nthat `libsndfile supports\n<http://www.mega-nerd.com/libsndfile/#Features>`__, for example WAV,\nFLAC, OGG and MAT files (see `Known Issues <https://github.com/bastibe/python-soundfile#known-issues>`__ below about writing OGG files).\n\nHere is an example for a program that reads a wave file and copies it\ninto an FLAC file:\n\n.. code:: python\n\n import soundfile as sf\n\n data, samplerate = sf.read('existing_file.wav')\n sf.write('new_file.flac', data, samplerate)\n\nBlock Processing\n----------------\n\nSound files can also be read in short, optionally overlapping blocks\nwith `soundfile.blocks()`.\nFor example, this calculates the signal level for each block of a long\nfile:\n\n.. code:: python\n\n import numpy as np\n import soundfile as sf\n\n rms = [np.sqrt(np.mean(block**2)) for block in\n sf.blocks('myfile.wav', blocksize=1024, overlap=512)]\n\n``SoundFile`` Objects\n---------------------\n\nSound files can also be opened as `SoundFile` objects. Every\n`SoundFile` has a specific sample rate, data format and a set number of\nchannels.\n\nIf a file is opened, it is kept open for as long as the `SoundFile`\nobject exists. The file closes when the object is garbage collected,\nbut you should use the `SoundFile.close()` method or the\ncontext manager to close the file explicitly:\n\n.. code:: python\n\n import soundfile as sf\n\n with sf.SoundFile('myfile.wav', 'r+') as f:\n while f.tell() < f.frames:\n pos = f.tell()\n data = f.read(1024)\n f.seek(pos)\n f.write(data*2)\n\nAll data access uses frames as index. A frame is one discrete time-step\nin the sound file. Every frame contains as many samples as there are\nchannels in the file.\n\nRAW Files\n---------\n\n`soundfile.read()` can usually auto-detect the file type of sound files. This\nis not possible for RAW files, though:\n\n.. code:: python\n\n import soundfile as sf\n\n data, samplerate = sf.read('myfile.raw', channels=1, samplerate=44100,\n subtype='FLOAT')\n\nNote that on x86, this defaults to ``endian='LITTLE'``. If you are\nreading big endian data (mostly old PowerPC/6800-based files), you\nhave to set ``endian='BIG'`` accordingly.\n\nYou can write RAW files in a similar way, but be advised that in most\ncases, a more expressive format is better and should be used instead.\n\nVirtual IO\n----------\n\nIf you have an open file-like object, `soundfile.read()` can open it just like\nregular files:\n\n.. code:: python\n\n import soundfile as sf\n with open('filename.flac', 'rb') as f:\n data, samplerate = sf.read(f)\n\nHere is an example using an HTTP request:\n\n.. code:: python\n\n import io\n import soundfile as sf\n from urllib.request import urlopen\n\n url = \"http://tinyurl.com/shepard-risset\"\n data, samplerate = sf.read(io.BytesIO(urlopen(url).read()))\n\nNote that the above example only works with Python 3.x.\nFor Python 2.x support, replace the third line with:\n\n.. code:: python\n\n from urllib2 import urlopen\n\nIn-memory files\n^^^^^^^^^^^^^^^\n\nChunks of audio, i.e. `bytes`, can also be read and written without touching the filesystem.\nIn the following example OGG is converted to WAV entirely in memory (without writing files to the disk):\n\n.. code:: python\n\n import io\n import soundfile as sf\n\n def ogg2wav(ogg: bytes):\n ogg_buf = io.BytesIO(ogg)\n ogg_buf.name = 'file.ogg'\n data, samplerate = sf.read(ogg_buf)\n wav_buf = io.BytesIO()\n wav_buf.name = 'file.wav'\n sf.write(wav_buf, data, samplerate)\n wav_buf.seek(0) # Necessary for `.read()` to return all bytes\n return wav_buf.read()\n\nControlling bitrate mode and compression level\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nFor some audio formats, you can control the bitrate and compression level. \n\n`compression_level` is a float between 0 and 1, with 1 being the highest compression, \nand `bitrate_mode` is 'VARIABLE', 'CONSTANT', or 'AVERAGE'.\n\n.. code:: python\n\n import soundfile as sf\n \n # for example, this uncompressed 5 minute wav file with 32 kHz sample rate is 18 Mb\n data, samplerate = sf.read('5min_32kHz.wav') \n \n # maximum mp3 compression results in 1.1 Mb file, with either CONSTANT or VARIABLE bit rate\n sf.write('max_compression_vbr.mp3', data, samplerate, bitrate_mode='VARIABLE', compression_level=.99) \n sf.write('max_compression_cbr.mp3', data, samplerate, bitrate_mode='CONSTANT', compression_level=.99)\n \n # minimum mp3 compression results in 3.5 Mb file\n sf.write('min_compression_vbr.mp3', data, samplerate, bitrate_mode='VARIABLE', compression_level=0)\n\nKnown Issues\n------------\n\nWriting to OGG files can result in empty files with certain versions of libsndfile. See `#130 <https://github.com/bastibe/python-soundfile/issues/130>`__ for news on this issue.\n\nIf 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 <https://bugs.python.org/issue13508>`__. 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 <https://github.com/bastibe/python-soundfile/issues/258>`__ for discussion on this issue.\n\nNews\n----\n\n2013-08-27 V0.1.0 Bastian Bechtold:\n Initial prototype. A simple wrapper for libsndfile in Python\n\n2013-08-30 V0.2.0 Bastian Bechtold:\n Bugfixes and more consistency with PySoundCard\n\n2013-08-30 V0.2.1 Bastian Bechtold:\n Bugfixes\n\n2013-09-27 V0.3.0 Bastian Bechtold:\n Added binary installer for Windows, and context manager\n\n2013-11-06 V0.3.1 Bastian Bechtold:\n Switched from distutils to setuptools for easier installation\n\n2013-11-29 V0.4.0 Bastian Bechtold:\n Thanks to David Blewett, now with Virtual IO!\n\n2013-12-08 V0.4.1 Bastian Bechtold:\n Thanks to Xidorn Quan, FLAC files are not float32 any more.\n\n2014-02-26 V0.5.0 Bastian Bechtold:\n Thanks to Matthias Geier, improved seeking and a flush() method.\n\n2015-01-19 V0.6.0 Bastian Bechtold:\n A big, big thank you to Matthias Geier, who did most of the work!\n\n - Switched to ``float64`` as default data type.\n - Function arguments changed for consistency.\n - Added unit tests.\n - Added global `read()`, `write()`, `blocks()` convenience\n functions.\n - Documentation overhaul and hosting on readthedocs.\n - Added ``'x'`` open mode.\n - Added `tell()` method.\n - Added ``__repr__()`` method.\n\n2015-04-12 V0.7.0 Bastian Bechtold:\n Again, thanks to Matthias Geier for all of his hard work, but also\n Nils Werner and Whistler7 for their many suggestions and help.\n\n - Renamed ``import pysoundfile`` to ``import soundfile``.\n - Installation through pip wheels that contain the necessary\n libraries for OS X and Windows.\n - Removed ``exclusive_creation`` argument to `write()`.\n - Added `truncate()` method.\n\n2015-10-20 V0.8.0 Bastian Bechtold:\n Again, Matthias Geier contributed a whole lot of hard work to this\n release.\n\n - Changed the default value of ``always_2d`` from ``True`` to\n ``False``.\n - Numpy is now optional, and only loaded for ``read`` and\n ``write``.\n - Added `SoundFile.buffer_read()` and\n `SoundFile.buffer_read_into()` and `SoundFile.buffer_write()`,\n which read/write raw data without involving Numpy.\n - Added `info()` function that returns metadata of a sound file.\n - Changed the argument order of the `write()` function from\n ``write(data, file, ...)`` to ``write(file, data, ...)``\n\n And many more minor bug fixes.\n\n2017-02-02 V0.9.0 Bastian Bechtold:\n Thank you, Matthias Geier, Tomas Garcia, and Todd, for contributions\n for this release.\n\n - Adds support for ALAC files.\n - Adds new member ``__libsndfile_version__``\n - Adds number of frames to ``info`` class\n - Adds ``dtype`` argument to ``buffer_*`` methods\n - Deprecates ``ctype`` argument to ``buffer_*`` methods\n - Adds official support for Python 3.6\n\n And some minor bug fixes.\n\n2017-11-12 V0.10.0 Bastian Bechtold:\n Thank you, Matthias Geier, Toni Barth, Jon Peirce, Till Hoffmann,\n and Tomas Garcia, for contributions to this release.\n\n - Should now work with cx_freeze.\n - Several documentation fixes in the README.\n - Removes deprecated ``ctype`` argument in favor of ``dtype`` in ``buffer_*()``.\n - Adds `SoundFile.frames` in favor of now-deprecated ``__len__()``.\n - Improves performance of `blocks()` and `SoundFile.blocks()`.\n - Improves import time by using CFFI's out of line mode.\n - Adds a build script for building distributions.\n\n2022-06-02 V0.11.0 Bastian Bechtold:\n Thank you, tennies, Hannes Helmholz, Christoph Boeddeker, Matt\n Vollrath, Matthias Geier, Jacek Konieczny, Boris Verkhovskiy,\n Jonas Haag, Eduardo Moguillansky, Panos Laganakos, Jarvy Jarvison,\n Domingo Ramirez, Tim Chagnon, Kyle Benesch, Fabian-Robert St\u00f6ter,\n Joe Todd\n\n - MP3 support\n - Adds binary wheels for macOS M1\n - Improves compatibility with macOS, specifically for M1 machines\n - Fixes file descriptor open for binary wheels on Windows and Python 3.5+\n - Updates libsndfile to v1.1.0\n - Adds get_strings method for retrieving all metadata at once\n - Improves documentation, error messages and tests\n - Displays length of very short files in samples\n - Supports the file system path protocol (pathlib et al)\n\n2023-02-02 V0.12.0 Bastian Bechtold\n Thank you, Barabazs, Andrew Murray, Jon Peirce, for contributions\n to this release.\n\n - Updated libsndfile to v1.2.0\n - Improves precompiled library location, especially with py2app or cx-freeze.\n - Now provide binary wheels for Linux x86_64\n - Now prefers packaged libsndfile over system-installed libsndfile\n\n2023-02-15 V0.12.1 Bastian Bechtold\n Thank you, funnypig, for the bug report\n\n - Fixed typo on library location detection if no packaged lib and\n no system lib was found\n\n2025-01-02 V0.13.0 Bastian Bechtold\n Thank you, Zhong Jianxin, mcclure, jneuendorf-i4h, aoirint, endolith, Guy Illes, ytya, Sam Lapp, Benjamin Moody\n\n - Linux arm64 builds added\n - Numpy is now a dependency\n - Fixed error in blocks, if file is very short\n - Compression level and bitrate controls added for compressed files\n - Various README improvements\n - Various build system improvements\n - Various improvements to error messages\n",
"bugtrack_url": null,
"license": "BSD 3-Clause License",
"summary": "An audio library based on libsndfile, CFFI and NumPy",
"version": "0.13.0",
"project_urls": {
"Homepage": "https://github.com/bastibe/python-soundfile"
},
"split_keywords": [
"audio",
" libsndfile"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d33579f0e4934eba4807dfcdc5589de4733186e1d89ecc313708f1ac0e255c45",
"md5": "d6d5255074e5ce1a980263871c7e3e9c",
"sha256": "6a732002843d267267de52367cbdb16dfea6c1f4e3de5e4ddfb2bd0b9b65dddf"
},
"downloads": -1,
"filename": "soundfile-0.13.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "d6d5255074e5ce1a980263871c7e3e9c",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 25706,
"upload_time": "2025-01-02T16:08:02",
"upload_time_iso_8601": "2025-01-02T16:08:02.792356Z",
"url": "https://files.pythonhosted.org/packages/d3/35/79f0e4934eba4807dfcdc5589de4733186e1d89ecc313708f1ac0e255c45/soundfile-0.13.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8dd4d21a409d4002b585e755fd0301dd9bc59c243e8be8013cade60506c8730c",
"md5": "0045cf888010783ffefe3f742de86f4f",
"sha256": "c28024e59ebf2e5b12f5d77a16eb3ef1527e7580d7bbebfb5645253368391385"
},
"downloads": -1,
"filename": "soundfile-0.13.0-py2.py3-none-macosx_10_9_x86_64.whl",
"has_sig": false,
"md5_digest": "0045cf888010783ffefe3f742de86f4f",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 1142204,
"upload_time": "2025-01-02T16:08:07",
"upload_time_iso_8601": "2025-01-02T16:08:07.271922Z",
"url": "https://files.pythonhosted.org/packages/8d/d4/d21a409d4002b585e755fd0301dd9bc59c243e8be8013cade60506c8730c/soundfile-0.13.0-py2.py3-none-macosx_10_9_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "24d9be447d3eb12d0e0c432f3c3d9c6baa6f27be475bcfd82d4e9c4f8f7e1838",
"md5": "c4c47ce84eaa9d60265ff4a8093a690e",
"sha256": "af3d06019040dd3db0e0b173baca76db9f7d59b5750a347892f2bf7763a8083c"
},
"downloads": -1,
"filename": "soundfile-0.13.0-py2.py3-none-macosx_11_0_arm64.whl",
"has_sig": false,
"md5_digest": "c4c47ce84eaa9d60265ff4a8093a690e",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 1101360,
"upload_time": "2025-01-02T16:08:10",
"upload_time_iso_8601": "2025-01-02T16:08:10.670037Z",
"url": "https://files.pythonhosted.org/packages/24/d9/be447d3eb12d0e0c432f3c3d9c6baa6f27be475bcfd82d4e9c4f8f7e1838/soundfile-0.13.0-py2.py3-none-macosx_11_0_arm64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "99c8cf31904a8c17f23fa2d31852b0cb509b5ea017783d6cbdf62d7c4ac0e631",
"md5": "6c8cef49a0e0d1f7852d950ba71fa845",
"sha256": "3adcf4b70d2c872f6cb5a1b3eeb916b90233860a7d13652652753c4e628ac9f5"
},
"downloads": -1,
"filename": "soundfile-0.13.0-py2.py3-none-manylinux_2_28_aarch64.whl",
"has_sig": false,
"md5_digest": "6c8cef49a0e0d1f7852d950ba71fa845",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 1235683,
"upload_time": "2025-01-02T16:08:14",
"upload_time_iso_8601": "2025-01-02T16:08:14.374629Z",
"url": "https://files.pythonhosted.org/packages/99/c8/cf31904a8c17f23fa2d31852b0cb509b5ea017783d6cbdf62d7c4ac0e631/soundfile-0.13.0-py2.py3-none-manylinux_2_28_aarch64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "212e93c9283e6d8a95900e390d888f9f3e2ae3ce274d3a11bd79117eeb8f0879",
"md5": "a8a8b8674db9d0ca4eca2d7377fa2de3",
"sha256": "5f2c7d389d6cde9af49ec73f5a32a06f7397a2add3808ceb94ebff4a116e3446"
},
"downloads": -1,
"filename": "soundfile-0.13.0-py2.py3-none-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "a8a8b8674db9d0ca4eca2d7377fa2de3",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 1313600,
"upload_time": "2025-01-02T16:08:17",
"upload_time_iso_8601": "2025-01-02T16:08:17.655219Z",
"url": "https://files.pythonhosted.org/packages/21/2e/93c9283e6d8a95900e390d888f9f3e2ae3ce274d3a11bd79117eeb8f0879/soundfile-0.13.0-py2.py3-none-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ff365b0758658194eaa4f90c126ebdbd56f6c35c9f15e1640afaaa584a476b4b",
"md5": "43743e1f106a30127586d0ac0bd5e97f",
"sha256": "217fd97b9a515b6b92d817c917bd7c3bc838e4ffe9b68c2a0659b70b9af1a5dd"
},
"downloads": -1,
"filename": "soundfile-0.13.0-py2.py3-none-win32.whl",
"has_sig": false,
"md5_digest": "43743e1f106a30127586d0ac0bd5e97f",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 899836,
"upload_time": "2025-01-02T16:08:20",
"upload_time_iso_8601": "2025-01-02T16:08:20.674415Z",
"url": "https://files.pythonhosted.org/packages/ff/36/5b0758658194eaa4f90c126ebdbd56f6c35c9f15e1640afaaa584a476b4b/soundfile-0.13.0-py2.py3-none-win32.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0369eda7a076709ada14a11604347ea3b80b3888101c93bbc739071d2f553df5",
"md5": "f19963b255509fd792bb5f3a25288025",
"sha256": "9fd67b1867fb7ce4a1bf1fd6600dfe9bf2af26b7ae3671719196c1d5632fa462"
},
"downloads": -1,
"filename": "soundfile-0.13.0-py2.py3-none-win_amd64.whl",
"has_sig": false,
"md5_digest": "f19963b255509fd792bb5f3a25288025",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 1019114,
"upload_time": "2025-01-02T16:08:23",
"upload_time_iso_8601": "2025-01-02T16:08:23.743919Z",
"url": "https://files.pythonhosted.org/packages/03/69/eda7a076709ada14a11604347ea3b80b3888101c93bbc739071d2f553df5/soundfile-0.13.0-py2.py3-none-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "767c4054082696ee09397eb88947f91cd2429bb7d20db0d4e82bfeb64c234589",
"md5": "24cdaa40eea44b9edc764d739c408ba1",
"sha256": "e83399d8bde7d73b117c33d6a1ec8571318338f89ce72f4c3d457e9768798355"
},
"downloads": -1,
"filename": "soundfile-0.13.0.tar.gz",
"has_sig": false,
"md5_digest": "24cdaa40eea44b9edc764d739c408ba1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 45971,
"upload_time": "2025-01-02T16:08:26",
"upload_time_iso_8601": "2025-01-02T16:08:26.445398Z",
"url": "https://files.pythonhosted.org/packages/76/7c/4054082696ee09397eb88947f91cd2429bb7d20db0d4e82bfeb64c234589/soundfile-0.13.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-02 16:08:26",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bastibe",
"github_project": "python-soundfile",
"travis_ci": true,
"coveralls": false,
"github_actions": true,
"lcname": "soundfile"
}