Debian Codegraph @

src/gui/midi_coreaudio.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: midi_coreaudio.h,v 1.12 2009-10-18 18:06:28 qbix79 Exp $ */
20
21 #include <AudioToolbox/AUGraph.h>
22 #include <CoreServices/CoreServices.h>
23
24 // A macro to simplify error handling a bit.
25 #define RequireNoErr(error) \
26 do { \
27 err = error; \
28 if (err != noErr) \
29 goto bail; \
30 } while (false)
31
32 // With the release of Mac OS X 10.5 in October 2007, Apple deprecated the
33 // AUGraphNewNode & AUGraphGetNodeInfo APIs in favor of the new AUGraphAddNode &
34 // AUGraphNodeInfo APIs. While it is easy to switch to those, it breaks
35 // compatibility with all pre-10.5 systems.
36 //
37 // Since 10.5 was the last system to support PowerPC, we use the old, deprecated
38 // APIs on PowerPC based systems by default. On all other systems (such as Mac
39 // OS X running on Intel hardware, or iOS running on ARM), we use the new API by
40 // default.
41 //
42 // This leaves Mac OS X 10.4 running on x86 processors as the only system
43 // combination that this code will not support by default. It seems quite
44 // reasonable to assume that anybody with an Intel system has since then moved
45 // on to a newer Mac OS X release. But if for some reason you absolutely need to
46 // build an x86 version of this code using the old, deprecated API, you can
47 // simply do so by manually enable the USE_DEPRECATED_COREAUDIO_API switch (e.g.
48 // by adding setting it suitably in CPPFLAGS).
49 #if !defined(USE_DEPRECATED_COREAUDIO_API)
50 # if TARGET_CPU_PPC || TARGET_CPU_PPC64
51 # define USE_DEPRECATED_COREAUDIO_API 1
52 # else
53 # define USE_DEPRECATED_COREAUDIO_API 0
54 # endif
55 #endif
56
57 class MidiHandler_coreaudio : public MidiHandler {
58 private:
59 AUGraph m_auGraph;
60 AudioUnit m_synth;
61 const char *soundfont;
62 public:
63 MidiHandler_coreaudio() : m_auGraph(0), m_synth(0) {}
64 const char * GetName(void) { return "coreaudio"; }
65 bool Open(const char * conf) {
66 OSStatus err = 0;
67
68 if (m_auGraph)
69 return false;
70
71 // Open the Music Device.
72 RequireNoErr(NewAUGraph(&m_auGraph));
73
74 AUNode outputNode, synthNode;
75 // OS X 10.5 SDK doesn't know AudioComponentDescription desc;
76 #if USE_DEPRECATED_COREAUDIO_API || (MAC_OS_X_VERSION_MAX_ALLOWED <= 1050)
77 ComponentDescription desc;
78 #else
79 AudioComponentDescription desc;
80 #endif
81
82 // The default output device
83 desc.componentType = kAudioUnitType_Output;
84 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
85 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
86 desc.componentFlags = 0;
87 desc.componentFlagsMask = 0;
88 #if USE_DEPRECATED_COREAUDIO_API
89 RequireNoErr(AUGraphNewNode(m_auGraph, &desc, 0, NULL, &outputNode));
90 #else
91 RequireNoErr(AUGraphAddNode(m_auGraph, &desc, &outputNode));
92 #endif
93
94 // The built-in default (softsynth) music device
95 desc.componentType = kAudioUnitType_MusicDevice;
96 desc.componentSubType = kAudioUnitSubType_DLSSynth;
97 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
98 #if USE_DEPRECATED_COREAUDIO_API
99 RequireNoErr(AUGraphNewNode(m_auGraph, &desc, 0, NULL, &synthNode));
100 #else
101 RequireNoErr(AUGraphAddNode(m_auGraph, &desc, &synthNode));
102 #endif
103
104 // Connect the softsynth to the default output
105 RequireNoErr(AUGraphConnectNodeInput(m_auGraph, synthNode, 0, outputNode, 0));
106
107 // Open and initialize the whole graph
108 RequireNoErr(AUGraphOpen(m_auGraph));
109 RequireNoErr(AUGraphInitialize(m_auGraph));
110
111 // Get the music device from the graph.
112 #if USE_DEPRECATED_COREAUDIO_API
113 RequireNoErr(AUGraphGetNodeInfo(m_auGraph, synthNode, NULL, NULL, NULL, &m_synth));
114 #else
115 RequireNoErr(AUGraphNodeInfo(m_auGraph, synthNode, NULL, &m_synth));
116 #endif
117
118 // Optionally load a soundfont
119 if (conf && conf[0]) {
120 soundfont = conf;
121 OSErr err;
122 #if USE_DEPRECATED_COREAUDIO_API
123 FSRef soundfontRef;
124 err = FSPathMakeRef((const UInt8*)soundfont, &soundfontRef, NULL);
125 if (!err) {
126 err = AudioUnitSetProperty(
127 m_synth,
128 kMusicDeviceProperty_SoundBankFSRef,
129 kAudioUnitScope_Global,
130 0,
131 &soundfontRef,
132 sizeof(soundfontRef)
133 );
134 }
135 #else
136 // kMusicDeviceProperty_SoundBankFSRef is present on 10.6+, but
137 // kMusicDeviceProperty_SoundBankURL was added in 10.5 as a future prooof replacement
138 CFURLRef url = CFURLCreateFromFileSystemRepresentation(
139 kCFAllocatorDefault,
140 (const UInt8*)soundfont,
141 strlen(soundfont), false
142 );
143 if (url) {
144 err = AudioUnitSetProperty(
145 m_synth, kMusicDeviceProperty_SoundBankURL,
146 kAudioUnitScope_Global, 0, &url, sizeof(url)
147 );
148 CFRelease(url);
149 } else {
150 LOG_MSG("Failed to allocate CFURLRef from %s",soundfont);
151 }
152 #endif
153 if (!err) {
154 LOG_MSG("MIDI:coreaudio: loaded soundfont: %s",soundfont);
155 } else {
156 LOG_MSG("Error loading CoreAudio SoundFont %s",soundfont);
157 // after trying and failing to load a soundfont it's better
158 // to fail initializing the CoreAudio driver or it might crash
159 return false;
160 }
161 }
162
163 // Finally: Start the graph!
164 RequireNoErr(AUGraphStart(m_auGraph));
165
166 return true;
167
168 bail:
169 if (m_auGraph) {
170 AUGraphStop(m_auGraph);
171 DisposeAUGraph(m_auGraph);
172 m_auGraph = 0;
173 }
174 return false;
175 }
176
177 void Close(void) {
178 if (m_auGraph) {
179 AUGraphStop(m_auGraph);
180 DisposeAUGraph(m_auGraph);
181 m_auGraph = 0;
182 }
183 }
184
185 void PlayMsg(Bit8u * msg) {
186 MusicDeviceMIDIEvent(m_synth, msg[0], msg[1], msg[2], 0);
187 }
188
189 void PlaySysex(Bit8u * sysex, Bitu len) {
190 MusicDeviceSysEx(m_synth, sysex, len);
191 }
192 };
193
194 #undef RequireNoErr
195
196 MidiHandler_coreaudio Midi_coreaudio;