-
-
Save Kyubyong/647b0e1a311c4688ca5143839f4574cb to your computer and use it in GitHub Desktop.
Generate a .wav sound file from a NumPy array.
This file contains 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 | |
from scipy.io.wavfile import write | |
# Samples per second | |
sps = 44100 | |
# Frequency / pitch of the sine wave | |
freq_hz = 440.0 | |
# Duration | |
duration_s = 5.0 | |
# NumpPy magic | |
each_sample_number = np.arange(duration_s * sps) | |
waveform = np.sin(2 * np.pi * each_sample_number * freq_hz / sps) | |
waveform_quiet = waveform * 0.3 | |
waveform_integers = np.int16(waveform_quiet * 32767) | |
# Write the .wav file | |
write('first_sine_wave.wav', sps, waveform_integers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pay attention to line 17.