Move ride audio into own namespace

This commit is contained in:
Ted John 2021-01-20 21:10:17 +00:00
parent 28f4ec2b43
commit a9755609c2
6 changed files with 367 additions and 361 deletions

View File

@ -1375,7 +1375,7 @@ static void window_options_audio_mouseup(rct_window* w, rct_widgetindex widgetIn
gConfigSound.ride_music_enabled = !gConfigSound.ride_music_enabled; gConfigSound.ride_music_enabled = !gConfigSound.ride_music_enabled;
if (!gConfigSound.ride_music_enabled) if (!gConfigSound.ride_music_enabled)
{ {
RideAudioStopAllChannels(); OpenRCT2::RideAudio::StopAllChannels();
} }
config_save_default(); config_save_default();
w->Invalidate(); w->Invalidate();

View File

@ -292,7 +292,7 @@ namespace OpenRCT2::Audio
{ {
StopTitleMusic(); StopTitleMusic();
StopVehicleSounds(); StopVehicleSounds();
RideAudioStopAllChannels(); RideAudio::StopAllChannels();
peep_stop_crowd_noise(); peep_stop_crowd_noise();
StopWeatherSound(); StopWeatherSound();
} }
@ -357,7 +357,7 @@ namespace OpenRCT2::Audio
{ {
peep_stop_crowd_noise(); peep_stop_crowd_noise();
StopTitleMusic(); StopTitleMusic();
RideAudioStopAllChannels(); RideAudio::StopAllChannels();
StopWeatherSound(); StopWeatherSound();
_currentAudioDevice = -1; _currentAudioDevice = -1;
} }
@ -382,7 +382,7 @@ namespace OpenRCT2::Audio
{ {
gGameSoundsOff = true; gGameSoundsOff = true;
StopVehicleSounds(); StopVehicleSounds();
RideAudioStopAllChannels(); RideAudio::StopAllChannels();
peep_stop_crowd_noise(); peep_stop_crowd_noise();
StopWeatherSound(); StopWeatherSound();
} }

View File

@ -1785,7 +1785,7 @@ void window_close_construction_windows()
*/ */
void window_update_viewport_ride_music() void window_update_viewport_ride_music()
{ {
RideAudioClearAllViewportInstances(); OpenRCT2::RideAudio::ClearAllViewportInstances();
g_music_tracking_viewport = nullptr; g_music_tracking_viewport = nullptr;
for (auto it = g_window_list.rbegin(); it != g_window_list.rend(); it++) for (auto it = g_window_list.rbegin(); it != g_window_list.rend(); it++)

View File

@ -2038,7 +2038,7 @@ void Ride::UpdateAll()
for (auto& ride : GetRideManager()) for (auto& ride : GetRideManager())
ride.Update(); ride.Update();
RideUpdateMusicChannels(); OpenRCT2::RideAudio::UpdateMusicChannels();
} }
std::unique_ptr<TrackDesign> Ride::SaveToTrackDesign() const std::unique_ptr<TrackDesign> Ride::SaveToTrackDesign() const
@ -2923,7 +2923,7 @@ static void ride_music_update(Ride* ride)
sampleRate += 22050; sampleRate += 22050;
} }
RideUpdateMusicInstance(*ride, rideCoords, sampleRate); OpenRCT2::RideAudio::UpdateMusicInstance(*ride, rideCoords, sampleRate);
} }
#pragma endregion #pragma endregion

View File

