Skip to content

Instantly share code, notes, and snippets.

@jarkkojs
Created August 24, 2021 03:03
Show Gist options
  • Save jarkkojs/c019104b9de642a3097c3aa37bc65c72 to your computer and use it in GitHub Desktop.
Save jarkkojs/c019104b9de642a3097c3aa37bc65c72 to your computer and use it in GitHub Desktop.
import numpy as np
import wave
NOTE_FREQUENCY = 261.63 # C4
NOTE_LENGTH = 1.0 / NOTE_FREQUENCY
SAMPLE_LENGTH = 1000.0 * NOTE_LENGTH
SAMPLE_RATE = 44100.0
t = np.linspace(0, SAMPLE_LENGTH, int(SAMPLE_LENGTH * SAMPLE_RATE))
s = np.sin(2 * np.pi * NOTE_FREQUENCY * t)
# 16-bit little-endian
s = (s * (2 ** 15 - 1)).astype("<h")
with wave.open("C4.wav", "w") as f:
f.setnchannels(2)
f.setsampwidth(2)
f.setframerate(SAMPLE_RATE)
f.writeframes(s.tobytes())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment