2017-03-28 19:58:15 +01:00
|
|
|
/*****************************************************************************
|
2024-12-31 20:30:04 +01:00
|
|
|
* Copyright (c) 2014-2025 OpenRCT2 developers
|
2017-03-28 19:58:15 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2017-03-28 19:58:15 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2017-03-28 19:58:15 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "AudioContext.h"
|
|
|
|
|
#include "AudioFormat.h"
|
2022-05-14 18:21:48 +01:00
|
|
|
#include "SDLAudioSource.h"
|
2017-03-28 19:58:15 +01:00
|
|
|
|
2019-01-02 21:23:58 +01:00
|
|
|
#include <SDL.h>
|
2018-06-22 23:18:51 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <openrct2/audio/AudioSource.h>
|
2022-06-27 17:41:53 +01:00
|
|
|
#include <stdexcept>
|
2018-06-22 23:18:51 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
2018-02-16 00:43:16 +01:00
|
|
|
namespace OpenRCT2::Audio
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
|
|
|
|
/**
|
2022-05-14 18:21:48 +01:00
|
|
|
* An audio source where raw PCM data is stored in RAM.
|
2017-03-28 19:58:15 +01:00
|
|
|
*/
|
2022-05-14 18:21:48 +01:00
|
|
|
class MemoryAudioSource final : public SDLAudioSource
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
|
|
|
|
private:
|
2018-06-22 23:18:51 +02:00
|
|
|
AudioFormat _format = {};
|
|
|
|
|
std::vector<uint8_t> _data;
|
2017-03-28 19:58:15 +01:00
|
|
|
|
|
|
|
|
public:
|
2022-05-14 18:21:48 +01:00
|
|
|
MemoryAudioSource(const AudioFormat& format, std::vector<uint8_t>&& pcmData)
|
|
|
|
|
: _format(format)
|
|
|
|
|
, _data(pcmData)
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 18:36:45 +02:00
|
|
|
[[nodiscard]] AudioFormat GetFormat() const override
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
|
|
|
|
return _format;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-14 18:21:48 +01:00
|
|
|
[[nodiscard]] uint64_t GetLength() const override
|
|
|
|
|
{
|
|
|
|
|
return _data.size();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:18:51 +02:00
|
|
|
size_t Read(void* dst, uint64_t offset, size_t len) override
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
|
|
|
|
size_t bytesToRead = 0;
|
2022-05-14 18:21:48 +01:00
|
|
|
if (offset < _data.size())
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
2022-05-14 18:21:48 +01:00
|
|
|
bytesToRead = static_cast<size_t>(std::min<uint64_t>(len, _data.size() - offset));
|
2018-01-19 00:09:42 +00:00
|
|
|
|
2022-05-14 18:21:48 +01:00
|
|
|
auto src = _data.data();
|
2018-01-19 00:09:42 +00:00
|
|
|
if (src != nullptr)
|
|
|
|
|
{
|
2020-03-28 15:19:48 -03:00
|
|
|
std::copy_n(src + offset, bytesToRead, reinterpret_cast<uint8_t*>(dst));
|
2018-01-19 00:09:42 +00:00
|
|
|
}
|
2017-03-28 19:58:15 +01:00
|
|
|
}
|
|
|
|
|
return bytesToRead;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-14 18:21:48 +01:00
|
|
|
protected:
|
|
|
|
|
void Unload() override
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
2018-01-19 00:09:42 +00:00
|
|
|
_data.clear();
|
|
|
|
|
_data.shrink_to_fit();
|
2017-03-28 19:58:15 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-14 18:21:48 +01:00
|
|
|
static bool ConvertPcmData(const AudioFormat& target, const AudioFormat& src, std::vector<uint8_t>& pcmData)
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
2022-05-14 18:21:48 +01:00
|
|
|
if (target != src)
|
2022-05-09 18:34:38 +01:00
|
|
|
{
|
2022-05-14 18:21:48 +01:00
|
|
|
SDL_AudioCVT cvt;
|
|
|
|
|
if (SDL_BuildAudioCVT(&cvt, src.format, src.channels, src.freq, target.format, target.channels, target.freq) >= 0)
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
2022-05-14 18:21:48 +01:00
|
|
|
auto srcData = pcmData.data();
|
|
|
|
|
auto srcLen = pcmData.size();
|
|
|
|
|
auto cvtBuffer = std::vector<uint8_t>(srcLen * cvt.len_mult);
|
|
|
|
|
std::copy_n(srcData, srcLen, cvtBuffer.data());
|
|
|
|
|
cvt.len = static_cast<int32_t>(srcLen);
|
|
|
|
|
cvt.buf = cvtBuffer.data();
|
|
|
|
|
if (SDL_ConvertAudio(&cvt) >= 0)
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
2022-05-14 18:21:48 +01:00
|
|
|
cvtBuffer.resize(cvt.len_cvt);
|
|
|
|
|
cvtBuffer.shrink_to_fit();
|
|
|
|
|
pcmData = std::move(cvtBuffer);
|
|
|
|
|
return true;
|
2017-03-28 19:58:15 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-14 18:21:48 +01:00
|
|
|
return true;
|
2017-03-28 19:58:15 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-14 18:21:48 +01:00
|
|
|
std::unique_ptr<SDLAudioSource> CreateMemoryAudioSource(
|
|
|
|
|
const AudioFormat& target, const AudioFormat& src, std::vector<uint8_t>&& pcmData)
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
2022-05-14 18:21:48 +01:00
|
|
|
if (!ConvertPcmData(target, src, pcmData))
|
|
|
|
|
throw std::runtime_error("Unable to convert PCM data to target format");
|
|
|
|
|
|
|
|
|
|
return std::make_unique<MemoryAudioSource>(target, std::move(pcmData));
|
2017-03-28 19:58:15 +01:00
|
|
|
}
|
2018-05-04 22:40:09 +02:00
|
|
|
} // namespace OpenRCT2::Audio
|