@ -24,14 +24,17 @@
using namespace OpenRCT2; using namespace OpenRCT2;
using namespace OpenRCT2::Audio; using namespace OpenRCT2::Audio;
constexpr size_t MAX_RIDE_MUSIC_CHANNELS = 32; namespace OpenRCT2::RideAudio
{
constexpr uint8_t TUNE_ID_NULL = 0xFF;
constexpr size_t MAX_RIDE_MUSIC_CHANNELS = 32;
/** /**
* Represents a particular instance of ride music that can be heard in a viewport. * Represents a particular instance of ride music that can be heard in a viewport.
* These are created each frame via enumerating each ride / viewport. * These are created each frame via enumerating each ride / viewport.
*/ */
struct ViewportRideMusicInstance struct ViewportRideMusicInstance
{ {
ride_id_t RideId; ride_id_t RideId;
uint8_t TrackIndex{}; uint8_t TrackIndex{};
@ -39,13 +42,13 @@ struct ViewportRideMusicInstance
int16_t Volume{}; int16_t Volume{};
int16_t Pan{}; int16_t Pan{};
uint16_t Frequency{}; uint16_t Frequency{};
}; };
/** /**
* Represents an audio channel to play a particular ride's music track. * Represents an audio channel to play a particular ride's music track.
*/ */
struct RideMusicChannel struct RideMusicChannel
{ {
ride_id_t RideId{}; ride_id_t RideId{};
uint8_t TrackIndex{}; uint8_t TrackIndex{};
@ -155,23 +158,23 @@ struct RideMusicChannel
} }
} }
} }
}; };
static std::vector<ViewportRideMusicInstance> _musicInstances; static std::vector<ViewportRideMusicInstance> _musicInstances;
static std::vector<RideMusicChannel> _musicChannels; static std::vector<RideMusicChannel> _musicChannels;
void RideAudioStopAllChannels() void StopAllChannels()
{ {
_musicChannels.clear(); _musicChannels.clear();
} }
void RideAudioClearAllViewportInstances() void ClearAllViewportInstances()
{ {
_musicInstances.clear(); _musicInstances.clear();
} }
static void StartRideMusicChannel(const ViewportRideMusicInstance& instance) static void StartRideMusicChannel(const ViewportRideMusicInstance& instance)
{ {
// Create new music channel // Create new music channel
auto ride = get_ride(instance.RideId); auto ride = get_ride(instance.RideId);
if (ride->type == RIDE_TYPE_CIRCUS) if (ride->type == RIDE_TYPE_CIRCUS)
@ -203,10 +206,10 @@ static void StartRideMusicChannel(const ViewportRideMusicInstance& instance)
} }
} }
} }
} }
static void StopInactiveRideMusicChannels() static void StopInactiveRideMusicChannels()
{ {
_musicChannels.erase( _musicChannels.erase(
std::remove_if( std::remove_if(
_musicChannels.begin(), _musicChannels.end(), _musicChannels.begin(), _musicChannels.end(),
@ -224,10 +227,10 @@ static void StopInactiveRideMusicChannels()
} }
}), }),
_musicChannels.end()); _musicChannels.end());
} }
static void UpdateRideMusicChannelForMusicParams(const ViewportRideMusicInstance& instance) static void UpdateRideMusicChannelForMusicParams(const ViewportRideMusicInstance& instance)
{ {
// Find existing music channel // Find existing music channel
auto foundChannel = std::find_if( auto foundChannel = std::find_if(
_musicChannels.begin(), _musicChannels.end(), [&instance](const RideMusicChannel& channel) { _musicChannels.begin(), _musicChannels.end(), [&instance](const RideMusicChannel& channel) {
@ -242,13 +245,13 @@ static void UpdateRideMusicChannelForMusicParams(const ViewportRideMusicInstance
{ {
StartRideMusicChannel(instance); StartRideMusicChannel(instance);
} }
} }
/** /**
* Start, update and stop audio channels for each ride music instance that can be heard across all viewports. * Start, update and stop audio channels for each ride music instance that can be heard across all viewports.
*/ */
void RideUpdateMusicChannels() void UpdateMusicChannels()
{ {
if ((gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) != 0 || (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO) != 0) if ((gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) != 0 || (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO) != 0)
return; return;
@ -261,10 +264,10 @@ void RideUpdateMusicChannels()
{ {
UpdateRideMusicChannelForMusicParams(instance); UpdateRideMusicChannelForMusicParams(instance);
} }
} }
static std::pair<size_t, size_t> RideMusicGetTrackOffsetLength(const Ride& ride) static std::pair<size_t, size_t> RideMusicGetTrackOffsetLength(const Ride& ride)
{ {
if (ride.type == RIDE_TYPE_CIRCUS) if (ride.type == RIDE_TYPE_CIRCUS)
{ {
return { 1378, 12427456 }; return { 1378, 12427456 };
@ -284,10 +287,10 @@ static std::pair<size_t, size_t> RideMusicGetTrackOffsetLength(const Ride& ride)
} }
} }
return { 0, 0 }; return { 0, 0 };
} }
static void RideUpdateMusicPosition(Ride& ride) static void RideUpdateMusicPosition(Ride& ride)
{ {
auto [trackOffset, trackLength] = RideMusicGetTrackOffsetLength(ride); auto [trackOffset, trackLength] = RideMusicGetTrackOffsetLength(ride);
auto position = ride.music_position + trackOffset; auto position = ride.music_position + trackOffset;
if (position < trackLength) if (position < trackLength)
@ -299,10 +302,11 @@ static void RideUpdateMusicPosition(Ride& ride)
ride.music_tune_id = TUNE_ID_NULL; ride.music_tune_id = TUNE_ID_NULL;
ride.music_position = 0; ride.music_position = 0;
} }
} }
static void RideUpdateMusicPosition(Ride& ride, size_t offset, size_t length, int16_t volume, int16_t pan, uint16_t sampleRate) static void RideUpdateMusicPosition(
{ Ride& ride, size_t offset, size_t length, int16_t volume, int16_t pan, uint16_t sampleRate)
{
if (offset < length) if (offset < length)
{ {
if (_musicInstances.size() < MAX_RIDE_MUSIC_CHANNELS) if (_musicInstances.size() < MAX_RIDE_MUSIC_CHANNELS)
@ -322,10 +326,10 @@ static void RideUpdateMusicPosition(Ride& ride, size_t offset, size_t length, in
ride.music_tune_id = TUNE_ID_NULL; ride.music_tune_id = TUNE_ID_NULL;
ride.music_position = 0; ride.music_position = 0;
} }
} }
static void RideUpdateMusicPosition(Ride& ride, int16_t volume, int16_t pan, uint16_t sampleRate) static void RideUpdateMusicPosition(Ride& ride, int16_t volume, int16_t pan, uint16_t sampleRate)
{ {
auto foundChannel = std::find_if(_musicChannels.begin(), _musicChannels.end(), [&ride](const auto& channel) { auto foundChannel = std::find_if(_musicChannels.begin(), _musicChannels.end(), [&ride](const auto& channel) {
return channel.RideId == ride.id && channel.TrackIndex == ride.music_tune_id; return channel.RideId == ride.id && channel.TrackIndex == ride.music_tune_id;
}); });
@ -352,10 +356,10 @@ static void RideUpdateMusicPosition(Ride& ride, int16_t volume, int16_t pan, uin
auto newOffset = ride.music_position + trackOffset; auto newOffset = ride.music_position + trackOffset;
RideUpdateMusicPosition(ride, newOffset, trackLength, volume, pan, sampleRate); RideUpdateMusicPosition(ride, newOffset, trackLength, volume, pan, sampleRate);
} }
} }
static uint8_t CalculateVolume(int32_t pan) static uint8_t CalculateVolume(int32_t pan)
{ {
uint8_t result = 255; uint8_t result = 255;
int32_t v = std::min(std::abs(pan), 6143) - 2048; int32_t v = std::min(std::abs(pan), 6143) - 2048;
if (v > 0) if (v > 0)
@ -364,13 +368,13 @@ static uint8_t CalculateVolume(int32_t pan)
result = static_cast<uint8_t>(std::clamp(v, 0, 255)); result = static_cast<uint8_t>(std::clamp(v, 0, 255));
} }
return result; return result;
} }
/** /**
* Register an instance of audible ride music for this frame at the given coordinates. * Register an instance of audible ride music for this frame at the given coordinates.
*/ */
void RideUpdateMusicInstance(Ride& ride, const CoordsXYZ& rideCoords, uint16_t sampleRate) void UpdateMusicInstance(Ride& ride, const CoordsXYZ& rideCoords, uint16_t sampleRate)
{ {
if (!(gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) && !gGameSoundsOff && g_music_tracking_viewport != nullptr) if (!(gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) && !gGameSoundsOff && g_music_tracking_viewport != nullptr)
{ {
auto rotatedCoords = translate_3d_to_2d_with_z(get_current_rotation(), rideCoords); auto rotatedCoords = translate_3d_to_2d_with_z(get_current_rotation(), rideCoords);
@ -419,4 +423,5 @@ void RideUpdateMusicInstance(Ride& ride, const CoordsXYZ& rideCoords, uint16_t s
} }
} }
} }
} }
} // namespace OpenRCT2::RideAudio

View File

@ -14,9 +14,10 @@
struct CoordsXYZ; struct CoordsXYZ;
struct Ride; struct Ride;
constexpr uint8_t TUNE_ID_NULL = 0xFF; namespace OpenRCT2::RideAudio
{
void RideAudioClearAllViewportInstances(); void ClearAllViewportInstances();
void RideAudioStopAllChannels(); void StopAllChannels();
void RideUpdateMusicChannels(); void UpdateMusicChannels();
void RideUpdateMusicInstance(Ride& ride, const CoordsXYZ& rideCoords, uint16_t sampleRate); void UpdateMusicInstance(Ride& ride, const CoordsXYZ& rideCoords, uint16_t sampleRate);
} // namespace OpenRCT2::RideAudio