Source code for a MIDI controller built from parts and designed to be extensible
Fork of USBMIDI_HelloWorld by
main.cpp@1:1e34feaa7774, 2016-01-04 (annotated)
- Committer:
- earlz
- Date:
- Mon Jan 04 05:00:13 2016 +0000
- Revision:
- 1:1e34feaa7774
- Parent:
- 0:4b55d56b6b61
- Child:
- 2:012e56772666
Making it work with a simple potentiometer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:4b55d56b6b61 | 1 | // Hello World example for the USBMIDI library |
simon | 0:4b55d56b6b61 | 2 | |
simon | 0:4b55d56b6b61 | 3 | #include "mbed.h" |
simon | 0:4b55d56b6b61 | 4 | #include "USBMIDI.h" |
earlz | 1:1e34feaa7774 | 5 | #include "MovingAverage.h" |
simon | 0:4b55d56b6b61 | 6 | |
simon | 0:4b55d56b6b61 | 7 | void show_message(MIDIMessage msg) { |
simon | 0:4b55d56b6b61 | 8 | switch (msg.type()) { |
simon | 0:4b55d56b6b61 | 9 | case MIDIMessage::NoteOnType: |
simon | 0:4b55d56b6b61 | 10 | printf("NoteOn key:%d, velocity: %d, channel: %d\n", msg.key(), msg.velocity(), msg.channel()); |
simon | 0:4b55d56b6b61 | 11 | break; |
simon | 0:4b55d56b6b61 | 12 | case MIDIMessage::NoteOffType: |
simon | 0:4b55d56b6b61 | 13 | printf("NoteOff key:%d, velocity: %d, channel: %d\n", msg.key(), msg.velocity(), msg.channel()); |
simon | 0:4b55d56b6b61 | 14 | break; |
simon | 0:4b55d56b6b61 | 15 | case MIDIMessage::ControlChangeType: |
simon | 0:4b55d56b6b61 | 16 | printf("ControlChange controller: %d, data: %d\n", msg.controller(), msg.value()); |
simon | 0:4b55d56b6b61 | 17 | break; |
simon | 0:4b55d56b6b61 | 18 | case MIDIMessage::PitchWheelType: |
simon | 0:4b55d56b6b61 | 19 | printf("PitchWheel channel: %d, pitch: %d\n", msg.channel(), msg.pitch()); |
simon | 0:4b55d56b6b61 | 20 | break; |
simon | 0:4b55d56b6b61 | 21 | default: |
simon | 0:4b55d56b6b61 | 22 | printf("Another message\n"); |
simon | 0:4b55d56b6b61 | 23 | } |
simon | 0:4b55d56b6b61 | 24 | } |
simon | 0:4b55d56b6b61 | 25 | |
earlz | 1:1e34feaa7774 | 26 | static const int MIDI_MAX_VALUE = 16384; |
earlz | 1:1e34feaa7774 | 27 | static const float MIDI_MAX_VALUE_F = 16384.0f; |
earlz | 1:1e34feaa7774 | 28 | |
earlz | 1:1e34feaa7774 | 29 | |
earlz | 1:1e34feaa7774 | 30 | static const float TOLERANCE = 0.01f; |
earlz | 1:1e34feaa7774 | 31 | static const int NOISE_FLOOR = 600; |
earlz | 1:1e34feaa7774 | 32 | |
earlz | 1:1e34feaa7774 | 33 | static const float LOWER_TOLERANCE = 0.016f; |
earlz | 1:1e34feaa7774 | 34 | static const float LOWER_TOLERANCE_BEGIN = 0.34f; |
earlz | 1:1e34feaa7774 | 35 | |
simon | 0:4b55d56b6b61 | 36 | USBMIDI midi; |
earlz | 1:1e34feaa7774 | 37 | AnalogIn pot1(p20); |
earlz | 1:1e34feaa7774 | 38 | Serial pc(USBTX, USBRX); // tx, rx |
earlz | 1:1e34feaa7774 | 39 | |
earlz | 1:1e34feaa7774 | 40 | void write_full_cc(int controlmsb, int controllsb, int channel, int value){ |
earlz | 1:1e34feaa7774 | 41 | int lsb = value / 128; //value & 0x7F; |
earlz | 1:1e34feaa7774 | 42 | int msb = value % 128; //value & 0x7F80 >> 7; |
earlz | 1:1e34feaa7774 | 43 | midi.write(MIDIMessage::ControlChange(controlmsb, msb, channel)); |
earlz | 1:1e34feaa7774 | 44 | midi.write(MIDIMessage::ControlChange(controllsb, lsb, channel)); |
earlz | 1:1e34feaa7774 | 45 | } |
earlz | 1:1e34feaa7774 | 46 | |
earlz | 1:1e34feaa7774 | 47 | #define SMOOTHING_AMOUNT 500 |
simon | 0:4b55d56b6b61 | 48 | |
earlz | 1:1e34feaa7774 | 49 | int main() { |
earlz | 1:1e34feaa7774 | 50 | //MovingAverage <float>vavg(100,0.0); |
simon | 0:4b55d56b6b61 | 51 | midi.attach(show_message); // call back for messages received |
earlz | 1:1e34feaa7774 | 52 | |
earlz | 1:1e34feaa7774 | 53 | float last_value = 0.0f; |
earlz | 1:1e34feaa7774 | 54 | while (1) { |
earlz | 1:1e34feaa7774 | 55 | //float values[SMOOTHING_AMOUNT] = {0.0f}; |
earlz | 1:1e34feaa7774 | 56 | float counter=0.0f; |
earlz | 1:1e34feaa7774 | 57 | for(int i=0;i<SMOOTHING_AMOUNT;i++){ |
earlz | 1:1e34feaa7774 | 58 | wait_us(10); |
earlz | 1:1e34feaa7774 | 59 | counter+=pot1; |
earlz | 1:1e34feaa7774 | 60 | } |
earlz | 1:1e34feaa7774 | 61 | float value = counter / SMOOTHING_AMOUNT; |
earlz | 1:1e34feaa7774 | 62 | int midi_value = (int)(MIDI_MAX_VALUE_F * value); |
earlz | 1:1e34feaa7774 | 63 | |
earlz | 1:1e34feaa7774 | 64 | //at low voltage noise takes over.. |
earlz | 1:1e34feaa7774 | 65 | if(midi_value < NOISE_FLOOR){ |
earlz | 1:1e34feaa7774 | 66 | value = 0.0f; |
earlz | 1:1e34feaa7774 | 67 | midi_value = 0; |
earlz | 1:1e34feaa7774 | 68 | } |
earlz | 1:1e34feaa7774 | 69 | if(value - last_value > TOLERANCE || value - last_value < -TOLERANCE){ |
earlz | 1:1e34feaa7774 | 70 | //as we approach noise floor, things get noisey.. |
earlz | 1:1e34feaa7774 | 71 | if(value < LOWER_TOLERANCE_BEGIN && !(value - last_value > LOWER_TOLERANCE || value - last_value < -LOWER_TOLERANCE)){ |
earlz | 1:1e34feaa7774 | 72 | continue; |
earlz | 1:1e34feaa7774 | 73 | } |
earlz | 1:1e34feaa7774 | 74 | pc.printf("sent: %f, %i\r\n", value, midi_value); |
earlz | 1:1e34feaa7774 | 75 | last_value = value; |
earlz | 1:1e34feaa7774 | 76 | write_full_cc(20, 52, 0, midi_value); |
earlz | 1:1e34feaa7774 | 77 | } |
earlz | 1:1e34feaa7774 | 78 | |
earlz | 1:1e34feaa7774 | 79 | /*for(int i=48; i<83; i++) { // send some messages! |
simon | 0:4b55d56b6b61 | 80 | midi.write(MIDIMessage::NoteOn(i)); |
simon | 0:4b55d56b6b61 | 81 | wait(0.25); |
simon | 0:4b55d56b6b61 | 82 | midi.write(MIDIMessage::NoteOff(i)); |
simon | 0:4b55d56b6b61 | 83 | wait(0.5); |
earlz | 1:1e34feaa7774 | 84 | }*/ |
simon | 0:4b55d56b6b61 | 85 | } |
simon | 0:4b55d56b6b61 | 86 | } |