| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <AudioToolbox/AUGraph.h> |
| 22 |
#include <CoreServices/CoreServices.h> |
| 23 |
|
| 24 |
|
| 25 |
#define RequireNoErr(error) \ |
| 26 |
do { \ |
| 27 |
err = error; \ |
| 28 |
if (err != noErr) \ |
| 29 |
goto bail; \ |
| 30 |
} while (false) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 72 |
RequireNoErr(NewAUGraph(&m_auGraph)); |
| 73 |
|
| 74 |
AUNode outputNode, synthNode; |
| 75 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 105 |
RequireNoErr(AUGraphConnectNodeInput(m_auGraph, synthNode, 0, outputNode, 0)); |
| 106 |
|
| 107 |
|
| 108 |
RequireNoErr(AUGraphOpen(m_auGraph)); |
| 109 |
RequireNoErr(AUGraphInitialize(m_auGraph)); |
| 110 |
|
| 111 |
|
| 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 |
|
| 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 |
|
| 137 |
|
| 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 |
|
| 158 |
|
| 159 |
return false; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 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; |