comparison projects/basic_midi/render.cpp @ 199:b128e3ea84ff

Fixed midi docs, implentation and example
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 05 Feb 2016 06:49:33 +0000
parents 265a527f8be8
children 97faaf985164
comparison
equal deleted inserted replaced
198:62f6269f4b3e 199:b128e3ea84ff
23 Midi midi; 23 Midi midi;
24 bool setup(BeagleRTContext *context, void *userData) 24 bool setup(BeagleRTContext *context, void *userData)
25 { 25 {
26 midi.readFrom(0); 26 midi.readFrom(0);
27 midi.writeTo(0); 27 midi.writeTo(0);
28 midi.enableParser(true); 28 // midi.enableParser(true);
29 if(context->analogFrames == 0) { 29 if(context->analogFrames == 0) {
30 rt_printf("Error: this example needs the matrix enabled\n"); 30 rt_printf("Error: this example needs the matrix enabled\n");
31 return false; 31 return false;
32 } 32 }
33 return true; 33 return true;
48 static bool noteOn = 0; 48 static bool noteOn = 0;
49 static int velocity = 0; 49 static int velocity = 0;
50 static int noteNumber = 0; 50 static int noteNumber = 0;
51 static int waitingFor = kNoteOn; 51 static int waitingFor = kNoteOn;
52 static int playingNote = -1; 52 static int playingNote = -1;
53 ///* 53 /*
54 int message; 54 int message;
55 if(0) // one way of getting the midi data is to parse them yourself (you should set midi.enableParser(false) above): 55 // one way of getting the midi data is to parse them yourself (you should set midi.enableParser(false) above):
56 while ((message = midi.getInput()) >= 0){ 56 while ((message = midi.getInput()) >= 0){
57 rt_printf("%d\n", message); 57 rt_printf("%d\n", message);
58 switch(waitingFor){ 58 switch(waitingFor){
59 case kNoteOn: 59 case kNoteOn:
60 if(message == noteOnStatus){ 60 if(message == noteOnStatus){
89 rt_printf("NoteOn: %d, NoteNumber: %d, velocity: %d\n", noteOn, noteNumber, velocity); 89 rt_printf("NoteOn: %d, NoteNumber: %d, velocity: %d\n", noteOn, noteNumber, velocity);
90 } 90 }
91 break; 91 break;
92 } 92 }
93 } 93 }
94 //*/ 94 */
95 int num; 95 int num;
96 //alternatively, you can use the built-in parser (only processes channel messages at the moment). 96 //alternatively, you can use the built-in parser (only processes channel messages at the moment).
97 while((num = midi.getParser()->numAvailableMessages()) > 0){ 97 while((num = midi.getParser()->numAvailableMessages()) > 0){
98 static MidiChannelMessage message; 98 static MidiChannelMessage message;
99 message = midi.getParser()->getNextChannelMessage(); 99 message = midi.getParser()->getNextChannelMessage();
100 message.prettyPrint(); 100 message.prettyPrint();
101 if(message.getType() == kmmNoteOn){
102 f0 = powf(2, (message.getDataByte(0)-69)/12.0f) * 440;
103 velocity = message.getDataByte(1);
104 phaseIncrement = 2 * M_PI * f0 / context->audioSampleRate;
105 noteOn = velocity > 0;
106 rt_printf("v0:%f, ph: %6.5f, velocity: %d\n", f0, phaseIncrement, velocity);
107 }
101 } 108 }
102 109
103 for(unsigned int n = 0; n < context->analogFrames; n++){ 110 for(unsigned int n = 0; n < context->analogFrames; n++){
104 static int count = 0; 111 static int count = 0;
105 static bool state = 0; 112 static bool state = 0;
107 if(count % 40000 == 0){ 114 if(count % 40000 == 0){
108 state = !state; 115 state = !state;
109 midi_byte_t bytes[6] = {176, 30, state*127, 176, 67, 30}; // toggle the OWL led and ask for the led status 116 midi_byte_t bytes[6] = {176, 30, state*127, 176, 67, 30}; // toggle the OWL led and ask for the led status
110 midi.writeOutput(bytes, 6); 117 midi.writeOutput(bytes, 6);
111 } 118 }
112 119 count++;
120 }
121 for(unsigned int n = 0; n < context->audioFrames; n++){
113 if(noteOn == 1){ 122 if(noteOn == 1){
114 static float phase = 0; 123 static float phase = 0;
115 phase += phaseIncrement; 124 phase += phaseIncrement;
116 if(phase > 2 * M_PI) 125 if(phase > 2 * M_PI)
117 phase -= 2 * M_PI; 126 phase -= 2 * M_PI;
120 audioWriteFrame(context, n, 1, value); 129 audioWriteFrame(context, n, 1, value);
121 } else { 130 } else {
122 audioWriteFrame(context, n, 0, 0); 131 audioWriteFrame(context, n, 0, 0);
123 audioWriteFrame(context, n, 1, 0); 132 audioWriteFrame(context, n, 1, 0);
124 } 133 }
125 count++;
126 } 134 }
127 } 135 }
128 136
129 // cleanup() is called once at the end, after the audio has stopped. 137 // cleanup() is called once at the end, after the audio has stopped.
130 // Release any resources that were allocated in setup(). 138 // Release any resources that were allocated in setup().