Skip to content

Latest commit

 

History

History
316 lines (249 loc) · 7.84 KB

native_midi_macosx.c

File metadata and controls

316 lines (249 loc) · 7.84 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
/*
native_midi_macosx: Native Midi support on Mac OS X for the SDL_mixer library
Copyright (C) 2009 Ryan C. Gordon
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Ryan C. Gordon
icculus@icculus.org
*/
/* This is Mac OS X only, using Core MIDI.
Mac OS 9 support via QuickTime is in native_midi_mac.c */
#include "SDL_config.h"
#if __MACOSX__
Oct 19, 2009
Oct 19, 2009
30
#include <Carbon/Carbon.h>
31
32
#include <AudioToolbox/AudioToolbox.h>
Oct 19, 2009
Oct 19, 2009
33
#include "../SDL_mixer.h"
34
35
36
37
38
39
40
41
42
#include "SDL_endian.h"
#include "native_midi.h"
/* Native Midi song */
struct _NativeMidiSong
{
MusicPlayer player;
MusicSequence sequence;
MusicTimeStamp endTime;
Oct 19, 2009
Oct 19, 2009
43
AudioUnit audiounit;
44
45
46
};
static NativeMidiSong *currentsong = NULL;
Oct 19, 2009
Oct 19, 2009
47
static int latched_volume = MIX_MAX_VOLUME;
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
static OSStatus
GetSequenceLength(MusicSequence sequence, MusicTimeStamp *_sequenceLength)
{
// http://lists.apple.com/archives/Coreaudio-api/2003/Jul/msg00370.html
// figure out sequence length
UInt32 ntracks, i;
MusicTimeStamp sequenceLength = 0;
OSStatus err;
err = MusicSequenceGetTrackCount(sequence, &ntracks);
if (err != noErr)
return err;
for (i = 0; i < ntracks; ++i)
{
MusicTrack track;
MusicTimeStamp tracklen = 0;
UInt32 tracklenlen = sizeof (tracklen);
err = MusicSequenceGetIndTrack(sequence, i, &track);
if (err != noErr)
return err;
err = MusicTrackGetProperty(track, kSequenceTrackProperty_TrackLength,
&tracklen, &tracklenlen);
if (err != noErr)
return err;
if (sequenceLength < tracklen)
sequenceLength = tracklen;
}
*_sequenceLength = sequenceLength;
return noErr;
}
Oct 19, 2009
Oct 19, 2009
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
/* we're looking for the sequence output audiounit. */
static OSStatus
GetSequenceAudioUnit(MusicSequence sequence, AudioUnit *aunit)
{
AUGraph graph;
UInt32 nodecount, i;
OSStatus err;
err = MusicSequenceGetAUGraph(sequence, &graph);
if (err != noErr)
return err;
err = AUGraphGetNodeCount(graph, &nodecount);
if (err != noErr)
return err;
for (i = 0; i < nodecount; i++) {
AUNode node;
if (AUGraphGetIndNode(graph, i, &node) != noErr)
continue; /* better luck next time. */
#if 1 /* this is deprecated, but works back to 10.0 */
{
struct ComponentDescription desc;
UInt32 classdatasize = 0;
void *classdata = NULL;
err = AUGraphGetNodeInfo(graph, node, &desc, &classdatasize,
&classdata, aunit);
if (err != noErr)
continue;
else if (desc.componentType != kAudioUnitType_Output)
continue;
else if (desc.componentSubType != kAudioUnitSubType_DefaultOutput)
continue;
}
#else /* not deprecated, but requires 10.5 or later */
{
AudioComponentDescription desc;
if (AUGraphNodeInfo(graph, node, &desc, aunit) != noErr)
continue;
else if (desc.componentType != kAudioUnitType_Output)
continue;
else if (desc.componentSubType != kAudioUnitSubType_DefaultOutput)
continue;
}
#endif
return noErr; /* found it! */
}
return kAUGraphErr_NodeNotFound;
}
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
int native_midi_detect()
{
return 1; /* always available. */
}
NativeMidiSong *native_midi_loadsong(const char *midifile)
{
NativeMidiSong *retval = NULL;
SDL_RWops *rw = SDL_RWFromFile(midifile, "rb");
if (rw != NULL) {
retval = native_midi_loadsong_RW(rw);
SDL_RWclose(rw);
}
return retval;
}
NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
{
NativeMidiSong *retval = NULL;
void *buf = NULL;
int len = 0;
CFDataRef data = NULL;
if (SDL_RWseek(rw, 0, RW_SEEK_END) < 0)
goto fail;
len = SDL_RWtell(rw);
if (len < 0)
goto fail;
if (SDL_RWseek(rw, 0, RW_SEEK_SET) < 0)
goto fail;
buf = malloc(len);
if (buf == NULL)
goto fail;
if (SDL_RWread(rw, buf, len, 1) != 1)
goto fail;
retval = malloc(sizeof(NativeMidiSong));
if (retval == NULL)
goto fail;
memset(retval, '\0', sizeof (*retval));
if (NewMusicPlayer(&retval->player) != noErr)
goto fail;
if (NewMusicSequence(&retval->sequence) != noErr)
goto fail;
data = CFDataCreate(NULL, (const UInt8 *) buf, len);
if (data == NULL)
goto fail;
free(buf);
buf = NULL;
#if 1 /* this is deprecated, but works back to 10.3 */
if (MusicSequenceLoadSMFDataWithFlags(retval->sequence, data, 0) != noErr)
goto fail;
#else /* not deprecated, but requires 10.5 or later */
if (MusicSequenceLoadData(retval->sequence, data, 0, 0) != noErr)
goto fail;
#endif
CFRelease(data);
data = NULL;
if (GetSequenceLength(retval->sequence, &retval->endTime) != noErr)
goto fail;
if (MusicPlayerSetSequence(retval->player, retval->sequence) != noErr)
goto fail;
return retval;
fail:
if (retval) {
if (retval->sequence)
DisposeMusicSequence(retval->sequence);
if (retval->player)
DisposeMusicPlayer(retval->player);
free(retval);
}
if (data)
CFRelease(data);
if (buf)
free(buf);
return NULL;
}
void native_midi_freesong(NativeMidiSong *song)
{
if (song != NULL) {
if (currentsong == song)
currentsong = NULL;
MusicPlayerStop(song->player);
DisposeMusicSequence(song->sequence);
DisposeMusicPlayer(song->player);
free(song);
}
}
void native_midi_start(NativeMidiSong *song)
{
Oct 19, 2009
Oct 19, 2009
250
251
int vol;
252
253
254
if (song == NULL)
return;
Oct 19, 2009
Oct 19, 2009
255
256
SDL_PauseAudio(1);
SDL_UnlockAudio();
257
258
259
260
261
262
263
if (currentsong)
MusicPlayerStop(currentsong->player);
currentsong = song;
MusicPlayerStart(song->player);
Oct 19, 2009
Oct 19, 2009
264
265
266
267
268
269
270
271
GetSequenceAudioUnit(song->sequence, &song->audiounit);
vol = latched_volume;
latched_volume++; /* just make this not match. */
native_midi_setvolume(vol);
SDL_LockAudio();
SDL_PauseAudio(0);
272
273
274
275
276
}
void native_midi_stop()
{
if (currentsong) {
Oct 19, 2009
Oct 19, 2009
277
278
SDL_PauseAudio(1);
SDL_UnlockAudio();
279
280
MusicPlayerStop(currentsong->player);
currentsong = NULL;
Oct 19, 2009
Oct 19, 2009
281
282
SDL_LockAudio();
SDL_PauseAudio(0);
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
}
}
int native_midi_active()
{
MusicTimeStamp currentTime = 0;
if (currentsong == NULL)
return 0;
MusicPlayerGetTime(currentsong->player, &currentTime);
return ((currentTime < currentsong->endTime) ||
(currentTime >= kMusicTimeStamp_EndOfTrack));
}
void native_midi_setvolume(int volume)
{
Oct 19, 2009
Oct 19, 2009
299
300
301
302
303
304
305
306
307
if (latched_volume == volume)
return;
latched_volume = volume;
if ((currentsong) && (currentsong->audiounit)) {
const float floatvol = ((float) volume) / ((float) MIX_MAX_VOLUME);
AudioUnitSetParameter(currentsong->audiounit, kHALOutputParam_Volume,
kAudioUnitScope_Global, 0, floatvol, 0);
}
308
309
310
311
312
313
314
315
}
const char *native_midi_error(void)
{
return ""; /* !!! FIXME */
}
#endif /* Mac OS X native MIDI support */