Python: Live Audio from NumPy Array
In this video, I show how to play sound from a NumPy array directly to the audio output.
The code for the tutorial is:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use the sounddevice module | |
# http://python-sounddevice.readthedocs.io/en/0.3.10/ | |
import numpy as np | |
import sounddevice as sd | |
import time | |
# Samples per second | |
sps = 44100 | |
# Frequency / pitch | |
freq_hz = 440.0 | |
# Duration | |
duration_s = 5.0 | |
# Attenuation so the sound is reasonable | |
atten = 0.3 | |
# NumpPy magic to calculate the waveform | |
each_sample_number = np.arange(duration_s * sps) | |
waveform = np.sin(2 * np.pi * each_sample_number * freq_hz / sps) | |
waveform_quiet = waveform * atten | |
# Play the waveform out the speakers | |
sd.play(waveform_quiet, sps) | |
time.sleep(duration_s) | |
sd.stop() |