refactor(emulator): improve audio backend initialization and adaptive buffering

- Enhanced audio backend initialization by adding comments for clarity and ensuring a moderate buffer size for optimal latency and stability.
- Updated the emulator's run logic to start in a running state by default and refined the auto-pause mechanism to only trigger during window resizing, removing aggressive focus-based pausing.
- Implemented adaptive audio buffering to maintain smooth playback, adjusting the number of queued samples based on current buffer status.

Benefits:
- Improved user experience with more intuitive audio handling and reduced latency.
- Enhanced stability during window operations, preventing crashes on macOS.
- Streamlined audio processing for better performance and responsiveness.
This commit is contained in:
scawful
2025-10-11 13:17:14 -04:00
parent e8ebf298c6
commit 88a4f29fe8
3 changed files with 123 additions and 121 deletions

View File

@@ -625,10 +625,26 @@ void Dsp::GetSamples(int16_t* sample_data, int samples_per_frame,
double location = static_cast<double>((lastFrameBoundary + 0x400) & 0x3ff);
location -= native_per_frame;
// Use linear interpolation for smoother resampling
for (int i = 0; i < samples_per_frame; i++) {
const int idx = static_cast<int>(location) & 0x3ff;
sample_data[(i * 2) + 0] = sampleBuffer[(idx * 2) + 0];
sample_data[(i * 2) + 1] = sampleBuffer[(idx * 2) + 1];
const int next_idx = (idx + 1) & 0x3ff;
// Calculate interpolation factor (0.0 to 1.0)
const double frac = location - static_cast<int>(location);
// Linear interpolation for left channel
const int16_t s0_l = sampleBuffer[(idx * 2) + 0];
const int16_t s1_l = sampleBuffer[(next_idx * 2) + 0];
sample_data[(i * 2) + 0] = static_cast<int16_t>(
s0_l + frac * (s1_l - s0_l));
// Linear interpolation for right channel
const int16_t s0_r = sampleBuffer[(idx * 2) + 1];
const int16_t s1_r = sampleBuffer[(next_idx * 2) + 1];
sample_data[(i * 2) + 1] = static_cast<int16_t>(
s0_r + frac * (s1_r - s0_r));
location += step;
}
}