Files
@ r28235:4e207e6deedc
Branch filter:
Location: cpp/openttd-patchpack/source/src/music/cocoa_m.cpp
r28235:4e207e6deedc
5.0 KiB
text/x-c
Change: Add outline of sprite to sprite aligner.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/**
* @file cocoa_m.cpp
* @brief MIDI music player for MacOS X using CoreAudio.
*/
#ifdef WITH_COCOA
#include "../stdafx.h"
#include "../os/macosx/macos.h"
#include "cocoa_m.h"
#include "midifile.hpp"
#include "../debug.h"
#include "../base_media_base.h"
#include <CoreServices/CoreServices.h>
#include <AudioUnit/AudioUnit.h>
#include <AudioToolbox/AudioToolbox.h>
#include "../safeguards.h"
#if !defined(HAVE_OSX_1011_SDK)
#define kMusicSequenceFile_AnyType 0
#endif
static FMusicDriver_Cocoa iFMusicDriver_Cocoa;
static MusicPlayer _player = nullptr;
static MusicSequence _sequence = nullptr;
static MusicTimeStamp _seq_length = 0;
static bool _playing = false;
static byte _volume = 127;
/** Set the volume of the current sequence. */
static void DoSetVolume()
{
if (_sequence == nullptr) return;
AUGraph graph;
MusicSequenceGetAUGraph(_sequence, &graph);
AudioUnit output_unit = nullptr;
/* Get output audio unit */
UInt32 node_count = 0;
AUGraphGetNodeCount(graph, &node_count);
for (UInt32 i = 0; i < node_count; i++) {
AUNode node;
AUGraphGetIndNode(graph, i, &node);
AudioUnit unit;
AudioComponentDescription desc;
AUGraphNodeInfo(graph, node, &desc, &unit);
if (desc.componentType == kAudioUnitType_Output) {
output_unit = unit;
break;
}
}
if (output_unit == nullptr) {
Debug(driver, 1, "cocoa_m: Failed to get output node to set volume");
return;
}
Float32 vol = _volume / 127.0f; // 0 - +127 -> 0.0 - 1.0
AudioUnitSetParameter(output_unit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0);
}
/**
* Initialized the MIDI player, including QuickTime initialization.
*/
const char *MusicDriver_Cocoa::Start(const StringList &)
{
if (NewMusicPlayer(&_player) != noErr) return "failed to create music player";
return nullptr;
}
/**
* Checks whether the player is active.
*/
bool MusicDriver_Cocoa::IsSongPlaying()
{
if (!_playing) return false;
MusicTimeStamp time = 0;
MusicPlayerGetTime(_player, &time);
return time < _seq_length;
}
/**
* Stops the MIDI player.
*/
void MusicDriver_Cocoa::Stop()
{
if (_player != nullptr) DisposeMusicPlayer(_player);
if (_sequence != nullptr) DisposeMusicSequence(_sequence);
}
/**
* Starts playing a new song.
*
* @param song Description of music to load and play
*/
void MusicDriver_Cocoa::PlaySong(const MusicSongInfo &song)
{
std::string filename = MidiFile::GetSMFFile(song);
Debug(driver, 2, "cocoa_m: trying to play '{}'", filename);
this->StopSong();
if (_sequence != nullptr) {
DisposeMusicSequence(_sequence);
_sequence = nullptr;
}
if (filename.empty()) return;
if (NewMusicSequence(&_sequence) != noErr) {
Debug(driver, 0, "cocoa_m: Failed to create music sequence");
return;
}
std::string os_file = OTTD2FS(filename);
CFAutoRelease<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8*)os_file.c_str(), os_file.length(), false));
if (MusicSequenceFileLoad(_sequence, url.get(), kMusicSequenceFile_AnyType, 0) != noErr) {
Debug(driver, 0, "cocoa_m: Failed to load MIDI file");
return;
}
/* Construct audio graph */
AUGraph graph = nullptr;
MusicSequenceGetAUGraph(_sequence, &graph);
AUGraphOpen(graph);
if (AUGraphInitialize(graph) != noErr) {
Debug(driver, 0, "cocoa_m: Failed to initialize AU graph");
return;
}
/* Figure out sequence length */
UInt32 num_tracks;
MusicSequenceGetTrackCount(_sequence, &num_tracks);
_seq_length = 0;
for (UInt32 i = 0; i < num_tracks; i++) {
MusicTrack track = nullptr;
MusicTimeStamp track_length = 0;
UInt32 prop_size = sizeof(MusicTimeStamp);
MusicSequenceGetIndTrack(_sequence, i, &track);
MusicTrackGetProperty(track, kSequenceTrackProperty_TrackLength, &track_length, &prop_size);
if (track_length > _seq_length) _seq_length = track_length;
}
/* Add 8 beats for reverb/long note release */
_seq_length += 8;
DoSetVolume();
MusicPlayerSetSequence(_player, _sequence);
MusicPlayerPreroll(_player);
if (MusicPlayerStart(_player) != noErr) return;
_playing = true;
Debug(driver, 3, "cocoa_m: playing '{}'", filename);
}
/**
* Stops playing the current song, if the player is active.
*/
void MusicDriver_Cocoa::StopSong()
{
MusicPlayerStop(_player);
MusicPlayerSetSequence(_player, nullptr);
_playing = false;
}
/**
* Changes the playing volume of the MIDI player.
*
* @param vol The desired volume, range of the value is @c 0-127
*/
void MusicDriver_Cocoa::SetVolume(byte vol)
{
_volume = vol;
DoSetVolume();
}
#endif /* WITH_COCOA */
|