summaryrefslogtreecommitdiff
path: root/src/sys/macosx/midi-macosx.c
blob: 3342e2c2a7e666a845176ad038b74acdd546fd2a (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Schism Tracker - a cross-platform Impulse Tracker clone
 * copyright (c) 2003-2005 Storlek <storlek@rigelseven.com>
 * copyright (c) 2005-2008 Mrs. Brisby <mrs.brisby@nimh.org>
 * copyright (c) 2009 Storlek & Mrs. Brisby
 * copyright (c) 2010-2012 Storlek
 * URL: http://schismtracker.org/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "headers.h"

#include "midi.h"

#include "util.h"

#ifdef MACOSX

#include <CoreServices/CoreServices.h>
#include <CoreMIDI/MIDIServices.h>
#include <CoreAudio/HostTime.h>

static MIDIClientRef    client = NULL;
static MIDIPortRef      portIn = NULL;
static MIDIPortRef      portOut = NULL;

static int max_outputs = 0;
static int max_inputs = 0;

struct macosx_midi {
	char *name;
	MIDIEndpointRef ep;
	unsigned char packet[1024];
	MIDIPacketList *pl;
	MIDIPacket *x;
};

static void readProc(const MIDIPacketList *np, UNUSED void *rc, void *crc)
{
	struct midi_port *p;
	struct macosx_midi *m;
	MIDIPacket *x;
	unsigned long i;

	p = (struct midi_port *)crc;
	m = (struct macosx_midi *)p->userdata;

	x = (MIDIPacket*)&np->packet[0];
	for (i = 0; i < np->numPackets; i++) {
		midi_received_cb(p, x->data, x->length);
		x = MIDIPacketNext(x);
	}
}
static void _macosx_send(struct midi_port *p, const unsigned char *data,
				unsigned int len, unsigned int delay)
{
	struct macosx_midi *m;

	m = (struct macosx_midi *)p->userdata;
	if (!m->x) {
		m->x = MIDIPacketListInit(m->pl);
	}

	/* msec to nsec? */
	m->x = MIDIPacketListAdd(m->pl, sizeof(m->packet),
			m->x, (MIDITimeStamp)AudioConvertNanosToHostTime(
			AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()) + (1000000*delay)),
			len, data);
}
static void _macosx_drain(struct midi_port *p)
{
	struct macosx_midi *m;

	m = (struct macosx_midi *)p->userdata;
	if (m->x) {
		MIDISend(portOut, m->ep, m->pl);
		m->x = NULL;
	}
}

/* lifted from portmidi */
static char *get_ep_name(MIDIEndpointRef ep)
{
	MIDIEntityRef entity;
	MIDIDeviceRef device;
	CFStringRef endpointName = NULL, deviceName = NULL, fullName = NULL;
	CFStringEncoding defaultEncoding;
	char* newName;

	/* get the default string encoding */
	defaultEncoding = CFStringGetSystemEncoding();

	/* get the entity and device info */
	MIDIEndpointGetEntity(ep, &entity);
	MIDIEntityGetDevice(entity, &device);

	/* create the nicely formated name */
	MIDIObjectGetStringProperty(ep, kMIDIPropertyName, &endpointName);
	MIDIObjectGetStringProperty(device, kMIDIPropertyName, &deviceName);
	if (deviceName != NULL) {
		fullName = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@: %@"),
						deviceName, endpointName);
	} else {
		fullName = endpointName;
	}

	/* copy the string into our buffer */
	newName = (char*)mem_alloc(CFStringGetLength(fullName) + 1);
	CFStringGetCString(fullName, newName, CFStringGetLength(fullName) + 1,
			defaultEncoding);

	/* clean up */
	if (endpointName) CFRelease(endpointName);
	if (deviceName) CFRelease(deviceName);
	if (fullName) CFRelease(fullName);

	return newName;
}

static int _macosx_start(struct midi_port *p)
{
	struct macosx_midi *m;
	m = (struct macosx_midi *)p->userdata;

	if (p->io & MIDI_INPUT
	&& MIDIPortConnectSource(portIn, m->ep, (void*)p) != noErr) {
		return 0;
	}

	if (p->io & MIDI_OUTPUT) {
		m->pl = (MIDIPacketList*)m->packet;
		m->x = NULL;
	}
	return 1;
}
static int _macosx_stop(struct midi_port *p)
{
	struct macosx_midi *m;
	m = (struct macosx_midi *)p->userdata;
	if (p->io & MIDI_INPUT
	&& MIDIPortDisconnectSource(portIn, m->ep) != noErr) {
		return 0;
	}
	return 1;
}

static void _macosx_poll(struct midi_provider *p)
{
	struct macosx_midi *data;
	MIDIEndpointRef ep;
	int i;

	int num_out, num_in;

	num_out = MIDIGetNumberOfDestinations();
	num_in = MIDIGetNumberOfSources();

	for (i = max_outputs; i < num_out; i++) {
		ep = MIDIGetDestination(i);
		if (!ep) continue;
		data = mem_alloc(sizeof(struct macosx_midi));
		memcpy(&data->ep, &ep, sizeof(ep));
		data->name = get_ep_name(ep);
		midi_port_register(p, MIDI_OUTPUT, data->name, data, 1);
	}
	max_outputs = i;


	for (i = max_inputs; i < num_in; i++) {
		ep = MIDIGetSource(i);
		if (!ep) continue;
		data = mem_alloc(sizeof(struct macosx_midi));
		memcpy(&data->ep, &ep, sizeof(ep));
		data->name = get_ep_name(ep);
		midi_port_register(p, MIDI_INPUT, data->name, data, 1);
	}
	max_inputs = i;

}

int macosx_midi_setup(void)
{
	static struct midi_driver driver;

	memset(&driver,0,sizeof(driver));
	driver.flags = MIDI_PORT_CAN_SCHEDULE;
	driver.poll = _macosx_poll;
	driver.thread = NULL;
	driver.enable = _macosx_start;
	driver.disable = _macosx_stop;
	driver.send = _macosx_send;
	driver.drain = _macosx_drain;

	if (MIDIClientCreate(CFSTR("Schism Tracker"), NULL, NULL, &client) != noErr) {
		return 0;
	}
	if (MIDIInputPortCreate(client, CFSTR("Input port"), readProc, NULL, &portIn) != noErr) {
		return 0;
	}
	if (MIDIOutputPortCreate(client, CFSTR("Output port"), &portOut) != noErr) {
		return 0;
	}

	if (!midi_provider_register("Mac OS X", &driver)) return 0;

	return 1;
}

#endif
© All Rights Reserved