annotate projects/basic_midi/render.cpp @ 191:b3a306da03e0

Implemented Midi output
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 03 Feb 2016 01:18:30 +0000
parents d7148d21aaa5
children 265a527f8be8
rev   line source
giuliomoro@181 1 /*
giuliomoro@181 2 * render.cpp
giuliomoro@181 3 *
giuliomoro@181 4 * Created on: Oct 24, 2014
giuliomoro@181 5 * Author: parallels
giuliomoro@181 6 */
giuliomoro@181 7
giuliomoro@181 8 #include <BeagleRT.h>
giuliomoro@181 9 #include <Midi.h>
giuliomoro@187 10 #include <Utilities.h>
giuliomoro@181 11 #include <stdlib.h>
giuliomoro@181 12 #include <rtdk.h>
giuliomoro@181 13 #include <cmath>
giuliomoro@181 14
giuliomoro@181 15 // setup() is called once before the audio rendering starts.
giuliomoro@181 16 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@181 17 // on the period size or sample rate.
giuliomoro@181 18 //
giuliomoro@181 19 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@181 20 // in from the call to initAudio().
giuliomoro@181 21 //
giuliomoro@181 22 // Return true on success; returning false halts the program.
giuliomoro@181 23 Midi midi;
giuliomoro@181 24 bool setup(BeagleRTContext *context, void *userData)
giuliomoro@181 25 {
giuliomoro@181 26 midi.readFrom(0);
giuliomoro@191 27 midi.writeTo(0);
giuliomoro@181 28 if(context->analogFrames == 0) {
giuliomoro@181 29 rt_printf("Error: this example needs the matrix enabled\n");
giuliomoro@181 30 return false;
giuliomoro@181 31 }
giuliomoro@181 32 return true;
giuliomoro@181 33 }
giuliomoro@181 34
giuliomoro@181 35 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@181 36 // Input and output are given from the audio hardware and the other
giuliomoro@181 37 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
giuliomoro@181 38 // will be 0.
giuliomoro@181 39
giuliomoro@191 40 static midi_byte_t noteOnStatus = 0x90; //on channel 1
giuliomoro@181 41
giuliomoro@181 42 enum {kVelocity, kNoteOn, kNoteNumber};
giuliomoro@181 43 void render(BeagleRTContext *context, void *userData)
giuliomoro@181 44 {
giuliomoro@181 45 static float f0;
giuliomoro@181 46 static float phaseIncrement = 0;
giuliomoro@181 47 int message;
giuliomoro@181 48 static bool noteOn = 0;
giuliomoro@181 49 static int velocity = 0;
giuliomoro@181 50 static int noteNumber = 0;
giuliomoro@181 51 static int waitingFor = kNoteOn;
giuliomoro@184 52 static int playingNote = -1;
giuliomoro@181 53 while ((message = midi.getInput()) >= 0){
giuliomoro@191 54 rt_printf("%d\n", message);
giuliomoro@181 55 switch(waitingFor){
giuliomoro@181 56 case kNoteOn:
giuliomoro@181 57 if(message == noteOnStatus){
giuliomoro@181 58 waitingFor = kNoteNumber;
giuliomoro@181 59 }
giuliomoro@181 60 break;
giuliomoro@181 61 case kNoteNumber:
giuliomoro@181 62 if((message & (1<<8)) == 0){
giuliomoro@181 63 noteNumber = message;
giuliomoro@181 64 waitingFor = kVelocity;
giuliomoro@181 65 }
giuliomoro@181 66 break;
giuliomoro@181 67 case kVelocity:
giuliomoro@181 68 if((message & (1<<8)) == 0){
giuliomoro@184 69 int _velocity = message;
giuliomoro@181 70 waitingFor = kNoteOn;
giuliomoro@184 71 // "monophonic" behaviour, with priority to the latest note on
giuliomoro@184 72 // i.e.: a note off from a previous note does not stop the current note
giuliomoro@184 73 // still you might end up having a key down and no note being played if you pressed and released another
giuliomoro@184 74 // key in the meantime
giuliomoro@184 75 if(_velocity == 0 && noteNumber == playingNote){
giuliomoro@181 76 noteOn = false;
giuliomoro@184 77 playingNote = -1;
giuliomoro@184 78 velocity = _velocity;
giuliomoro@184 79 } else if (_velocity > 0) {
giuliomoro@184 80 noteOn = true;
giuliomoro@184 81 velocity = _velocity;
giuliomoro@184 82 playingNote = noteNumber;
giuliomoro@184 83 f0 = powf(2, (playingNote-69)/12.0f) * 440;
giuliomoro@184 84 phaseIncrement = 2 * M_PI * f0 / context->audioSampleRate;
giuliomoro@184 85 }
giuliomoro@181 86 rt_printf("NoteOn: %d, NoteNumber: %d, velocity: %d\n", noteOn, noteNumber, velocity);
giuliomoro@181 87 }
giuliomoro@181 88 break;
giuliomoro@181 89 }
giuliomoro@181 90 }
giuliomoro@191 91
giuliomoro@191 92 for(unsigned int n = 0; n < context->analogFrames; n++){
giuliomoro@191 93 static int count = 0;
giuliomoro@191 94 static bool state = 0;
giuliomoro@191 95 analogWriteFrameOnce(context, n, 1, state);
giuliomoro@191 96 if(count % 40000 == 0){
giuliomoro@191 97 state = !state;
giuliomoro@191 98 midi_byte_t bytes[6] = {176, 30, state*127, 176, 67, 30}; // toggle the OWL led and ask for the led status
giuliomoro@191 99 midi.writeOutput(bytes, 6);
giuliomoro@191 100 }
giuliomoro@191 101
giuliomoro@181 102 if(noteOn == 1){
giuliomoro@181 103 static float phase = 0;
giuliomoro@181 104 phase += phaseIncrement;
giuliomoro@181 105 if(phase > 2 * M_PI)
giuliomoro@181 106 phase -= 2 * M_PI;
giuliomoro@181 107 float value = sinf(phase) * velocity/128.0f;
giuliomoro@181 108 audioWriteFrame(context, n, 0, value);
giuliomoro@181 109 audioWriteFrame(context, n, 1, value);
giuliomoro@181 110 } else {
giuliomoro@181 111 audioWriteFrame(context, n, 0, 0);
giuliomoro@181 112 audioWriteFrame(context, n, 1, 0);
giuliomoro@181 113 }
giuliomoro@191 114 count++;
giuliomoro@181 115 }
giuliomoro@181 116 }
giuliomoro@181 117
giuliomoro@181 118 // cleanup() is called once at the end, after the audio has stopped.
giuliomoro@181 119 // Release any resources that were allocated in setup().
giuliomoro@181 120
giuliomoro@181 121 void cleanup(BeagleRTContext *context, void *userData)
giuliomoro@181 122 {
giuliomoro@181 123
giuliomoro@181 124 }