|
| 1 | +# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import board |
| 5 | +import usb_midi |
| 6 | +import adafruit_midi |
| 7 | +import simpleio |
| 8 | +from analogio import AnalogIn |
| 9 | +from adafruit_midi.control_change import ControlChange |
| 10 | +from adafruit_midi.pitch_bend import PitchBend |
| 11 | + |
| 12 | +# midi setup |
| 13 | +midi = adafruit_midi.MIDI( |
| 14 | + midi_in=usb_midi.ports[0], in_channel=0, midi_out=usb_midi.ports[1], out_channel=0 |
| 15 | +) |
| 16 | + |
| 17 | +# potentiometer setup |
| 18 | +mod_pot = AnalogIn(board.A0) |
| 19 | +pitchDown_pot = AnalogIn(board.A1) |
| 20 | +pitchUp_pot = AnalogIn(board.A2) |
| 21 | +sus_pot = AnalogIn(board.A3) |
| 22 | + |
| 23 | +# function to read analog input |
| 24 | +def val(pin): |
| 25 | + return pin.value |
| 26 | + |
| 27 | +# variables for last read value |
| 28 | +# defaults to 0 |
| 29 | +# no pitchbend is 8192 |
| 30 | +mod_val2 = 0 |
| 31 | +pitchDown_val2 = 8192 |
| 32 | +pitchUp_val2 = 8192 |
| 33 | +sus_val2 = 0 |
| 34 | + |
| 35 | +while True: |
| 36 | + |
| 37 | + # map range of analog input to midi values |
| 38 | + # pitchbend range is 0 to 16383 with 8192 centered or no pitchbend |
| 39 | + mod_val1 = round(simpleio.map_range(val(mod_pot), 0, 65535, 0, 127)) |
| 40 | + pitchDown_val1 = round(simpleio.map_range(val(pitchDown_pot), 0, 65535, 0, 8192)) |
| 41 | + pitchUp_val1 = round(simpleio.map_range(val(pitchUp_pot), 0, 65535, 8192, 16383)) |
| 42 | + sus_val1 = round(simpleio.map_range(val(sus_pot), 0, 65535, 0, 127)) |
| 43 | + |
| 44 | + # if modulation value is updated... |
| 45 | + if abs(mod_val1 - mod_val2) > 2: |
| 46 | + # update mod_val2 |
| 47 | + mod_val2 = mod_val1 |
| 48 | + # create integer |
| 49 | + modulation = int(mod_val2) |
| 50 | + # create CC message |
| 51 | + modWheel = ControlChange(1, modulation) |
| 52 | + # send CC message |
| 53 | + midi.send(modWheel) |
| 54 | + |
| 55 | + # pitchbend down value is updated... |
| 56 | + if abs(pitchDown_val1 - pitchDown_val2) > 75: |
| 57 | + # update pitchDown_val2 |
| 58 | + pitchDown_val2 = pitchDown_val1 |
| 59 | + # create PitchBend message |
| 60 | + pitchDown = PitchBend(int(pitchDown_val2)) |
| 61 | + # send PitchBend message |
| 62 | + midi.send(pitchDown) |
| 63 | + |
| 64 | + # pitchbend up value is updated... |
| 65 | + if abs(pitchUp_val1 - pitchUp_val2) > 75: |
| 66 | + # updated pitchUp_val2 |
| 67 | + pitchUp_val2 = pitchUp_val1 |
| 68 | + # create PitchBend message |
| 69 | + pitchUp = PitchBend(int(pitchUp_val2)) |
| 70 | + # send PitchBend message |
| 71 | + midi.send(pitchUp) |
| 72 | + |
| 73 | + # sustain value is updated... |
| 74 | + if abs(sus_val1 - sus_val2) > 2: |
| 75 | + # update sus_val2 |
| 76 | + sus_val2 = sus_val1 |
| 77 | + # create integer |
| 78 | + sustain = int(sus_val2) |
| 79 | + # create CC message |
| 80 | + sustainPedal = ControlChange(64, sustain) |
| 81 | + # send CC message |
| 82 | + midi.send(sustainPedal) |
0 commit comments