# Conflicts: # cmake/CompileFlags.cmake # src/3rdparty/squirrel/squirrel/sqclosure.h # src/3rdparty/squirrel/squirrel/sqobject.h # src/3rdparty/squirrel/squirrel/sqvm.h # src/aircraft.h # src/airport_gui.cpp # src/blitter/32bpp_sse_func.hpp # src/blitter/null.hpp # src/bridge_gui.cpp # src/build_vehicle_gui.cpp # src/cargotype.h # src/cheat_gui.cpp # src/command.cpp # src/command_func.h # src/company_gui.cpp # src/console_gui.cpp # src/date_gui.cpp # src/depot_gui.cpp # src/dock_gui.cpp # src/economy.cpp # src/error_gui.cpp # src/fileio.cpp # src/fios.cpp # src/fios_gui.cpp # src/fontcache/spritefontcache.h # src/framerate_gui.cpp # src/game/game_text.cpp # src/gamelog.cpp # src/genworld_gui.cpp # src/gfx_layout_fallback.cpp # src/group_gui.cpp # src/highscore_gui.cpp # src/hotkeys.cpp # src/industry_cmd.cpp # src/industry_gui.cpp # src/landscape.cpp # src/main_gui.cpp # src/misc_cmd.cpp # src/misc_gui.cpp # src/network/core/tcp_game.cpp # src/network/core/udp.cpp # src/network/network_chat_gui.cpp # src/network/network_content_gui.cpp # src/network/network_gui.cpp # src/network/network_server.cpp # src/network/network_server.h # src/newgrf_airport.cpp # src/newgrf_airport.h # src/newgrf_airporttiles.cpp # src/newgrf_airporttiles.h # src/newgrf_animation_base.h # src/newgrf_canal.cpp # src/newgrf_commons.h # src/newgrf_config.cpp # src/newgrf_debug_gui.cpp # src/newgrf_engine.cpp # src/newgrf_engine.h # src/newgrf_generic.cpp # src/newgrf_gui.cpp # src/newgrf_house.cpp # src/newgrf_house.h # src/newgrf_industries.cpp # src/newgrf_industries.h # src/newgrf_industrytiles.cpp # src/newgrf_industrytiles.h # src/newgrf_object.cpp # src/newgrf_object.h # src/newgrf_railtype.cpp # src/newgrf_railtype.h # src/newgrf_roadstop.cpp # src/newgrf_roadstop.h # src/newgrf_roadtype.cpp # src/newgrf_roadtype.h # src/newgrf_spritegroup.cpp # src/newgrf_spritegroup.h # src/newgrf_station.cpp # src/newgrf_station.h # src/newgrf_town.cpp # src/newgrf_town.h # src/news_gui.cpp # src/object_gui.cpp # src/order_gui.cpp # src/os/macosx/crashlog_osx.cpp # src/os/unix/crashlog_unix.cpp # src/os/windows/crashlog_win.cpp # src/os/windows/win32.cpp # src/os/windows/win32_main.cpp # src/pathfinder/npf/npf.cpp # src/pathfinder/npf/queue.cpp # src/rail_cmd.cpp # src/rail_gui.cpp # src/road_gui.cpp # src/roadveh.h # src/saveload/saveload.cpp # src/screenshot.cpp # src/script/api/script_text.hpp # src/settings.cpp # src/settings_gui.cpp # src/settings_internal.h # src/settings_table.cpp # src/signs_cmd.cpp # src/signs_gui.cpp # src/smallmap_gui.cpp # src/smallmap_gui.h # src/spriteloader/grf.hpp # src/station_cmd.cpp # src/station_gui.cpp # src/station_map.h # src/statusbar_gui.cpp # src/stdafx.h # src/strgen/strgen.cpp # src/table/newgrf_debug_data.h # src/terraform_gui.cpp # src/timer/timer_game_calendar.cpp # src/timer/timer_window.cpp # src/town.h # src/town_cmd.cpp # src/town_gui.cpp # src/train_gui.cpp # src/transparency_gui.cpp # src/vehicle_gui.cpp # src/water_cmd.cpp # src/waypoint_cmd.cpp # src/widget.cpp # src/widget_type.h # src/widgets/dropdown.cpp # src/widgets/rail_widget.h # src/widgets/terraform_widget.h # src/window.cpp # src/window_gui.h
203 lines
5.1 KiB
C++
203 lines
5.1 KiB
C++
/*
|
|
* 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 '%s'", filename.c_str());
|
|
|
|
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 '%s'", filename.c_str());
|
|
}
|
|
|
|
|
|
/**
|
|
* 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 */
|