Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import numpy
- import pyaudio
- import matplotlib.pyplot as plt
- import time
- from Tkinter import *
- def sine(frequency, length, rate):
- length = int(length * rate)
- factor = float(frequency) * (math.pi * 2) / rate
- return numpy.sin(numpy.arange(length) * factor)
- def taper(data, taperlength):
- i = 1.001
- for x in range(len(data) - taperlength,len(data)):
- data[x] = data[x] / i
- i *= 1.01
- i = 1.001
- for x in range(taperlength,0,-1):
- data[x] = data[x] / i
- i *= 1.01
- return data
- def play_tone(stream, frequency=300, length=1, rate=44100):
- chunks = []
- chunks.append(sine(frequency, length, rate))
- chunk = numpy.concatenate(chunks) * 0.25
- chunk = taper(chunk, 8000)
- stream.write(chunk.astype(numpy.float32).tostring())
- def play_stream(chunk,stream):
- stream.write(chunk.astype(numpy.float32).tostring())
- def get_data(frequency=300, length=1, rate=44100):
- chunks = []
- chunks.append(sine(frequency, length, rate))
- chunk = numpy.concatenate(chunks) * 0.25
- chunk = taper(chunk, 8000)
- return chunk
- def show_wav(data, printlen=0):
- y = numpy.arange(len(data))
- if printlen:
- print(len(data))
- plt.plot(y,data) # should be length times rate
- axes = plt.gca()
- axes.set_xlim(00000,45000)
- plt.show()
- if __name__ == '__main__':
- p = pyaudio.PyAudio()
- stream = p.open(format=pyaudio.paFloat32,
- channels=1, rate=44100, output=1)
- step = 2.0**(1/12.0)
- # Here's an example of a simple melody (notes)
- # paired with a suitable harmony (harms)
- # Change some of the numbers to change the notes
- notes = [0,5,9,7,9,7,5]
- harms = [9,10,12,10,12,10,9]
- acc_stream = []
- for i in notes:
- data = get_data(300*step**i)
- acc_stream.append(data)
- acc_stream = numpy.concatenate(acc_stream) * 0.25
- harm_stream = []
- for i in harms:
- data = get_data(300*step**i)
- harm_stream.append(data)
- harm_stream = numpy.concatenate(harm_stream) * 0.25
- # combine the melody and the harmony
- #
- full_stream = acc_stream + harm_stream
- def play_harmonies_demo():
- play_stream(full_stream,stream)
- def play_melodies_demo():
- play_stream(acc_stream, stream)
- C = get_data(300*step**0)
- Db = get_data(300*step**1)
- D = get_data(300*step**2)
- Eb = get_data(300*step**3)
- E = get_data(300*step**4)
- F = get_data(300*step**5)
- Gb = get_data(300*step**6)
- G = get_data(300*step**7)
- Ab = get_data(300*step**8)
- A = get_data(300*step**9)
- Bb = get_data(300*step**10)
- B = get_data(300*step**11)
- C2 = get_data(300*step**12)
- # Some examples of chords
- # Create additional chords by combining notes as desired
- # these are just examples
- Cmajor = C+E+G
- Dminor = D+F+A
- Gmajor = G + B + D
- Fmajor = F + A + C
- maj = C+E+G
- minor = C+Eb+G
- dom = C+E+G+Bb
- maj7 = C+E+G+B
- diminished = C+Eb+Gb+A
- # Uncomment any of the lines here to hear what these
- # chords sound like
- def play_qualities_demo():
- play_stream(maj,stream)
- play_stream(minor,stream)
- play_stream(dom,stream)
- play_stream(maj7,stream)
- play_stream(diminished, stream)
- def play_IIVVI():
- play_stream(Cmajor, stream)
- play_stream(Fmajor, stream)
- play_stream(Gmajor, stream)
- play_stream(Cmajor, stream)
- root = Tk()
- def play_note(note):
- play_stream(note, stream)
- def foo():
- print 'hi'
- note_names = [A,Bb,B,C, Db,D,Eb,E,F,Gb,G]
- def getButton():
- note_labels = ['A','Bb','B','C','Db','D','Eb','E','F','Gb','G']
- note_names = [A,Bb,B,C,Db,D,Eb,E,F,Gb,G]
- for i, label in note_labels:
- yield Button(root, text = note_labels[i], command = play_note(note_names[i]))
- CA = []
- def playc():
- play_stream(C, stream)
- def playdb():
- play_stream(Db, stream)
- def playd():
- play_stream(D, stream)
- def playeb():
- play_stream(Eb, stream)
- def playe():
- play_stream(E, stream)
- def playf():
- play_stream(F, stream)
- def playgb():
- play_stream(Gb, stream)
- def playg():
- play_stream(G, stream)
- def playab():
- play_stream(Ab, stream)
- def playa():
- play_stream(A, stream)
- def playbb():
- play_stream(Bb, stream)
- bC = Button(root, text = "C", command = playc)
- bDb = Button(root, text = "Db", command = playdb)
- bD = Button(root, text = "D", command = playd)
- bEb = Button(root, text = "Eb", command = playeb)
- bE = Button(root, text = "E", command = playe)
- bF= Button(root, text = "F", command = playf)
- bGb = Button(root, text = "Gb", command = playgb)
- bG = Button(root, text = "G", command = playg)
- bAb = Button(root, text = "Ab", command = playab)
- bA = Button(root, text = "A", command = playa)
- bBb = Button(root, text = "Bb", command = playbb)
- pqd = Button(root, text = "Chord Qualities Demo", command = play_qualities_demo)
- harmdemo = Button(root, text = "Harmony Demo", command = play_harmonies_demo)
- meldemo = Button(root, text = "Melody Demo", command = play_melodies_demo)
- progdemo = Button(root, text = "Progression Demo", command = play_IIVVI)
- bC.pack()
- bDb.pack()
- bD.pack()
- bEb.pack()
- bE.pack()
- bF.pack()
- bGb.pack()
- bG.pack()
- bAb.pack()
- bA.pack()
- bBb.pack()
- pqd.pack()
- meldemo.pack()
- harmdemo.pack()
- progdemo.pack()
- root.mainloop()
- # Uncomment this to see hear a I-IV-V-I progression
- # play_IIVVI()
- #
- # Uncomment this to hear a demo of chord qualities
- # play_qualities_demo()
- stream.close()
- p.terminate()
- # For testing purposes, a graphical display of the waveform
- # Seeing it can be visually informative for troubleshooting
- # weird sound events, like clicks and buzzes. Uncomment to view:
- #
- # show_wav(data)
- #
- #
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement