Advertisement
talofer99

drum code

Mar 22nd, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. import time
  2.  
  3. # Import SPI library (for hardware SPI) and MCP3008 library.
  4. import Adafruit_GPIO.SPI as SPI
  5. import Adafruit_MCP3008
  6. import fluidsynth
  7.  
  8. fs = fluidsynth.Synth()
  9. fs.start(driver="alsa")
  10. sfid0 = fs.sfload("Audio - Sound Font - Sonic Implants Session Drums.sf2")
  11. fs.program_select(0, sfid0, 0, 0)
  12.  
  13. # Hardware SPI configuration:
  14. SPI_PORT = 0
  15. SPI_DEVICE = 0
  16. mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
  17.  
  18. # channel state and settings
  19. MIDI_NOTES = [48, 38, 51, 50, 55, 49, 50, 51];
  20. NUMBER_OF_CHANNELS = 6
  21. MILLIS_BETWEEN_PICKS_AND_OFF = 50
  22. MILLIS_BETWEEN_TWO_OF_THE_SAME_NOTE = 10
  23.  
  24. channel_state = [0]*NUMBER_OF_CHANNELS
  25. channel_pick = [0]*NUMBER_OF_CHANNELS
  26. channel_pick_time = [0]*NUMBER_OF_CHANNELS
  27. channel_thrushhold = [30, 30, 30, 20, 60, 30, 30, 30]
  28. channel_state_millis = [0]*NUMBER_OF_CHANNELS
  29.  
  30. #millis = int(round(time.time() * 1000))
  31. # Main program loop.
  32. while True:
  33. # Read all the ADC channel values in a list.
  34. values = [0]*NUMBER_OF_CHANNELS
  35. for i in range(NUMBER_OF_CHANNELS):
  36. # The read_adc function will get the value of the specified channel (0-7).
  37. values[i] = mcp.read_adc(i)
  38. #Stat 0 - IDLE
  39. if channel_state[i] == 0:
  40. #if we cross the threshhold
  41. if values[i] > channel_thrushhold[i] and channel_state_millis[i] + MILLIS_BETWEEN_TWO_OF_THE_SAME_NOTE <= int(round(time.time() * 1000)) :
  42. #set state to 1
  43. channel_state[i] = 1
  44. channel_state_millis[i] = int(round(time.time() * 1000))
  45. elif channel_state[i] == 1:
  46. # while till we over the pick
  47. if channel_pick[i] < values[i]:
  48. #play note
  49. fs.noteon(0, MIDI_NOTES[i] , 127)
  50. #set state to 2
  51. channel_state[i] = 2
  52. channel_state_millis[i] = int(round(time.time() * 1000))
  53. else:
  54. # save last pick
  55. channel_pick[i] = values[i]
  56. elif channel_state[i] == 2:
  57. #if the note is playing for long enough
  58. if channel_state_millis[i] + MILLIS_BETWEEN_PICKS_AND_OFF <= int(round(time.time() * 1000)):
  59. #note off
  60. fs.noteoff(0, MIDI_NOTES[i])
  61. #clear the pick
  62. channel_pick[i] = 0
  63. #set state back to IDLE
  64. channel_state[i] = 0
  65. channel_state_millis[i] = int(round(time.time() * 1000))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement