Basic example for send MIDI messages

Dependencies:   mbed

Committer:
jose_23991
Date:
Mon Sep 29 15:45:18 2014 +0000
Revision:
0:2f27565123e1
Version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jose_23991 0:2f27565123e1 1 #include "midii.h"
jose_23991 0:2f27565123e1 2 #include "mbed.h"
jose_23991 0:2f27565123e1 3
jose_23991 0:2f27565123e1 4 MIDII::MIDII(PinName p_tx, PinName p_rx) : serial(p_tx, p_rx)
jose_23991 0:2f27565123e1 5 {
jose_23991 0:2f27565123e1 6 serial.baud(MIDI_VELOCITY);
jose_23991 0:2f27565123e1 7 notes_init();
jose_23991 0:2f27565123e1 8 }
jose_23991 0:2f27565123e1 9
jose_23991 0:2f27565123e1 10 MIDII::~MIDII()
jose_23991 0:2f27565123e1 11 {}
jose_23991 0:2f27565123e1 12
jose_23991 0:2f27565123e1 13 void MIDII::noteOn(unsigned int channel, unsigned int key, unsigned int octave, unsigned int velocity)
jose_23991 0:2f27565123e1 14 {
jose_23991 0:2f27565123e1 15 if(channel > CH_16)
jose_23991 0:2f27565123e1 16 channel = CH_16;
jose_23991 0:2f27565123e1 17 if(velocity > 127)
jose_23991 0:2f27565123e1 18 velocity = 127;
jose_23991 0:2f27565123e1 19 if(octave > 10)
jose_23991 0:2f27565123e1 20 octave = 10;
jose_23991 0:2f27565123e1 21
jose_23991 0:2f27565123e1 22 serial.putc(NOTE_ON & channel);
jose_23991 0:2f27565123e1 23 serial.putc(note[octave][key]);
jose_23991 0:2f27565123e1 24 serial.putc(velocity);
jose_23991 0:2f27565123e1 25 }
jose_23991 0:2f27565123e1 26
jose_23991 0:2f27565123e1 27 void MIDII::noteOff(unsigned int channel, unsigned int key, unsigned int octave, unsigned int velocity)
jose_23991 0:2f27565123e1 28 {
jose_23991 0:2f27565123e1 29 if(channel > CH_16)
jose_23991 0:2f27565123e1 30 channel = CH_16;
jose_23991 0:2f27565123e1 31 if(velocity > 127)
jose_23991 0:2f27565123e1 32 velocity = 127;
jose_23991 0:2f27565123e1 33
jose_23991 0:2f27565123e1 34 serial.putc(NOTE_OFF & channel);
jose_23991 0:2f27565123e1 35 serial.putc(key);
jose_23991 0:2f27565123e1 36 serial.putc(velocity);
jose_23991 0:2f27565123e1 37 }
jose_23991 0:2f27565123e1 38
jose_23991 0:2f27565123e1 39 void MIDII::programChange(unsigned int channel, unsigned int program)
jose_23991 0:2f27565123e1 40 {
jose_23991 0:2f27565123e1 41 if(channel > CH_16)
jose_23991 0:2f27565123e1 42 channel = CH_16;
jose_23991 0:2f27565123e1 43
jose_23991 0:2f27565123e1 44 serial.putc(NOTE_PROGRAM_CHANGE & channel);
jose_23991 0:2f27565123e1 45 serial.putc(program);
jose_23991 0:2f27565123e1 46 }
jose_23991 0:2f27565123e1 47
jose_23991 0:2f27565123e1 48 //send MIDI message
jose_23991 0:2f27565123e1 49 void MIDII::MIDImessage(unsigned int command, unsigned int channel, unsigned int note, unsigned int octave, unsigned int velocity)
jose_23991 0:2f27565123e1 50 {
jose_23991 0:2f27565123e1 51 serial.putc(command & channel);//send note on or note off command
jose_23991 0:2f27565123e1 52 serial.putc(note);//send pitch data
jose_23991 0:2f27565123e1 53 serial.putc(velocity);//send velocity data
jose_23991 0:2f27565123e1 54 }
jose_23991 0:2f27565123e1 55
jose_23991 0:2f27565123e1 56 void MIDII::notes_init()
jose_23991 0:2f27565123e1 57 {
jose_23991 0:2f27565123e1 58 int a, b, c;
jose_23991 0:2f27565123e1 59
jose_23991 0:2f27565123e1 60 c = 0;
jose_23991 0:2f27565123e1 61 for(a = 0; a < OCTAVE; a++)
jose_23991 0:2f27565123e1 62 {
jose_23991 0:2f27565123e1 63 for(b = 0; b < NUMBER; b++)
jose_23991 0:2f27565123e1 64 {
jose_23991 0:2f27565123e1 65 note[a][b] = c;
jose_23991 0:2f27565123e1 66 c++;
jose_23991 0:2f27565123e1 67 }
jose_23991 0:2f27565123e1 68 }
jose_23991 0:2f27565123e1 69 }