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