Advertisement
simoneb

continuous hi-hat control with hydrogen and mididings

Dec 22nd, 2012
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. ###################################
  2. # Continuous hi-hat controller v0.1
  3. #
  4. # A python script for mididings.
  5. # This code is public domain.
  6. #
  7. # 2012-12-22 Simone Baracchi
  8. ###################################
  9.  
  10. from mididings import *
  11. from mididings.engine import *
  12. from mididings.event import *
  13. from time import time
  14.  
  15. #### Hi-hat parameters ####
  16.  
  17. # Full closed hihat(s).
  18. # These are the notes issued when the hi-hat is fully closed.
  19. # The number of items should match the number of zones your hi-hat controller has.
  20. hihats_base = [ 37 ]
  21.  
  22. # Pedal pressure ranges.
  23. # Uses first sample (fully closed) if pedal is pushed more than 85 (e.g. first value here);
  24. # second sample (slightly less closed) if pedal is pushed more than 35; etc
  25. # The number of items must match the number of samples per hi-hat
  26. hihats_pedal_range = [ 85, 35, 20, 10, 5, 1, 0 ]
  27.  
  28. # Mutes the hi-hat when the pedal is pushed more than the first value.
  29. # The hi-hat must be opened again more than the second value to trigger other note-offs
  30. hihats_noteoff_range = [ 30, 20 ]
  31.  
  32. # Samples to be muted when issuing a note-off.
  33. # Sounds better if you stop only the more open hi-hats.
  34. hihats_noteoff_notes = [ 39, 40, 41, 42, 43,    46, 47, 48, 49, 50 ]
  35.  
  36. # Sends a noteoff after a note hasn't been played for <noteoff_interval> seconds
  37. noteoff_interval = 4
  38.  
  39. #### end of hi-hat parameters ####
  40.  
  41. class HiHatController:
  42.     def __init__(self):
  43.         self.pedal = -1
  44.         self.open = 1;
  45.         self.close = 0;
  46.         self.notes = {}
  47.         print "Starting..."
  48.     def __call__(self, ev):
  49.     global hihats_base, hihats_pedal_range, hihats_noteoff_range, hihats_noteoff_notes, noteoff_interval
  50.         if ev.type_ == CTRL and ev.param == 4:
  51.             self.pedal = ev.value
  52.             print "hh: pedal ", self.pedal
  53.             if (ev.value < hihats_noteoff_range[1]):
  54.                 self.open = 1
  55.                 self.close = 0
  56.             elif (ev.value >= hihats_noteoff_range[0]):
  57.                 self.close = 1
  58.             if (self.open == 1) and (self.close == 1):
  59.                 self.open = 0
  60.                 self.close = 0
  61.                 print "hh: stop everything!"
  62.         queue = []
  63.         for note in hihats_noteoff_notes:
  64.                     queue.append(NoteOffEvent(ev.port, ev.channel, note, 127))
  65.                 return queue
  66.             return None
  67.         elif (ev.type_ == NOTEON) and (ev.note in hihats_base) and (self.pedal != -1):
  68.             var = ev.note
  69.             for range in hihats_pedal_range:
  70.                 if(self.pedal < range):
  71.                     ev.note += 1
  72.             print "hh: in=", var, " out=", ev.note
  73.  
  74.         # check for noteoffs
  75.     self.notes[ev.note] = time()
  76.     res = []
  77.         for note,t in self.notes.iteritems():
  78.             if ((time() - t) > noteoff_interval):
  79.                 res.append(NoteOffEvent(ev.port, ev.channel, note, 127))
  80.                 print "stale: ", note
  81.         for note in res:
  82.             del self.notes[note.note]
  83.         res.append(ev)
  84.         return res
  85.  
  86. run([
  87.         Filter(NOTEOFF) >> Discard(),
  88.         Filter(NOTEON|CTRL) >> Process(HiHatController()),
  89.     Filter(AFTERTOUCH|POLY_AFTERTOUCH) >> VelocityFilter(0) >> NoteOff(EVENT_NOTE, 0),
  90.     ] >> Velocity(gamma=0.75)
  91. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement