diff options
author | Nick Miller <nick.miller.83@gmail.com> | 2021-06-19 13:17:04 -0700 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-06-21 03:13:59 +0430 |
commit | 9a2c80c791756652fe92f2cd22cbf82fc1ab95ad (patch) | |
tree | 5c95ee8ea7b71e01950a31ced0d40d8507d7af2b /Userland/Applications/SoundPlayer/PlaybackManager.h | |
parent | 34a3d08e651d75b5f83deec0282cf54092c4d753 (diff) | |
download | serenity-9a2c80c791756652fe92f2cd22cbf82fc1ab95ad.zip |
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.
Diffstat (limited to 'Userland/Applications/SoundPlayer/PlaybackManager.h')
-rw-r--r-- | Userland/Applications/SoundPlayer/PlaybackManager.h | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/Userland/Applications/SoundPlayer/PlaybackManager.h b/Userland/Applications/SoundPlayer/PlaybackManager.h index 6b333e813a..35afc3fa2c 100644 --- a/Userland/Applications/SoundPlayer/PlaybackManager.h +++ b/Userland/Applications/SoundPlayer/PlaybackManager.h @@ -12,9 +12,6 @@ #include <LibAudio/Loader.h> #include <LibCore/Timer.h> -#define PLAYBACK_MANAGER_BUFFER_SIZE 48 * KiB -#define PLAYBACK_MANAGER_RATE 44100 - class PlaybackManager final { public: PlaybackManager(NonnullRefPtr<Audio::ClientConnection>); @@ -27,6 +24,7 @@ public: void loop(bool); bool toggle_pause(); void set_loader(NonnullRefPtr<Audio::Loader>&&); + size_t device_sample_rate() const { return m_device_sample_rate; } int last_seek() const { return m_last_seek; } bool is_paused() const { return m_paused; } @@ -42,18 +40,23 @@ public: private: void next_buffer(); void set_paused(bool); - void load_next_buffer(); - void remove_dead_buffers(); bool m_paused { true }; bool m_loop = { false }; - size_t m_next_ptr { 0 }; size_t m_last_seek { 0 }; float m_total_length { 0 }; + // FIXME: Get this from the audio server + size_t m_device_sample_rate { 44100 }; + size_t m_device_samples_per_buffer { 0 }; + size_t m_source_buffer_size_bytes { 0 }; RefPtr<Audio::Loader> m_loader { nullptr }; NonnullRefPtr<Audio::ClientConnection> m_connection; - RefPtr<Audio::Buffer> m_next_buffer; RefPtr<Audio::Buffer> m_current_buffer; - Vector<RefPtr<Audio::Buffer>> m_buffers; RefPtr<Core::Timer> m_timer; + + // Controls the GUI update rate. A smaller value makes the visualizations nicer. + static constexpr u32 update_rate_ms = 50; + + // Number of milliseconds of audio data contained in each audio buffer + static constexpr u32 buffer_size_ms = 100; }; |