Search code examples
pythonaudiosampling

How do I sample audio for real-time processing?


Audio is a complex sine wave. I want to sample data from an audio file playing and manipulate it.

Each sample should be a list of the amplitudes of the sine wave at a number of intervals.

Thanks in advance!


Solution

  • If you're just looking to read a wav file, the wave library should do this fine: http://docs.python.org/2/library/wave.html

    For example:

    import wave
    CHUNK_SIZE=1024
    wf = wave.open('filename.wav')
    data = wf.readframes(CHUNK_SIZE)
    while data != '':
        do_something(data)
        data = wf.readframes(CHUNK_SIZE)
    

    Will read 1024 samples into data. That is, data will be an array of 1024 entries, each the amplitude at a particularly time, where that time is dependent on the framerate of the file. See this question: What does a audio frame contain? for more explanation.