Search code examples
pythonnumpyscipyscikits

How to read a wave file block by block in python (compatible with numpy array)?


Instead of loading the entire wave file, I want to read a wave file block by block in python (compatible with numpy array). That is, only a portion of wave file is to be loaded into the numpy array at a time. I am aware of scipy.io.wavfile.read(somefile) and scikits.audiolab.wavread(filename). But they do not support blockwise. Entire wavefile is loaded. The problem is when the wave file size is very large, a lot of memory is used.


Solution

  • How about trying PySoundFile? According to the site:

    Sound files can also be read in short, optionally overlapping blocks. For example, this calculates the signal level for each block of a long file:

    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)]
    

    I hope this helps.