31 static constexpr size_t MIN_OPUS_FILE_SIZE = 57U;
35 static constexpr size_t DECODE_BUFFER_SAMPLES = 5760 * 2;
36 static constexpr size_t DECODE_BUFFER_BYTES = DECODE_BUFFER_SAMPLES *
sizeof(opus_int16);
38 bool Load(
SoundEntry &sound,
bool new_format, std::vector<std::byte> &data)
const override
40 if (!new_format)
return false;
43 if (sound.file_size < MIN_OPUS_FILE_SIZE)
return false;
46 auto filepos = sound.file->
GetPos();
47 std::vector<uint8_t> tmp(MIN_OPUS_FILE_SIZE);
48 sound.file->
ReadBlock(tmp.data(), tmp.size());
49 if (op_test(
nullptr, tmp.data(), tmp.size()) != 0)
return false;
52 tmp.resize(sound.file_size);
53 sound.file->
SeekTo(filepos, SEEK_SET);
54 sound.file->
ReadBlock(tmp.data(), tmp.size());
59 Debug(grf, 0,
"SoundLoader_Opus: Unable to open stream.");
65 data.resize(datapos + DECODE_BUFFER_BYTES);
68 int read = op_read(of.get(),
reinterpret_cast<opus_int16 *
>(&data[datapos]), DECODE_BUFFER_BYTES, &link_index);
72 Debug(grf, 0,
"SoundLoader_Opus: Unexpected end of stream.");
77 int channels = op_channel_count(of.get(), link_index);
79 Debug(grf, 0,
"SoundLoader_Opus: Unsupported channels {}, expected 1.", channels);
84 datapos += read *
sizeof(opus_int16);
93 data.resize(data.size() - DECODE_BUFFER_BYTES);
Helper for std::unique_ptr to use an arbitrary function as the deleter.
std::unique_ptr< T, DeleterFromFunc< Tfunc > > AutoRelease
Specialisation of std::unique_ptr for objects which must be deleted by calling a function.
void ReadBlock(void *ptr, size_t size)
Read a block.
size_t GetPos() const
Get position in the file.
void SeekTo(size_t pos, int mode)
Seek in the current file.
static constexpr uint16_t OPUS_SAMPLE_RATE
OpusFile always decodes at 48kHz.
static constexpr uint8_t OPUS_SAMPLE_BITS
OpusFile op_read() uses 16 bits per sample.
Base interface for a SoundLoader implementation.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Class related to random access to files.
A number of safeguards to prevent using unsafe methods.
Types related to sound loaders.
Definition of base types and functions in a cross-platform compatible way.