Skip to content

Instantly share code, notes, and snippets.

@briehanlombaard
Created January 19, 2017 08:07
Show Gist options
  • Select an option

  • Save briehanlombaard/ab44606143a39b335a7f21a3c9551f23 to your computer and use it in GitHub Desktop.

Select an option

Save briehanlombaard/ab44606143a39b335a7f21a3c9551f23 to your computer and use it in GitHub Desktop.
import argparse
import math
import random
import struct
import sys
import wave
import matplotlib.pyplot as plt
SAMPLE_RATE = 44100
SAMPLE_LENGTH = 1
MIN = -32767
MAX = 32767
AMPLITUDE = 10000
PHASE = 0
FREQUENCY = 261.6 # Middle C
def cot(x):
return math.cos(x) / math.sin(x)
w = wave.open('test.wav', 'w')
w.setparams((2, 2, SAMPLE_RATE, 0, 'NONE', 'not compressed'))
values = []
for x in range(1, SAMPLE_LENGTH * SAMPLE_RATE):
# Noise
#y = random.randint(MIN, MAX)
# Sine
#y = AMPLITUDE * math.sin(2 * math.pi * FREQUENCY * x + PHASE)
# Square
#y = AMPLITUDE * math.copysign(1, math.sin(2 * math.pi * FREQUENCY * x))
# Sawtooth
#y = - (2 * AMPLITUDE) / math.pi * math.atan(cot(x * math.pi / FREQUENCY))
# Triangle
#y = (2 * AMPLITUDE) / math.pi * math.asin(math.sin(2 * math.pi / FREQUENCY * x))
#value = struct.pack('h', y)
#values.append(value)
#values.append(value)
#w.writeframes(''.join(values))
#w.close()
#plt.axis([0, SAMPLE_LENGTH * SAMPLE_RATE, MIN, MAX])
#plt.plot(values)
#plt.ylabel('signal')
#plt.show()
#if __name__ == '__main__':
# parser = argparse.ArgumentParser()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment