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-08-12 12:50:40 +01:00
|
|
|
#include <algorithm>
|
2018-06-22 23:18:51 +02:00
|
|
|
#include <openrct2/audio/AudioSource.h>
|
|
|
|
|
#include <openrct2/common.h>
|
|
|
|
|
|
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 streamed directly from
|
|
|
|
|
* a file.
|
|
|
|
|
*/
|
2017-03-29 17:46:02 +01:00
|
|
|
class FileAudioSource final : public ISDLAudioSource
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
|
|
|
|
private:
|
2018-06-05 14:37:24 +02:00
|
|
|
AudioFormat _format = {};
|
2018-06-22 23:18:51 +02:00
|
|
|
SDL_RWops* _rw = nullptr;
|
|
|
|
|
uint64_t _dataBegin = 0;
|
|
|
|
|
uint64_t _dataLength = 0;
|
2017-03-28 19:58:15 +01:00
|
|
|
|
|
|
|
|
public:
|
2020-05-10 15:05:26 +02:00
|
|
|
~FileAudioSource() override
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
|
|
|
|
Unload();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-10 15:05:26 +02:00
|
|
|
[[nodiscard]] uint64_t GetLength() const override
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
|
|
|
|
return _dataLength;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-10 15:05:26 +02:00
|
|
|
[[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 bytesRead = 0;
|
2018-06-20 17:28:51 +02:00
|
|
|
int64_t currentPosition = SDL_RWtell(_rw);
|
2017-03-28 19:58:15 +01:00
|
|
|
if (currentPosition != -1)
|
|
|
|
|
{
|
2020-03-28 15:19:48 -03:00
|
|
|
size_t bytesToRead = static_cast<size_t>(std::min<uint64_t>(len, _dataLength - offset));
|
2018-06-20 17:28:51 +02:00
|
|
|
int64_t dataOffset = _dataBegin + offset;
|
2017-03-28 19:58:15 +01:00
|
|
|
if (currentPosition != dataOffset)
|
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int64_t newPosition = SDL_RWseek(_rw, dataOffset, SEEK_SET);
|
2017-03-28 19:58:15 +01:00
|
|
|
if (newPosition == -1)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bytesRead = SDL_RWread(_rw, dst, 1, bytesToRead);
|
|
|
|
|
}
|
|
|
|
|
return bytesRead;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:18:51 +02:00
|
|
|
bool LoadWAV(SDL_RWops* rw)
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
const uint32_t DATA = 0x61746164;
|
2018-06-22 23:18:51 +02:00
|
|
|
const uint32_t FMT = 0x20746D66;
|
2018-06-20 17:28:51 +02:00
|
|
|
const uint32_t RIFF = 0x46464952;
|
|
|
|
|
const uint32_t WAVE = 0x45564157;
|
|
|
|
|
const uint16_t pcmformat = 0x0001;
|
2017-03-28 19:58:15 +01:00
|
|
|
|
|
|
|
|
Unload();
|
|
|
|
|
|
|
|
|
|
if (rw == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_rw = rw;
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t chunkId = SDL_ReadLE32(rw);
|
2017-03-28 19:58:15 +01:00
|
|
|
if (chunkId != RIFF)
|
|
|
|
|
{
|
|
|
|
|
log_verbose("Not a WAV file");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read and discard chunk size
|
|
|
|
|
SDL_ReadLE32(rw);
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t chunkFormat = SDL_ReadLE32(rw);
|
2017-03-28 19:58:15 +01:00
|
|
|
if (chunkFormat != WAVE)
|
|
|
|
|
{
|
|
|
|
|
log_verbose("Not in WAVE format");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t fmtChunkSize = FindChunk(rw, FMT);
|
2017-03-28 19:58:15 +01:00
|
|
|
if (!fmtChunkSize)
|
|
|
|
|
{
|
|
|
|
|
log_verbose("Could not find FMT chunk");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint64_t chunkStart = SDL_RWtell(rw);
|
2017-03-28 19:58:15 +01:00
|
|
|
|
2020-05-10 15:05:26 +02:00
|
|
|
WaveFormat waveFormat{};
|
2017-03-28 19:58:15 +01:00
|
|
|
SDL_RWread(rw, &waveFormat, sizeof(waveFormat), 1);
|
|
|
|
|
SDL_RWseek(rw, chunkStart + fmtChunkSize, RW_SEEK_SET);
|
2018-06-22 23:18:51 +02:00
|
|
|
if (waveFormat.encoding != pcmformat)
|
|
|
|
|
{
|
2017-03-28 19:58:15 +01:00
|
|
|
log_verbose("Not in proper format");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_format.freq = waveFormat.frequency;
|
2018-06-22 23:18:51 +02:00
|
|
|
switch (waveFormat.bitspersample)
|
|
|
|
|
{
|
|
|
|
|
case 8:
|
|
|
|
|
_format.format = AUDIO_U8;
|
|
|
|
|
break;
|
|
|
|
|
case 16:
|
|
|
|
|
_format.format = AUDIO_S16LSB;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
log_verbose("Invalid bits per sample");
|
|
|
|
|
return false;
|
2017-03-28 19:58:15 +01:00
|
|
|
}
|
|
|
|
|
_format.channels = waveFormat.channels;
|
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t dataChunkSize = FindChunk(rw, DATA);
|
2017-03-28 19:58:15 +01:00
|
|
|
if (dataChunkSize == 0)
|
|
|
|
|
{
|
|
|
|
|
log_verbose("Could not find DATA chunk");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_dataLength = dataChunkSize;
|
|
|
|
|
_dataBegin = SDL_RWtell(rw);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2020-05-10 15:05:26 +02:00
|
|
|
static uint32_t FindChunk(SDL_RWops* rw, uint32_t wantedId)
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t subchunkId = SDL_ReadLE32(rw);
|
|
|
|
|
uint32_t subchunkSize = SDL_ReadLE32(rw);
|
2017-03-28 19:58:15 +01:00
|
|
|
if (subchunkId == wantedId)
|
|
|
|
|
{
|
|
|
|
|
return subchunkSize;
|
|
|
|
|
}
|
2018-06-20 17:28:51 +02:00
|
|
|
const uint32_t FACT = 0x74636166;
|
|
|
|
|
const uint32_t LIST = 0x5453494c;
|
|
|
|
|
const uint32_t BEXT = 0x74786562;
|
|
|
|
|
const uint32_t JUNK = 0x4B4E554A;
|
2017-03-28 19:58:15 +01:00
|
|
|
while (subchunkId == FACT || subchunkId == LIST || subchunkId == BEXT || subchunkId == JUNK)
|
|
|
|
|
{
|
|
|
|
|
SDL_RWseek(rw, subchunkSize, RW_SEEK_CUR);
|
|
|
|
|
subchunkId = SDL_ReadLE32(rw);
|
|
|
|
|
subchunkSize = SDL_ReadLE32(rw);
|
|
|
|
|
if (subchunkId == wantedId)
|
|
|
|
|
{
|
|
|
|
|
return subchunkSize;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Unload()
|
|
|
|
|
{
|
|
|
|
|
if (_rw != nullptr)
|
|
|
|
|
{
|
|
|
|
|
SDL_RWclose(_rw);
|
|
|
|
|
_rw = nullptr;
|
|
|
|
|
}
|
|
|
|
|
_dataBegin = 0;
|
|
|
|
|
_dataLength = 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-22 23:18:51 +02:00
|
|
|
IAudioSource* AudioSource::CreateStreamFromWAV(const std::string& path)
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
2018-06-22 23:18:51 +02:00
|
|
|
IAudioSource* source = nullptr;
|
2017-03-28 19:58:15 +01:00
|
|
|
SDL_RWops* rw = SDL_RWFromFile(path.c_str(), "rb");
|
|
|
|
|
if (rw != nullptr)
|
|
|
|
|
{
|
|
|
|
|
return AudioSource::CreateStreamFromWAV(rw);
|
|
|
|
|
}
|
|
|
|
|
return source;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 23:18:51 +02:00
|
|
|
IAudioSource* AudioSource::CreateStreamFromWAV(SDL_RWops* rw)
|
2017-03-28 19:58:15 +01:00
|
|
|
{
|
|
|
|
|
auto source = new FileAudioSource();
|
|
|
|
|
if (!source->LoadWAV(rw))
|
|
|
|
|
{
|
|
|
|
|
delete source;
|
|
|
|
|
source = nullptr;
|
|
|
|
|
}
|
|
|
|
|
return source;
|
|
|
|
|
}
|
2018-05-04 22:40:09 +02:00
|
|
|
} // namespace OpenRCT2::Audio
|