Created
August 28, 2015 15:55
-
-
Save giginet/3d5ab903b789de0d46b0 to your computer and use it in GitHub Desktop.
Sound generation in Python
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 sys | |
import math | |
import wave | |
import array | |
DURATION = 3 | |
FREQUENCY = 440 | |
VOLUME = 100 | |
SAMPLERATE = 44100 | |
DATASIZE = 2 | |
CHANNEL = 2 | |
RANGE = (2 << 14) - 1 | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print("Usage ./{} [sine/triangle/square]".format(sys.argv[0])) | |
exit(1) | |
data = array.array('h') | |
fpr = int(SAMPLERATE / FREQUENCY) | |
samples_length = SAMPLERATE * DURATION * CHANNEL | |
mode = sys.argv[1] | |
if not mode in ['triangle', 'sine', 'square']: | |
print("mode is must be 'triangle', 'sine' or 'square'") | |
exit(1) | |
for i in range(samples_length): | |
sample = RANGE * float(VOLUME) / 100 | |
if mode == 'sine': | |
sample *= math.sin(math.pi * 2 * (i % fpr) / fpr) | |
elif mode == 'triangle': | |
sample *= abs((i % fpr) - fpr / 2) / float(fpr) | |
elif mode == 'square': | |
sample *= 1 if i % fpr < fpr / 2 else 0 | |
data.append(int(sample)) | |
f = wave.open('{}.wav'.format(mode), 'w') | |
f.setparams((CHANNEL, DATASIZE, SAMPLERATE, samples_length, "NONE", "Uncompressed")) | |
f.writeframes(data.tostring()) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment