2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-11-04 19:45:19 +01:00
|
|
|
#include "PlaybackManager.h"
|
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
PlaybackManager::PlaybackManager(NonnullRefPtr<Audio::ClientConnection> connection)
|
2019-11-09 17:15:16 +01:00
|
|
|
: m_connection(connection)
|
2019-11-04 19:45:19 +01:00
|
|
|
{
|
2021-06-19 13:17:04 -07:00
|
|
|
m_timer = Core::Timer::construct(PlaybackManager::update_rate_ms, [&]() {
|
2019-11-09 17:15:16 +01:00
|
|
|
if (!m_loader)
|
|
|
|
return;
|
|
|
|
next_buffer();
|
|
|
|
});
|
|
|
|
m_timer->stop();
|
2019-11-04 19:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PlaybackManager::~PlaybackManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-01 20:20:46 +01:00
|
|
|
void PlaybackManager::set_loader(NonnullRefPtr<Audio::Loader>&& loader)
|
2019-11-09 17:15:16 +01:00
|
|
|
{
|
|
|
|
stop();
|
2020-12-01 20:20:46 +01:00
|
|
|
m_loader = loader;
|
2019-11-09 17:15:16 +01:00
|
|
|
if (m_loader) {
|
|
|
|
m_total_length = m_loader->total_samples() / static_cast<float>(m_loader->sample_rate());
|
2021-06-19 13:17:04 -07:00
|
|
|
m_device_samples_per_buffer = PlaybackManager::buffer_size_ms / 1000.0f * m_device_sample_rate;
|
|
|
|
u32 source_samples_per_buffer = PlaybackManager::buffer_size_ms / 1000.0f * m_loader->sample_rate();
|
|
|
|
m_source_buffer_size_bytes = source_samples_per_buffer * m_loader->num_channels() * m_loader->bits_per_sample() / 8;
|
2019-11-09 17:15:16 +01:00
|
|
|
m_timer->start();
|
|
|
|
} else {
|
|
|
|
m_timer->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:45:19 +01:00
|
|
|
void PlaybackManager::stop()
|
|
|
|
{
|
|
|
|
set_paused(true);
|
|
|
|
m_connection->clear_buffer(true);
|
|
|
|
m_last_seek = 0;
|
|
|
|
m_current_buffer = nullptr;
|
2019-11-09 17:15:16 +01:00
|
|
|
|
|
|
|
if (m_loader)
|
|
|
|
m_loader->reset();
|
2019-11-04 19:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlaybackManager::play()
|
|
|
|
{
|
|
|
|
set_paused(false);
|
|
|
|
}
|
|
|
|
|
2020-11-01 06:19:49 +00:00
|
|
|
void PlaybackManager::loop(bool loop)
|
|
|
|
{
|
|
|
|
m_loop = loop;
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:45:19 +01:00
|
|
|
void PlaybackManager::seek(const int position)
|
|
|
|
{
|
2019-11-09 17:15:16 +01:00
|
|
|
if (!m_loader)
|
|
|
|
return;
|
|
|
|
|
2019-11-04 19:45:19 +01:00
|
|
|
m_last_seek = position;
|
|
|
|
bool paused_state = m_paused;
|
|
|
|
set_paused(true);
|
|
|
|
|
|
|
|
m_connection->clear_buffer(true);
|
|
|
|
m_current_buffer = nullptr;
|
2019-11-09 17:15:16 +01:00
|
|
|
m_loader->seek(position);
|
2019-11-04 19:45:19 +01:00
|
|
|
|
|
|
|
if (!paused_state)
|
|
|
|
set_paused(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaybackManager::pause()
|
|
|
|
{
|
|
|
|
set_paused(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaybackManager::set_paused(bool paused)
|
|
|
|
{
|
|
|
|
m_paused = paused;
|
|
|
|
m_connection->set_paused(paused);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaybackManager::toggle_pause()
|
|
|
|
{
|
|
|
|
if (m_paused) {
|
|
|
|
play();
|
|
|
|
} else {
|
|
|
|
pause();
|
|
|
|
}
|
|
|
|
return m_paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaybackManager::next_buffer()
|
|
|
|
{
|
|
|
|
if (on_update)
|
|
|
|
on_update();
|
2021-06-19 13:17:04 -07:00
|
|
|
|
2019-11-04 19:45:19 +01:00
|
|
|
if (m_paused)
|
|
|
|
return;
|
|
|
|
|
2021-06-19 13:17:04 -07:00
|
|
|
u32 audio_server_remaining_samples = m_connection->get_remaining_samples();
|
|
|
|
bool all_samples_loaded = (m_loader->loaded_samples() >= m_loader->total_samples());
|
|
|
|
bool audio_server_done = (audio_server_remaining_samples == 0);
|
2019-11-04 19:45:19 +01:00
|
|
|
|
2021-06-19 13:17:04 -07:00
|
|
|
if (all_samples_loaded && audio_server_done) {
|
|
|
|
stop();
|
|
|
|
if (on_finished_playing)
|
|
|
|
on_finished_playing();
|
2019-11-04 19:45:19 +01:00
|
|
|
return;
|
2021-06-19 13:17:04 -07:00
|
|
|
}
|
2019-11-04 19:45:19 +01:00
|
|
|
|
2021-06-19 13:17:04 -07:00
|
|
|
if (audio_server_remaining_samples < m_device_samples_per_buffer) {
|
|
|
|
m_current_buffer = m_loader->get_more_samples(m_source_buffer_size_bytes);
|
|
|
|
if (m_current_buffer)
|
|
|
|
m_connection->enqueue(*m_current_buffer);
|
|
|
|
}
|
2019-11-04 19:45:19 +01:00
|
|
|
}
|