How to generate audio from a numpy array?

PythonAudioNumpyMatplotlib

Python Problem Overview


I want to create "heart rate monitor" effect from a 2D array in numpy and want the tone to reflect the values in the array.

Python Solutions


Solution 1 - Python

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 np
from scipy.io.wavfile import write

data = np.random.uniform(-1,1,44100) # 44100 random samples between -1 and 1
scaled = 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.

Solution 2 - Python

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 np
import sounddevice as sd

fs = 44100
data = np.random.uniform(-1, 1, fs)
sd.play(data, fs)

Solution 3 - Python

in Jupyter the best option is:

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

Solution 4 - Python

In addition, you could try scikits.audiolab. It features file IO and the ability to 'play' arrays. Arrays don't have to be integers. To mimick dbaupp's example:

import numpy as np
import scikits.audiolab

data = np.random.uniform(-1,1,44100)
# write array to file:
scikits.audiolab.wavwrite(data, 'test.wav', fs=44100, enc='pcm16')
# play the array:
scikits.audiolab.play(data, fs=44100)

Solution 5 - Python

I had some problems using scikit.audiolabs, so I looked for some other options for this task. I came up with [sounddevice][1], which seems a lot more up-to-date. I have not checked if it works with Python 3.

A simple way to perform what you want is this:

import numpy as np
import sounddevice as sd

sd.default.samplerate = 44100

time = 2.0
frequency = 440

# Generate time of samples between 0 and two seconds
samples = np.arange(44100 * time) / 44100.0
# Recall that a sinusoidal wave of frequency f has formula w(t) = A*sin(2*pi*f*t)
wave = 10000 * np.sin(2 * np.pi * frequency * samples)
# Convert it to wav format (16 bits)
wav_wave = np.array(wave, dtype=np.int16)

sd.play(wav_wave, blocking=True)

[1]: https://github.com/spatialaudio/python-sounddevice/ "sounddevice"

Solution 6 - Python

PyGame has the module pygame.sndarray which can play numpy data as audio. The other answers are probably better, as PyGame can be difficult to get up and running. Then again, scipy and numpy come with their own difficulties, so maybe it isn't a large step to add PyGame into the mix.

http://www.pygame.org/docs/ref/sndarray.html

Solution 7 - Python

Another modern and convenient solution is to use pysoundfile, which can read and write a wide range of audio file formats:

import numpy as np
import soundfile as sf

data = np.random.uniform(-1, 1, 44100)
sf.write('new_file.wav', data, 44100)

Solution 8 - Python

Not sure of the particulars of how you would produce the audio from the array, but I have found mpg321 to be a great command-line audio player, and could potentially work for you.

I use it as my player of choice for Anki, which is written in python and has libraries that could be a great starting place for interfacing your code/arrays with audio.

Check out:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongisgyaanView Question on Stackoverflow
Solution 1 - PythonhuonView Answer on Stackoverflow
Solution 2 - Pythonmdornfe1View Answer on Stackoverflow
Solution 3 - PythonAlleoView Answer on Stackoverflow
Solution 4 - PythonmwvView Answer on Stackoverflow
Solution 5 - PythonfiniteautomataView Answer on Stackoverflow
Solution 6 - PythonBryanView Answer on Stackoverflow
Solution 7 - PythonNils WernerView Answer on Stackoverflow
Solution 8 - PythonryanjdillonView Answer on Stackoverflow