annotate projects/basic_midi/render.cpp @ 187:d7148d21aaa5

Utilities are now optimizable. Closes #1526
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 19 Jan 2016 02:54:13 +0000
parents 9108a0a34cb8
children b3a306da03e0
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@181 27 if(context->analogFrames == 0) {
giuliomoro@181 28 rt_printf("Error: this example needs the matrix enabled\n");
giuliomoro@181 29 return false;
giuliomoro@181 30 }
giuliomoro@181 31 return true;
giuliomoro@181 32 }
giuliomoro@181 33
giuliomoro@181 34 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@181 35 // Input and output are given from the audio hardware and the other
giuliomoro@181 36 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
giuliomoro@181 37 // will be 0.
giuliomoro@181 38
giuliomoro@181 39 static midi_byte_t noteOnStatus =0x90; //on channel 1
giuliomoro@181 40
giuliomoro@181 41 enum {kVelocity, kNoteOn, kNoteNumber};
giuliomoro@181 42 void render(BeagleRTContext *context, void *userData)
giuliomoro@181 43 {
giuliomoro@181 44 static float f0;
giuliomoro@181 45 static float phaseIncrement = 0;
giuliomoro@181 46 int message;
giuliomoro@181 47 static bool noteOn = 0;
giuliomoro@181 48 static int velocity = 0;
giuliomoro@181 49 static int noteNumber = 0;
giuliomoro@181 50 static int waitingFor = kNoteOn;
giuliomoro@184 51 static int playingNote = -1;
giuliomoro@181 52 while ((message = midi.getInput()) >= 0){
giuliomoro@181 53 switch(waitingFor){
giuliomoro@181 54 case kNoteOn:
giuliomoro@181 55 if(message == noteOnStatus){
giuliomoro@181 56 waitingFor = kNoteNumber;
giuliomoro@181 57 }
giuliomoro@181 58 break;
giuliomoro@181 59 case kNoteNumber:
giuliomoro@181 60 if((message & (1<<8)) == 0){
giuliomoro@181 61 noteNumber = message;
giuliomoro@181 62 waitingFor = kVelocity;
giuliomoro@181 63 }
giuliomoro@181 64 break;
giuliomoro@181 65 case kVelocity:
giuliomoro@181 66 if((message & (1<<8)) == 0){
giuliomoro@184 67 int _velocity = message;
giuliomoro@181 68 waitingFor = kNoteOn;
giuliomoro@184 69 // "monophonic" behaviour, with priority to the latest note on
giuliomoro@184 70 // i.e.: a note off from a previous note does not stop the current note
giuliomoro@184 71 // still you might end up having a key down and no note being played if you pressed and released another
giuliomoro@184 72 // key in the meantime
giuliomoro@184 73 if(_velocity == 0 && noteNumber == playingNote){
giuliomoro@181 74 noteOn = false;
giuliomoro@184 75 playingNote = -1;
giuliomoro@184 76 velocity = _velocity;
giuliomoro@184 77 } else if (_velocity > 0) {
giuliomoro@184 78 noteOn = true;
giuliomoro@184 79 velocity = _velocity;
giuliomoro@184 80 playingNote = noteNumber;
giuliomoro@184 81 f0 = powf(2, (playingNote-69)/12.0f) * 440;
giuliomoro@184 82 phaseIncrement = 2 * M_PI * f0 / context->audioSampleRate;
giuliomoro@184 83 }
giuliomoro@181 84 rt_printf("NoteOn: %d, NoteNumber: %d, velocity: %d\n", noteOn, noteNumber, velocity);
giuliomoro@181 85 }
giuliomoro@181 86 break;
giuliomoro@181 87 }
giuliomoro@181 88 }
giuliomoro@181 89 for(unsigned int n = 0; n < context->audioFrames; n++){
giuliomoro@181 90 if(noteOn == 1){
giuliomoro@181 91 static float phase = 0;
giuliomoro@181 92 phase += phaseIncrement;
giuliomoro@181 93 if(phase > 2 * M_PI)
giuliomoro@181 94 phase -= 2 * M_PI;
giuliomoro@181 95 float value = sinf(phase) * velocity/128.0f;
giuliomoro@181 96 audioWriteFrame(context, n, 0, value);
giuliomoro@181 97 audioWriteFrame(context, n, 1, value);
giuliomoro@181 98 } else {
giuliomoro@181 99 audioWriteFrame(context, n, 0, 0);
giuliomoro@181 100 audioWriteFrame(context, n, 1, 0);
giuliomoro@181 101 }
giuliomoro@181 102 }
giuliomoro@181 103 }
giuliomoro@181 104
giuliomoro@181 105 // cleanup() is called once at the end, after the audio has stopped.
giuliomoro@181 106 // Release any resources that were allocated in setup().
giuliomoro@181 107
giuliomoro@181 108 void cleanup(BeagleRTContext *context, void *userData)
giuliomoro@181 109 {
giuliomoro@181 110
giuliomoro@181 111 }