28 static constexpr size_t MIN_OPUS_FILE_SIZE = 57U;
32 static constexpr size_t DECODE_BUFFER_SAMPLES = 5760 * 2;
33 static constexpr size_t DECODE_BUFFER_BYTES = DECODE_BUFFER_SAMPLES *
sizeof(opus_int16);
35 bool Load(
SoundEntry &sound,
bool new_format, std::vector<uint8_t> &data)
override
37 if (!new_format)
return false;
40 if (sound.file_size < MIN_OPUS_FILE_SIZE)
return false;
43 auto filepos = sound.file->
GetPos();
44 std::vector<uint8_t> tmp(MIN_OPUS_FILE_SIZE);
45 sound.file->
ReadBlock(tmp.data(), tmp.size());
46 if (op_test(
nullptr, tmp.data(), tmp.size()) != 0)
return false;
49 tmp.resize(sound.file_size);
50 sound.file->
SeekTo(filepos, SEEK_SET);
51 sound.file->
ReadBlock(tmp.data(), tmp.size());
54 auto of = std::unique_ptr<OggOpusFile, OggOpusFileDeleter>(op_open_memory(tmp.data(), tmp.size(), &error));
55 if (error != 0)
return false;
59 data.resize(datapos + DECODE_BUFFER_BYTES);
62 int read = op_read(of.get(),
reinterpret_cast<opus_int16 *
>(&data[datapos]), DECODE_BUFFER_BYTES, &link_index);
65 if (read < 0 || op_channel_count(of.get(), link_index) != 1) {
71 datapos += read *
sizeof(opus_int16);
80 data.resize(data.size() - DECODE_BUFFER_BYTES);
88 void operator()(OggOpusFile *of)
90 if (of !=
nullptr) op_free(of);