Simple midi controller for Freedom KL25 board which generates tone (midi signal) according X-axis accelerometer. Modulation wheel is controlled by Y-axis and pitch wheel is controlled by touch slider

Dependencies:   MMA8451Q TSI USBDevice mbed

Fork of Midiudelator by Pavel M

Committer:
stefanimrich
Date:
Tue Aug 20 20:37:42 2013 +0000
Revision:
5:421d532c6403
Parent:
4:9c81faf1b372
X-axis plays 4 octave tones; Y-axes controls modulation wheel; touch slider controls pitch wheel

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PavelM 0:8e6b0e518883 1 #include "mbed.h"
PavelM 1:970adc6aae96 2 #include "MMA8451Q.h"
PavelM 1:970adc6aae96 3 #include "USBMIDI.h"
PavelM 4:9c81faf1b372 4 #include "TSISensor.h"
PavelM 1:970adc6aae96 5
PavelM 1:970adc6aae96 6 #define MMA8451_I2C_ADDRESS (0x1d<<1)
PavelM 0:8e6b0e518883 7
stefanimrich 5:421d532c6403 8 #define C1 0,187
stefanimrich 5:421d532c6403 9 #define D1 0.250
stefanimrich 5:421d532c6403 10 #define E1 0.312
stefanimrich 5:421d532c6403 11 #define F1 0.375
stefanimrich 5:421d532c6403 12 #define G1 0.437
stefanimrich 5:421d532c6403 13 #define A1 0.5
stefanimrich 5:421d532c6403 14 #define B1 0.562
stefanimrich 5:421d532c6403 15 #define C2 0.625
stefanimrich 5:421d532c6403 16 #define D2 0.687
stefanimrich 5:421d532c6403 17 #define E2 0.750
stefanimrich 5:421d532c6403 18 #define F2 0.812
stefanimrich 5:421d532c6403 19 #define G2 0.875
stefanimrich 5:421d532c6403 20 #define A2 0.937
stefanimrich 5:421d532c6403 21 #define B2 1
stefanimrich 5:421d532c6403 22 #define C3 1.062
stefanimrich 5:421d532c6403 23 #define D3 1.125
stefanimrich 5:421d532c6403 24 #define E3 1,187
stefanimrich 5:421d532c6403 25 #define F3 1.250
stefanimrich 5:421d532c6403 26 #define G3 1.312
stefanimrich 5:421d532c6403 27 #define A3 1.375
stefanimrich 5:421d532c6403 28 #define B3 1.437
stefanimrich 5:421d532c6403 29 #define C4 1.5
stefanimrich 5:421d532c6403 30 #define D4 1.562
stefanimrich 5:421d532c6403 31 #define E4 1.625
stefanimrich 5:421d532c6403 32 #define F4 1.687
stefanimrich 5:421d532c6403 33 #define G4 1.749
stefanimrich 5:421d532c6403 34 #define A4 1.811
stefanimrich 5:421d532c6403 35 #define B4 1.873
stefanimrich 5:421d532c6403 36
stefanimrich 5:421d532c6403 37 //1.935
stefanimrich 5:421d532c6403 38
stefanimrich 5:421d532c6403 39
PavelM 1:970adc6aae96 40
stefanimrich 2:c79be10b5ff3 41 #define NC1 48
stefanimrich 2:c79be10b5ff3 42 #define ND1 50
stefanimrich 2:c79be10b5ff3 43 #define NE1 52
stefanimrich 2:c79be10b5ff3 44 #define NF1 53
stefanimrich 2:c79be10b5ff3 45 #define NG1 55
stefanimrich 2:c79be10b5ff3 46 #define NA1 57
stefanimrich 5:421d532c6403 47 #define NB1 59
stefanimrich 2:c79be10b5ff3 48 #define NC2 60
stefanimrich 2:c79be10b5ff3 49 #define ND2 62
stefanimrich 2:c79be10b5ff3 50 #define NE2 64
stefanimrich 2:c79be10b5ff3 51 #define NF2 65
stefanimrich 2:c79be10b5ff3 52 #define NG2 67
stefanimrich 2:c79be10b5ff3 53 #define NA2 69
stefanimrich 5:421d532c6403 54 #define NB2 71
stefanimrich 2:c79be10b5ff3 55 #define NC3 72
stefanimrich 5:421d532c6403 56 #define ND3 74
stefanimrich 5:421d532c6403 57 #define NE3 76
stefanimrich 5:421d532c6403 58 #define NF3 77
stefanimrich 5:421d532c6403 59 #define NG3 79
stefanimrich 5:421d532c6403 60 #define NA3 81
stefanimrich 5:421d532c6403 61 #define NB3 83
stefanimrich 5:421d532c6403 62 #define NC4 84
stefanimrich 5:421d532c6403 63 #define ND4 86
stefanimrich 5:421d532c6403 64 #define NE4 88
stefanimrich 5:421d532c6403 65 #define NF4 89
stefanimrich 5:421d532c6403 66 #define NG4 91
stefanimrich 5:421d532c6403 67 #define NA4 93
stefanimrich 5:421d532c6403 68 #define NB4 95
PavelM 4:9c81faf1b372 69
PavelM 1:970adc6aae96 70 Serial pc(USBTX,USBRX);
PavelM 1:970adc6aae96 71 USBMIDI midi;
PavelM 4:9c81faf1b372 72 TSISensor tsi;
stefanimrich 3:e0684e01fca1 73
PavelM 1:970adc6aae96 74
PavelM 1:970adc6aae96 75 void show_message(MIDIMessage msg) {
PavelM 1:970adc6aae96 76 switch (msg.type()) {
PavelM 1:970adc6aae96 77 case MIDIMessage::NoteOnType:
PavelM 1:970adc6aae96 78 printf("NoteOn key:%d, velocity: %d, channel: %d\n", msg.key(), msg.velocity(), msg.channel());
PavelM 1:970adc6aae96 79 break;
PavelM 1:970adc6aae96 80 case MIDIMessage::NoteOffType:
PavelM 1:970adc6aae96 81 printf("NoteOff key:%d, velocity: %d, channel: %d\n", msg.key(), msg.velocity(), msg.channel());
PavelM 1:970adc6aae96 82 break;
PavelM 1:970adc6aae96 83 case MIDIMessage::ControlChangeType:
PavelM 1:970adc6aae96 84 printf("ControlChange controller: %d, data: %d\n", msg.controller(), msg.value());
PavelM 1:970adc6aae96 85 break;
PavelM 1:970adc6aae96 86 case MIDIMessage::PitchWheelType:
PavelM 1:970adc6aae96 87 printf("PitchWheel channel: %d, pitch: %d\n", msg.channel(), msg.pitch());
PavelM 1:970adc6aae96 88 break;
PavelM 1:970adc6aae96 89 default:
PavelM 1:970adc6aae96 90 printf("Another message\n");
PavelM 1:970adc6aae96 91 }
PavelM 1:970adc6aae96 92 }
PavelM 1:970adc6aae96 93
PavelM 1:970adc6aae96 94
PavelM 1:970adc6aae96 95
PavelM 1:970adc6aae96 96 int main() {
PavelM 1:970adc6aae96 97
stefanimrich 5:421d532c6403 98 int note=48, n=48;
PavelM 1:970adc6aae96 99 float notechng;
stefanimrich 5:421d532c6403 100 int pitch;
stefanimrich 5:421d532c6403 101 int modulation;
stefanimrich 5:421d532c6403 102 float m;
PavelM 1:970adc6aae96 103
PavelM 1:970adc6aae96 104 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
PavelM 1:970adc6aae96 105
PavelM 1:970adc6aae96 106 midi.attach(show_message); // call back for messages received
PavelM 4:9c81faf1b372 107
PavelM 1:970adc6aae96 108 while (1) {
PavelM 4:9c81faf1b372 109 #if 0
stefanimrich 5:421d532c6403 110 /* TEST CODE */
PavelM 4:9c81faf1b372 111 /* test pitch - commented in in - endif*/
PavelM 4:9c81faf1b372 112 midi.write(MIDIMessage::NoteOn(57));
stefanimrich 5:421d532c6403 113 for(int c=0; c<127; c= c+1){
PavelM 4:9c81faf1b372 114
stefanimrich 5:421d532c6403 115 midi.write(MIDIMessage::ControlChange(1, c));
stefanimrich 5:421d532c6403 116 //midi.write(MIDIMessage::PitchWheel(c));
PavelM 4:9c81faf1b372 117 pc.printf("Pitch: %d \n", c);
PavelM 4:9c81faf1b372 118 }
PavelM 4:9c81faf1b372 119 midi.write(MIDIMessage::NoteOff(57));
PavelM 4:9c81faf1b372 120 wait(0.5);
PavelM 4:9c81faf1b372 121 #endif
stefanimrich 5:421d532c6403 122 #if 1
PavelM 4:9c81faf1b372 123 /* read note */
PavelM 1:970adc6aae96 124 notechng = 1 - acc.getAccX();
stefanimrich 5:421d532c6403 125
stefanimrich 5:421d532c6403 126 /* get modulatio */
stefanimrich 5:421d532c6403 127 m = ((1 + acc.getAccY()) * 63);
stefanimrich 5:421d532c6403 128 modulation = int(m);
PavelM 4:9c81faf1b372 129
stefanimrich 5:421d532c6403 130 /* get pitch */
stefanimrich 5:421d532c6403 131 pitch = (int(tsi.readPercentage() * 8192) - 8192);
PavelM 0:8e6b0e518883 132
PavelM 1:970adc6aae96 133 if(notechng > C1) note= NC1;
PavelM 1:970adc6aae96 134 if(notechng > D1) note= ND1;
PavelM 1:970adc6aae96 135 if(notechng > E1) note= NE1;
PavelM 1:970adc6aae96 136 if(notechng > F1) note= NF1;
PavelM 1:970adc6aae96 137 if(notechng > G1) note= NG1;
PavelM 1:970adc6aae96 138 if(notechng > A1) note= NA1;
stefanimrich 5:421d532c6403 139 if(notechng > B1) note= NB1;
PavelM 1:970adc6aae96 140 if(notechng > C2) note= NC2;
PavelM 1:970adc6aae96 141 if(notechng > D2) note= ND2;
PavelM 1:970adc6aae96 142 if(notechng > E2) note= NE2;
PavelM 1:970adc6aae96 143 if(notechng > F2) note= NF2;
PavelM 1:970adc6aae96 144 if(notechng > G2) note= NG2;
PavelM 1:970adc6aae96 145 if(notechng > A2) note= NA2;
stefanimrich 5:421d532c6403 146 if(notechng > B2) note= NB2;
stefanimrich 5:421d532c6403 147 if(notechng > C3) note= NC3;
stefanimrich 5:421d532c6403 148 if(notechng > D3) note= ND3;
stefanimrich 5:421d532c6403 149 // if(notechng > E3) note= NE3;
stefanimrich 5:421d532c6403 150 if(notechng > F3) note= NF3;
stefanimrich 5:421d532c6403 151 if(notechng > G3) note= NG3;
stefanimrich 5:421d532c6403 152 if(notechng > A3) note= NA3;
stefanimrich 5:421d532c6403 153 if(notechng > B3) note= NB3;
stefanimrich 5:421d532c6403 154 if(notechng > C4) note= NC4;
stefanimrich 5:421d532c6403 155 if(notechng > D4) note= ND4;
stefanimrich 5:421d532c6403 156 if(notechng > E4) note= NE4;
stefanimrich 5:421d532c6403 157 if(notechng > F4) note= NF4;
stefanimrich 5:421d532c6403 158 if(notechng > G4) note= NG4;
stefanimrich 5:421d532c6403 159 if(notechng > A4) note= NA4;
stefanimrich 5:421d532c6403 160 if(notechng > B4) note= NB4;
stefanimrich 5:421d532c6403 161
stefanimrich 5:421d532c6403 162 pc.printf("note: %d \n", note);
stefanimrich 5:421d532c6403 163 pc.printf("notechng: %f \n", notechng);
PavelM 1:970adc6aae96 164
stefanimrich 5:421d532c6403 165 if (note != n) {
stefanimrich 5:421d532c6403 166 midi.write(MIDIMessage::NoteOff(n));
stefanimrich 5:421d532c6403 167 n=note;
stefanimrich 5:421d532c6403 168 midi.write(MIDIMessage::NoteOn(n));
stefanimrich 5:421d532c6403 169 }
stefanimrich 5:421d532c6403 170
stefanimrich 5:421d532c6403 171 midi.write(MIDIMessage::ControlChange(1, modulation));
stefanimrich 5:421d532c6403 172 midi.write(MIDIMessage::PitchWheel(pitch));
stefanimrich 5:421d532c6403 173
stefanimrich 5:421d532c6403 174 wait(0.1);
stefanimrich 5:421d532c6403 175 #endif
PavelM 0:8e6b0e518883 176 }
PavelM 1:970adc6aae96 177 }