SoundPlayer: Handle any input file sample rate

This commit addresses two issues:
1. If you play a 96 KHz Wave file, the slider position is incorrect,
   because it is assumed all files are 44.1 KHz.
2. For high-bitrate files, there are audio dropouts due to not
   buffering enough audio data.

Issue 1 is addressed by scaling the number of played samples by the
ratio between the source and destination sample rates.

Issue 2 is addressed by buffering a certain number of milliseconds
worth of audio data (instead of a fixed number of bytes).
This makes the the buffer size independent of the source sample rate.

Some of the code is redesigned to be simpler. The code that did the
book-keeping of which buffers need to be loaded and which have been
already played has been removed. Instead, we enqueue a new buffer based
on a low watermark of samples remaining in the audio server queue.

Other small fixes include:
1. Disable the stop button when playback is finished.
2. Remove hard-coded instances of 44100.
3. Update the GUI every 50 ms (was 100), which improves visualizations.
This commit is contained in:
Nick Miller 2021-06-19 13:17:04 -07:00 committed by Ali Mohammad Pur
parent 34a3d08e65
commit 9a2c80c791
Notes: sideshowbarker 2024-07-18 11:57:57 +09:00
7 changed files with 53 additions and 79 deletions

View file

@ -49,7 +49,7 @@ SoundPlayerWidgetAdvancedView::SoundPlayerWidgetAdvancedView(GUI::Window& window
// Set a temporary value for total samples.
// This value will be set properly when we load a new file.
const int total_samples = this->manager().total_length() * 44100;
const int total_samples = this->manager().total_length() * this->manager().device_sample_rate();
m_playback_progress_slider = m_player_view->add<AutoSlider>(Orientation::Horizontal);
m_playback_progress_slider->set_fixed_height(20);
@ -142,16 +142,20 @@ SoundPlayerWidgetAdvancedView::SoundPlayerWidgetAdvancedView(GUI::Window& window
set_nonlinear_volume_slider(false);
manager().on_update = [&]() {
//TODO: make this program support other sample rates
int samples_played = client_connection().get_played_samples() + this->manager().last_seek();
int current_second = samples_played / 44100;
// Determine how many of the source file samples have played.
int samples_played = client_connection().get_played_samples();
float source_to_dest_ratio = static_cast<float>(loaded_file_samplerate()) / manager().device_sample_rate();
samples_played *= source_to_dest_ratio;
samples_played += this->manager().last_seek();
int current_second = samples_played / loaded_file_samplerate();
timestamp_label.set_text(String::formatted("Elapsed: {:02}:{:02}:{:02}", current_second / 3600, current_second / 60, current_second % 60));
if (!m_playback_progress_slider->mouse_is_down()) {
m_playback_progress_slider->set_value(samples_played);
}
dynamic_cast<Visualization*>(m_visualization.ptr())->set_buffer(this->manager().current_buffer());
dynamic_cast<Visualization*>(m_visualization.ptr())->set_samplerate(loaded_file_samplerate());
dynamic_cast<Visualization*>(m_visualization.ptr())->set_samplerate(manager().device_sample_rate());
};
manager().on_load_sample_buffer = [&](Audio::Buffer&) {
@ -176,6 +180,8 @@ SoundPlayerWidgetAdvancedView::SoundPlayerWidgetAdvancedView(GUI::Window& window
} else
open_file((it + 1)->path);
}
m_stop_button->set_enabled(false);
};
}