import musicpy import midiutil from midiutil import MIDIFile #xMyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track is created def convert_seconds_to_quarter(time_in_sec, bpm): quarter_per_second = (bpm/60) time_in_quarter = time_in_sec * quarter_per_second return time_in_quarter import moduletabla from moduletabla import * degree=[] for i in range(128) : degree.append(-1) dur=[] for i in range(128) : dur.append(1) im=0 fic=open("tabpiano.txt") tabpiano=fic.read() fic.close() exec(tabpiano) tab=poly[1] if tab=="" : tab="p" print("tab 1",tab) fic=open("parapiano.txt") parapiano=fic.read() fic.close() exec(parapiano) tab=tablarep(tab) print("Entrez un motif de tablature pour la main droite") motif=input() motif=normaliser(motif) motif=repmotif(motif) tab=tablamotif(tab,motif) tab=tablarep(tab) print("Tablature avec motif",tab) degree=voix(tab) dur=durvoix(tab) track = 0 channel = 0 time = 0 # In beats duration = 1 # In beats MyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track is created # automatically) MyMIDI.addTempo(track, time, tempo) MyMIDI.addProgramChange(track, channel, time, program) def addNotes(channel, degree,dur): j=0 for k in range(0,128): if degree[k]==-1 : degree[k]=0 else : j=j+1 print(j) #for k in range(0,j): #print(degree[k]) #print(dur[k]) t=dur for i in range(0,j) : d=dur[i] if d==0.5 : t[i]=1/2 elif d==.25 : t[i]=1/4 elif d==.75 : t[i]=3/4 elif d==.375 : t[i]=3/8 elif d==.125 : t[i]=1/8 elif d==.1875 : t[i]=3/16 elif d==.0625 : t[i]=1/16 elif d==.03125 : t[i]=1/32 elif d==4 : t[i]=4 elif d==3.0 : t[i]=3 elif d==2 or d==2.0 : t[i]=2 elif d==1.5 : t[i]=3/2 elif d==1 or d==1.0 : t[i]=1 elif d==0 : t[i]=0 else : t[i]=1 t[0]=0 for i in range (j): t[i+1]=t[i]+t[i+1] for i, pitch in enumerate(degree): MyMIDI.addNote(track, channel, pitch ,t[i],1 , volume) #for i, pitch in enumerate(degree): #if pitch >= 0: # MIDI note numbers are greater than zero, so this # allows skipping beats using negative degree #MyMIDI.addNote(track, channel, pitch, t[i], duration, volume) addNotes(channel=channel, # function argument "channel" gets the variable "channel" degree=degree,dur=dur) # same with degree degree=[] for i in range(128) : degree.append(-1) dur=[] for i in range(128) : dur.append(1) #degree=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1] #dur=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] if nvoix>1 : tab=poly[2] program=instru(tab) if program=="" : program=1 else : para="i:"+program tab=otepara(para,tab) program=int(program) tempo=temp(tab) if tempo=="" : tempo=120 else : para="t:"+tempo tab=otepara(para,tab) tempo=int(tempo) volume=vol(tab) if volume=="" : volume=120 else : para="v:"+volume tab=otepara(para,tab) volume=int(volume) tab=tablarep(tab) print("Entrez un motif de tablature pour la main gauche") motif=input() motif=normaliser(motif) motif=repmotif(motif) tab=tablamotif(tab,motif) tab=tablarep(tab) print("Tablature avec motif",tab) if demiton != "" : tab=transpotabla(tab,idemiton) tab=transpotabla(tab,-12) degree=voix(tab) dur=durvoix(tab) MyMIDI.addProgramChange(track, channel, time, program) addNotes(channel=1, # here's a more direct a pproach degree=degree,dur=dur) degree=[] for i in range(128) : degree.append(-1) dur=[] for i in range(128) : dur.append(1) #degree=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1] #dur=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] if nvoix>2 : tab=poly[3] if mesu=="4/4" : tab="CppppCppppCppppppp" elif mesu=="3/4" : tab="Cppcppcppcpp" elif mesu=="6/8" : tab="cpppppcpppppcppppp" elif mesu=="2/4" : tab="cpcpcpcpcpcpcpcp" program=instru(tab) if program=="" : program=1 else : para="i:"+program tab=otepara(para,tab) program=int(program) tempo=temp(tab) if tempo=="" : tempo=120 else : para="t:"+tempo tab=otepara(para,tab) tempo=int(tempo) volume=vol(tab) if volume=="" : volume=120 else : para="v:"+volume tab=otepara(para,tab) volume=int(volume) tab=tablarep(tab) print("Entrez un motif de tablature pour la voix 3") motif=input() motif=normaliser(motif) motif=repmotif(motif) tab=tablamotif(tab,motif) tab=tablarep(tab) print("Tablature avec motif",tab) if demiton != "" : tab=transpotabla(tab,idemiton) degree=voix(tab) dur=durvoix(tab) if mesu != "" : programm=115 MyMIDI.addProgramChange(track, channel, time, program) addNotes(channel=2, # the same with some beats skipped degree=degree,dur=dur) degree=[] for i in range(128) : degree.append(-1) dur=[] for i in range(128) : dur.append(1) #degree=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1] #dur=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] if nvoix>3 : tab=poly[4] if mesu=="4/4" : tab="p4p4p4p4" elif mesu=="3/4" : tab="p3/4p/4p3/4p/4p3/4p/4" elif mesu=="6/8" : tab="pppppppppppppppppp" elif mesu=="2/4" : tab="ppcpcppppppppppp" program=instru(tab) if program=="" : program=1 else : para="i:"+program tab=otepara(para,tab) program=int(program) tempo=temp(tab) if tempo=="" : tempo=120 else : para="t:"+tempo tab=otepara(para,tab) tempo=int(tempo) volume=vol(tab) if volume=="" : volume=120 else : para="v:"+volume tab=otepara(para,tab) volume=int(volume) tab=tablarep(tab) print("Entrez un motif de tablature pour la voix 4") motif=input() motif=normaliser(motif) motif=repmotif(motif) tab=tablamotif(tab,motif) tab=tablarep(tab) print("Tablature avec motif",tab) if demiton != "" : tab=transpotabla(tab,idemiton) degree=voix(tab) dur=durvoix(tab) if mesu != "" : programm=118 MyMIDI.addProgramChange(track, channel, time, program) addNotes(channel=3, # the same with some beats skipped degree=degree,dur=dur) with open("tabla4.mid", "wb") as output_file: MyMIDI.writeFile(output_file)