16 #include "../stdafx.h"
17 #include "../os/macosx/macos.h"
19 #include "midifile.hpp"
21 #include "../base_media_base.h"
23 #include <CoreServices/CoreServices.h>
24 #include <AudioUnit/AudioUnit.h>
25 #include <AudioToolbox/AudioToolbox.h>
27 #include "../safeguards.h"
29 #if !defined(HAVE_OSX_1011_SDK)
30 #define kMusicSequenceFile_AnyType 0
36 static MusicPlayer _player =
nullptr;
37 static MusicSequence _sequence =
nullptr;
38 static MusicTimeStamp _seq_length = 0;
39 static bool _playing =
false;
40 static uint8_t _volume = 127;
44 static void DoSetVolume()
46 if (_sequence ==
nullptr)
return;
49 MusicSequenceGetAUGraph(_sequence, &graph);
51 AudioUnit output_unit =
nullptr;
54 UInt32 node_count = 0;
55 AUGraphGetNodeCount(graph, &node_count);
56 for (UInt32 i = 0; i < node_count; i++) {
58 AUGraphGetIndNode(graph, i, &node);
61 AudioComponentDescription desc;
62 AUGraphNodeInfo(graph, node, &desc, &unit);
64 if (desc.componentType == kAudioUnitType_Output) {
69 if (output_unit ==
nullptr) {
70 Debug(driver, 1,
"cocoa_m: Failed to get output node to set volume");
74 Float32 vol = _volume / 127.0f;
75 AudioUnitSetParameter(output_unit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0);
84 if (NewMusicPlayer(&_player) != noErr)
return "failed to create music player";
95 if (!_playing)
return false;
97 MusicTimeStamp time = 0;
98 MusicPlayerGetTime(_player, &time);
99 return time < _seq_length;
108 if (_player !=
nullptr) DisposeMusicPlayer(_player);
109 if (_sequence !=
nullptr) DisposeMusicSequence(_sequence);
122 Debug(driver, 2,
"cocoa_m: trying to play '{}'", filename);
125 if (_sequence !=
nullptr) {
126 DisposeMusicSequence(_sequence);
130 if (filename.empty())
return;
132 if (NewMusicSequence(&_sequence) != noErr) {
133 Debug(driver, 0,
"cocoa_m: Failed to create music sequence");
137 std::string os_file =
OTTD2FS(filename);
138 CFAutoRelease<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (
const UInt8*)os_file.c_str(), os_file.length(),
false));
140 if (MusicSequenceFileLoad(_sequence, url.get(), kMusicSequenceFile_AnyType, 0) != noErr) {
141 Debug(driver, 0,
"cocoa_m: Failed to load MIDI file");
146 AUGraph graph =
nullptr;
148 MusicSequenceGetAUGraph(_sequence, &graph);
150 if (AUGraphInitialize(graph) != noErr) {
151 Debug(driver, 0,
"cocoa_m: Failed to initialize AU graph");
157 MusicSequenceGetTrackCount(_sequence, &num_tracks);
159 for (UInt32 i = 0; i < num_tracks; i++) {
160 MusicTrack track =
nullptr;
161 MusicTimeStamp track_length = 0;
162 UInt32 prop_size =
sizeof(MusicTimeStamp);
163 MusicSequenceGetIndTrack(_sequence, i, &track);
164 MusicTrackGetProperty(track, kSequenceTrackProperty_TrackLength, &track_length, &prop_size);
165 if (track_length > _seq_length) _seq_length = track_length;
171 MusicPlayerSetSequence(_player, _sequence);
172 MusicPlayerPreroll(_player);
173 if (MusicPlayerStart(_player) != noErr)
return;
176 Debug(driver, 3,
"cocoa_m: playing '{}'", filename);
185 MusicPlayerStop(_player);
186 MusicPlayerSetSequence(_player,
nullptr);
void SetVolume(uint8_t vol) override
Set the volume, if possible.
bool IsSongPlaying() override
Are we currently playing a song?
void Stop() override
Stop this driver.
void StopSong() override
Stop playing the current song.
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
std::optional< std::string_view > Start(const StringList ¶m) override
Start this driver.
Base of music playback via CoreAudio.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
std::unique_ptr< typename std::remove_pointer< T >::type, CFDeleter< typename std::remove_pointer< T >::type > > CFAutoRelease
Specialisation of std::unique_ptr for CoreFoundation objects.
std::vector< std::string > StringList
Type for a list of strings.
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Metadata about a music track.
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.