Skip to content

Latest commit

 

History

History
347 lines (279 loc) · 9.27 KB

native_midi_macosx.c

File metadata and controls

347 lines (279 loc) · 9.27 KB
 
Dec 31, 2011
Dec 31, 2011
2
3
native_midi_macosx: Native Midi support on Mac OS X for the SDL_mixer library
Copyright (C) 2009 Ryan C. Gordon <icculus@icculus.org>
Dec 31, 2011
Dec 31, 2011
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Dec 31, 2011
Dec 31, 2011
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
Dec 31, 2011
Dec 31, 2011
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
22
23
24
25
26
27
28
*/
/* 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__
May 22, 2013
May 22, 2013
29
#include <CoreServices/CoreServices.h> /* ComponentDescription */
Aug 12, 2012
Aug 12, 2012
30
#include <AudioUnit/AudioUnit.h>
31
#include <AudioToolbox/AudioToolbox.h>
Nov 2, 2009
Nov 2, 2009
32
#include <AvailabilityMacros.h>
33
34
#include "SDL_endian.h"
Jan 29, 2016
Jan 29, 2016
35
36
#include "../SDL_mixer.h"
#include "../mixer.h"
37
38
39
40
41
42
43
44
#include "native_midi.h"
/* Native Midi song */
struct _NativeMidiSong
{
MusicPlayer player;
MusicSequence sequence;
MusicTimeStamp endTime;
Oct 19, 2009
Oct 19, 2009
45
AudioUnit audiounit;
Jan 1, 2012
Jan 1, 2012
46
int loops;
47
48
49
};
static NativeMidiSong *currentsong = NULL;
Oct 19, 2009
Oct 19, 2009
50
static int latched_volume = MIX_MAX_VOLUME;
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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* 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. */
Aug 12, 2012
Aug 12, 2012
112
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1050 /* this is deprecated, but works back to 10.0 */
Oct 19, 2009
Oct 19, 2009
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
{
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 */
{
Aug 12, 2012
Aug 12, 2012
128
129
130
131
132
133
134
135
# if !defined(AUDIO_UNIT_VERSION) || ((AUDIO_UNIT_VERSION + 0) < 1060)
/* AUGraphAddNode () is changed to take an AudioComponentDescription*
* desc parameter instead of a ComponentDescription* in the 10.6 SDK.
* AudioComponentDescription is in 10.6 or newer, but it is actually
* the same as struct ComponentDescription with 20 bytes of size and
* the same offsets of all members, therefore, is binary compatible. */
# define AudioComponentDescription ComponentDescription
# endif
Oct 19, 2009
Oct 19, 2009
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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;
}
Oct 17, 2017
Oct 17, 2017
153
int native_midi_detect(void)
154
155
156
157
{
return 1; /* always available. */
}
Jun 2, 2013
Jun 2, 2013
158
NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *src, int freesrc)
159
160
161
{
NativeMidiSong *retval = NULL;
void *buf = NULL;
Jun 2, 2013
Jun 2, 2013
162
Sint64 len = 0;
163
164
CFDataRef data = NULL;
Jun 2, 2013
Jun 2, 2013
165
if (SDL_RWseek(src, 0, RW_SEEK_END) < 0)
Jun 2, 2013
Jun 2, 2013
167
len = SDL_RWtell(src);
168
169
if (len < 0)
goto fail;
Jun 2, 2013
Jun 2, 2013
170
if (SDL_RWseek(src, 0, RW_SEEK_SET) < 0)
171
172
173
174
175
176
goto fail;
buf = malloc(len);
if (buf == NULL)
goto fail;
Jun 2, 2013
Jun 2, 2013
177
if (SDL_RWread(src, buf, len, 1) != 1)
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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;
Aug 12, 2012
Aug 12, 2012
198
199
200
201
202
203
204
205
206
207
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
/* MusicSequenceLoadSMFData() (avail. in 10.2, no 64 bit) is
* equivalent to calling MusicSequenceLoadSMFDataWithFlags()
* with a flags value of 0 (avail. in 10.3, avail. 64 bit).
* So, we use MusicSequenceLoadSMFData() for powerpc versions
* but the *WithFlags() on intel which require 10.4 anyway. */
# if defined(__ppc__) || defined(__POWERPC__)
if (MusicSequenceLoadSMFData(song->sequence, data) != noErr)
goto fail;
# else
208
209
if (MusicSequenceLoadSMFDataWithFlags(retval->sequence, data, 0) != noErr)
goto fail;
Aug 12, 2012
Aug 12, 2012
210
211
# endif
#else /* MusicSequenceFileLoadData() requires 10.5 or later. */
Nov 2, 2009
Nov 2, 2009
212
if (MusicSequenceFileLoadData(retval->sequence, data, 0, 0) != noErr)
213
214
215
216
217
218
219
220
221
222
223
224
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;
Jun 2, 2013
Jun 2, 2013
225
226
if (freesrc)
SDL_RWclose(src);
Dec 31, 2011
Dec 31, 2011
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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);
}
}
Jan 1, 2012
Jan 1, 2012
260
void native_midi_start(NativeMidiSong *song, int loops)
Oct 19, 2009
Oct 19, 2009
262
263
int vol;
264
265
266
if (song == NULL)
return;
Oct 19, 2009
Oct 19, 2009
267
SDL_PauseAudio(1);
Jan 29, 2016
Jan 29, 2016
268
Mix_UnlockAudio();
269
270
271
272
273
if (currentsong)
MusicPlayerStop(currentsong->player);
currentsong = song;
Jan 1, 2012
Jan 1, 2012
274
currentsong->loops = loops;
Jan 1, 2012
Jan 1, 2012
275
276
277
MusicPlayerPreroll(song->player);
MusicPlayerSetTime(song->player, 0);
278
279
MusicPlayerStart(song->player);
Oct 19, 2009
Oct 19, 2009
280
281
282
283
284
285
GetSequenceAudioUnit(song->sequence, &song->audiounit);
vol = latched_volume;
latched_volume++; /* just make this not match. */
native_midi_setvolume(vol);
Jan 29, 2016
Jan 29, 2016
286
Mix_LockAudio();
Oct 19, 2009
Oct 19, 2009
287
SDL_PauseAudio(0);
Oct 18, 2017
Oct 18, 2017
290
291
292
293
294
295
296
297
void native_midi_pause(void)
{
}
void native_midi_resume(void)
{
}
Oct 17, 2017
Oct 17, 2017
298
void native_midi_stop(void)
299
300
{
if (currentsong) {
Oct 19, 2009
Oct 19, 2009
301
SDL_PauseAudio(1);
Jan 29, 2016
Jan 29, 2016
302
Mix_UnlockAudio();
303
304
MusicPlayerStop(currentsong->player);
currentsong = NULL;
Jan 29, 2016
Jan 29, 2016
305
Mix_LockAudio();
Oct 19, 2009
Oct 19, 2009
306
SDL_PauseAudio(0);
Oct 17, 2017
Oct 17, 2017
310
int native_midi_active(void)
311
312
313
314
315
316
{
MusicTimeStamp currentTime = 0;
if (currentsong == NULL)
return 0;
MusicPlayerGetTime(currentsong->player, &currentTime);
Jan 1, 2012
Jan 1, 2012
317
318
319
320
321
322
323
324
325
if ((currentTime < currentsong->endTime) ||
(currentTime >= kMusicTimeStamp_EndOfTrack)) {
return 1;
} else if (currentsong->loops) {
--currentsong->loops;
MusicPlayerSetTime(currentsong->player, 0);
return 1;
}
return 0;
326
327
328
329
}
void native_midi_setvolume(int volume)
{
Oct 19, 2009
Oct 19, 2009
330
331
332
333
334
335
336
337
338
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);
}
339
340
341
342
343
344
345
346
}
const char *native_midi_error(void)
{
return ""; /* !!! FIXME */
}
#endif /* Mac OS X native MIDI support */