14#include "../os/macosx/macos.h"
18#include "../base_media_base.h"
20#include <CoreServices/CoreServices.h>
21#include <AudioUnit/AudioUnit.h>
22#include <AudioToolbox/AudioToolbox.h>
24#include "../safeguards.h"
29static MusicPlayer _player =
nullptr;
30static MusicSequence _sequence =
nullptr;
31static MusicTimeStamp _seq_length = 0;
32static bool _playing =
false;
33static uint8_t _volume = 127;
37static void DoSetVolume()
39 if (_sequence ==
nullptr)
return;
42 MusicSequenceGetAUGraph(_sequence, &graph);
44 AudioUnit output_unit =
nullptr;
47 UInt32 node_count = 0;
48 AUGraphGetNodeCount(graph, &node_count);
49 for (UInt32 i = 0; i < node_count; i++) {
51 AUGraphGetIndNode(graph, i, &node);
54 AudioComponentDescription desc;
55 AUGraphNodeInfo(graph, node, &desc, &unit);
57 if (desc.componentType == kAudioUnitType_Output) {
62 if (output_unit ==
nullptr) {
63 Debug(driver, 1,
"cocoa_m: Failed to get output node to set volume");
67 Float32 vol = _volume / 127.0f;
68 AudioUnitSetParameter(output_unit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0);
77 if (NewMusicPlayer(&_player) != noErr)
return "failed to create music player";
88 if (!_playing)
return false;
90 MusicTimeStamp time = 0;
91 MusicPlayerGetTime(_player, &time);
92 return time < _seq_length;
101 if (_player !=
nullptr) DisposeMusicPlayer(_player);
102 if (_sequence !=
nullptr) DisposeMusicSequence(_sequence);
115 Debug(driver, 2,
"cocoa_m: trying to play '{}'", filename);
118 if (_sequence !=
nullptr) {
119 DisposeMusicSequence(_sequence);
123 if (filename.empty())
return;
125 if (NewMusicSequence(&_sequence) != noErr) {
126 Debug(driver, 0,
"cocoa_m: Failed to create music sequence");
130 std::string os_file =
OTTD2FS(filename);
131 CFAutoRelease<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (
const UInt8*)os_file.data(), os_file.length(),
false));
133 if (MusicSequenceFileLoad(_sequence, url.get(), kMusicSequenceFile_AnyType, 0) != noErr) {
134 Debug(driver, 0,
"cocoa_m: Failed to load MIDI file");
139 AUGraph graph =
nullptr;
141 MusicSequenceGetAUGraph(_sequence, &graph);
143 if (AUGraphInitialize(graph) != noErr) {
144 Debug(driver, 0,
"cocoa_m: Failed to initialize AU graph");
150 MusicSequenceGetTrackCount(_sequence, &num_tracks);
152 for (UInt32 i = 0; i < num_tracks; i++) {
153 MusicTrack track =
nullptr;
154 MusicTimeStamp track_length = 0;
155 UInt32 prop_size =
sizeof(MusicTimeStamp);
156 MusicSequenceGetIndTrack(_sequence, i, &track);
157 MusicTrackGetProperty(track, kSequenceTrackProperty_TrackLength, &track_length, &prop_size);
158 if (track_length > _seq_length) _seq_length = track_length;
164 MusicPlayerSetSequence(_player, _sequence);
165 MusicPlayerPreroll(_player);
166 if (MusicPlayerStart(_player) != noErr)
return;
169 Debug(driver, 3,
"cocoa_m: playing '{}'", filename);
178 MusicPlayerStop(_player);
179 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.
std::optional< std::string_view > Start(const StringList ¶m) override
Start this driver.
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
Base of music playback via CoreAudio.
#define Debug(category, level, format_string,...)
Output 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.
Parser for standard MIDI files.
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(std::string_view name)
Convert from OpenTTD's encoding to a wide string.