beetbox / audioread

cross-library (GStreamer + Core Audio + MAD + FFmpeg) audio decoding for Python
MIT License
481 stars 108 forks source link

converting data to a numpy array #120

Open gmabey opened 2 years ago

gmabey commented 2 years ago

The docs would be much more useful to me if this introductory sample

with audioread.audio_open(filename) as f:
    print(f.channels, f.samplerate, f.duration)
    for buf in f:
        do_something(buf)

included some notion of how to convert buf to a numpy array. If you have a minute. (and, if that's easy to do)

sampsyo commented 2 years ago

While I don't have a code snippet lying around, a good place to start would be numpy's built-in frombuffer method: https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html

As you likely already know, audioread produces buffers containing 16-bit little-endian signed integers. So you'll want to invoke frombuffer with that number format.

JustasB commented 1 year ago

The following function will read a stereo file into numpy arrays:

import numpy as np
import audioread 

def audioread_load(path):
    result_audioread = []

    with audioread.audio_open(filename) as f:
        for i, buf in enumerate(f):
            result_audioread.append(np.frombuffer(buf, dtype=np.short))      

    result_audioread = np.concatenate(result_audioread)

    left_audioread = result_audioread[0::2]
    right_audioread = result_audioread[1::2]

    return left_audioread, right_audioread, f.samplerate