PySoundFile


NamePySoundFile JSON
Version 0.9.0.post1 PyPI version JSON
download
home_pagehttps://github.com/bastibe/PySoundFile
SummaryAn audio library based on libsndfile, CFFI and NumPy
upload_time2017-02-03 08:34:48
maintainerNone
docs_urlNone
authorBastian Bechtold
requires_pythonNone
licenseBSD 3-Clause License
keywords audio libsndfile
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage
            PySoundFile
===========

`PySoundFile <https://github.com/bastibe/PySoundFile>`__ is an audio
library based on libsndfile, CFFI and NumPy. Full documentation is
available on http://pysoundfile.readthedocs.org/.

PySoundFile 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 <http://cffi.readthedocs.org/>`__, 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.

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


Breaking Changes
----------------

PySoundFile 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
------------

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

To install the Python dependencies, I recommend using the `Anaconda
<http://continuum.io/downloads>`__ distribution of Python 3. This will
come with all dependencies pre-installed. To install the dependencies
manually, you can use the ``conda`` package manager, which will
install all dependencies using ``conda install cffi numpy`` (conda is
also available independently of Anaconda with ``pip install conda;
conda init``).

With CFFI and NumPy installed, you can use ``pip install pysoundfile``
to download and install the latest release of PySoundFile. 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``.

Read/Write Functions
--------------------

Data can be written to the file using `soundfile.write()`, or read from
the file using `soundfile.read()`. PySoundFile can open all file formats
that `libsndfile supports
<http://www.mega-nerd.com/libsndfile/#Features>`__, for example WAV,
FLAC, OGG and MAT files.

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

