Mercurial > hg > beaglert
comparison core/Midi.cpp @ 191:b3a306da03e0
Implemented Midi output
| author | Giulio Moro <giuliomoro@yahoo.it> |
|---|---|
| date | Wed, 03 Feb 2016 01:18:30 +0000 |
| parents | 9108a0a34cb8 |
| children | 265a527f8be8 |
comparison
equal
deleted
inserted
replaced
| 189:7144c5594d16 | 191:b3a306da03e0 |
|---|---|
| 7 | 7 |
| 8 #include "Midi.h" | 8 #include "Midi.h" |
| 9 #include <fcntl.h> | 9 #include <fcntl.h> |
| 10 #include <errno.h> | 10 #include <errno.h> |
| 11 | 11 |
| 12 #define kMidiInput 0 | |
| 13 #define kMidiOutput 1 | |
| 14 | |
| 12 bool Midi::staticConstructed; | 15 bool Midi::staticConstructed; |
| 13 AuxiliaryTask Midi::midiInputTask; | 16 AuxiliaryTask Midi::midiInputTask; |
| 14 AuxiliaryTask Midi::midiOutputTask; | 17 AuxiliaryTask Midi::midiOutputTask; |
| 15 std::vector<Midi *> Midi::objAddrs(0); | 18 std::vector<Midi *> Midi::objAddrs[2]; |
| 16 | 19 |
| 17 Midi::Midi(){ | 20 Midi::Midi(){ |
| 18 outputPort = -1; | 21 outputPort = -1; |
| 19 inputPort = -1; | 22 inputPort = -1; |
| 20 size_t inputBytesInitialSize = 1000; | 23 size_t inputBytesInitialSize = 1000; |
| 21 inputBytes.resize(inputBytesInitialSize); | 24 inputBytes.resize(inputBytesInitialSize); |
| 25 outputBytes.resize(inputBytesInitialSize); | |
| 22 inputBytesWritePointer = 0; | 26 inputBytesWritePointer = 0; |
| 23 inputBytesReadPointer = inputBytes.size() - 1; | 27 inputBytesReadPointer = inputBytes.size() - 1; |
| 24 if(!staticConstructed){ | 28 if(!staticConstructed){ |
| 25 staticConstructor(); | 29 staticConstructor(); |
| 26 } | 30 } |
| 27 } | 31 } |
| 28 | 32 |
| 29 void Midi::staticConstructor(){ | 33 void Midi::staticConstructor(){ |
| 30 staticConstructed = true; | 34 staticConstructed = true; |
| 31 midiInputTask = BeagleRT_createAuxiliaryTask(Midi::midiInputLoop, 50, "MidiInput"); | 35 midiInputTask = BeagleRT_createAuxiliaryTask(Midi::midiInputLoop, 50, "MidiInput"); |
| 32 midiOutputTask = BeagleRT_createAuxiliaryTask(Midi::midiInputLoop, 50, "MidiOutupt"); | 36 midiOutputTask = BeagleRT_createAuxiliaryTask(Midi::midiOutputLoop, 50, "MidiOutupt"); |
| 33 } | 37 } |
| 34 | 38 |
| 35 Midi::~Midi(){} | 39 Midi::~Midi(){} |
| 40 void Midi::midiInputLoop(){ | |
| 41 printf("Midi input loop %d\n", objAddrs[kMidiInput].size()); | |
| 42 for(unsigned int n = 0; n < objAddrs[kMidiInput].size(); n++){ | |
| 43 objAddrs[kMidiInput][n] -> readInputLoop(); | |
| 44 } | |
| 45 } | |
| 36 | 46 |
| 37 void Midi::midiInputLoop(){ | 47 void Midi::midiOutputLoop(){ |
| 38 printf("Midi input loop %d\n", objAddrs.size()); | 48 printf("Midi output loop %d\n", objAddrs[kMidiOutput].size()); |
| 39 for(unsigned int n = 0; n < objAddrs.size(); n++){ | 49 for(unsigned int n = 0; n < objAddrs[kMidiOutput].size(); n++){ |
| 40 objAddrs[n] -> readInputLoop(); | 50 objAddrs[kMidiOutput][n] -> writeOutputLoop(); |
| 41 } | 51 } |
| 42 } | 52 } |
| 43 | 53 |
| 44 void Midi::readInputLoop(){ | 54 void Midi::readInputLoop(){ |
| 45 while(!gShouldStop){ | 55 while(!gShouldStop){ |
| 46 int maxBytesToRead = inputBytes.size() - inputBytesWritePointer; | 56 int maxBytesToRead = inputBytes.size() - inputBytesWritePointer; |
| 47 int ret = read(inputPort, &inputBytes[inputBytesWritePointer], sizeof(midi_byte_t)*maxBytesToRead); | 57 int ret = read(inputPort, &inputBytes[inputBytesWritePointer], sizeof(midi_byte_t)*maxBytesToRead); |
| 48 static int count = 0; | |
| 49 count++; | |
| 50 if(ret < 0){ | 58 if(ret < 0){ |
| 51 if(errno != EAGAIN){ // read() would return EAGAIN when no data are available to read just now | 59 if(errno != EAGAIN){ // read() would return EAGAIN when no data are available to read just now |
| 52 rt_printf("Error while reading midi %d\n", errno); | 60 rt_printf("Error while reading midi %d\n", errno); |
| 53 } | 61 } |
| 54 usleep(1000); | 62 usleep(1000); |
| 62 usleep(1000); | 70 usleep(1000); |
| 63 } // otherwise there might be more data ready to be read, so don't sleep | 71 } // otherwise there might be more data ready to be read, so don't sleep |
| 64 } | 72 } |
| 65 } | 73 } |
| 66 | 74 |
| 75 void Midi::writeOutputLoop(){ | |
| 76 while(!gShouldStop){ | |
| 77 usleep(1000); | |
| 78 int length = outputBytesWritePointer - outputBytesReadPointer; | |
| 79 if(length < 0){ | |
| 80 length = outputBytes.size() - outputBytesReadPointer; | |
| 81 } | |
| 82 if(length == 0){ //nothing to be written | |
| 83 continue; | |
| 84 } | |
| 85 int ret; | |
| 86 ret = write(outputPort, &outputBytes[outputBytesReadPointer], sizeof(midi_byte_t)*length); | |
| 87 if(ret < 0){ //error occurred | |
| 88 // if(errno != EAGAIN){ // () would return EAGAIN when no data are available to read just now | |
| 89 // rt_printf("Error while writing midi %d\n", errno); | |
| 90 // } | |
| 91 rt_printf("error occurred while writing: %d\n", errno); | |
| 92 usleep(10000); //wait before retrying | |
| 93 continue; | |
| 94 } | |
| 95 | |
| 96 // inputBytesWritePointer += ret; | |
| 97 // if(inputBytesWritePointer == inputBytes.size()){ //wrap pointer around | |
| 98 // inputBytesWritePointer = 0; | |
| 99 // } | |
| 100 // if(ret < maxBytesToRead){ //no more data to retrieve at the moment | |
| 101 // usleep(1000); | |
| 102 // } // otherwise there might be more data ready to be read, so don't sleep | |
| 103 } | |
| 104 } | |
| 67 int Midi::readFrom(int port){ | 105 int Midi::readFrom(int port){ |
| 68 objAddrs.push_back(this); | 106 objAddrs[kMidiInput].push_back(this); |
| 69 inputPort = open("/dev/midi1", O_RDONLY | O_NONBLOCK | O_NOCTTY); | 107 inputPort = open("/dev/midi1", O_RDONLY | O_NONBLOCK | O_NOCTTY); |
| 70 if(inputPort < 0){ | 108 if(inputPort < 0){ |
| 71 printf("Error occurred while opening midi port %d: %d", port, inputPort); | 109 printf("Error occurred while opening midi input port %d: %d", port, inputPort); |
| 72 return -1; | 110 return -1; |
| 73 } else { | 111 } else { |
| 74 printf("Reading from port %d\n", inputPort); | 112 printf("Reading from port %d\n", port); |
| 75 BeagleRT_scheduleAuxiliaryTask(midiInputTask); | 113 BeagleRT_scheduleAuxiliaryTask(midiInputTask); |
| 76 return 1; | 114 return 1; |
| 77 } | 115 } |
| 78 } | 116 } |
| 79 | 117 |
| 80 int Midi::getInput(){ | 118 int Midi::writeTo(int port){ |
| 119 objAddrs[kMidiOutput].push_back(this); | |
| 120 outputPort = open("/dev/midi1", O_WRONLY, 0); | |
| 121 if(outputPort < 0){ | |
| 122 printf("Error occurred while opening midi output port %d: %d", port, outputPort); | |
| 123 return -1; | |
| 124 } else { | |
| 125 printf("Writing to Midi port %d\n", port); | |
| 126 BeagleRT_scheduleAuxiliaryTask(midiOutputTask); | |
| 127 return 1; | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 int Midi::getInput(){ | |
| 81 if(inputPort < 0) | 132 if(inputPort < 0) |
| 82 return -2; | 133 return -2; |
| 83 if(inputBytesReadPointer == inputBytesWritePointer){ | 134 if(inputBytesReadPointer == inputBytesWritePointer){ |
| 84 return -1; // no bytes to read | 135 return -1; // no bytes to read |
| 85 } | 136 } |
| 87 if(inputBytesReadPointer == inputBytes.size()){ // wrap pointer | 138 if(inputBytesReadPointer == inputBytes.size()){ // wrap pointer |
| 88 inputBytesReadPointer = 0; | 139 inputBytesReadPointer = 0; |
| 89 } | 140 } |
| 90 return inputMessage; | 141 return inputMessage; |
| 91 } | 142 } |
| 143 | |
| 144 int Midi::writeOutput(midi_byte_t byte){ | |
| 145 return writeOutput(&byte, 1); | |
| 146 } | |
| 147 | |
| 148 int Midi::writeOutput(midi_byte_t* bytes, unsigned int length){ | |
| 149 int ret = write(outputPort, bytes, length); | |
| 150 if(ret < 0) | |
| 151 return -1; | |
| 152 else | |
| 153 return 1; | |
| 154 } |
