Python module — aubio 0.4.6 documentation

# Python module

The aubio extension for Python is available for Python 2.7 and Python 3.

## Installing aubio with pip

aubio can now be installed using `pip`:

    $ pip install aubio

## Building the module

From `aubio` source directory, run the following:

    $ ./setup.py clean
    $ ./setup.py build
    $ sudo ./setup.py install

## Using aubio in python

Once you have python-aubio installed, you should be able to run `python -c "import aubio; print(aubio.version)"`.

### A simple example

Here is a `simple script` that reads all the samples from a media file:

    #! /usr/bin/env python
    import sys, aubio

    samplerate = 0  # use original source samplerate
    hop_size = 256 # number of frames to read in one block
    s = aubio.source(sys.argv[1], samplerate, hop_size)
    total_frames = 0

    while True: # reading loop
        samples, read = s()
        total_frames += read
        if read < hop_size: break # end of file reached

    fmt_string = "read {:d} frames at {:d}Hz from {:s}"
    print (fmt_string.format(total_frames, s.samplerate, sys.argv[1]))

### Filtering an input sound file

Here is a more complete example, `demo_filter.py`. This files executes the following:

- read an input media file (`aubio.source`)
- filter it using an A-weighting filter (`aubio.digital_filter`)
- write result to a new file (`aubio.sink`)

    #! /usr/bin/env python

    def apply_filter(path):
        from aubio import source, sink, digital_filter
        from os.path import basename, splitext

        # open input file, get its samplerate
        s = source(path)
        samplerate = s.samplerate

        # create an A-weighting filter
        f = digital_filter(7)
        f.set_a_weighting(samplerate)
        # alternatively, apply another filter

        # create output file
        o = sink("filtered_" + splitext(basename(path))[0] + ".wav", samplerate)

        total_frames = 0
        while True:
            samples, read = s()
            filtered_samples = f(samples)
            o(filtered_samples, read)
            total_frames += read
            if read < s.hop_size: break

        duration = total_frames / float(samplerate)
        print ("read {:s}".format(s.uri))
        print ("applied A-weighting filtered ({:d} Hz)".format(samplerate))
        print ("wrote {:s} ({:.2f} s)".format(o.uri, duration))

    if __name__ == '__main__':
        import sys
        for f in sys.argv[1:]:
            apply_filter(f)

### More demos

Check out the python demos folder for more examples.

## Python tests

A number of python tests are provided. To run them, use `python/tests/run_all_tests`.