Created
December 13, 2018 14:03
-
-
Save sammosummo/d256a40f49e1ab42896923377ca53a85 to your computer and use it in GitHub Desktop.
Simple script for generating and playing sounds in Python 3
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
import numpy as np | |
import simpleaudio as sa | |
samplerate = 44100 | |
ref_amplitude = 1e-5 | |
level = 80 | |
ref_rms = ref_amplitude / np.sqrt(2) | |
rms = 10 ** (level / 20) * ref_rms | |
amplitude = rms * np.sqrt(2) | |
# or more simply ... | |
# amplitude = 10 ** (level / 20) * ref_amplitude | |
duration = 1 | |
timepoints = np.arange(0, duration * samplerate) / samplerate | |
frequency = 1000 | |
phase = 0 | |
sinusoid = amplitude * np.sin(2 * np.pi * frequency * timepoints + phase) | |
sound = np.fix(sinusoid * 32767).astype(np.int16) | |
playback = sa.play_buffer(sound, 1, 2, samplerate) | |
playback.wait_done() | |
noise = amplitude / np.sqrt(2) * np.random.normal(size=duration * samplerate) | |
sound = np.fix(noise * 32767).astype(np.int16) | |
playback = sa.play_buffer(sound, 1, 2, samplerate) | |
playback.wait_done() | |
sound = np.concatenate((noise, silence, sinusoid)) | |
sound = np.fix(sound * 32767).astype(np.int16) | |
playback = sa.play_buffer(sound, 1, 2, samplerate) | |
playback.wait_done() | |
sound = sinusoid + noise | |
sound = np.fix(sound * 32767).astype(np.int16) | |
playback = sa.play_buffer(sound, 1, 2, samplerate) | |
playback.wait_done() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment