Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@akey7
Created March 31, 2018 02:55
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save akey7/725b2b7b68482525fff6620cb8cf05d4 to your computer and use it in GitHub Desktop.
Save akey7/725b2b7b68482525fff6620cb8cf05d4 to your computer and use it in GitHub Desktop.
Generate a .wav sound file from a NumPy array.
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)
@d1nch8g
Copy link

d1nch8g commented Dec 9, 2019

can you tell if that is possible to get a 2d numpy array from wav file?

@d1nch8g
Copy link

d1nch8g commented Dec 9, 2019

just trying to do that for so long

@akey7
Copy link
Author

akey7 commented Dec 9, 2019

@dancheng97 2D NumPy array: What are you trying to do with it? Read each stereo channel separately?

@d1nch8g
Copy link

d1nch8g commented Dec 10, 2019

Single channel would be enough for me

@Prasunsk
Copy link

try soundfile.read('link/to/wav/file.wav')
It will return a numpy array and sample_rate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment