Merge pull request #225 from DrChat/audio_fix

Fix audio decoder - mono sounds are working now!
This commit is contained in:
Ben Vanik 2015-05-24 20:28:50 -07:00
commit 517dce01b1
2 changed files with 26 additions and 25 deletions

View File

@ -87,7 +87,7 @@ int AudioDecoder::Initialize(int bits) {
// Current frame stuff whatever // Current frame stuff whatever
// samples per frame * 2 max channels * output bytes // samples per frame * 2 max channels * output bytes
current_frame_ = new uint8_t[XMAContextData::kSamplesPerFrame * 2 * (bits / 8)]; current_frame_ = new uint8_t[XMAContextData::kSamplesPerFrame * 2 * (bits/8)];
current_frame_pos_ = 0; current_frame_pos_ = 0;
frame_samples_size_ = 0; frame_samples_size_ = 0;
@ -179,6 +179,7 @@ int AudioDecoder::DecodePacket(uint8_t* output, size_t output_offset, size_t out
// Successfully decoded a frame // Successfully decoded a frame
if (got_frame) { if (got_frame) {
// Validity checks.
if (decoded_frame_->nb_samples > XMAContextData::kSamplesPerFrame) { if (decoded_frame_->nb_samples > XMAContextData::kSamplesPerFrame) {
return -2; return -2;
} else if (context_->sample_fmt != AV_SAMPLE_FMT_FLTP) { } else if (context_->sample_fmt != AV_SAMPLE_FMT_FLTP) {
@ -193,24 +194,26 @@ int AudioDecoder::DecodePacket(uint8_t* output, size_t output_offset, size_t out
return -4; return -4;
} }
float* curSample = (float *)decoded_frame_->data[0]; // Output sample array
float* sample_array = (float *)decoded_frame_->data[0];
// Loop through every sample, convert and drop it into the output array // Loop through every sample, convert and drop it into the output array
for (int i = 0; i < decoded_frame_->nb_samples; i++) { for (int i = 0; i < decoded_frame_->nb_samples; i++) {
float fSample = curSample[i] * (1 << (bits_ - 1)); // Raw sample should be within [-1, 1]
// Weird problem: Sometimes the samples are outside [-1,1] float fRawSample = sample_array[i];
if (fSample >= 0) { float fScaledSample = fRawSample * (1 << (bits_ - 1));
fSample += 0.5f;
if (fSample > (1 << (bits_ - 1)) - 1) { // Clamp the sample in range
fSample = (float)(1 << (bits_ - 1)) - 1; int64_t range = (1 << (bits_ - 1));
} if (fScaledSample > (range - 1)) {
} else { fScaledSample = (float)range;
fSample -= 0.5f; } else if (fScaledSample < (-range + 1)) {
fScaledSample = (float)-range;
} }
// Convert the sample // Convert the sample and output it in big endian
int sample = (int)fSample; int sample = (int)fScaledSample;
for (uint32_t j = 0; j < sample_size; j++) { for (int32_t j = sample_size-1; j >= 0; j--) {
current_frame_[i * sample_size + j] = sample & 0xFF; current_frame_[i * sample_size + j] = sample & 0xFF;
sample >>= 8; sample >>= 8;
} }

View File

@ -246,8 +246,8 @@ void AudioSystem::DecoderThreadMain() {
data.input_buffer_1_block_count) * 2048; data.input_buffer_1_block_count) * 2048;
size_t input_offset = (data.input_buffer_read_offset / 8 - 4); size_t input_offset = (data.input_buffer_read_offset / 8 - 4);
size_t input_remaining = input_size - input_offset; size_t input_remaining = input_size - input_offset;
if (input_remaining == 0) { if (input_offset > input_size) {
// We're finished! // We're finished. Break.
break; break;
} }
@ -267,27 +267,24 @@ void AudioSystem::DecoderThreadMain() {
int read = context.decoder->DecodePacket(out, output_offset, int read = context.decoder->DecodePacket(out, output_offset,
output_remaining); output_remaining);
if (read < 0) { if (read < 0) {
// Sometimes the decoder will fail on a packet. I think it's
// looking for cross-packet frames and failing. If you run it again
// on the same packet it'll work though.
XELOGAPU("APU failed to decode packet (returned %.8X)", -read); XELOGAPU("APU failed to decode packet (returned %.8X)", -read);
context.decoder->DiscardPacket();
// TODO: Set error state continue;
break;
} }
if (read == 0) { if (read == 0) {
// Select sample rate. // Select sample rate.
int sample_rate = 0; int sample_rate = 0;
if (data.sample_rate == 0) { if (data.sample_rate == 0) {
// TODO: Test this
sample_rate = 24000; sample_rate = 24000;
} else if (data.sample_rate == 1) { } else if (data.sample_rate == 1) {
sample_rate = 32000; sample_rate = 32000;
} else if (data.sample_rate == 2) { } else if (data.sample_rate == 2) {
// TODO: Test this
sample_rate = 44100; sample_rate = 44100;
} else if (data.sample_rate == 3) { } else if (data.sample_rate == 3) {
// TODO: Test this
sample_rate = 48000; sample_rate = 48000;
} }
@ -300,13 +297,14 @@ void AudioSystem::DecoderThreadMain() {
// New packet time. // New packet time.
// TODO: Select input buffer 1 if necessary. // TODO: Select input buffer 1 if necessary.
auto packet = in0 + input_offset; auto packet = in0 + input_offset;
context.decoder->PreparePacket(packet, 2048, sample_rate, channels); context.decoder->PreparePacket(packet, 2048, sample_rate,
channels);
input_offset += 2048; input_offset += 2048;
} }
output_offset += read; output_offset += read;
// blah copy these back to the context // Copy the variables we changed back to the context.
data.input_buffer_read_offset = (input_offset + 4) * 8; data.input_buffer_read_offset = (input_offset + 4) * 8;
data.output_buffer_write_offset = output_offset / 256; data.output_buffer_write_offset = output_offset / 256;
} }