18 #include "../stdafx.h"
19 #include "../os/macosx/macos.h"
24 #define Point OTTDPoint
25 #include <CoreServices/CoreServices.h>
26 #include <AudioUnit/AudioUnit.h>
27 #include <AudioToolbox/AudioToolbox.h>
31 #include "../safeguards.h"
36 static MusicPlayer _player = NULL;
37 static MusicSequence _sequence = NULL;
38 static MusicTimeStamp _seq_length = 0;
39 static bool _playing =
false;
40 static byte _volume = 127;
44 static void DoSetVolume()
46 if (_sequence == NULL)
return;
49 MusicSequenceGetAUGraph(_sequence, &graph);
51 AudioUnit output_unit = NULL;
54 UInt32 node_count = 0;
55 AUGraphGetNodeCount(graph, &node_count);
56 for (UInt32 i = 0; i < node_count; i++) {
58 AUGraphGetIndNode(graph, i, &node);
63 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
71 #ifdef __AUDIOCOMPONENT_H__
72 AudioComponentDescription desc;
74 ComponentDescription desc;
76 AUGraphNodeInfo(graph, node, &desc, &unit);
77 comp_type = desc.componentType;
81 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
82 ComponentDescription desc;
83 AUGraphGetNodeInfo(graph, node, &desc, NULL, NULL, &unit);
84 comp_type = desc.componentType;
88 if (comp_type == kAudioUnitType_Output) {
93 if (output_unit == NULL) {
94 DEBUG(driver, 1,
"cocoa_m: Failed to get output node to set volume");
98 Float32 vol = _volume / 127.0f;
99 AudioUnitSetParameter(output_unit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0);
108 if (NewMusicPlayer(&_player) != noErr)
return "failed to create music player";
119 if (!_playing)
return false;
121 MusicTimeStamp time = 0;
122 MusicPlayerGetTime(_player, &time);
123 return time < _seq_length;
132 if (_player != NULL) DisposeMusicPlayer(_player);
133 if (_sequence != NULL) DisposeMusicSequence(_sequence);
144 DEBUG(driver, 2,
"cocoa_m: trying to play '%s'", filename);
147 if (_sequence != NULL) {
148 DisposeMusicSequence(_sequence);
152 if (NewMusicSequence(&_sequence) != noErr) {
153 DEBUG(driver, 0,
"cocoa_m: Failed to create music sequence");
157 const char *os_file =
OTTD2FS(filename);
158 CFURLRef url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (
const UInt8*)os_file, strlen(os_file),
false);
160 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
162 if (MusicSequenceFileLoad(_sequence, url, 0, 0) != noErr) {
163 DEBUG(driver, 0,
"cocoa_m: Failed to load MIDI file");
170 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
172 if (!CFURLGetFSRef(url, &ref_file)) {
173 DEBUG(driver, 0,
"cocoa_m: Failed to make FSRef");
177 if (MusicSequenceLoadSMFWithFlags(_sequence, &ref_file, 0) != noErr) {
178 DEBUG(driver, 0,
"cocoa_m: Failed to load MIDI file old style");
187 AUGraph graph = NULL;
189 MusicSequenceGetAUGraph(_sequence, &graph);
191 if (AUGraphInitialize(graph) != noErr) {
192 DEBUG(driver, 0,
"cocoa_m: Failed to initialize AU graph");
198 MusicSequenceGetTrackCount(_sequence, &num_tracks);
200 for (UInt32 i = 0; i < num_tracks; i++) {
201 MusicTrack track = NULL;
202 MusicTimeStamp track_length = 0;
203 UInt32 prop_size =
sizeof(MusicTimeStamp);
204 MusicSequenceGetIndTrack(_sequence, i, &track);
205 MusicTrackGetProperty(track, kSequenceTrackProperty_TrackLength, &track_length, &prop_size);
206 if (track_length > _seq_length) _seq_length = track_length;
212 MusicPlayerSetSequence(_player, _sequence);
213 MusicPlayerPreroll(_player);
214 if (MusicPlayerStart(_player) != noErr)
return;
217 DEBUG(driver, 3,
"cocoa_m: playing '%s'", filename);
226 MusicPlayerStop(_player);
227 MusicPlayerSetSequence(_player, NULL);