PySoundFile is an audio library based on libsndfile, CFFI and Numpy
PySoundFile can read and write sound files. File reading/writing is supported through libsndfile, which is a free, cross-platform, open-source 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+. PySoundFile represents audio data as NumPy arrays.
On the Python side, you need to have CFFI and Numpy in order to use
PySoundFile. Additionally, You need the library libsndfile installed on
your computer. On Unix, use your package manager to install libsndfile.
Then just install PySoundFile using pip or python setup.py install.
If you are running Windows, I recommend using WinPython or some similar distribution. This should set you up with Numpy. However, you also need CFFI and it's dependency, PyCParser. A good place to get these are the Unofficial Windows Binaries for Python. Having installed those, you can download the Windows installers for PySoundFile:
Each SoundFile can either open a sound file on the disk, or a file-like
object (using libsndfile's virtual file
interface).
Every sound file has a specific samplerate, data format and a set number
of channels.
You can read and write any file that libsndfile can open. This includes Microsoft WAV, OGG, FLAC and Matlab MAT files.
If a file on disk is opened, it is kept open for as long as the SoundFile object exists and closes automatically when it goes out of scope. Alternatively, the SoundFile object can be used as a context manager, which closes the file when it exits.
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.
Data can be written to the file using write(), or read from the
file using read().
Here is an example for a program that reads a wave file and copies it into an ogg-vorbis file:
import pysoundfile as sf
data, samplerate = sf.read('existing_file.wav')
sf.write(data, 'new_file.ogg', samplerate=samplerate)If you have an open file-like object, you can use something similar to this to decode it:
from pysoundfile import SoundFile
with SoundFile('filename.flac', 'rb') as fObj:
data, samplerate = sf.read(fObj)Here is an example using an HTTP request:
from io import BytesIO
import pysoundfile as sf
import requests
fObj = BytesIO()
response = requests.get('http://www.example.com/my.flac', stream=True)
for data in response.iter_content(4096):
if data:
fObj.write(data)
fObj.seek(0)
data, samplerate = sf.read(fObj)In addition to audio data, there are a number of text fields in every sound file. In particular, you can set a title, a copyright notice, a software description, the artist name, a comment, a date, the album name, a license, a tracknumber and a genre. Note however, that not all of these fields are supported for every file format.