How to generate audio from a numpy array? How to generate audio from a numpy array? python python

How to generate audio from a numpy array?


You can use the write function from scipy.io.wavfile to create a wav file which you can then play however you wish. Note that the array must be integers, so if you have floats, you might want to scale them appropriately:

import numpy as npfrom scipy.io.wavfile import writedata = np.random.uniform(-1,1,44100) # 44100 random samples between -1 and 1scaled = np.int16(data/np.max(np.abs(data)) * 32767)write('test.wav', 44100, scaled)

If you want Python to actually play audio, then this page provides an overview of some of the packages/modules.


For the people coming here in 2016 scikits.audiolab doesn't really seem to work anymore. I was able to get a solution using sounddevice.

import numpy as npimport sounddevice as sdfs = 44100data = np.random.uniform(-1, 1, fs)sd.play(data, fs)


in Jupyter the best option is:

from IPython.display import Audiowave_audio = numpy.sin(numpy.linspace(0, 3000, 20000))Audio(wave_audio, rate=20000)