Merge pull request #225 from DrChat/audio_fix
Fix audio decoder - mono sounds are working now!
This commit is contained in:
commit
517dce01b1
|
@ -87,7 +87,7 @@ int AudioDecoder::Initialize(int bits) {
|
|||
|
||||
// Current frame stuff whatever
|
||||
// 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;
|
||||
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
|
||||
if (got_frame) {
|
||||
// Validity checks.
|
||||
if (decoded_frame_->nb_samples > XMAContextData::kSamplesPerFrame) {
|
||||
return -2;
|
||||
} 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;
|
||||
}
|
||||
|
||||
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
|
||||
for (int i = 0; i < decoded_frame_->nb_samples; i++) {
|
||||
float fSample = curSample[i] * (1 << (bits_ - 1));
|
||||
// Weird problem: Sometimes the samples are outside [-1,1]
|
||||
if (fSample >= 0) {
|
||||
fSample += 0.5f;
|
||||
if (fSample > (1 << (bits_ - 1)) - 1) {
|
||||
fSample = (float)(1 << (bits_ - 1)) - 1;
|
||||
}
|
||||
} else {
|
||||
fSample -= 0.5f;
|
||||
// Raw sample should be within [-1, 1]
|
||||
float fRawSample = sample_array[i];
|
||||
float fScaledSample = fRawSample * (1 << (bits_ - 1));
|
||||
|
||||
// Clamp the sample in range
|
||||
int64_t range = (1 << (bits_ - 1));
|
||||
if (fScaledSample > (range - 1)) {
|
||||
fScaledSample = (float)range;
|
||||
} else if (fScaledSample < (-range + 1)) {
|
||||
fScaledSample = (float)-range;
|
||||
}
|
||||
|
||||
// Convert the sample
|
||||
int sample = (int)fSample;
|
||||
for (uint32_t j = 0; j < sample_size; j++) {
|
||||
// Convert the sample and output it in big endian
|
||||
int sample = (int)fScaledSample;
|
||||
for (int32_t j = sample_size-1; j >= 0; j--) {
|
||||
current_frame_[i * sample_size + j] = sample & 0xFF;
|
||||
sample >>= 8;
|
||||
}
|
||||
|
|
|
@ -246,8 +246,8 @@ void AudioSystem::DecoderThreadMain() {
|
|||
data.input_buffer_1_block_count) * 2048;
|
||||
size_t input_offset = (data.input_buffer_read_offset / 8 - 4);
|
||||
size_t input_remaining = input_size - input_offset;
|
||||
if (input_remaining == 0) {
|
||||
// We're finished!
|
||||
if (input_offset > input_size) {
|
||||
// We're finished. Break.
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -267,27 +267,24 @@ void AudioSystem::DecoderThreadMain() {
|
|||
int read = context.decoder->DecodePacket(out, output_offset,
|
||||
output_remaining);
|
||||
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);
|
||||
context.decoder->DiscardPacket();
|
||||
|
||||
// TODO: Set error state
|
||||
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (read == 0) {
|
||||
// Select sample rate.
|
||||
int sample_rate = 0;
|
||||
if (data.sample_rate == 0) {
|
||||
// TODO: Test this
|
||||
sample_rate = 24000;
|
||||
} else if (data.sample_rate == 1) {
|
||||
sample_rate = 32000;
|
||||
} else if (data.sample_rate == 2) {
|
||||
// TODO: Test this
|
||||
sample_rate = 44100;
|
||||
} else if (data.sample_rate == 3) {
|
||||
// TODO: Test this
|
||||
sample_rate = 48000;
|
||||
}
|
||||
|
||||
|
@ -300,13 +297,14 @@ void AudioSystem::DecoderThreadMain() {
|
|||
// New packet time.
|
||||
// TODO: Select input buffer 1 if necessary.
|
||||
auto packet = in0 + input_offset;
|
||||
context.decoder->PreparePacket(packet, 2048, sample_rate, channels);
|
||||
context.decoder->PreparePacket(packet, 2048, sample_rate,
|
||||
channels);
|
||||
input_offset += 2048;
|
||||
}
|
||||
|
||||
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.output_buffer_write_offset = output_offset / 256;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue