Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
audio_core\hle\source.cpp: Improve accuracy of SourceStatus
Browse files Browse the repository at this point in the history
1. Skip initial frame generation if the current buffer is empty
2. Consume input samples if play_position > 0
3. Remove mostly unused next_sample_number
4. retype play_position, as it is an internal state variable it doesnt have to be u32_dsp
  • Loading branch information
SachinVin committed Feb 11, 2024
1 parent b9c9bee commit e54799d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
30 changes: 20 additions & 10 deletions src/audio_core/hle/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ void Source::ParseConfig(SourceConfiguration::Configuration& config,
b.buffer_id,
state.mono_or_stereo,
state.format,
true,
{}, // 0 in u32_dsp
false,
true, // from_queue
0, // play_position
false, // has_played
});
}
LOG_TRACE(Audio_DSP, "enqueuing queued {} addr={:#010x} len={} id={}", i,
Expand All @@ -321,7 +321,11 @@ void Source::ParseConfig(SourceConfiguration::Configuration& config,
void Source::GenerateFrame() {
current_frame.fill({});

if (state.current_buffer.empty() && !DequeueBuffer()) {
if (state.current_buffer.empty()) {
// TODO(SachinV): Should dequeue happen at the end of the frame generation?
if (DequeueBuffer()) {
return;
}
state.enabled = false;
state.buffer_update = true;
state.last_buffer_id = state.current_buffer_id;
Expand All @@ -330,8 +334,6 @@ void Source::GenerateFrame() {
}

std::size_t frame_position = 0;

state.current_sample_number = state.next_sample_number;
while (frame_position < current_frame.size()) {
if (state.current_buffer.empty() && !DequeueBuffer()) {
break;
Expand All @@ -358,7 +360,7 @@ void Source::GenerateFrame() {
}
// TODO(jroweboy): Keep track of frame_position independently so that it doesn't lose precision
// over time
state.next_sample_number += static_cast<u32>(frame_position * state.rate_multiplier);
state.current_sample_number += static_cast<u32>(frame_position * state.rate_multiplier);

state.filters.ProcessFrame(current_frame);
}
Expand Down Expand Up @@ -409,7 +411,6 @@ bool Source::DequeueBuffer() {

// the first playthrough starts at play_position, loops start at the beginning of the buffer
state.current_sample_number = (!buf.has_played) ? buf.play_position : 0;
state.next_sample_number = state.current_sample_number;
state.current_buffer_physical_address = buf.physical_address;
state.current_buffer_id = buf.buffer_id;
state.last_buffer_id = 0;
Expand All @@ -420,8 +421,17 @@ bool Source::DequeueBuffer() {
state.input_queue.push(buf);
}

LOG_TRACE(Audio_DSP, "source_id={} buffer_id={} from_queue={} current_buffer.size()={}",
source_id, buf.buffer_id, buf.from_queue, state.current_buffer.size());
// Because our interpolation consumes samples instead of using an index,
// let's just consume the samples up to the current sample number.
state.current_buffer.erase(
state.current_buffer.begin(),
std::next(state.current_buffer.begin(), state.current_sample_number));

LOG_TRACE(Audio_DSP,
"source_id={} buffer_id={} from_queue={} current_buffer.size()={}, "
"buf.has_played={}, buf.play_position={}",
source_id, buf.buffer_id, buf.from_queue, state.current_buffer.size(), buf.has_played,
buf.play_position);
return true;
}

Expand Down
5 changes: 2 additions & 3 deletions src/audio_core/hle/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class Source final {
Format format;

bool from_queue;
u32_dsp play_position; // = 0;
bool has_played; // = false;
u32 play_position; // = 0;
bool has_played; // = false;

private:
template <class Archive>
Expand Down Expand Up @@ -136,7 +136,6 @@ class Source final {
// Current buffer

u32 current_sample_number = 0;
u32 next_sample_number = 0;
PAddr current_buffer_physical_address = 0;
AudioInterp::StereoBuffer16 current_buffer = {};

Expand Down

0 comments on commit e54799d

Please sign in to comment.