Guest User

LUCASZANELLA.COM - LAUNCHPAD V1.0

a guest
Aug 18th, 2013
30,020
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #LAUNCHPAD V1.0 - lucaszanella.com - 13/06/2013
  2. #
  3. #
  4.  
  5. import _thread
  6. import serial
  7. import time
  8. import pyaudio
  9. import wave
  10. import sys
  11. version = '1.0'
  12. #CLASS OF SAMPLES
  13. class sample:
  14.     def __init__(self, bts, n_of_samples):
  15.         self.bts = bts
  16.         self.n_of_samples = n_of_samples
  17.         self.list = []
  18.         self.stream = []
  19.         self.pyList = []
  20.     def load(self, list_of_samples):
  21.         if len(list_of_samples)==self.n_of_samples:
  22.             for self.a in range(0, self.n_of_samples):
  23.                 self.list.append(self.a)
  24.                 try:
  25.                     self.list[self.a] = wave.open(list_of_samples[self.a], 'rb')
  26.                 except:
  27.                     print ("Unexpected error:", sys.exc_info()[0])
  28.                     raise
  29.         else:
  30.             print("error: list_of_samples length is different from number os samples")
  31.     def load_unic(self, index, lis_of_s):
  32.         self.list[index] = wave.open(lis_of_s[index], 'rb')
  33.     def read(self, index):
  34.         return(self.list[index].readframes(self.bts))                
  35.     def start(self):
  36.         for self.n in range(0, self.n_of_samples):
  37.             self.stream.append("")
  38.             self.pyList.append("")
  39.             self.pyList[self.n] = pyaudio.PyAudio()
  40.             self.stream[self.n] = (self.pyList[self.n].open(format=self.pyList[self.n].get_format_from_width(self.list[self.n].getsampwidth()),
  41.                     channels=self.list[self.n].getnchannels(),
  42.                     rate=self.list[self.n].getframerate(),
  43.                     output=True))
  44.     def reload(self, n):
  45.         self.stream[self.n] = (self.pyList[self.n].open(format=self.pyList[self.n].get_format_from_width(self.list[self.n].getsampwidth()),
  46.                     channels=self.list[self.n].getnchannels(),
  47.                     rate=self.list[self.n].getframerate(),
  48.                     output=True))
  49.     def rewind(self, index):
  50.         self.list[index].rewind()
  51.     def close(self):
  52.         for self.h in range(0, self.n_of_samples):
  53.             self.stream[self.h].stop_stream()
  54.             self.stream[self.h].close()
  55.             self.pyList[self.h].terminate()
  56.            
  57. #DEFINITIONS
  58. bt = 3400 #bytes to read per time
  59. #sync strings
  60. thet = 'abcdefghijklmnop'
  61. thef = 'qrstuvxwyz-+*123'
  62. #//
  63. pack = 0
  64. size_pack = 16
  65. pasta = []
  66. objetos = []
  67. samples_in = []
  68. samples = []
  69. musicas = []
  70. to_play = []
  71. command = []
  72. restart = []
  73. playing = []
  74. permission = []
  75. block = []
  76. count = 0
  77. pasta.append ('packs/coca/')
  78. pasta.append ('packs/daft2/')
  79. pasta.append ('packs/altj/')
  80. pasta.append ('packs/sk/')
  81. pasta.append ('packs/nyanc/')
  82. pasta.append ('packs/loops/')
  83. pasta.append ('packs/synth_notes/')
  84. pasta.append ('packs/test/')
  85. pasta.append ('packs/paradise/')
  86. pasta.append ('packs/skrillex/')
  87. pasta.append ('packs/dub1/')
  88. pasta.append ('packs/parting2/')
  89. pasta.append ('packs/krun/')
  90. pasta.append ('packs/havoc/')
  91. pasta.append ('packs/piano2/')
  92. pasta.append ('packs/vaski/')
  93.  
  94. qtd_packs = len(pasta)
  95.  
  96. for y in range(0, size_pack):
  97.     playing.append(False)
  98.     permission.append(True)
  99.     to_play.append(False)
  100.     command.append(False)
  101.     restart.append(False)
  102.     block.append(False)
  103. block[0] = True
  104. block[1] = True
  105.  
  106. for k in range(0, qtd_packs):
  107.     for n in range(1, size_pack+1):
  108.         samples_in.append (pasta[k]+str(n)+'.wav')
  109.     samples.append(samples_in)
  110.     samples_in = []
  111.  
  112.  
  113. print('opening packs...')
  114. for t in range(0, qtd_packs):
  115.     objetos.append(sample(bt, len(samples[t])))
  116.     objetos[t].load(samples[t])
  117.     objetos[t].start()
  118. ss = ['packs/silencio.wav']
  119. waiter = sample(bt, 1)
  120. waiter.load(ss)
  121. waiter.start()
  122. print('done')
  123. #if you want to pause everything
  124. def verify():
  125.     return(True)
  126.        
  127. #//
  128. def stop_all():
  129.     for o in range(0, size_pack):
  130.         to_play[o] = False
  131. def plays():
  132.     while True:
  133.         while verify():
  134.             for g in range(0, size_pack):
  135.                 tv = False
  136.                 for q in range(0, size_pack):
  137.                     if to_play[q]:
  138.                         tv = True
  139.                 if to_play[g]:
  140.                     j = objetos[pack].read(g)
  141.                     if j!='':
  142.                         if tv:
  143.                             objetos[pack].stream[g].write(j)
  144.                     else:                        
  145.                         to_play[g] = False
  146.                         playing[g] = False
  147.                         command[g] = False
  148.                         restart[g] = True
  149.                 else:
  150.                     time.sleep(0.0001)
  151. def hold(h):
  152.     key = 0
  153.     ser = serial.Serial('COM6', 115200)
  154.     global pack;
  155.     count = 0
  156.     countmode = 0
  157.     npacks = qtd_packs-1
  158.     print("waiting serial...")
  159.     time.sleep(2)
  160.     print("lucaszanella.com - Version: "+version+" python launchpad")
  161.     print("number of packs: "+str(qtd_packs))
  162.    
  163.     while True:
  164.         count +=1
  165.         #ser.flushInput()
  166.         m = ser.readline()
  167.         buff = str(ser.read(ser.inWaiting()))
  168.         nlocal = buff.rfind('n')
  169.         if nlocal!=0:
  170.             u = buff[nlocal-20:nlocal-2]
  171.         if len(u)==18:
  172.             for x in range(0, size_pack):
  173.                 if u[x]==thef[x]:
  174.                     command[x] = False
  175.                 if u[x]==thet[x]:
  176.                     if permission[x]==True:
  177.                         if command[x]==False:
  178.                             if to_play[x]==True:
  179.                                 objetos[pack].rewind(x)
  180.                             if restart[x]:
  181.                                 objetos[pack].rewind(x)
  182.                     if block[pack]:
  183.                         if key!=x:
  184.                             to_play[key] = False
  185.                             key = x
  186.                     to_play[x] = True
  187.                     playing[x] = True
  188.                     command[x] = True
  189.                 if count>10:
  190.                     count = 0
  191.             if u[16]=='@':
  192.                 countmode+=1
  193.                 if countmode>100:
  194.                     pack+=1
  195.                     stop_all()
  196.                     if pack>npacks:
  197.                         pack=0
  198.                     countmode=0
  199.                     print("Now in pack: "+str(pack))
  200.  
  201.  
  202. _thread.start_new_thread(hold, (0,))
  203.  
  204. plays()
  205. #close the objects here if needed
RAW Paste Data