robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: robert@464: #include robert@464: #include robert@464: #include robert@464: #include robert@464: #include robert@464: robert@464: float gFreq; robert@464: float gPhaseIncrement = 0; robert@464: bool gIsNoteOn = 0; robert@464: int gVelocity = 0; robert@464: float gSamplingPeriod = 0; robert@464: robert@464: void midiMessageCallback(MidiChannelMessage message, void* arg){ robert@464: if(arg != NULL){ robert@464: rt_printf("Message from midi port %d: ", *(int*)arg); robert@464: } robert@464: message.prettyPrint(); robert@464: if(message.getType() == kmmNoteOn){ robert@464: gFreq = powf(2, (message.getDataByte(0)-69)/12.0f) * 440; robert@464: gVelocity = message.getDataByte(1); robert@464: gPhaseIncrement = 2 * M_PI * gFreq * gSamplingPeriod; robert@464: gIsNoteOn = gVelocity > 0; robert@464: rt_printf("v0:%f, ph: %6.5f, gVelocity: %d\n", gFreq, gPhaseIncrement, gVelocity); robert@464: } robert@464: } robert@464: // setup() is called once before the audio rendering starts. robert@464: // Use it to perform any initialisation and allocation which is dependent robert@464: // on the period size or sample rate. robert@464: // robert@464: // userData holds an opaque pointer to a data structure that was passed robert@464: // in from the call to initAudio(). robert@464: // robert@464: // Return true on success; returning false halts the program. robert@464: Midi midi; robert@464: int gMidiPort0 = 0; robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@464: midi.readFrom(gMidiPort0); robert@464: midi.writeTo(gMidiPort0); robert@464: midi.enableParser(true); robert@464: midi.setParserCallback(midiMessageCallback, &gMidiPort0); robert@464: if(context->analogFrames == 0) { robert@464: rt_printf("Error: this example needs the analog I/O to be enabled\n"); robert@464: return false; robert@464: } chris@543: chris@543: if(context->audioOutChannels <= 2 || chris@543: context->analogOutChannels <= 2){ chris@543: printf("Error: for this project, you need at least 2 analog and audio output channels.\n"); chris@543: return false; chris@543: } chris@543: robert@464: gSamplingPeriod = 1/context->audioSampleRate; robert@464: return true; robert@464: } robert@464: robert@464: // render() is called regularly at the highest priority by the audio engine. robert@464: // Input and output are given from the audio hardware and the other robert@464: // ADCs and DACs (if available). If only audio is available, numMatrixFrames robert@464: // will be 0. robert@464: robert@464: robert@464: enum {kVelocity, kNoteOn, kNoteNumber}; robert@464: void render(BelaContext *context, void *userData) robert@464: { robert@464: // one way of getting the midi data is to parse them yourself robert@464: // (you should set midi.enableParser(false) above): robert@464: /* robert@464: static midi_byte_t noteOnStatus = 0x90; //on channel 1 robert@464: static int noteNumber = 0; robert@464: static int waitingFor = kNoteOn; robert@464: static int playingNote = -1; robert@464: int message; robert@464: while ((message = midi.getInput()) >= 0){ robert@464: rt_printf("%d\n", message); robert@464: switch(waitingFor){ robert@464: case kNoteOn: robert@464: if(message == noteOnStatus){ robert@464: waitingFor = kNoteNumber; robert@464: } robert@464: break; robert@464: case kNoteNumber: robert@464: if((message & (1<<8)) == 0){ robert@464: noteNumber = message; robert@464: waitingFor = kVelocity; robert@464: } robert@464: break; robert@464: case kVelocity: robert@464: if((message & (1<<8)) == 0){ robert@464: int _velocity = message; robert@464: waitingFor = kNoteOn; robert@464: // "monophonic" behaviour, with priority to the latest note on robert@464: // i.e.: a note off from a previous note does not stop the current note robert@464: // still you might end up having a key down and no note being played if you pressed and released another robert@464: // key in the meantime robert@464: if(_velocity == 0 && noteNumber == playingNote){ robert@464: noteOn = false; robert@464: playingNote = -1; robert@464: velocity = _velocity; robert@464: } else if (_velocity > 0) { robert@464: noteOn = true; robert@464: velocity = _velocity; robert@464: playingNote = noteNumber; robert@464: f0 = powf(2, (playingNote-69)/12.0f) * 440; robert@464: phaseIncrement = 2 * M_PI * f0 / context->audioSampleRate; robert@464: } robert@464: rt_printf("NoteOn: %d, NoteNumber: %d, velocity: %d\n", noteOn, noteNumber, velocity); robert@464: } robert@464: break; robert@464: } robert@464: } robert@464: */ robert@464: /* robert@464: int num; robert@464: //alternatively, you can use the built-in parser (only processes channel messages at the moment). robert@464: while((num = midi.getParser()->numAvailableMessages()) > 0){ robert@464: static MidiChannelMessage message; robert@464: message = midi.getParser()->getNextChannelMessage(); robert@464: message.prettyPrint(); robert@464: if(message.getType() == kmmNoteOn){ robert@464: f0 = powf(2, (message.getDataByte(0)-69)/12.0f) * 440; robert@464: velocity = message.getDataByte(1); robert@464: phaseIncrement = 2 * M_PI * f0 / context->audioSampleRate; robert@464: noteOn = velocity > 0; robert@464: rt_printf("v0:%f, ph: %6.5f, velocity: %d\n", f0, phaseIncrement, gVelocity); robert@464: } robert@464: } robert@464: */ robert@464: // the following block toggles the LED on an Owl pedal robert@464: // and asks the pedal to return the status of the LED robert@464: // using MIDI control changes robert@464: for(unsigned int n = 0; n < context->analogFrames; n++){ robert@464: static int count = 0; robert@464: static bool state = 0; robert@464: analogWriteOnce(context, n, 1, state); robert@464: if(count % 40000 == 0){ robert@464: state = !state; robert@464: midi_byte_t bytes[6] = {176, 30, (char)(state*127), 176, 67, 30}; // toggle the OWL led and ask for the led status robert@464: midi.writeOutput(bytes, 6); robert@464: } robert@464: count++; robert@464: } robert@464: for(unsigned int n = 0; n < context->audioFrames; n++){ robert@464: if(gIsNoteOn == 1){ robert@464: static float phase = 0; robert@464: phase += gPhaseIncrement; robert@464: if(phase > 2 * M_PI) robert@464: phase -= 2 * M_PI; robert@464: float value = sinf(phase) * gVelocity/128.0f; robert@464: audioWrite(context, n, 0, value); robert@464: audioWrite(context, n, 1, value); robert@464: } else { robert@464: audioWrite(context, n, 0, 0); robert@464: audioWrite(context, n, 1, 0); robert@464: } robert@464: } robert@464: } robert@464: robert@464: // cleanup() is called once at the end, after the audio has stopped. robert@464: // Release any resources that were allocated in setup(). robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: robert@464: }