Skip to content

Instantly share code, notes, and snippets.

@akey7
Created June 28, 2020 20:22
Show Gist options
  • Save akey7/72f2fd0b943ac22ffe90c451ec18a6b8 to your computer and use it in GitHub Desktop.
Save akey7/72f2fd0b943ac22ffe90c451ec18a6b8 to your computer and use it in GitHub Desktop.
Plays a sine wave generated in NumPy to the sound device.
# 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment