Cleanup some audio system code.
This commit is contained in:
@@ -333,13 +333,7 @@ void AudioSystem::UnregisterClient(size_t index) {
|
||||
void AudioSystem::ProcessXmaContext(XMAContext& context, XMAContextData& data) {
|
||||
SCOPE_profile_cpu_f("apu");
|
||||
|
||||
// Translate pointers for future use.
|
||||
uint8_t* in0 = data.input_buffer_0_valid
|
||||
? memory()->TranslatePhysical(data.input_buffer_0_ptr)
|
||||
: nullptr;
|
||||
uint8_t* in1 = data.input_buffer_1_valid
|
||||
? memory()->TranslatePhysical(data.input_buffer_1_ptr)
|
||||
: nullptr;
|
||||
// Translate this for future use.
|
||||
uint8_t* out = memory()->TranslatePhysical(data.output_buffer_ptr);
|
||||
|
||||
// What I see:
|
||||
@@ -371,9 +365,9 @@ void AudioSystem::ProcessXmaContext(XMAContext& context, XMAContextData& data) {
|
||||
uint32_t output_write_offset_bytes = data.output_buffer_write_offset * 256;
|
||||
uint32_t output_read_offset_bytes = data.output_buffer_read_offset * 256;
|
||||
|
||||
RingBuffer out_buffer(out, output_size_bytes, output_write_offset_bytes);
|
||||
RingBuffer output_buffer(out, output_size_bytes, output_write_offset_bytes);
|
||||
size_t output_remaining_bytes
|
||||
= out_buffer.DistanceToOffset(output_read_offset_bytes);
|
||||
= output_buffer.DistanceToOffset(output_read_offset_bytes);
|
||||
|
||||
if (!output_remaining_bytes) {
|
||||
// Can't write any more data. Break.
|
||||
@@ -392,7 +386,7 @@ void AudioSystem::ProcessXmaContext(XMAContext& context, XMAContextData& data) {
|
||||
read_bytes = context.decoder->DecodePacket(tmp_buff, 0,
|
||||
output_remaining_bytes);
|
||||
if (read_bytes >= 0) {
|
||||
out_buffer.Write(tmp_buff, read_bytes);
|
||||
output_buffer.Write(tmp_buff, read_bytes);
|
||||
|
||||
// Ok.
|
||||
break;
|
||||
@@ -426,58 +420,13 @@ void AudioSystem::ProcessXmaContext(XMAContext& context, XMAContextData& data) {
|
||||
|
||||
// If we need more data and the input buffers have it, grab it.
|
||||
if (read_bytes) {
|
||||
// Still outputting.
|
||||
// Haven't finished with current packet.
|
||||
continue;
|
||||
} else if (data.input_buffer_0_valid || data.input_buffer_1_valid) {
|
||||
// Done with previous packet, so grab a new one.
|
||||
int sample_rate = 0;
|
||||
if (data.sample_rate == 0) {
|
||||
sample_rate = 24000;
|
||||
} else if (data.sample_rate == 1) {
|
||||
sample_rate = 32000;
|
||||
} else if (data.sample_rate == 2) {
|
||||
sample_rate = 44100;
|
||||
} else if (data.sample_rate == 3) {
|
||||
sample_rate = 48000;
|
||||
}
|
||||
int channels = data.is_stereo ? 2 : 1;
|
||||
|
||||
// See if we've finished with the input.
|
||||
// Block count is in packets, so expand by packet size.
|
||||
uint32_t input_size_0_bytes = (data.input_buffer_0_packet_count) * 2048;
|
||||
uint32_t input_size_1_bytes = (data.input_buffer_1_packet_count) * 2048;
|
||||
|
||||
// Total input size
|
||||
uint32_t input_size_bytes = input_size_0_bytes + input_size_1_bytes;
|
||||
|
||||
// Input read offset is in bits. Typically starts at 32 (4 bytes).
|
||||
// "Sequence" offset - used internally for WMA Pro decoder.
|
||||
// Just the read offset.
|
||||
uint32_t seq_offset_bytes = (data.input_buffer_read_offset & ~0x7FF) / 8;
|
||||
|
||||
if (seq_offset_bytes < input_size_bytes) {
|
||||
// Setup input offset and input buffer.
|
||||
uint32_t input_offset_bytes = seq_offset_bytes;
|
||||
auto input_buffer = in0;
|
||||
|
||||
if (seq_offset_bytes >= input_size_0_bytes) {
|
||||
// Size overlap, select input buffer 1.
|
||||
input_offset_bytes -= input_size_0_bytes;
|
||||
input_buffer = in1;
|
||||
}
|
||||
|
||||
// Still have data to read.
|
||||
auto packet = input_buffer + input_offset_bytes;
|
||||
context.decoder->PreparePacket(packet, seq_offset_bytes, 2048,
|
||||
sample_rate, channels);
|
||||
data.input_buffer_read_offset += 2048 * 8;
|
||||
if (seq_offset_bytes + 2048 >= input_size_bytes) {
|
||||
// Used the last of the data.
|
||||
data.input_buffer_0_valid = 0;
|
||||
data.input_buffer_1_valid = 0;
|
||||
}
|
||||
} else {
|
||||
// No more data available (for now).
|
||||
int ret = PrepareXMAPacket(context, data);
|
||||
if (ret <= 0) {
|
||||
// No more data (but may have prepared a packet)
|
||||
data.input_buffer_0_valid = 0;
|
||||
data.input_buffer_1_valid = 0;
|
||||
}
|
||||
@@ -488,6 +437,74 @@ void AudioSystem::ProcessXmaContext(XMAContext& context, XMAContextData& data) {
|
||||
}
|
||||
}
|
||||
|
||||
int AudioSystem::PrepareXMAPacket(XMAContext &context, XMAContextData &data) {
|
||||
// Translate pointers for future use.
|
||||
uint8_t* in0 = data.input_buffer_0_valid
|
||||
? memory()->TranslatePhysical(data.input_buffer_0_ptr)
|
||||
: nullptr;
|
||||
uint8_t* in1 = data.input_buffer_1_valid
|
||||
? memory()->TranslatePhysical(data.input_buffer_1_ptr)
|
||||
: nullptr;
|
||||
|
||||
int sample_rate = 0;
|
||||
if (data.sample_rate == 0) {
|
||||
sample_rate = 24000;
|
||||
} else if (data.sample_rate == 1) {
|
||||
sample_rate = 32000;
|
||||
} else if (data.sample_rate == 2) {
|
||||
sample_rate = 44100;
|
||||
} else if (data.sample_rate == 3) {
|
||||
sample_rate = 48000;
|
||||
}
|
||||
int channels = data.is_stereo ? 2 : 1;
|
||||
|
||||
// See if we've finished with the input.
|
||||
// Block count is in packets, so expand by packet size.
|
||||
uint32_t input_size_0_bytes = (data.input_buffer_0_packet_count) * 2048;
|
||||
uint32_t input_size_1_bytes = (data.input_buffer_1_packet_count) * 2048;
|
||||
|
||||
// Total input size
|
||||
uint32_t input_size_bytes = input_size_0_bytes + input_size_1_bytes;
|
||||
|
||||
// Input read offset is in bits. Typically starts at 32 (4 bytes).
|
||||
// "Sequence" offset - used internally for WMA Pro decoder.
|
||||
// Just the read offset.
|
||||
uint32_t seq_offset_bytes = (data.input_buffer_read_offset & ~0x7FF) / 8;
|
||||
uint32_t input_remaining_bytes = input_size_bytes - seq_offset_bytes;
|
||||
|
||||
if (seq_offset_bytes < input_size_bytes) {
|
||||
// Setup input offset and input buffer.
|
||||
uint32_t input_offset_bytes = seq_offset_bytes;
|
||||
auto input_buffer = in0;
|
||||
|
||||
if (seq_offset_bytes >= input_size_0_bytes) {
|
||||
// Size overlap, select input buffer 1.
|
||||
// TODO: This needs testing.
|
||||
input_offset_bytes -= input_size_0_bytes;
|
||||
input_buffer = in1;
|
||||
}
|
||||
|
||||
// Still have data to read.
|
||||
auto packet = input_buffer + input_offset_bytes;
|
||||
context.decoder->PreparePacket(packet, seq_offset_bytes,
|
||||
XMAContextData::kBytesPerPacket,
|
||||
sample_rate, channels);
|
||||
data.input_buffer_read_offset += XMAContextData::kBytesPerPacket * 8;
|
||||
|
||||
input_remaining_bytes -= XMAContextData::kBytesPerPacket;
|
||||
if (input_remaining_bytes <= 0) {
|
||||
// Used the last of the data but prepared a packet
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
// No more data available and no packet prepared.
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
return input_remaining_bytes;
|
||||
}
|
||||
|
||||
// free60 may be useful here, however it looks like it's using a different
|
||||
// piece of hardware:
|
||||
// https://github.com/Free60Project/libxenon/blob/master/libxenon/drivers/xenon_sound/sound.c
|
||||
@@ -547,7 +564,7 @@ void AudioSystem::WriteRegister(uint32_t addr, uint64_t value) {
|
||||
XELOGAPU("AudioSystem: kicking context %d (%d/%d bytes)", context_id,
|
||||
(data.input_buffer_read_offset & ~0x7FF) / 8,
|
||||
(data.input_buffer_0_packet_count + data.input_buffer_1_packet_count)
|
||||
* XMAContextData::kBytesPerBlock);
|
||||
* XMAContextData::kBytesPerPacket);
|
||||
|
||||
// Reset valid flags so our audio decoder knows to process this one.
|
||||
data.input_buffer_0_valid = data.input_buffer_0_ptr != 0;
|
||||
@@ -597,6 +614,7 @@ void AudioSystem::WriteRegister(uint32_t addr, uint64_t value) {
|
||||
data.input_buffer_0_valid = 0;
|
||||
data.input_buffer_1_valid = 0;
|
||||
data.output_buffer_valid = 0;
|
||||
data.output_buffer_read_offset = 31;
|
||||
|
||||
data.Store(context_ptr);
|
||||
context.lock.unlock();
|
||||
|
||||
Reference in New Issue
Block a user