mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:54:58 +00:00
AudioServer: Clean up ClientAudioStream APIs
- Use Optional references instead of pointers - Clean up some const and nullability weirdness - Use proper error return value for get_next_sample
This commit is contained in:
parent
aacb4fc590
commit
d905498fb6
3 changed files with 29 additions and 25 deletions
|
@ -15,9 +15,9 @@ ClientAudioStream::ClientAudioStream(ConnectionFromClient& client)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionFromClient* ClientAudioStream::client()
|
Optional<ConnectionFromClient&> ClientAudioStream::client()
|
||||||
{
|
{
|
||||||
return m_client.ptr();
|
return m_client.has_value() ? *m_client : Optional<ConnectionFromClient&> {};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientAudioStream::is_connected() const
|
bool ClientAudioStream::is_connected() const
|
||||||
|
@ -25,14 +25,14 @@ bool ClientAudioStream::is_connected() const
|
||||||
return m_client && m_client->is_open();
|
return m_client && m_client->is_open();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientAudioStream::get_next_sample(Audio::Sample& sample, u32 audiodevice_sample_rate)
|
ErrorOr<Audio::Sample, ClientAudioStream::ErrorState> ClientAudioStream::get_next_sample(u32 audiodevice_sample_rate)
|
||||||
{
|
{
|
||||||
// Note: Even though we only check client state here, we will probably close the client much earlier.
|
// Note: Even though we only check client state here, we will probably close the client much earlier.
|
||||||
if (!is_connected())
|
if (!is_connected())
|
||||||
return false;
|
return ErrorState::ClientDisconnected;
|
||||||
|
|
||||||
if (m_paused)
|
if (m_paused)
|
||||||
return false;
|
return ErrorState::ClientUnderrun;
|
||||||
|
|
||||||
if (m_in_chunk_location >= m_current_audio_chunk.size()) {
|
if (m_in_chunk_location >= m_current_audio_chunk.size()) {
|
||||||
auto result = m_buffer->dequeue();
|
auto result = m_buffer->dequeue();
|
||||||
|
@ -41,30 +41,26 @@ bool ClientAudioStream::get_next_sample(Audio::Sample& sample, u32 audiodevice_s
|
||||||
dbgln_if(AUDIO_DEBUG, "Audio client {} can't keep up!", m_client->client_id());
|
dbgln_if(AUDIO_DEBUG, "Audio client {} can't keep up!", m_client->client_id());
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return ErrorState::ClientUnderrun;
|
||||||
}
|
}
|
||||||
// FIXME: Our resampler and the way we resample here are bad.
|
// FIXME: Our resampler and the way we resample here are bad.
|
||||||
// Ideally, we should both do perfect band-corrected resampling,
|
// Ideally, we should both do perfect band-corrected resampling,
|
||||||
// as well as carry resampling state over between buffers.
|
// as well as carry resampling state over between buffers.
|
||||||
auto attempted_resample = Audio::ResampleHelper<Audio::Sample> {
|
auto maybe_resampled = Audio::ResampleHelper<Audio::Sample> { m_sample_rate == 0 ? audiodevice_sample_rate : m_sample_rate, audiodevice_sample_rate }
|
||||||
m_sample_rate == 0 ? audiodevice_sample_rate : m_sample_rate, audiodevice_sample_rate
|
.try_resample(result.release_value());
|
||||||
}
|
if (maybe_resampled.is_error())
|
||||||
.try_resample(result.release_value());
|
return ErrorState::ResamplingError;
|
||||||
if (attempted_resample.is_error())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// If the sample rate changes underneath us, we will still play the existing buffer unchanged until we're done.
|
// If the sample rate changes underneath us, we will still play the existing buffer unchanged until we're done.
|
||||||
// This is not a significant problem since the buffers are very small (~100 samples or less).
|
// This is not a significant problem since the buffers are very small (~100 samples or less).
|
||||||
m_current_audio_chunk = attempted_resample.release_value();
|
m_current_audio_chunk = maybe_resampled.release_value();
|
||||||
m_in_chunk_location = 0;
|
m_in_chunk_location = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
sample = m_current_audio_chunk[m_in_chunk_location++];
|
return m_current_audio_chunk[m_in_chunk_location++];
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientAudioStream::set_buffer(OwnPtr<Audio::AudioQueue> buffer)
|
void ClientAudioStream::set_buffer(NonnullOwnPtr<Audio::AudioQueue> buffer)
|
||||||
{
|
{
|
||||||
m_buffer = move(buffer);
|
m_buffer = move(buffer);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
#include "ConnectionFromClient.h"
|
#include "ConnectionFromClient.h"
|
||||||
#include "FadingProperty.h"
|
#include "FadingProperty.h"
|
||||||
#include <AK/Atomic.h>
|
#include <AK/Atomic.h>
|
||||||
#include <AK/Badge.h>
|
|
||||||
#include <AK/Debug.h>
|
#include <AK/Debug.h>
|
||||||
#include <AK/RefCounted.h>
|
#include <AK/RefCounted.h>
|
||||||
#include <AK/WeakPtr.h>
|
#include <AK/WeakPtr.h>
|
||||||
|
@ -20,22 +19,29 @@ namespace AudioServer {
|
||||||
|
|
||||||
class ClientAudioStream : public RefCounted<ClientAudioStream> {
|
class ClientAudioStream : public RefCounted<ClientAudioStream> {
|
||||||
public:
|
public:
|
||||||
|
enum class ErrorState {
|
||||||
|
ClientDisconnected,
|
||||||
|
ClientPaused,
|
||||||
|
ClientUnderrun,
|
||||||
|
ResamplingError,
|
||||||
|
};
|
||||||
|
|
||||||
explicit ClientAudioStream(ConnectionFromClient&);
|
explicit ClientAudioStream(ConnectionFromClient&);
|
||||||
~ClientAudioStream() = default;
|
~ClientAudioStream() = default;
|
||||||
|
|
||||||
bool get_next_sample(Audio::Sample& sample, u32 audiodevice_sample_rate);
|
ErrorOr<Audio::Sample, ErrorState> get_next_sample(u32 audiodevice_sample_rate);
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
bool is_connected() const;
|
bool is_connected() const;
|
||||||
|
|
||||||
ConnectionFromClient* client();
|
Optional<ConnectionFromClient&> client();
|
||||||
|
|
||||||
void set_buffer(OwnPtr<Audio::AudioQueue> buffer);
|
void set_buffer(NonnullOwnPtr<Audio::AudioQueue> buffer);
|
||||||
|
|
||||||
void set_paused(bool paused);
|
void set_paused(bool paused);
|
||||||
FadingProperty<double>& volume();
|
FadingProperty<double>& volume();
|
||||||
double volume() const;
|
double volume() const;
|
||||||
void set_volume(double const volume);
|
void set_volume(double volume);
|
||||||
bool is_muted() const;
|
bool is_muted() const;
|
||||||
void set_muted(bool muted);
|
void set_muted(bool muted);
|
||||||
u32 sample_rate() const;
|
u32 sample_rate() const;
|
||||||
|
|
|
@ -73,18 +73,20 @@ void Mixer::mix()
|
||||||
|
|
||||||
// Mix the buffers together into the output
|
// Mix the buffers together into the output
|
||||||
for (auto& queue : active_mix_queues) {
|
for (auto& queue : active_mix_queues) {
|
||||||
if (!queue->client()) {
|
if (!queue->client().has_value()) {
|
||||||
queue->clear();
|
queue->clear();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
queue->volume().advance_time();
|
queue->volume().advance_time();
|
||||||
|
|
||||||
|
// FIXME: Perform sample extraction and mixing in two separate loops so they can be more easily vectorized.
|
||||||
for (auto& mixed_sample : mixed_buffer) {
|
for (auto& mixed_sample : mixed_buffer) {
|
||||||
Audio::Sample sample;
|
auto sample_or_error = queue->get_next_sample(audiodevice_get_sample_rate());
|
||||||
if (!queue->get_next_sample(sample, audiodevice_get_sample_rate()))
|
if (sample_or_error.is_error())
|
||||||
break;
|
break;
|
||||||
if (queue->is_muted())
|
if (queue->is_muted())
|
||||||
continue;
|
continue;
|
||||||
|
auto sample = sample_or_error.release_value();
|
||||||
sample.log_multiply(SAMPLE_HEADROOM);
|
sample.log_multiply(SAMPLE_HEADROOM);
|
||||||
sample.log_multiply(static_cast<float>(queue->volume()));
|
sample.log_multiply(static_cast<float>(queue->volume()));
|
||||||
mixed_sample += sample;
|
mixed_sample += sample;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue