Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. import mido
  2. import clipboard
  3. from operator import itemgetter
  4.  
  5. mid = mido.MidiFile('soulless4.mid')
  6. result = []
  7. notes_on=[]
  8. instruments = {}
  9. volumed = {}
  10. total_time = 0
  11. current_instrument = 1
  12. def get_instrument(msg):
  13.         prog = instruments[msg.channel]
  14.         ins = 1
  15.         if prog in [1,2,3,4]: ins = 1 #piano
  16.         elif prog in [5,6]: ins = 2 #electric piano
  17.         elif prog in [15,17,18,19,20,21,22,23,24]: ins = 3 #organ
  18.         elif prog in [25,26,32]: ins = 4 #guitar
  19.         elif prog in [27,28,29,30,31]: ins = 5 #electric guitar
  20.         elif prog in [33,34,35,36,37,38,39,40]: ins = 6 #bass
  21.         elif prog in [7,46,47,48]: ins = 7 #pizzacito
  22.         elif prog in [41,42,43,44,45]: ins = 8 #cello
  23.         elif prog in [57,58,59,60,61,62,63,64]: ins = 9 #trombone
  24.         elif prog in [8,72]: ins = 10 #clarinet
  25.         elif prog in [65,66,67,68,69,70]: ins = 11 #saxophone
  26.         elif prog in [74,75,76,79,80]: ins = 12 #flute
  27.         elif prog in [73,77,78]: ins = 13 #wooden flute
  28.         elif prog in [71]: ins = 14 #bassoon
  29.         elif prog in [49,50,51,52,53,54,55,56]: ins = 15 #choir
  30.         elif prog in [9,10,12,14,16]: ins = 16 #vibraphone
  31.         elif prog in [11]: ins = 17 #music box
  32.         elif prog in [113,114,115,116,117,118,119,120]: ins = 18 #steel drum
  33.         elif prog in [13]: ins = 19 #marimba
  34.         elif prog in [81,82,83,84,85,86,87,88]: ins = 20 #synth lead
  35.         elif prog in [89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104]: ins = 21#synth pad
  36.         else: ins = 1
  37.         return ins
  38. for msg in mid:
  39.     if msg.type == 'note_on':
  40.         notes_on.append([msg,total_time+msg.time])#[str(total_time+msg.time),str(ins),str(msg.note),"0.5",str(msg.velocity)])
  41.     if msg.type == 'note_off':
  42.         i=0
  43.         for x in range(len(notes_on)):
  44.             note = notes_on[i][0]
  45.             if note.channel == msg.channel and note.note == msg.note:
  46.                 result.append([notes_on[i][1],get_instrument(note),note.note,(msg.time+total_time)-notes_on[i][1],(note.velocity*100)/64])
  47.                 notes_on.pop(i)
  48.                 i-=1
  49.             i+=1
  50.     if msg.type == 'program_change':
  51.         channel = msg.channel
  52.         program = msg.program
  53.         instruments[channel] = program
  54.        
  55.     total_time += msg.time
  56.  
  57. final = sorted(result,key=itemgetter(0))
  58. clipboard.copy(','.join([','.join([str(y) for y in x]) for x in final]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement