1

I have a python script in which I get bytes corresponding to an audio (I make an API call to another program which returns audio bytes in wav format). I want to do some signal processing using numpy on this audio. Currently I am writing the bytes to a file and loading the wav file back using librosa. But due to the intermediate disk I/O, there is latency. Can I get ndarray from these audio bytes without writing to a file? Below is my code in which I write to a file and read ndarray

def func1():
    audioBytes = api_call_to_another_program()
    fp = open("tmp.wav", "wb")
    fp.write(audioBytes)
    return path

def func2(path, sample_rate=someNumber):
    myNdArray = librosa.core.load(path, sr=sample_rate)[0]
    # some signal processing stuff..

I want something like this

def func1():
    audioBytes = api_call_to_another_program()
    interRep = someFunction(audioBytes) # an intermediate representation for compressing the data a bit
    return interRep

def func2(interRep):
    myNdArray = inverseOfSomeFunction(interRep)
    # some signal processing stuff..
BaluRaman
  • 265
  • 5
  • 16
  • Start with the docs on [np.frombuffer()](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.frombuffer.html#numpy.frombuffer). (assuming you want raw bytes -> array; not sure what special things are being done in wav files; i think there is some wav-reader in scipy, which might be possible to use with some wrapper as bytesio to not read from file: untested). – sascha May 03 '18 at 14:04

0 Answers0