Skip to main content

An audio library based on libsndfile, CFFI and NumPy

Project description

version python status license

contributors downloads

SoundFile is an audio library based on libsndfile, CFFI and NumPy. Full documentation is available on http://pysoundfile.readthedocs.org/.

SoundFile 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 through CFFI, which is a foreign function interface for Python calling C code. CFFI is supported for CPython 2.6+, 3.x and PyPy 2.0+. SoundFile represents audio data as NumPy arrays.

SoundFile is BSD licensed (BSD 3-Clause License).
(c) 2013, Bastian Bechtold

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

Breaking Changes

SoundFile has evolved rapidly during the last few releases. 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.

Installation

SoundFile depends on the Python packages CFFI and NumPy, and the system library libsndfile.

In a modern Python, you can use pip install soundfile to download and install the latest release of SoundFile and its dependencies. On Windows and OS X, this will also install the library libsndfile. On Linux, you need to install libsndfile using your distribution’s package manager, for example sudo apt-get 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 or the Unofficial Windows Binaries for Python Extension Packages.

Read/Write Functions

Data can be written to the file using soundfile.write(), or read from the file using soundfile.read(). SoundFile 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.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.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

Pysoundfile 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, Pysoundfile 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

Known Issues

Writing to OGG files can result in empty files with certain versions of libsndfile. See #130 for news 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SoundFile-0.10.3.post1.tar.gz (36.5 kB view details)

Uploaded Source

Built Distributions

SoundFile-0.10.3.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win_amd64.whl (689.4 kB view details)

Uploaded CPython 2.6CPython 2.7CPython 3.2CPython 3.3CPython 3.4CPython 3.5CPython 3.6PyPyPython 2Python 3Windows x86-64

SoundFile-0.10.3.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win32.whl (651.4 kB view details)

Uploaded CPython 2.6CPython 2.7CPython 3.2CPython 3.3CPython 3.4CPython 3.5CPython 3.6PyPyPython 2Python 3Windows x86

SoundFile-0.10.3.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-macosx_10_5_x86_64.macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl (613.4 kB view details)

Uploaded CPython 2.6CPython 2.7CPython 3.2CPython 3.3CPython 3.4CPython 3.5CPython 3.6PyPyPython 2Python 3macOS 10.5+ x86-64macOS 10.6+ Intel (x86-64, i386)macOS 10.9+ Intel (x86-64, i386)macOS 10.9+ x86-64

SoundFile-0.10.3.post1-py2.py3-none-any.whl (21.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file SoundFile-0.10.3.post1.tar.gz.

File metadata

  • Download URL: SoundFile-0.10.3.post1.tar.gz
  • Upload date:
  • Size: 36.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.6.9

File hashes

Hashes for SoundFile-0.10.3.post1.tar.gz
Algorithm Hash digest
SHA256 490cff42650733d1832728b937fe99fa1802896f5ef4d61bcf78cf7ebecb107b
MD5 a40320392dd3567fb7433f2ae41dfe1a
BLAKE2b-256 bc7c440d411e1bf2ef40ec450bb65a2b85ed8b23aaf12b7a99a1888ab551029c

See more details on using hashes here.

File details

Details for the file SoundFile-0.10.3.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win_amd64.whl.

File metadata

File hashes

Hashes for SoundFile-0.10.3.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win_amd64.whl
Algorithm Hash digest
SHA256 b361d4ac1519a2e516cabafa6bf7e93492f999f35d7d25350cd87fdc3e5cb27e
MD5 597550314814d52ef8d30c9db465257f
BLAKE2b-256 b8de24e4035f06540ebb4e9993238ede787063875b003e79c537511d32a74d29

See more details on using hashes here.

File details

Details for the file SoundFile-0.10.3.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win32.whl.

File metadata

File hashes

Hashes for SoundFile-0.10.3.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win32.whl
Algorithm Hash digest
SHA256 4555438c2c4f02b39fea2ed40f6ddeda88a80cd1ee9dd129be4d5f5134698cc2
MD5 83db7cf0ff2e925a9fbcbc242cb311b8
BLAKE2b-256 7363ebf4332964fba68f72cd78723a2722d77cf78f988b13461c6fbf30fc96bc

See more details on using hashes here.

File details

Details for the file SoundFile-0.10.3.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-macosx_10_5_x86_64.macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for SoundFile-0.10.3.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-macosx_10_5_x86_64.macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5e342ee293b896d31da67617fe65d0bdca217af193991b0cb6052353b1e0e506
MD5 6a6baf01fa867e2ee707cad5d3109092
BLAKE2b-256 1363073fc76f2405af3ad1a2e667ac237a527fcdd81d4cbc7e2b721727687453

See more details on using hashes here.

File details

Details for the file SoundFile-0.10.3.post1-py2.py3-none-any.whl.

File metadata

  • Download URL: SoundFile-0.10.3.post1-py2.py3-none-any.whl
  • Upload date:
  • Size: 21.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.6.9

File hashes

Hashes for SoundFile-0.10.3.post1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2d17e0a6fc2af0d6c1d868bafa5ec80aae6e186a97fec8db07ad6af29842fbc7
MD5 2ab2d5e742a472a537b5dc9dca97c9fd
BLAKE2b-256 ebf23cbbbf3b96fb9fa91582c438b574cff3f45b29c772f94c400e2c99ef5db9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page