MidiMain.c++
4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/******************************************************************************
*
* midiMain.c++
* Main function for the midi thread
*
*****************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <strings.h>
#include <dmedia/midi.h>
#include "MidiMain.h"
#include "AudioApp.h"
#include "Mgrs.h"
// int gCurChanInst[MAX_SEQ_CHANNELS];
int numPorts;
MDport midiInPort[MAX_MIDI_PORTS];
MDevent midiEvents[MAX_MIDI_MSGS];
int midiFds[MAX_MIDI_PORTS];
fd_set inports;
int highfd;
int midiVerbose;
void ReceiveMidi(MDport inPort);
int initMidi(void);
void midiMain(void)
{
int nfds, c;
if(initMidi() == MIDI_OK)
{
while(1)
{
nfds = select(highfd,&inports,0,0,0);
for (c = 0; c < numPorts; c++)
{
if (FD_ISSET(midiFds[c],&inports))
ReceiveMidi(midiInPort[c]);
else
FD_SET(midiFds[c],&inports);
}
}
}
// if you get here, there is no midi, don't quit or that will cause
// a child died signal. Instead just go to sleep forever.
while(1)
sleep(0xFFFFFFFF);
}
int initMidi(void)
{
int c;
char *portname;
numPorts=mdInit(); /* start midi lib stuff, and open in port */
if (numPorts<1)
{
fprintf(stderr,"No ports configured for midi.\n");
return(NO_MIDI);
}
if(numPorts>MAX_MIDI_PORTS)
{
fprintf(stderr,"More than %d ports configured for midi.\n",MAX_MIDI_PORTS);
fprintf(stderr,"Will use only the first %d ports.\n",MAX_MIDI_PORTS);
numPorts=MAX_MIDI_PORTS;
}
for(c=0;c<numPorts;c++)
{
portname=mdGetName(c);
if(portname==NULL)
{
fprintf(stderr,"Port %d is not correctly set for midi.\n",c+1);
return(MIDI_INIT_ERR);
}
midiInPort[c]=mdOpenInPort(portname);
if(midiInPort[c]==NULL)
{
fprintf(stderr,"Failure opening midi in port for %s\n",portname);
return(MIDI_INIT_ERR);
}
mdSetStampMode(midiInPort[c],MD_NOSTAMP);
midiFds[c]=mdGetFd(midiInPort[c]);
FD_SET(midiFds[c],&inports);
highfd = midiFds[c] + 1;
fprintf(stderr,"%s successfully opened for MIDI receive\n",portname);
}
return(MIDI_OK);
}
/********************************************************************
*
* Receive Midi. Primary midi receiving routine. Upon receiving
* midi, seperate out system exclusive, from regular status mesgs.
* Send regular messages in one block, for each system exclusive,
* Handle one at a time, by calling ProcessSysEx
*
*********************************************************************/
void ReceiveMidi(MDport inPort)
{
int validMsgs = 0;
int numMsgs,ct,msgType;
char chan;
unsigned char status;
CommPckt commPckt;
#ifdef TIMING_DEBUG
unsigned long long deltaTime,gioTime;
float deltaFlt;
#endif
numMsgs=mdReceive(inPort,midiEvents,MAX_MIDI_MSGS);
if(numMsgs < 0)
{
fprintf(stderr,"ERROR receiving midi data!\n");
return;
}
else if(numMsgs > 0)
{
for(ct=0;ct<numMsgs;ct++)
{
#ifdef TIMING_DEBUG
syssgi(SGI_GET_UST,&deltaTime,0);
deltaTime -= gMidiEvents[ct].stamp;
deltaFlt = deltaTime;
deltaFlt /= 1000; /* nanoseconds to microseconds */
#endif
status = midiEvents[ct].msg[0] & 0xF0;
if(status) /* status has a value */
{
switch(status)
{
case 0x80: /* note off */
case 0x90: /* note on */
case 0xA0: /* poly pressure */
case 0xB0: /* controller */
case 0xC0: /* program change */
case 0xD0: /* channel pressure */
case 0xE0: /* pitch bend */
commPckt.d.mb.mess[validMsgs].midiByte[0]=midiEvents[ct].msg[0];
commPckt.d.mb.mess[validMsgs].midiByte[1]=midiEvents[ct].msg[1];
commPckt.d.mb.mess[validMsgs].midiByte[2]=midiEvents[ct].msg[2];
validMsgs++;
break;
case 0xF0:
if(midiEvents[ct].msg[0] == 0xFF)
{
commPckt.d.mb.mess[validMsgs].midiByte[0]=midiEvents[ct].msg[0];
commPckt.d.mb.mess[validMsgs].midiByte[1]=midiEvents[ct].msg[1];
commPckt.d.mb.mess[validMsgs].midiByte[2]=midiEvents[ct].msg[2];
validMsgs++;
}
break;
}
if(midiVerbose)
printf("midi data: %x %x %x\n",midiEvents[ct].msg[0],
midiEvents[ct].msg[1],midiEvents[ct].msg[2]);
}
}
if(validMsgs)
{
commPckt.pcktType = STATUS_TYPE;
commPckt.d.mb.numEvts = validMsgs;
theU64Dev->SendMidiBlock(&commPckt);
}
}
}