.. code:: python

    import soundfile as sf

    data, samplerate = sf.read('existing_file.wav')
    sf.write('new_file.ogg', 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.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:

.. code:: python

   import soundfile as sf

   with sf.SoundFile('myfile.wav', 'r+') as f:
       while f.tell() < len(f):
           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:

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

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.
            

Raw data

            {
    "maintainer": null, 
    "docs_url": null, 
    "requires_python": null, 
    "maintainer_email": null, 
    "cheesecake_code_kwalitee_id": null, 
    "keywords": "audio,libsndfile", 
    "upload_time": "2017-02-03 08:34:48", 
    "author": "Bastian Bechtold", 
    "home_page": "https://github.com/bastibe/PySoundFile", 
    "github_user": "bastibe", 
    "download_url": "https://pypi.python.org/packages/07/fa/bbe4d1c4328eaa83c0656c729eabbf811377fc1e8416d42bf7f7af63ef8c/PySoundFile-0.9.0.post1.tar.gz", 
    "platform": "any", 
    "version": "0.9.0.post1", 
    "cheesecake_documentation_id": null, 
    "description": "PySoundFile\n===========\n\n`PySoundFile <https://github.com/bastibe/PySoundFile>`__ is an audio\nlibrary based on libsndfile, CFFI and NumPy. Full documentation is\navailable on http://pysoundfile.readthedocs.org/.\n\nPySoundFile 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 <http://cffi.readthedocs.org/>`__, which is a foreign function\ninterface for Python calling C code. CFFI is supported for CPython 2.6+,\n3.x and PyPy 2.0+. PySoundFile represents audio data as NumPy arrays.\n\n| PySoundFile is BSD licensed (BSD 3-Clause License).\n| (c) 2013, Bastian Bechtold\n\n\nBreaking Changes\n----------------\n\nPySoundFile has evolved rapidly during the last few releases. 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\nInstallation\n------------\n\nPySoundFile depends on the Python packages CFFI and NumPy, and the\nsystem library libsndfile.\n\nTo install the Python dependencies, I recommend using the `Anaconda\n<http://continuum.io/downloads>`__ distribution of Python 3. This will\ncome with all dependencies pre-installed. To install the dependencies\nmanually, you can use the ``conda`` package manager, which will\ninstall all dependencies using ``conda install cffi numpy`` (conda is\nalso available independently of Anaconda with ``pip install conda;\nconda init``).\n\nWith CFFI and NumPy installed, you can use ``pip install pysoundfile``\nto download and install the latest release of PySoundFile. On Windows\nand OS X, this will also install the library libsndfile. On Linux, you\nneed to install libsndfile using your distribution's package manager,\nfor example ``sudo apt-get install libsndfile1``.\n\nRead/Write Functions\n--------------------\n\nData can be written to the file using `soundfile.write()`, or read from\nthe file using `soundfile.read()`. PySoundFile can open all file formats\nthat `libsndfile supports\n<http://www.mega-nerd.com/libsndfile/#Features>`__, for example WAV,\nFLAC, OGG and MAT files.\n\nHere is an example for a program that reads a wave file and copies it\ninto an ogg-vorbis 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.ogg', 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\nSoundFile Objects\n-----------------\n\nSound files can also be opened as `soundfile.SoundFile` objects. Every\nSoundFile 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.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() < len(f):\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\nPysoundfile 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, Pysoundfile 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\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.", 
    "lcname": "pysoundfile", 
    "bugtrack_url": null, 
    "github": true, 
    "coveralls": true, 
    "name": "PySoundFile", 
    "license": "BSD 3-Clause License", 
    "travis_ci": true, 
    "github_project": "PySoundFile", 
    "summary": "An audio library based on libsndfile, CFFI and NumPy", 
    "split_keywords": [
        "audio", 
        "libsndfile"
    ], 
    "author_email": "basti@bastibe.de", 
    "urls": [
        {
            "has_sig": false, 
            "upload_time": "2017-02-03T08:34:43", 
            "comment_text": "", 
            "python_version": "3.4", 
            "url": "https://pypi.python.org/packages/2a/b3/0b871e5fd31b9a8e54b4ee359384e705a1ca1e2870706d2f081dc7cc1693/PySoundFile-0.9.0.post1-py2.py3-none-any.whl", 
            "md5_digest": "3ec5830c9ef1398e29510e4d1ae6b32f", 
            "downloads": 0, 
            "filename": "PySoundFile-0.9.0.post1-py2.py3-none-any.whl", 
            "packagetype": "bdist_wheel", 
            "path": "2a/b3/0b871e5fd31b9a8e54b4ee359384e705a1ca1e2870706d2f081dc7cc1693/PySoundFile-0.9.0.post1-py2.py3-none-any.whl", 
            "size": 24161
        }, 
        {
            "has_sig": false, 
            "upload_time": "2017-02-03T08:34:30", 
            "comment_text": "", 
            "python_version": "3.4", 
            "url": "https://pypi.python.org/packages/f7/62/bddd31ba4f575eca81c6642d809fe01ed5ee5f55ebb4e9b14d0a033cc6bf/PySoundFile-0.9.0.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", 
            "md5_digest": "19bac6d7d8a57ffecd2ec5742330d46c", 
            "downloads": 0, 
            "filename": "PySoundFile-0.9.0.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", 
            "packagetype": "bdist_wheel", 
            "path": "f7/62/bddd31ba4f575eca81c6642d809fe01ed5ee5f55ebb4e9b14d0a033cc6bf/PySoundFile-0.9.0.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", 
            "size": 573152
        }, 
        {
            "has_sig": false, 
            "upload_time": "2017-02-03T08:34:35", 
            "comment_text": "", 
            "python_version": "3.4", 
            "url": "https://pypi.python.org/packages/b8/d4/4c02ba1cee60e5c5c546de4761bcb7e42cba577216561067ffe40cf8fb0a/PySoundFile-0.9.0.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win32.whl", 
            "md5_digest": "06b1d7a654356d94f717bcf0b7773f50", 
            "downloads": 0, 
            "filename": "PySoundFile-0.9.0.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win32.whl", 
            "packagetype": "bdist_wheel", 
            "path": "b8/d4/4c02ba1cee60e5c5c546de4761bcb7e42cba577216561067ffe40cf8fb0a/PySoundFile-0.9.0.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win32.whl", 
            "size": 639232
        }, 
        {
            "has_sig": false, 
            "upload_time": "2017-02-03T08:34:39", 
            "comment_text": "", 
            "python_version": "3.4", 
            "url": "https://pypi.python.org/packages/9d/8e/30d9f80802e8ea2c5b96db2f320fdb147e25a84fd74caa251b57bedeeb33/PySoundFile-0.9.0.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win_amd64.whl", 
            "md5_digest": "21248257b79817ebe02d1eda239ece65", 
            "downloads": 0, 
            "filename": "PySoundFile-0.9.0.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win_amd64.whl", 
            "packagetype": "bdist_wheel", 
            "path": "9d/8e/30d9f80802e8ea2c5b96db2f320fdb147e25a84fd74caa251b57bedeeb33/PySoundFile-0.9.0.post1-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33-none-win_amd64.whl", 
            "size": 671839
        }, 
        {
            "has_sig": false, 
            "upload_time": "2017-02-03T08:34:48", 
            "comment_text": "", 
            "python_version": "source", 
            "url": "https://pypi.python.org/packages/07/fa/bbe4d1c4328eaa83c0656c729eabbf811377fc1e8416d42bf7f7af63ef8c/PySoundFile-0.9.0.post1.tar.gz", 
            "md5_digest": "99c64d39b5aaaa13c3a6c4a5fd0e3a74", 
            "downloads": 0, 
            "filename": "PySoundFile-0.9.0.post1.tar.gz", 
            "packagetype": "sdist", 
            "path": "07/fa/bbe4d1c4328eaa83c0656c729eabbf811377fc1e8416d42bf7f7af63ef8c/PySoundFile-0.9.0.post1.tar.gz", 
            "size": 1817942
        }
    ], 
    "_id": null, 
    "cheesecake_installability_id": null
}