annotate projects/basic_midi/render.cpp @ 184:9108a0a34cb8

Better(non ideal) monophonic behaviour of the basic midi implementation. Would require to remember what notes are being held down and release either the most recent or low/high priority.
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 17 Jan 2016 21:56:13 +0000
parents 391ad036557d
children d7148d21aaa5
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
giuliomoro@181 9 #include <BeagleRT.h>
giuliomoro@181 10 #include <Midi.h>
giuliomoro@181 11 #include <stdlib.h>
giuliomoro@181 12 #include <Utilities.h>
giuliomoro@181 13 #include <rtdk.h>
giuliomoro@181 14 #include <cmath>
giuliomoro@181 15
giuliomoro@181 16 // setup() is called once before the audio rendering starts.
giuliomoro@181 17 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@181 18 // on the period size or sample rate.
giuliomoro@181 19 //
giuliomoro@181 20 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@181 21 // in from the call to initAudio().
giuliomoro@181 22 //
giuliomoro@181 23 // Return true on success; returning false halts the program.
giuliomoro@181 24 Midi midi;
giuliomoro@181 25 bool setup(BeagleRTContext *context, void *userData)
giuliomoro@181 26 {
giuliomoro@181 27 midi.readFrom(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@181 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@181 54 switch(waitingFor){
giuliomoro@181 55 case kNoteOn:
giuliomoro@181 56 if(message == noteOnStatus){
giuliomoro@181 57 waitingFor = kNoteNumber;
giuliomoro@181 58 }
giuliomoro@181 59 break;
giuliomoro@181 60 case kNoteNumber:
giuliomoro@181 61 if((message & (1<<8)) == 0){
giuliomoro@181 62 noteNumber = message;
giuliomoro@181 63 waitingFor = kVelocity;
giuliomoro@181 64 }
giuliomoro@181 65 break;
giuliomoro@181 66 case kVelocity:
giuliomoro@181 67 if((message & (1<<8)) == 0){
giuliomoro@184 68 int _velocity = message;
giuliomoro@181 69 waitingFor = kNoteOn;
giuliomoro@184 70 // "monophonic" behaviour, with priority to the latest note on
giuliomoro@184 71 // i.e.: a note off from a previous note does not stop the current note
giuliomoro@184 72 // still you might end up having a key down and no note being played if you pressed and released another
giuliomoro@184 73 // key in the meantime
giuliomoro@184 74 if(_velocity == 0 && noteNumber == playingNote){
giuliomoro@181 75 noteOn = false;
giuliomoro@184 76 playingNote = -1;
giuliomoro@184 77 velocity = _velocity;
giuliomoro@184 78 } else if (_velocity > 0) {
giuliomoro@184 79 noteOn = true;
giuliomoro@184 80 velocity = _velocity;
giuliomoro@184 81 playingNote = noteNumber;
giuliomoro@184 82 f0 = powf(2, (playingNote-69)/12.0f) * 440;
giuliomoro@184 83 phaseIncrement = 2 * M_PI * f0 / context->audioSampleRate;
giuliomoro@184 84 }
giuliomoro@181 85 rt_printf("NoteOn: %d, NoteNumber: %d, velocity: %d\n", noteOn, noteNumber, velocity);
giuliomoro@181 86 }
giuliomoro@181 87 break;
giuliomoro@181 88 }
giuliomoro@181 89 }
giuliomoro@181 90 for(unsigned int n = 0; n < context->audioFrames; n++){
giuliomoro@181 91 if(noteOn == 1){
giuliomoro@181 92 static float phase = 0;
giuliomoro@181 93 phase += phaseIncrement;
giuliomoro@181 94 if(phase > 2 * M_PI)
giuliomoro@181 95 phase -= 2 * M_PI;
giuliomoro@181 96 float value = sinf(phase) * velocity/128.0f;
giuliomoro@181 97 audioWriteFrame(context, n, 0, value);
giuliomoro@181 98 audioWriteFrame(context, n, 1, value);
giuliomoro@181 99 } else {
giuliomoro@181 100 audioWriteFrame(context, n, 0, 0);
giuliomoro@181 101 audioWriteFrame(context, n, 1, 0);
giuliomoro@181 102 }
giuliomoro@181 103 }
giuliomoro@181 104 }
giuliomoro@181 105
giuliomoro@181 106 // cleanup() is called once at the end, after the audio has stopped.
giuliomoro@181 107 // Release any resources that were allocated in setup().
giuliomoro@181 108
giuliomoro@181 109 void cleanup(BeagleRTContext *context, void *userData)
giuliomoro@181 110 {
giuliomoro@181 111
giuliomoro@181 112 }