Kaoru Onoe / Mbed 2 deprecated MIDI_Interpreter

Dependencies:   MIDI TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers events.cpp Source File

events.cpp

00001 #include <climits>
00002 #include "mbed.h"
00003 
00004 #include "define.h"
00005 #include "Instrument.h"
00006 #include "RingBuffer.h"
00007 
00008 extern Timer t;
00009 
00010 extern Instrument inst[NUM_INSTRUMENT];
00011 extern uint8_t const performerAddress[NUM_PERFORMER];
00012 
00013 
00014 void dispatchNoteOff(uint8_t statusByte, uint8_t noteNumber) {
00015     uint16_t targetPerformer;
00016     char message[2];
00017 
00018     // Find an Instrument sounding the specified note
00019     for (uint16_t i = 0; i < NUM_INSTRUMENT; i++) {
00020         if (inst[i].channel == (statusByte & 0x0f) + 1
00021             && inst[i].noteNumber == noteNumber) {
00022             inst[i].noteOnMs = 0;
00023             inst[i].channel = 0;
00024             inst[i].noteNumber = 0;
00025             inst[i].velocity = 0;
00026             
00027             // Send note off message to Performer
00028             targetPerformer = i / NUM_INSTRUMENT_IN_PERFORMER;
00029             message[0] = 0x80;
00030             message[1] = i % NUM_INSTRUMENT_IN_PERFORMER;
00031             // i2c.write(performerAddress[targetPerformer] << 1, message, 2);
00032             return;
00033         }
00034     }
00035 }
00036 
00037 void dispatchNoteOn(uint8_t statusByte, uint8_t noteNumber, uint8_t velocity) {
00038     uint32_t currentMs = t.read_ms();
00039     uint32_t oldestMs = UINT_MAX;
00040     uint16_t oldest = 0;
00041     uint16_t targetPerformer;
00042     char message[4];
00043     
00044     // Find a free Instrument or the one with the oldest note
00045     for (uint16_t i = 0; i < NUM_INSTRUMENT; i++) {
00046         if (inst[i].noteOnMs < oldestMs) {
00047             oldest = i;
00048             oldestMs = inst[i].noteOnMs;
00049         }
00050     }
00051     
00052     inst[oldest].noteOnMs = currentMs;
00053     inst[oldest].channel = (statusByte & 0x0f) + 1;
00054     inst[oldest].noteNumber = noteNumber;
00055     inst[oldest].velocity = velocity;
00056         
00057     // Send note on message to Performer
00058     targetPerformer = oldest / NUM_INSTRUMENT_IN_PERFORMER;
00059     message[0] = statusByte;
00060     message[1] = oldest % NUM_INSTRUMENT_IN_PERFORMER;
00061     message[2] = noteNumber;
00062     message[3] = velocity;
00063     // i2c.write(performerAddress[targetPerformer] << 1, message, 4);
00064 }
00065 
00066 void setControlChange(uint8_t statusByte, uint8_t controlNumber, uint8_t value) {
00067     char message[3];
00068     
00069     message[0] = statusByte;
00070     message[1] = controlNumber;
00071     message[2] = value;
00072     /*
00073     for (uint8_t i = 0; i < NUM_PERFORMER; i++) {
00074         i2c.write(performerAddress[i] << 1, message, 3);
00075     }
00076     */
00077 }
00078 
00079 void setProgramChange(uint8_t statusByte, uint8_t programNumber) {
00080     char message[2];
00081     
00082     message[0] = statusByte;
00083     message[1] = programNumber;
00084     /*
00085     for (uint8_t i = 0; i < NUM_PERFORMER; i++) {
00086         i2c.write(performerAddress[i] << 1, message, 2);
00087     }
00088     */
00089 }
00090 
00091 void setPitchBend(uint8_t statusByte, uint16_t bend) {
00092     char message[3];
00093     
00094     message[0] = statusByte;
00095     message[1] = bend >> 8;
00096     message[2] = bend & 0x00ff;
00097     /*
00098     for (uint8_t i = 0; i < NUM_PERFORMER; i++) {
00099         i2c.write(performerAddress[i] << 1, message, 3);
00100     }
00101     */
00102 }