OpenTTD Source  20241121-master-g67a0fccfad
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 <http://www.gnu.org/licenses/>.
6  */
7 
14 #ifdef WITH_COCOA
15 
16 #include "../stdafx.h"
17 #include "../os/macosx/macos.h"
18 #include "cocoa_m.h"
19 #include "midifile.hpp"
20 #include "../debug.h"
21 #include "../base_media_base.h"
22 
23 #include <CoreServices/CoreServices.h>
24 #include <AudioUnit/AudioUnit.h>
25 #include <AudioToolbox/AudioToolbox.h>
26 
27 #include "../safeguards.h"
28 
29 #if !defined(HAVE_OSX_1011_SDK)
30 #define kMusicSequenceFile_AnyType 0
31 #endif
32 
33 static FMusicDriver_Cocoa iFMusicDriver_Cocoa;
34 
35 
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;
41 
42 
44 static void DoSetVolume()
45 {
46  if (_sequence == nullptr) return;
47 
48  AUGraph graph;
49  MusicSequenceGetAUGraph(_sequence, &graph);
50 
51  AudioUnit output_unit = nullptr;
52 
53  /* Get output audio unit */
54  UInt32 node_count = 0;
55  AUGraphGetNodeCount(graph, &node_count);
56  for (UInt32 i = 0; i < node_count; i++) {
57  AUNode node;
58  AUGraphGetIndNode(graph, i, &node);
59 
60  AudioUnit unit;
61  AudioComponentDescription desc;
62  AUGraphNodeInfo(graph, node, &desc, &unit);
63 
64  if (desc.componentType == kAudioUnitType_Output) {
65  output_unit = unit;
66  break;
67  }
68  }
69  if (output_unit == nullptr) {
70  Debug(driver, 1, "cocoa_m: Failed to get output node to set volume");
71  return;
72  }
73 
74  Float32 vol = _volume / 127.0f; // 0 - +127 -> 0.0 - 1.0
75  AudioUnitSetParameter(output_unit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0);
76 }
77 
78 
82 std::optional<std::string_view> MusicDriver_Cocoa::Start(const StringList &)
83 {
84  if (NewMusicPlayer(&_player) != noErr) return "failed to create music player";
85 
86  return std::nullopt;
87 }
88 
89 
94 {
95  if (!_playing) return false;
96 
97  MusicTimeStamp time = 0;
98  MusicPlayerGetTime(_player, &time);
99  return time < _seq_length;
100 }
101 
102 
107 {
108  if (_player != nullptr) DisposeMusicPlayer(_player);
109  if (_sequence != nullptr) DisposeMusicSequence(_sequence);
110 }
111 
112 
119 {
120  std::string filename = MidiFile::GetSMFFile(song);
121 
122  Debug(driver, 2, "cocoa_m: trying to play '{}'", filename);
123 
124  this->StopSong();
125  if (_sequence != nullptr) {
126  DisposeMusicSequence(_sequence);
127  _sequence = nullptr;
128  }
129 
130  if (filename.empty()) return;
131 
132  if (NewMusicSequence(&_sequence) != noErr) {
133  Debug(driver, 0, "cocoa_m: Failed to create music sequence");
134  return;
135  }
136 
137  std::string os_file = OTTD2FS(filename);
138  CFAutoRelease<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8*)os_file.c_str(), os_file.length(), false));
139 
140  if (MusicSequenceFileLoad(_sequence, url.get(), kMusicSequenceFile_AnyType, 0) != noErr) {
141  Debug(driver, 0, "cocoa_m: Failed to load MIDI file");
142  return;
143  }
144 
145  /* Construct audio graph */
146  AUGraph graph = nullptr;
147 
148  MusicSequenceGetAUGraph(_sequence, &graph);
149  AUGraphOpen(graph);
150  if (AUGraphInitialize(graph) != noErr) {
151  Debug(driver, 0, "cocoa_m: Failed to initialize AU graph");
152  return;
153  }
154 
155  /* Figure out sequence length */
156  UInt32 num_tracks;
157  MusicSequenceGetTrackCount(_sequence, &num_tracks);
158  _seq_length = 0;
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;
166  }
167  /* Add 8 beats for reverb/long note release */
168  _seq_length += 8;
169 
170  DoSetVolume();
171  MusicPlayerSetSequence(_player, _sequence);
172  MusicPlayerPreroll(_player);
173  if (MusicPlayerStart(_player) != noErr) return;
174  _playing = true;
175 
176  Debug(driver, 3, "cocoa_m: playing '{}'", filename);
177 }
178 
179 
184 {
185  MusicPlayerStop(_player);
186  MusicPlayerSetSequence(_player, nullptr);
187  _playing = false;
188 }
189 
190 
196 void MusicDriver_Cocoa::SetVolume(uint8_t vol)
197 {
198  _volume = vol;
199  DoSetVolume();
200 }
201 
202 #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.
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Base of music playback via CoreAudio.
#define Debug(category, level, format_string,...)
Ouptut 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
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.
Definition: midifile.cpp:1025
Metadata about a music track.
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
Definition: win32.cpp:354