Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.86 KB | None | 0 0
  1. import math
  2. import numpy
  3. import pyaudio
  4. import matplotlib.pyplot as plt
  5. import time
  6. from Tkinter import *
  7.  
  8.  
  9. def sine(frequency, length, rate):
  10.     length = int(length * rate)
  11.     factor = float(frequency) * (math.pi * 2) / rate
  12.     return numpy.sin(numpy.arange(length) * factor)
  13.  
  14. def taper(data, taperlength):
  15.     i = 1.001
  16.     for x in range(len(data) - taperlength,len(data)):
  17.         data[x] = data[x] / i
  18.         i *= 1.01
  19.     i = 1.001
  20.     for x in range(taperlength,0,-1):
  21.         data[x] = data[x] / i
  22.         i *= 1.01
  23.     return data
  24.  
  25. def play_tone(stream, frequency=300, length=1, rate=44100):
  26.     chunks = []
  27.     chunks.append(sine(frequency, length, rate))
  28.  
  29.     chunk = numpy.concatenate(chunks) * 0.25
  30.  
  31.     chunk = taper(chunk, 8000)
  32.  
  33.     stream.write(chunk.astype(numpy.float32).tostring())
  34. def play_stream(chunk,stream):
  35.     stream.write(chunk.astype(numpy.float32).tostring())
  36.  
  37. def get_data(frequency=300, length=1, rate=44100):
  38.     chunks = []
  39.     chunks.append(sine(frequency, length, rate))
  40.     chunk = numpy.concatenate(chunks) * 0.25
  41.     chunk = taper(chunk, 8000)
  42.     return chunk
  43. def show_wav(data, printlen=0):
  44.     y = numpy.arange(len(data))
  45.     if printlen:
  46.         print(len(data))
  47.     plt.plot(y,data)  # should be length times rate
  48.     axes = plt.gca()
  49.     axes.set_xlim(00000,45000)
  50.     plt.show()
  51. if __name__ == '__main__':
  52.     p = pyaudio.PyAudio()
  53.     stream = p.open(format=pyaudio.paFloat32,
  54.                     channels=1, rate=44100, output=1)
  55.     step = 2.0**(1/12.0)
  56.  
  57.  
  58.     # Here's an example of a simple melody (notes)
  59.     # paired with a suitable harmony (harms)
  60.     # Change some of the numbers to change the notes
  61.     notes = [0,5,9,7,9,7,5]
  62.     harms = [9,10,12,10,12,10,9]
  63.     acc_stream = []
  64.     for i in notes:
  65.         data = get_data(300*step**i)
  66.         acc_stream.append(data)
  67.  
  68.     acc_stream = numpy.concatenate(acc_stream) * 0.25
  69.     harm_stream = []
  70.     for i in harms:
  71.         data = get_data(300*step**i)
  72.         harm_stream.append(data)
  73.    
  74.     harm_stream = numpy.concatenate(harm_stream) * 0.25
  75.  
  76.     # combine the melody and the harmony
  77.     #
  78.     full_stream = acc_stream + harm_stream
  79.     def play_harmonies_demo():
  80.  
  81.         play_stream(full_stream,stream)
  82.     def play_melodies_demo():
  83.         play_stream(acc_stream, stream)
  84.  
  85.     C = get_data(300*step**0)
  86.     Db = get_data(300*step**1)
  87.     D = get_data(300*step**2)
  88.     Eb = get_data(300*step**3)
  89.     E = get_data(300*step**4)
  90.     F = get_data(300*step**5)
  91.     Gb = get_data(300*step**6)
  92.     G = get_data(300*step**7)
  93.     Ab = get_data(300*step**8)
  94.     A = get_data(300*step**9)
  95.     Bb = get_data(300*step**10)
  96.     B = get_data(300*step**11)
  97.     C2 = get_data(300*step**12)
  98.  
  99.     # Some examples of chords
  100.     # Create additional chords by combining notes as desired
  101.     # these are just examples
  102.     Cmajor = C+E+G
  103.     Dminor = D+F+A
  104.     Gmajor = G + B + D
  105.     Fmajor = F + A + C
  106.  
  107.     maj = C+E+G
  108.     minor = C+Eb+G
  109.     dom = C+E+G+Bb
  110.     maj7 = C+E+G+B
  111.     diminished = C+Eb+Gb+A
  112.  
  113.  
  114.     # Uncomment any of the lines here to hear what these
  115.     # chords sound like                          
  116.     def play_qualities_demo():
  117.         play_stream(maj,stream)
  118.         play_stream(minor,stream)
  119.         play_stream(dom,stream)
  120.         play_stream(maj7,stream)
  121.         play_stream(diminished, stream)
  122.  
  123.     def play_IIVVI():
  124.         play_stream(Cmajor, stream)
  125.         play_stream(Fmajor, stream)
  126.         play_stream(Gmajor, stream)
  127.         play_stream(Cmajor, stream)
  128.  
  129. root = Tk()
  130. def play_note(note):
  131.     play_stream(note, stream)
  132. def foo():
  133.     print 'hi'
  134. note_names = [A,Bb,B,C, Db,D,Eb,E,F,Gb,G]
  135. def getButton():
  136.     note_labels = ['A','Bb','B','C','Db','D','Eb','E','F','Gb','G']
  137.     note_names = [A,Bb,B,C,Db,D,Eb,E,F,Gb,G]
  138.     for i, label in note_labels:
  139.         yield Button(root, text = note_labels[i], command = play_note(note_names[i]))
  140. CA = []
  141.  
  142. def playc():
  143.    play_stream(C, stream)
  144. def playdb():
  145.    play_stream(Db, stream)
  146. def playd():
  147.    play_stream(D, stream)
  148. def playeb():
  149.    play_stream(Eb, stream)
  150. def playe():
  151.    play_stream(E, stream)
  152. def playf():
  153.    play_stream(F, stream)
  154. def playgb():
  155.    play_stream(Gb, stream)
  156. def playg():
  157.    play_stream(G, stream)
  158. def playab():
  159.    play_stream(Ab, stream)
  160. def playa():
  161.    play_stream(A, stream)
  162. def playbb():
  163.    play_stream(Bb, stream)
  164. bC = Button(root, text = "C", command = playc)
  165. bDb = Button(root, text = "Db", command = playdb)
  166. bD = Button(root, text = "D", command = playd)
  167. bEb = Button(root, text = "Eb", command = playeb)
  168. bE = Button(root, text = "E", command = playe)
  169. bF= Button(root, text = "F", command = playf)
  170. bGb = Button(root, text = "Gb", command = playgb)
  171. bG = Button(root, text = "G", command = playg)
  172. bAb = Button(root, text = "Ab", command = playab)
  173. bA = Button(root, text = "A", command = playa)
  174. bBb = Button(root, text = "Bb", command = playbb)
  175. pqd = Button(root, text = "Chord Qualities Demo", command = play_qualities_demo)
  176. harmdemo = Button(root, text = "Harmony Demo", command = play_harmonies_demo)
  177. meldemo = Button(root, text = "Melody Demo", command = play_melodies_demo)
  178. progdemo = Button(root, text = "Progression Demo", command = play_IIVVI)
  179. bC.pack()
  180. bDb.pack()
  181. bD.pack()
  182. bEb.pack()
  183. bE.pack()
  184. bF.pack()
  185. bGb.pack()
  186. bG.pack()
  187. bAb.pack()
  188. bA.pack()
  189. bBb.pack()
  190. pqd.pack()
  191. meldemo.pack()
  192. harmdemo.pack()
  193. progdemo.pack()
  194.  
  195. root.mainloop()
  196.     # Uncomment this to see hear a I-IV-V-I progression
  197.     # play_IIVVI()
  198.     #
  199.     # Uncomment this to hear a demo of chord qualities
  200.     # play_qualities_demo()
  201.    
  202. stream.close()
  203. p.terminate()
  204.  
  205.     # For testing purposes, a graphical display of the waveform
  206.     # Seeing it can be visually informative for troubleshooting
  207.     # weird sound events, like clicks and buzzes.  Uncomment to view:
  208.     #
  209.     # show_wav(data)
  210.     #
  211.     #
  212.     #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement