OpenTTD Source 20260109-master-g241b5fcdfe
cocoa_m.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
11#ifdef WITH_COCOA
12
13#include "../stdafx.h"
14#include "../os/macosx/macos.h"
15#include "cocoa_m.h"
16#include "midifile.hpp"
17#include "../debug.h"
18#include "../base_media_base.h"
19
20#include <CoreServices/CoreServices.h>
21#include <AudioUnit/AudioUnit.h>
22#include <AudioToolbox/AudioToolbox.h>
23
24#include "../safeguards.h"
25
26static FMusicDriver_Cocoa iFMusicDriver_Cocoa;
27
28
29static MusicPlayer _player = nullptr;
30static MusicSequence _sequence = nullptr;
31static MusicTimeStamp _seq_length = 0;
32static bool _playing = false;
33static uint8_t _volume = 127;
34
35
37static void DoSetVolume()
38{
39 if (_sequence == nullptr) return;
40
41 AUGraph graph;
42 MusicSequenceGetAUGraph(_sequence, &graph);
43
44 AudioUnit output_unit = nullptr;
45
46 /* Get output audio unit */
47 UInt32 node_count = 0;
48 AUGraphGetNodeCount(graph, &node_count);
49 for (UInt32 i = 0; i < node_count; i++) {
50 AUNode node;
51 AUGraphGetIndNode(graph, i, &node);
52
53 AudioUnit unit;
54 AudioComponentDescription desc;
55 AUGraphNodeInfo(graph, node, &desc, &unit);
56
57 if (desc.componentType == kAudioUnitType_Output) {
58 output_unit = unit;
59 break;
60 }
61 }
62 if (output_unit == nullptr) {
63 Debug(driver, 1, "cocoa_m: Failed to get output node to set volume");
64 return;
65 }
66
67 Float32 vol = _volume / 127.0f; // 0 - +127 -> 0.0 - 1.0
68 AudioUnitSetParameter(output_unit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0);
69}
70
71
75std::optional<std::string_view> MusicDriver_Cocoa::Start(const StringList &)
76{
77 if (NewMusicPlayer(&_player) != noErr) return "failed to create music player";
78
79 return std::nullopt;
80}
81
82
87{
88 if (!_playing) return false;
89
90 MusicTimeStamp time = 0;
91 MusicPlayerGetTime(_player, &time);
92 return time < _seq_length;
93}
94
95
100{
101 if (_player != nullptr) DisposeMusicPlayer(_player);
102 if (_sequence != nullptr) DisposeMusicSequence(_sequence);
103}
104
105
112{
113 std::string filename = MidiFile::GetSMFFile(song);
114
115 Debug(driver, 2, "cocoa_m: trying to play '{}'", filename);
116
117 this->StopSong();
118 if (_sequence != nullptr) {
119 DisposeMusicSequence(_sequence);
120 _sequence = nullptr;
121 }
122
123 if (filename.empty()) return;
124
125 if (NewMusicSequence(&_sequence) != noErr) {
126 Debug(driver, 0, "cocoa_m: Failed to create music sequence");
127 return;
128 }
129
130 std::string os_file = OTTD2FS(filename);
131 CFAutoRelease<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8*)os_file.data(), os_file.length(), false));
132
133 if (MusicSequenceFileLoad(_sequence, url.get(), kMusicSequenceFile_AnyType, 0) != noErr) {
134 Debug(driver, 0, "cocoa_m: Failed to load MIDI file");
135 return;
136 }
137
138 /* Construct audio graph */
139 AUGraph graph = nullptr;
140
141 MusicSequenceGetAUGraph(_sequence, &graph);
142 AUGraphOpen(graph);
143 if (AUGraphInitialize(graph) != noErr) {
144 Debug(driver, 0, "cocoa_m: Failed to initialize AU graph");
145 return;
146 }
147
148 /* Figure out sequence length */
149 UInt32 num_tracks;
150 MusicSequenceGetTrackCount(_sequence, &num_tracks);
151 _seq_length = 0;
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;
159 }
160 /* Add 8 beats for reverb/long note release */
161 _seq_length += 8;
162
163 DoSetVolume();
164 MusicPlayerSetSequence(_player, _sequence);
165 MusicPlayerPreroll(_player);
166 if (MusicPlayerStart(_player) != noErr) return;
167 _playing = true;
168
169 Debug(driver, 3, "cocoa_m: playing '{}'", filename);
170}
171
172
177{
178 MusicPlayerStop(_player);
179 MusicPlayerSetSequence(_player, nullptr);
180 _playing = false;
181}
182
183
189void MusicDriver_Cocoa::SetVolume(uint8_t vol)
190{
191 _volume = vol;
192 DoSetVolume();
193}
194
195#endif /* WITH_COCOA */
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 &param) 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.
Definition debug.h:37
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.
Definition macos.h:54
Parser for standard MIDI files.
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
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.
Definition win32.cpp:357