Add opus codec test

This commit is contained in:
dijunkun
2023-11-21 22:30:25 -08:00
parent 19506af831
commit e44c5b1cc7
12 changed files with 368 additions and 2 deletions

View File

@@ -0,0 +1,39 @@
#include "OpusDecoderImpl.h"
#define MAX_FRAME_SIZE 6 * 960
#define CHANNELS 2
OpusDecoderImpl::OpusDecoderImpl(int sampleRate, int channel) {
int err;
decoder = opus_decoder_create(sampleRate, channel, &err);
opus_decoder_ctl(decoder, OPUS_SET_LSB_DEPTH(16));
sample_rate = sample_rate;
channel_num = channel;
if (err < 0 || decoder == NULL) {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>\n");
return;
}
pcm_file = fopen("decode.pcm", "wb+");
}
bool OpusDecoderImpl::Decode(unsigned char* in_data, int len) {
unsigned char pcm_bytes[MAX_FRAME_SIZE * CHANNELS * 2];
opus_int16 out[MAX_FRAME_SIZE * CHANNELS];
auto frame_size = opus_decode(decoder, in_data, len, out, MAX_FRAME_SIZE, 0);
if (frame_size < 0) {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>\n");
return false;
}
for (auto i = 0; i < channel_num * frame_size; i++) {
pcm_bytes[2 * i] = out[i] & 0xFF;
pcm_bytes[2 * i + 1] = (out[i] >> 8) & 0xFF;
}
fwrite(pcm_bytes, sizeof(short), frame_size * channel_num, pcm_file);
fflush(pcm_file);
return true;
}
OpusDecoderImpl::~OpusDecoderImpl() {}