Files
OpenRCT2-Web/src/openrct2/object/MusicObject.cpp
T

230 lines
6.4 KiB
C++
Raw Normal View History

2021-01-09 20:54:02 +00:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
2021-01-09 20:54:02 +00:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "MusicObject.h"
2022-09-18 16:15:42 +01:00
#include "../AssetPackManager.h"
2021-01-09 20:54:02 +00:00
#include "../Context.h"
#include "../OpenRCT2.h"
#include "../PlatformEnvironment.h"
2022-05-14 03:05:03 +01:00
#include "../audio/AudioContext.h"
#include "../audio/AudioSource.h"
2021-01-09 20:54:02 +00:00
#include "../core/IStream.hpp"
#include "../core/Json.hpp"
#include "../core/Path.hpp"
2022-10-25 21:44:58 +02:00
#include "../drawing/Image.h"
2021-01-09 20:54:02 +00:00
#include "../localisation/StringIds.h"
#include "../ride/Ride.h"
#include "RideObject.h"
#include <memory>
using namespace OpenRCT2;
2022-09-18 16:15:42 +01:00
using namespace OpenRCT2::Audio;
2021-01-09 20:54:02 +00:00
constexpr size_t DEFAULT_BYTES_PER_TICK = 1378;
void MusicObject::Load()
{
GetStringTable().Sort();
NameStringId = LanguageAllocateObjectString(GetName());
2021-01-09 20:54:02 +00:00
2022-09-18 16:15:42 +01:00
// Start with base images
_loadedSampleTable.LoadFrom(_sampleTable, 0, _sampleTable.GetCount());
// Override samples from asset packs
auto context = GetContext();
auto assetManager = context->GetAssetPackManager();
if (assetManager != nullptr)
{
assetManager->LoadSamplesForObject(GetIdentifier(), _loadedSampleTable);
}
// Load metadata of samples
2022-05-14 03:05:03 +01:00
auto audioContext = GetContext()->GetAudioContext();
2021-01-09 20:54:02 +00:00
for (auto& track : _tracks)
{
2022-05-14 03:05:03 +01:00
auto stream = track.Asset.GetStream();
if (stream != nullptr)
{
auto source = audioContext->CreateStreamFromWAV(std::move(stream));
if (source != nullptr)
{
track.BytesPerTick = source->GetBytesPerSecond() / 40;
track.Size = source->GetLength();
source->Release();
}
else
{
track.BytesPerTick = DEFAULT_BYTES_PER_TICK;
track.Size = track.Asset.GetSize();
}
}
else
{
track.BytesPerTick = DEFAULT_BYTES_PER_TICK;
track.Size = track.Asset.GetSize();
}
2021-01-09 20:54:02 +00:00
}
2022-10-25 21:44:58 +02:00
_hasPreview = !!GetImageTable().GetCount();
_previewImageId = GfxObjectAllocateImages(GetImageTable().GetImages(), GetImageTable().GetCount());
2021-01-09 20:54:02 +00:00
}
void MusicObject::Unload()
{
LanguageFreeObjectString(NameStringId);
GfxObjectFreeImages(_previewImageId, GetImageTable().GetCount());
_hasPreview = false;
_previewImageId = 0;
2021-01-09 20:54:02 +00:00
NameStringId = 0;
}
void MusicObject::DrawPreview(DrawPixelInfo* dpi, int32_t width, int32_t height) const
2021-01-09 20:54:02 +00:00
{
// Write (no image)
int32_t x = width / 2;
int32_t y = height / 2;
2022-10-25 21:44:58 +02:00
if (_hasPreview)
GfxDrawSprite(dpi, ImageId(_previewImageId), { 0, 0 });
2022-10-25 21:44:58 +02:00
else
DrawTextBasic(dpi, { x, y }, STR_WINDOW_NO_IMAGE, {}, { TextAlignment::CENTRE });
2021-01-09 20:54:02 +00:00
}
void MusicObject::ReadJson(IReadObjectContext* context, json_t& root)
{
_originalStyleId = {};
_rideTypes.clear();
_tracks.clear();
2022-01-17 15:37:38 +01:00
_niceFactor = MusicNiceFactor::Neutral;
2021-01-09 20:54:02 +00:00
auto& properties = root["properties"];
if (properties != nullptr)
{
const auto& originalStyleId = properties["originalStyleId"];
if (originalStyleId.is_number_integer())
{
_originalStyleId = originalStyleId.get<uint8_t>();
}
2022-01-17 15:37:38 +01:00
const auto& niceFactor = properties["niceFactor"];
if (niceFactor.is_number_integer())
{
_niceFactor = static_cast<MusicNiceFactor>(std::clamp<int8_t>(niceFactor.get<int8_t>(), -1, 1));
}
2021-01-09 20:54:02 +00:00
const auto& jRideTypes = properties["rideTypes"];
if (jRideTypes.is_array())
{
ParseRideTypes(jRideTypes);
}
auto& jTracks = properties["tracks"];
if (jTracks.is_array())
{
ParseTracks(*context, jTracks);
}
}
PopulateTablesFromJson(context, root);
}
void MusicObject::ParseRideTypes(const json_t& jRideTypes)
{
for (const auto& jRideType : jRideTypes)
{
auto szRideType = Json::GetString(jRideType);
if (!szRideType.empty())
{
auto rideType = RideObject::ParseRideType(szRideType);
if (rideType != RIDE_TYPE_NULL)
{
_rideTypes.push_back(rideType);
}
}
}
}
void MusicObject::ParseTracks(IReadObjectContext& context, json_t& jTracks)
{
2022-09-18 16:15:42 +01:00
auto& entries = _sampleTable.GetEntries();
2021-01-09 20:54:02 +00:00
for (auto& jTrack : jTracks)
{
if (jTrack.is_object())
{
MusicObjectTrack track;
track.Name = Json::GetString(jTrack["name"]);
track.Composer = Json::GetString(jTrack["composer"]);
2021-01-09 20:54:02 +00:00
auto source = Json::GetString(jTrack["source"]);
if (source.empty())
{
context.LogError(ObjectError::InvalidProperty, "Invalid audio track definition.");
}
else
{
2022-09-18 16:15:42 +01:00
auto asset = GetAsset(context, source);
auto& entry = entries.emplace_back();
entry.Asset = asset;
track.Asset = asset;
2021-01-09 20:54:02 +00:00
_tracks.push_back(std::move(track));
}
}
}
}
std::optional<uint8_t> MusicObject::GetOriginalStyleId() const
{
return _originalStyleId;
}
bool MusicObject::SupportsRideType(ride_type_t rideType)
2021-01-09 20:54:02 +00:00
{
if (_rideTypes.size() == 0)
{
// Default behaviour for music is to only exclude from merry-go-round
return rideType != RIDE_TYPE_MERRY_GO_ROUND;
}
2021-09-15 22:22:15 +02:00
auto it = std::find(_rideTypes.begin(), _rideTypes.end(), rideType);
return it != _rideTypes.end();
2021-01-09 20:54:02 +00:00
}
size_t MusicObject::GetTrackCount() const
{
return _tracks.size();
}
const MusicObjectTrack* MusicObject::GetTrack(size_t trackIndex) const
{
if (_tracks.size() > trackIndex)
{
return &_tracks[trackIndex];
}
return {};
}
2022-09-18 16:15:42 +01:00
IAudioSource* MusicObject::GetTrackSample(size_t trackIndex) const
{
return _loadedSampleTable.LoadSample(static_cast<uint32_t>(trackIndex));
}
2021-01-09 20:54:02 +00:00
ObjectAsset MusicObject::GetAsset(IReadObjectContext& context, std::string_view path)
{
if (path.find("$RCT2:DATA/") == 0)
{
2022-08-16 20:01:40 +01:00
auto env = GetContext()->GetPlatformEnvironment();
auto path2 = env->FindFile(DIRBASE::RCT2, DIRID::DATA, path.substr(11));
2021-01-09 20:54:02 +00:00
return ObjectAsset(path2);
}
2021-09-15 22:22:15 +02:00
return context.GetAsset(path);
2021-01-09 20:54:02 +00:00
}