Files
OpenRCT2-Web/src/openrct2-ui/audio/MemoryAudioSource.cpp
T

240 lines
7.2 KiB
C++
Raw Normal View History

2017-03-28 19:58:15 +01:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 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"
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>
#include <openrct2/common.h>
#include <vector>
2018-02-16 00:43:16 +01:00
namespace OpenRCT2::Audio
2017-03-28 19:58:15 +01:00
{
/**
* An audio source where raw PCM data is initially loaded into RAM from
* a file and then streamed.
*/
2017-03-29 17:46:02 +01:00
class MemoryAudioSource final : public ISDLAudioSource
2017-03-28 19:58:15 +01:00
{
private:
2018-06-22 23:18:51 +02:00
AudioFormat _format = {};
std::vector<uint8_t> _data;
uint8_t* _dataSDL = nullptr;
size_t _length = 0;
2018-01-19 00:09:42 +00:00
2018-06-22 23:18:51 +02:00
const uint8_t* GetData()
2018-01-19 00:09:42 +00:00
{
return _dataSDL != nullptr ? _dataSDL : _data.data();
}
2017-03-28 19:58:15 +01:00
public:
~MemoryAudioSource() override
2017-03-28 19:58:15 +01:00
{
Unload();
}
[[nodiscard]] uint64_t GetLength() const override
2017-03-28 19:58:15 +01:00
{
return _length;
}
[[nodiscard]] AudioFormat GetFormat() const override
2017-03-28 19:58:15 +01:00
{
return _format;
}
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;
if (offset < _length)
{
2020-03-28 15:19:48 -03:00
bytesToRead = static_cast<size_t>(std::min<uint64_t>(len, _length - offset));
2018-01-19 00:09:42 +00:00
auto src = GetData();
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;
}
2018-06-22 23:18:51 +02:00
bool LoadWAV(const utf8* path)
2017-03-28 19:58:15 +01:00
{
log_verbose("MemoryAudioSource::LoadWAV(%s)", path);
Unload();
bool result = false;
2018-06-22 23:18:51 +02:00
SDL_RWops* rw = SDL_RWFromFile(path, "rb");
2017-03-28 19:58:15 +01:00
if (rw != nullptr)
{
SDL_AudioSpec audiospec = {};
uint32_t audioLen;
2018-06-22 23:18:51 +02:00
SDL_AudioSpec* spec = SDL_LoadWAV_RW(rw, false, &audiospec, &_dataSDL, &audioLen);
2017-03-28 19:58:15 +01:00
if (spec != nullptr)
{
_format.freq = spec->freq;
_format.format = spec->format;
_format.channels = spec->channels;
_length = audioLen;
result = true;
}
else
{
log_verbose("Error loading %s, unsupported WAV format", path);
}
SDL_RWclose(rw);
}
else
{
log_verbose("Error loading %s", path);
}
return result;
}
2018-06-22 23:18:51 +02:00
bool LoadCSS1(const utf8* path, size_t index)
2017-03-28 19:58:15 +01:00
{
log_verbose("MemoryAudioSource::LoadCSS1(%s, %d)", path, index);
Unload();
bool result = false;
2018-06-22 23:18:51 +02:00
SDL_RWops* rw = SDL_RWFromFile(path, "rb");
2017-03-28 19:58:15 +01:00
if (rw != nullptr)
{
2021-01-06 23:29:40 +02:00
uint32_t numSounds{};
2017-03-28 19:58:15 +01:00
SDL_RWread(rw, &numSounds, sizeof(numSounds), 1);
if (index < numSounds)
{
SDL_RWseek(rw, index * 4, RW_SEEK_CUR);
2021-01-06 23:29:40 +02:00
uint32_t pcmOffset{};
2017-03-28 19:58:15 +01:00
SDL_RWread(rw, &pcmOffset, sizeof(pcmOffset), 1);
SDL_RWseek(rw, pcmOffset, RW_SEEK_SET);
2021-01-06 23:29:40 +02:00
uint32_t pcmSize{};
2017-03-28 19:58:15 +01:00
SDL_RWread(rw, &pcmSize, sizeof(pcmSize), 1);
_length = pcmSize;
WaveFormatEx waveFormat{};
2017-03-28 19:58:15 +01:00
SDL_RWread(rw, &waveFormat, sizeof(waveFormat), 1);
_format.freq = waveFormat.frequency;
_format.format = AUDIO_S16LSB;
_format.channels = waveFormat.channels;
2018-01-19 00:09:42 +00:00
try
2017-03-28 19:58:15 +01:00
{
2018-01-19 00:09:42 +00:00
_data.resize(_length);
SDL_RWread(rw, _data.data(), _length, 1);
2017-03-28 19:58:15 +01:00
result = true;
}
2018-06-22 23:18:51 +02:00
catch (const std::bad_alloc&)
2017-03-28 19:58:15 +01:00
{
log_verbose("Unable to allocate data");
}
}
SDL_RWclose(rw);
}
else
{
log_verbose("Unable to load %s", path);
}
return result;
}
2018-06-22 23:18:51 +02:00
bool Convert(const AudioFormat* format)
2017-03-28 19:58:15 +01:00
{
if (*format != _format)
{
SDL_AudioCVT cvt;
2018-06-22 23:18:51 +02:00
if (SDL_BuildAudioCVT(
&cvt, _format.format, _format.channels, _format.freq, format->format, format->channels, format->freq)
>= 0)
2017-03-28 19:58:15 +01:00
{
2018-01-19 00:09:42 +00:00
auto src = GetData();
auto cvtBuffer = std::vector<uint8_t>(_length * cvt.len_mult);
2018-01-19 00:09:42 +00:00
std::copy_n(src, _length, cvtBuffer.data());
2020-03-28 15:19:48 -03:00
cvt.len = static_cast<int32_t>(_length);
2018-01-19 00:09:42 +00:00
cvt.buf = cvtBuffer.data();
2017-03-28 19:58:15 +01:00
if (SDL_ConvertAudio(&cvt) >= 0)
{
2018-01-19 00:09:42 +00:00
cvtBuffer.resize(cvt.len_cvt);
2017-03-28 19:58:15 +01:00
Unload();
2018-01-19 00:09:42 +00:00
_data = std::move(cvtBuffer);
2017-03-28 19:58:15 +01:00
_length = cvt.len_cvt;
_format = *format;
return true;
}
}
}
return false;
}
private:
void Unload()
{
2018-01-19 00:09:42 +00:00
// Free our data
_data.clear();
_data.shrink_to_fit();
// Free SDL2's data
SDL_FreeWAV(_dataSDL);
_dataSDL = nullptr;
2017-03-28 19:58:15 +01:00
_length = 0;
}
};
2018-06-22 23:18:51 +02:00
IAudioSource* AudioSource::CreateMemoryFromCSS1(const std::string& path, size_t index, const AudioFormat* targetFormat)
2017-03-28 19:58:15 +01:00
{
auto source = new MemoryAudioSource();
if (source->LoadCSS1(path.c_str(), index))
{
2018-02-25 02:40:36 +00:00
if (targetFormat != nullptr && source->GetFormat() != *targetFormat)
2017-03-28 19:58:15 +01:00
{
if (!source->Convert(targetFormat))
{
delete source;
source = nullptr;
}
}
}
else
{
delete source;
source = nullptr;
}
return source;
}
2018-06-22 23:18:51 +02:00
IAudioSource* AudioSource::CreateMemoryFromWAV(const std::string& path, const AudioFormat* targetFormat)
2017-03-28 19:58:15 +01:00
{
auto source = new MemoryAudioSource();
if (source->LoadWAV(path.c_str()))
{
2018-02-25 02:40:36 +00:00
if (targetFormat != nullptr && source->GetFormat() != *targetFormat)
2017-03-28 19:58:15 +01:00
{
if (!source->Convert(targetFormat))
{
SafeDelete(source);
}
}
}
else
{
delete source;
source = nullptr;
}
return source;
}
2018-05-04 22:40:09 +02:00
} // namespace OpenRCT2::Audio