Advertisement
trodland

midichords

May 22nd, 2021
1,315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. recievedData = [] # List to keep track of the latest keys that has been pressed.
  2.  
  3.  
  4. mididata = [[[144, 43, 100, 0], 13132],
  5.             [[144, 36, 100, 0], 13152],
  6.             [[144, 40, 100, 0], 13152]
  7.            ]
  8.  
  9.  
  10. for event in mididata:
  11. # Above this line is just to emulate the mididata you are getting in. Only the recievedData = [] is needed in your code.
  12. # The line below this corelates to your line number 55 (data = event[0])
  13.  
  14.  
  15.     data = event[0]
  16.     note = data[1]
  17.     time = event[1]
  18.    
  19.    
  20.     # if theres is no data in recievedData, add the current note and timestamp
  21.     if len(recievedData) == 0:
  22.         recievedData.append([time,note])
  23.    
  24.     # else if there is less than 10 notes in the list add the current note IF the
  25.     # timestamp does not differ "too much"
  26.     elif len(recievedData) < 10:
  27.    
  28.         # check if the timestamp of this note is not later than 100ms after the previous.
  29.         if not time > recievedData[-1][0] + 100: # [-1] = the last element, [0] = time of the last element.
  30.             recievedData.append([time,note])
  31.         else:
  32.             # to much time has past, empty the recievedData list
  33.             recievedData = []
  34.            
  35.     # if theres is more than 10 notes in the list its probably time to empty the list.
  36.     else:
  37.        recievedData = []
  38.    
  39.    
  40.     print(recievedData)
  41.     ################
  42.     # Check the recievedData list to see if we can find any chords.
  43.    
  44.     if len(recievedData) > 2: #needs at least three notes to make a chord.
  45.        
  46.         templist = [] # a temporary list just to cointain the notes, and not timestamps.
  47.        
  48.         for data in recievedData:
  49.             templist.append(data[1])
  50.            
  51.         templist.sort() # Make sure the keys are presented in ascending order.
  52.        
  53.        
  54.         if templist[0] == 36 and templist[1] == 40 and templist[2] == 43:
  55.             print("C major")
  56.            
  57.        
  58.    
  59.  
  60.    
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement