82

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.

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
gisgyaan
  • 835
  • 1
  • 7
  • 5

8 Answers8

105

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

rate = 44100
data = np.random.uniform(-1, 1, rate) # 1 second worth of random samples between -1 and 1
scaled = np.int16(data / np.max(np.abs(data)) * 32767)
write('test.wav', rate, scaled)

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

Walf
  • 8,535
  • 2
  • 44
  • 59
huon
  • 94,605
  • 21
  • 231
  • 225
43

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)
mdornfe1
  • 1,982
  • 1
  • 24
  • 42
  • 1
    I actually tried and scikits.audiolab worked on my Ubuntu 16.04 + python 2.7 (anaconda 4.1.1). I just needed to `sudo apt-get install libsndfile1-dev`. And on the other hand, sounddevice doesn't work for me: nothing is played when I replace scikits.audiolab with sd. – ftfish Nov 22 '16 at 16:11
  • no luck with python3, even with libsndfile1-dev installed, better luck with sounddevice – julienfr112 Jan 26 '17 at 18:25
  • 13
    Thanks, works perfectly! `sd.play(data, fs, blocking=True)` to make the program wait until the sound is played. – Snow bunting Mar 15 '17 at 09:24
40

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)
Alleo
  • 7,891
  • 2
  • 40
  • 30
  • 3
    This is THE solution I have been looking for for years! – FJDU Feb 23 '21 at 02:48
  • not yet supported in vscode/jupyter. see https://github.com/spatialaudio/python-sounddevice/issues/270#issuecomment-939432642 – michael Oct 10 '21 at 08:53
16

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)
mwv
  • 4,221
  • 2
  • 19
  • 9
16

I had some problems using scikit.audiolabs, so I looked for some other options for this task. I came up with sounddevice, 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)
finiteautomata
  • 3,753
  • 4
  • 31
  • 41
  • 2
    It works fine with Python 3. If you use the above code in a script, you should make sure to use `blocking=True`, otherwise the script will exit without finishing playback. – Matthias Jul 04 '16 at 16:44
  • I did that, in fact. Updated answer. Thanks! – finiteautomata Jul 31 '16 at 20:15
  • `sounddevice` has a problem on Mac OS X, you need to give your application [which one?] the entitlement `com.apple.security.cs.allow-unsigned-executable-memory` – michael Oct 10 '21 at 08:52
3

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)
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
2

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

Bryan
  • 440
  • 5
  • 14
1

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:

ryanjdillon
  • 17,658
  • 9
  • 85
  • 110