Created
January 27, 2021 10:51
-
-
Save tomazas/156c616d4fe16327c6c7c5cbc27b04d0 to your computer and use it in GitHub Desktop.
Generate 440Hz sine signal-audio with Python wave library
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 wave, struct, math, random | |
sampleRate = 44100 # samples/sec (Fs) | |
duration = 10 # seconds | |
frequency = 440 # hertz | |
obj = wave.open('sound.wav','w') | |
obj.setnchannels(1) # mono | |
obj.setsampwidth(2) # 16-bit | |
obj.setframerate(sampleRate) | |
t_incr = 1.0/sampleRate*duration | |
t = 0 | |
for j in xrange(0, duration): | |
frame = "" | |
for i in xrange(0, sampleRate): | |
value = 32767*math.sin(6.28*frequency*t) | |
frame = frame + struct.pack('<h', value) | |
t = t + t_incr | |
obj.writeframes(frame) # accepts full frames only | |
obj.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment