| 1 |
/* |
| 2 |
|
| 3 |
CsoundMIDI.m: |
| 4 |
|
| 5 |
Copyright (C) 2011 Steven Yi, Victor Lazzarini |
| 6 |
|
| 7 |
This file is part of Csound for iOS. |
| 8 |
|
| 9 |
The Csound for iOS Library is free software; you can redistribute it |
| 10 |
and/or modify it under the terms of the GNU Lesser General Public |
| 11 |
License as published by the Free Software Foundation; either |
| 12 |
version 2.1 of the License, or (at your option) any later version. |
| 13 |
|
| 14 |
Csound is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU Lesser General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU Lesser General Public |
| 20 |
License along with Csound; if not, write to the Free Software |
| 21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 22 |
02111-1307 USA |
| 23 |
|
| 24 |
*/ |
| 25 |
|
| 26 |
#import "CsoundMIDI.h" |
| 27 |
#include <stdio.h> |
| 28 |
#include <CoreMidi/CoreMidi.h> |
| 29 |
#include <time.h> |
| 30 |
#include <stdlib.h> |
| 31 |
|
| 32 |
void ReadProc(const MIDIPacketList *pktlist, void *refcon, void *srcConnRefCon); |
| 33 |
|
| 34 |
@implementation CsoundMIDI |
| 35 |
|
| 36 |
/*=============================================*/ |
| 37 |
/* start coremidi - csound code */ |
| 38 |
|
| 39 |
/* MIDI message queue size */ |
| 40 |
#define DSIZE 1024 |
| 41 |
|
| 42 |
|
| 43 |
/* MIDI data struct */ |
| 44 |
typedef struct { |
| 45 |
Byte status; |
| 46 |
Byte data1; |
| 47 |
Byte data2; |
| 48 |
Byte flag; |
| 49 |
} MIDIdata; |
| 50 |
|
| 51 |
/* user data for MIDI callbacks */ |
| 52 |
typedef struct _cdata { |
| 53 |
MIDIdata *mdata; |
| 54 |
int p, q, pnot, pchn; |
| 55 |
MIDIClientRef mclient; |
| 56 |
} cdata; |
| 57 |
|
| 58 |
/* coremidi callback, called when MIDI data is available */ |
| 59 |
void ReadProc(const MIDIPacketList *pktlist, void *refcon, void *srcConnRefCon){ |
| 60 |
cdata *data = (cdata *)refcon; |
| 61 |
MIDIdata *mdata = data->mdata; |
| 62 |
int *p = &data->p, i, j; |
| 63 |
MIDIPacket *packet = &((MIDIPacketList *)pktlist)->packet[0]; |
| 64 |
Byte *curpack; |
| 65 |
|
| 66 |
for (i = 0; i < pktlist->numPackets; i++) { |
| 67 |
//printf("len %d \n", packet->length); |
| 68 |
for(j=0; j < packet->length; j+=3){ |
| 69 |
while(mdata[*p].flag); /* in case data was not read, spin */ |
| 70 |
curpack = packet->data+j; |
| 71 |
memcpy(&mdata[*p], curpack, 3); |
| 72 |
mdata[*p].flag = 1; |
| 73 |
(*p)++; |
| 74 |
if(*p == DSIZE) *p = 0; |
| 75 |
} |
| 76 |
packet = MIDIPacketNext(packet); |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/* csound MIDI input open callback, sets the device for input */ |
| 82 |
static int MidiInDeviceOpen(CSOUND *csound, void **userData, const char *dev) |
| 83 |
{ |
| 84 |
int k; |
| 85 |
ItemCount endpoints; |
| 86 |
|
| 87 |
CFStringRef name = NULL, cname = NULL, pname = NULL; |
| 88 |
CFStringEncoding defaultEncoding = CFStringGetSystemEncoding(); |
| 89 |
MIDIClientRef mclient = 0; |
| 90 |
MIDIPortRef mport = 0; |
| 91 |
MIDIEndpointRef endpoint; |
| 92 |
MIDIdata *mdata = (MIDIdata *) malloc(DSIZE*sizeof(MIDIdata)); |
| 93 |
OSStatus ret; |
| 94 |
cdata *refcon = (cdata *) malloc(sizeof(cdata)); |
| 95 |
memset(mdata, 0, sizeof(MIDIdata)*DSIZE); |
| 96 |
refcon->mdata = mdata; |
| 97 |
refcon->p = 0; |
| 98 |
refcon->q = 0; |
| 99 |
refcon->pnot = refcon->pchn = 0; |
| 100 |
|
| 101 |
/* MIDI client */ |
| 102 |
cname = CFStringCreateWithCString(NULL, "my client", defaultEncoding); |
| 103 |
ret = MIDIClientCreate(cname, NULL, NULL, &mclient); |
| 104 |
if(!ret){ |
| 105 |
/* MIDI output port */ |
| 106 |
pname = CFStringCreateWithCString(NULL, "outport", defaultEncoding); |
| 107 |
ret = MIDIInputPortCreate(mclient, pname, ReadProc, refcon, &mport); |
| 108 |
if(!ret){ |
| 109 |
/* sources, we connect to all available input sources */ |
| 110 |
endpoints = MIDIGetNumberOfSources(); |
| 111 |
csoundMessage(csound, "midi srcs %lu\n", endpoints); |
| 112 |
for(k=0; k < endpoints; k++){ |
| 113 |
endpoint = MIDIGetSource(k); |
| 114 |
MIDIPortConnectSource(mport, endpoint, NULL); |
| 115 |
|
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
refcon->mclient = mclient; |
| 120 |
*userData = (void*) refcon; |
| 121 |
if(name) CFRelease(name); |
| 122 |
if(pname) CFRelease(pname); |
| 123 |
if(cname) CFRelease(cname); |
| 124 |
/* report success */ |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
/* used to distinguish between 1 and 2-byte messages */ |
| 129 |
static const int datbyts[8] = { 2, 2, 2, 2, 1, 1, 2, 0 }; |
| 130 |
|
| 131 |
/* csound MIDI read callback, called every k-cycle */ |
| 132 |
static int MidiDataRead(CSOUND *csound, void *userData, |
| 133 |
unsigned char *mbuf, int nbytes) |
| 134 |
{ |
| 135 |
cdata * data = (cdata *)userData; |
| 136 |
if(data == NULL) return 0; |
| 137 |
|
| 138 |
MIDIdata *mdata = data->mdata; |
| 139 |
int *q = &data->q, st, d1, d2, n = 0; |
| 140 |
|
| 141 |
/* check if there is new data on circular queue */ |
| 142 |
while (mdata[*q].flag) { |
| 143 |
st = (int) mdata[*q].status; |
| 144 |
d1 = (int) mdata[*q].data1; |
| 145 |
d2 = (int) mdata[*q].data2; |
| 146 |
|
| 147 |
if (st < 0x80) |
| 148 |
goto next; |
| 149 |
|
| 150 |
if (st >= 0xF0 && |
| 151 |
!(st == 0xF8 || st == 0xFA || st == 0xFB || |
| 152 |
st == 0xFC || st == 0xFF)) |
| 153 |
goto next; |
| 154 |
nbytes -= (datbyts[(st - 0x80) >> 4] + 1); |
| 155 |
if (nbytes < 0) break; |
| 156 |
|
| 157 |
/* write to csound midi buffer */ |
| 158 |
n += (datbyts[(st - 0x80) >> 4] + 1); |
| 159 |
switch (datbyts[(st - 0x80) >> 4]) { |
| 160 |
case 0: |
| 161 |
*mbuf++ = (unsigned char) st; |
| 162 |
break; |
| 163 |
case 1: |
| 164 |
*mbuf++ = (unsigned char) st; |
| 165 |
*mbuf++ = (unsigned char) d1; |
| 166 |
break; |
| 167 |
case 2: |
| 168 |
*mbuf++ = (unsigned char) st; |
| 169 |
*mbuf++ = (unsigned char) d1; |
| 170 |
*mbuf++ = (unsigned char) d2; |
| 171 |
break; |
| 172 |
} |
| 173 |
next: |
| 174 |
mdata[*q].flag = 0; |
| 175 |
(*q)++; |
| 176 |
if(*q==DSIZE) *q = 0; |
| 177 |
} |
| 178 |
|
| 179 |
/* return the number of bytes read */ |
| 180 |
return n; |
| 181 |
} |
| 182 |
|
| 183 |
/* csound close device callback */ |
| 184 |
static int MidiInDeviceClose(CSOUND *csound, void *userData) |
| 185 |
{ |
| 186 |
cdata * data = (cdata *)userData; |
| 187 |
if (data != NULL) { |
| 188 |
MIDIClientDispose(data->mclient); |
| 189 |
free(data->mdata); |
| 190 |
free(data); |
| 191 |
} |
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
/* callback setting code */ |
| 196 |
+(void)setMidiInCallbacks:(CSOUND *)csound { |
| 197 |
csoundSetHostImplementedMIDIIO(csound, 1); |
| 198 |
csoundSetExternalMidiInOpenCallback(csound, MidiInDeviceOpen); |
| 199 |
csoundSetExternalMidiReadCallback(csound, MidiDataRead); |
| 200 |
csoundSetExternalMidiInCloseCallback(csound, MidiInDeviceClose); |
| 201 |
} |
| 202 |
|
| 203 |
@end |