annotate core/Midi.cpp @ 181:391ad036557d

Basic Midi input implementation
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 15 Jan 2016 21:50:46 +0000
parents
children 9108a0a34cb8
rev   line source
giuliomoro@181 1 /*
giuliomoro@181 2 * Midi.cpp
giuliomoro@181 3 *
giuliomoro@181 4 * Created on: 15 Jan 2016
giuliomoro@181 5 * Author: giulio
giuliomoro@181 6 */
giuliomoro@181 7
giuliomoro@181 8 #include "Midi.h"
giuliomoro@181 9 #include <fcntl.h>
giuliomoro@181 10 #include <errno.h>
giuliomoro@181 11
giuliomoro@181 12 bool Midi::staticConstructed;
giuliomoro@181 13 AuxiliaryTask Midi::midiInputTask;
giuliomoro@181 14 AuxiliaryTask Midi::midiOutputTask;
giuliomoro@181 15 std::vector<Midi *> Midi::objAddrs(0);
giuliomoro@181 16
giuliomoro@181 17 Midi::Midi(){
giuliomoro@181 18 outputPort = -1;
giuliomoro@181 19 inputPort = -1;
giuliomoro@181 20 size_t inputBytesInitialSize = 1000;
giuliomoro@181 21 inputBytes.resize(inputBytesInitialSize);
giuliomoro@181 22 inputBytesWritePointer = 0;
giuliomoro@181 23 inputBytesReadPointer = inputBytes.size() - 1;
giuliomoro@181 24 if(!staticConstructed){
giuliomoro@181 25 staticConstructor();
giuliomoro@181 26 }
giuliomoro@181 27 }
giuliomoro@181 28
giuliomoro@181 29 void Midi::staticConstructor(){
giuliomoro@181 30 staticConstructed = true;
giuliomoro@181 31 midiInputTask = BeagleRT_createAuxiliaryTask(Midi::midiInputLoop, 50, "MidiInput");
giuliomoro@181 32 midiOutputTask = BeagleRT_createAuxiliaryTask(Midi::midiInputLoop, 50, "MidiOutupt");
giuliomoro@181 33 }
giuliomoro@181 34
giuliomoro@181 35 Midi::~Midi(){}
giuliomoro@181 36
giuliomoro@181 37 void Midi::midiInputLoop(){
giuliomoro@181 38 printf("Midi input loop %d\n", objAddrs.size());
giuliomoro@181 39 for(unsigned int n = 0; n < objAddrs.size(); n++){
giuliomoro@181 40 printf("In the for\n");
giuliomoro@181 41 objAddrs[n] -> readInputLoop();
giuliomoro@181 42 }
giuliomoro@181 43 }
giuliomoro@181 44
giuliomoro@181 45 void Midi::readInputLoop(){
giuliomoro@181 46 while(!gShouldStop){
giuliomoro@181 47 int maxBytesToRead = inputBytes.size() - inputBytesWritePointer;
giuliomoro@181 48 int ret = read(inputPort, &inputBytes[inputBytesWritePointer], sizeof(midi_byte_t)*maxBytesToRead);
giuliomoro@181 49 static int count = 0;
giuliomoro@181 50 count++;
giuliomoro@181 51 if(ret < 0){
giuliomoro@181 52 if(errno != EAGAIN){ // read() would return EAGAIN when no data are available to read just now
giuliomoro@181 53 rt_printf("Error while reading midi %d\n", errno);
giuliomoro@181 54 }
giuliomoro@181 55 usleep(1000);
giuliomoro@181 56 continue;
giuliomoro@181 57 }
giuliomoro@181 58 inputBytesWritePointer += ret;
giuliomoro@181 59 if(inputBytesWritePointer == inputBytes.size()){ //wrap pointer around
giuliomoro@181 60 inputBytesWritePointer = 0;
giuliomoro@181 61 }
giuliomoro@181 62 if(ret < maxBytesToRead){ //no more data to retrieve at the moment
giuliomoro@181 63 usleep(1000);
giuliomoro@181 64 } // otherwise there might be more data ready to be read, so don't sleep
giuliomoro@181 65 }
giuliomoro@181 66 }
giuliomoro@181 67
giuliomoro@181 68 int Midi::readFrom(int port){
giuliomoro@181 69 objAddrs.push_back(this);
giuliomoro@181 70 inputPort = open("/dev/midi1", O_RDONLY | O_NONBLOCK | O_NOCTTY);
giuliomoro@181 71 if(inputPort < 0){
giuliomoro@181 72 printf("Error occurred while opening midi port %d: %d", port, inputPort);
giuliomoro@181 73 return -1;
giuliomoro@181 74 } else {
giuliomoro@181 75 printf("Reading from port %d\n", inputPort);
giuliomoro@181 76 BeagleRT_scheduleAuxiliaryTask(midiInputTask);
giuliomoro@181 77 return 1;
giuliomoro@181 78 }
giuliomoro@181 79 }
giuliomoro@181 80
giuliomoro@181 81 int Midi::getInput(){
giuliomoro@181 82 if(inputPort < 0)
giuliomoro@181 83 return -2;
giuliomoro@181 84 if(inputBytesReadPointer == inputBytesWritePointer){
giuliomoro@181 85 return -1; // no bytes to read
giuliomoro@181 86 }
giuliomoro@181 87 midi_byte_t inputMessage = inputBytes[inputBytesReadPointer++];
giuliomoro@181 88 if(inputBytesReadPointer == inputBytes.size()){ // wrap pointer
giuliomoro@181 89 inputBytesReadPointer = 0;
giuliomoro@181 90 }
giuliomoro@181 91 return inputMessage;
giuliomoro@181 92 }