mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
Add opus codec test
This commit is contained in:
39
tests/opus/OpusDecoderImpl.cpp
Normal file
39
tests/opus/OpusDecoderImpl.cpp
Normal 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() {}
|
||||
Reference in New Issue
Block a user