Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #LAUNCHPAD V1.0 - lucaszanella.com - 13/06/2013
- #
- #
- import _thread
- import serial
- import time
- import pyaudio
- import wave
- import sys
- version = '1.0'
- #CLASS OF SAMPLES
- class sample:
- def __init__(self, bts, n_of_samples):
- self.bts = bts
- self.n_of_samples = n_of_samples
- self.list = []
- self.stream = []
- self.pyList = []
- def load(self, list_of_samples):
- if len(list_of_samples)==self.n_of_samples:
- for self.a in range(0, self.n_of_samples):
- self.list.append(self.a)
- try:
- self.list[self.a] = wave.open(list_of_samples[self.a], 'rb')
- except:
- print ("Unexpected error:", sys.exc_info()[0])
- raise
- else:
- print("error: list_of_samples length is different from number os samples")
- def load_unic(self, index, lis_of_s):
- self.list[index] = wave.open(lis_of_s[index], 'rb')
- def read(self, index):
- return(self.list[index].readframes(self.bts))
- def start(self):
- for self.n in range(0, self.n_of_samples):
- self.stream.append("")
- self.pyList.append("")
- self.pyList[self.n] = pyaudio.PyAudio()
- self.stream[self.n] = (self.pyList[self.n].open(format=self.pyList[self.n].get_format_from_width(self.list[self.n].getsampwidth()),
- channels=self.list[self.n].getnchannels(),
- rate=self.list[self.n].getframerate(),
- output=True))
- def reload(self, n):
- self.stream[self.n] = (self.pyList[self.n].open(format=self.pyList[self.n].get_format_from_width(self.list[self.n].getsampwidth()),
- channels=self.list[self.n].getnchannels(),
- rate=self.list[self.n].getframerate(),
- output=True))
- def rewind(self, index):
- self.list[index].rewind()
- def close(self):
- for self.h in range(0, self.n_of_samples):
- self.stream[self.h].stop_stream()
- self.stream[self.h].close()
- self.pyList[self.h].terminate()
- #DEFINITIONS
- bt = 3400 #bytes to read per time
- #sync strings
- thet = 'abcdefghijklmnop'
- thef = 'qrstuvxwyz-+*123'
- #//
- pack = 0
- size_pack = 16
- pasta = []
- objetos = []
- samples_in = []
- samples = []
- musicas = []
- to_play = []
- command = []
- restart = []
- playing = []
- permission = []
- block = []
- count = 0
- pasta.append ('packs/coca/')
- pasta.append ('packs/daft2/')
- pasta.append ('packs/altj/')
- pasta.append ('packs/sk/')
- pasta.append ('packs/nyanc/')
- pasta.append ('packs/loops/')
- pasta.append ('packs/synth_notes/')
- pasta.append ('packs/test/')
- pasta.append ('packs/paradise/')
- pasta.append ('packs/skrillex/')
- pasta.append ('packs/dub1/')
- pasta.append ('packs/parting2/')
- pasta.append ('packs/krun/')
- pasta.append ('packs/havoc/')
- pasta.append ('packs/piano2/')
- pasta.append ('packs/vaski/')
- qtd_packs = len(pasta)
- for y in range(0, size_pack):
- playing.append(False)
- permission.append(True)
- to_play.append(False)
- command.append(False)
- restart.append(False)
- block.append(False)
- block[0] = True
- block[1] = True
- for k in range(0, qtd_packs):
- for n in range(1, size_pack+1):
- samples_in.append (pasta[k]+str(n)+'.wav')
- samples.append(samples_in)
- samples_in = []
- print('opening packs...')
- for t in range(0, qtd_packs):
- objetos.append(sample(bt, len(samples[t])))
- objetos[t].load(samples[t])
- objetos[t].start()
- ss = ['packs/silencio.wav']
- waiter = sample(bt, 1)
- waiter.load(ss)
- waiter.start()
- print('done')
- #if you want to pause everything
- def verify():
- return(True)
- #//
- def stop_all():
- for o in range(0, size_pack):
- to_play[o] = False
- def plays():
- while True:
- while verify():
- for g in range(0, size_pack):
- tv = False
- for q in range(0, size_pack):
- if to_play[q]:
- tv = True
- if to_play[g]:
- j = objetos[pack].read(g)
- if j!='':
- if tv:
- objetos[pack].stream[g].write(j)
- else:
- to_play[g] = False
- playing[g] = False
- command[g] = False
- restart[g] = True
- else:
- time.sleep(0.0001)
- def hold(h):
- key = 0
- ser = serial.Serial('COM6', 115200)
- global pack;
- count = 0
- countmode = 0
- npacks = qtd_packs-1
- print("waiting serial...")
- time.sleep(2)
- print("lucaszanella.com - Version: "+version+" python launchpad")
- print("number of packs: "+str(qtd_packs))
- while True:
- count +=1
- #ser.flushInput()
- m = ser.readline()
- buff = str(ser.read(ser.inWaiting()))
- nlocal = buff.rfind('n')
- if nlocal!=0:
- u = buff[nlocal-20:nlocal-2]
- if len(u)==18:
- for x in range(0, size_pack):
- if u[x]==thef[x]:
- command[x] = False
- if u[x]==thet[x]:
- if permission[x]==True:
- if command[x]==False:
- if to_play[x]==True:
- objetos[pack].rewind(x)
- if restart[x]:
- objetos[pack].rewind(x)
- if block[pack]:
- if key!=x:
- to_play[key] = False
- key = x
- to_play[x] = True
- playing[x] = True
- command[x] = True
- if count>10:
- count = 0
- if u[16]=='@':
- countmode+=1
- if countmode>100:
- pack+=1
- stop_all()
- if pack>npacks:
- pack=0
- countmode=0
- print("Now in pack: "+str(pack))
- _thread.start_new_thread(hold, (0,))
- plays()
- #close the objects here if needed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement