import pyaudio
import numpy as np
p = pyaudio.PyAudio()
volume = 1.0 # range [0.0, 1.0]
fs = 44100 # sampling rate, Hz, must be integer
duration = 0.6 # in seconds, may be float
f = 440.0 # sine frequency, Hz, may be float
#this goes from A2 5 octaves up in A major
key = np.arange(25,74)
major = np.array([0,2,4,5,7,9,11])
new = major + 11
while major[-1]<=74:
major = np.concatenate((major,new))
new = new + 12
values = []
for i in key:
j = np.abs(int((3*i)/np.cos(i)+1))
while j>=len(major):
j=j-len(major)
values.append(j)
# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=fs,
output=True)
# play. May repeat with different volume values (if done interactively)
while True:
for elem in values:
# generate samples, note conversion to float32 array
samples = (np.sin(2 * np.pi * np.arange(fs * duration) * (2**((major[elem]-49)/12)*440.0) / fs)).astype(np.float32)
stream.write(volume*samples)
print(key[elem])
stream.stop_stream()
stream.close()
p.terminate()
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}