[SciPy-user] pyaudio, a module to make noise from numpy arrays

David Cournapeau david at ar.media.kyoto-u.ac.jp
Fri Oct 27 04:46:03 CDT 2006

    I announce the first release of pyaudio, a module to make noise from 
numpy arrays (read, write and play audio files with numpy arrays).

 * WHAT FOR ?:

    The Goal is to give to a numpy/scipy environmenet some basic audio 
IO facilities (ala sound, wavread, wavwrite of matlab).

    With pyaudio, you should be able to read and write most common audio 
files from and to numpy arrays. The underlying IO operations are done 
using libsndfile from Erik Castro Lopo 
(http://www.mega-nerd.com/libsndfile/), so he is the one who did the 
hard work. As libsndfile has support for a vast number of audio files 
(including wav, aiff, but also htk, ircam, and flac, an open source 
lossless codec), pyaudio enables the import from and export to a fairly 
large number of file formats.
    There is also a really crude player, which uses tempfile to play 
audio, and which only works for linux-alsa anyway. I intend to add 
better support at least for linux, and for other platforms if this does 
not involve too much hassle.

    So basically, if you are lucky enough to use a recent linux system, 
pyaudio already gives you the equivalent of wavread, wavwrite and sound.




    Just untar the package and drop it into scipy/Lib/sandbox, and add 
the two following lines to scipy/Lib/sandbox/setup.py:
    # Package to make some noise using numpy

    (if libsndfile.so is not in /usr/lib, a fortiori if you are a 
windows user, you should also change set the right location for 
libsndfile in pyaudio/pysndfile.py, at the line 
_snd.cdll.LoadLibrary('/usr/lib/libsndfile.so') )


    == Reading example ==

    # Reading from '/home/david/blop.flac'
    from scipy.sandbox.pyaudio import sndfile

    a = sndfile('/home/david/blop.flac')
    print a

    tmp = a.read_frames_float(1024)

    --> Prints:

    File        : /home/david/blop.flac
    Sample rate : 44100
    Channels    : 2
    Frames      : 9979776
    Format      : 0x00170002
    Sections    : 1
    Seekable    : True
    Duration    : 00:03:46.298

    And put into tmp the 1024 first frames (a frame is the equivalent of 
a sample, but taking into account the number of channels: so 1024 frames 
gives you 2048 samples here).

    == Writing example ==
    # Writing to a wavfile:
    from scipy.sandbox.pyaudio import sndfile
    import numpy as N

    noise     = N.random.randn((44100))
    a         = sndfile('/home/david/blop.flac', sfm['SFM_WRITE'], 
sf_format['SF_FORMAT_WAV'] | sf_format['SF_FORMAT_PCM16'],
        1, 44100)

    a.write_frames(noise, 44100)

    -> should gives you a lossless compressed white noise !

    This is really a first release, not really tested, not much 
documentation, I can just say it works for me. I haven't found a good 
way to emulate enumerations, which libsndfile uses a lot, so I am using 
dictionaries generated from the library C header to get a relation enum 
label <=> value. If someone has a better idea, I am open to suggestions !



More information about the SciPy-user mailing list