mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-09 13:07:03 +03:00
46 lines
1.1 KiB
C++
46 lines
1.1 KiB
C++
#include "OPUSDecoder.h"
|
|
#include "opus.h"
|
|
|
|
using namespace bell;
|
|
|
|
#define MAX_FRAME_SIZE 6 * 960
|
|
#define MAX_CHANNELS 2
|
|
|
|
// dummy structure, just to get access to channels
|
|
struct OpusDecoder {
|
|
int dummy1;
|
|
int dummy2;
|
|
int channels;
|
|
};
|
|
|
|
OPUSDecoder::OPUSDecoder() {
|
|
opus = nullptr;
|
|
pcmData = (int16_t*)malloc(MAX_FRAME_SIZE * MAX_CHANNELS * sizeof(int16_t));
|
|
}
|
|
|
|
OPUSDecoder::~OPUSDecoder() {
|
|
if (opus)
|
|
opus_decoder_destroy(opus);
|
|
free(pcmData);
|
|
}
|
|
|
|
bool OPUSDecoder::setup(uint32_t sampleRate, uint8_t channelCount,
|
|
uint8_t bitDepth) {
|
|
if (opus)
|
|
opus_decoder_destroy(opus);
|
|
opus = opus_decoder_create((int32_t)sampleRate, channelCount, &lastErrno);
|
|
return !lastErrno;
|
|
}
|
|
|
|
uint8_t* OPUSDecoder::decode(uint8_t* inData, uint32_t& inLen,
|
|
uint32_t& outLen) {
|
|
if (!inData)
|
|
return nullptr;
|
|
outLen =
|
|
opus_decode(opus, static_cast<unsigned char*>(inData),
|
|
static_cast<int32_t>(inLen), pcmData, MAX_FRAME_SIZE, false);
|
|
outLen *= opus->channels * sizeof(int16_t);
|
|
inLen = 0;
|
|
return (uint8_t*)pcmData;
|
|
}
|