Skip to content

Commit

Permalink
LibAudio: Put all classes in the Audio namespace and remove leading A
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Feb 6, 2020
1 parent 0bce5f7 commit 92f7786
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Applications/Piano/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main(int argc, char** argv)
{
GUI::Application app(argc, argv);

auto audio_client = AClientConnection::construct();
auto audio_client = Audio::ClientConnection::construct();
audio_client->handshake();

AudioEngine audio_engine;
Expand Down
4 changes: 2 additions & 2 deletions Applications/SoundPlayer/PlaybackManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "PlaybackManager.h"

PlaybackManager::PlaybackManager(NonnullRefPtr<AClientConnection> connection)
PlaybackManager::PlaybackManager(NonnullRefPtr<Audio::ClientConnection> connection)
: m_connection(connection)
{
m_timer = Core::Timer::construct(100, [&]() {
Expand All @@ -41,7 +41,7 @@ PlaybackManager::~PlaybackManager()
{
}

void PlaybackManager::set_loader(OwnPtr<AWavLoader>&& loader)
void PlaybackManager::set_loader(OwnPtr<Audio::WavLoader>&& loader)
{
stop();
m_loader = move(loader);
Expand Down
18 changes: 9 additions & 9 deletions Applications/SoundPlayer/PlaybackManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@

class PlaybackManager final {
public:
PlaybackManager(NonnullRefPtr<AClientConnection>);
PlaybackManager(NonnullRefPtr<Audio::ClientConnection>);
~PlaybackManager();

void play();
void stop();
void pause();
void seek(const int position);
bool toggle_pause();
void set_loader(OwnPtr<AWavLoader>&&);
void set_loader(OwnPtr<Audio::WavLoader>&&);

int last_seek() const { return m_last_seek; }
bool is_paused() const { return m_paused; }
float total_length() const { return m_total_length; }
RefPtr<ABuffer> current_buffer() const { return m_current_buffer; }
RefPtr<Audio::Buffer> current_buffer() const { return m_current_buffer; }

NonnullRefPtr<AClientConnection> connection() const { return m_connection; }
NonnullRefPtr<Audio::ClientConnection> connection() const { return m_connection; }

Function<void()> on_update;

Expand All @@ -65,10 +65,10 @@ class PlaybackManager final {
int m_next_ptr { 0 };
int m_last_seek { 0 };
float m_total_length { 0 };
OwnPtr<AWavLoader> m_loader { nullptr };
NonnullRefPtr<AClientConnection> m_connection;
RefPtr<ABuffer> m_next_buffer;
RefPtr<ABuffer> m_current_buffer;
Vector<RefPtr<ABuffer>> m_buffers;
OwnPtr<Audio::WavLoader> 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;
};
2 changes: 1 addition & 1 deletion Applications/SoundPlayer/SampleWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void SampleWidget::paint_event(GUI::PaintEvent& event)
}
}

void SampleWidget::set_buffer(ABuffer* buffer)
void SampleWidget::set_buffer(Audio::Buffer* buffer)
{
if (m_buffer == buffer)
return;
Expand Down
8 changes: 5 additions & 3 deletions Applications/SoundPlayer/SampleWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@

#include <LibGUI/GFrame.h>

class ABuffer;
namespace Audio {
class Buffer;
}

class SampleWidget final : public GUI::Frame {
C_OBJECT(SampleWidget)
public:
virtual ~SampleWidget() override;

void set_buffer(ABuffer*);
void set_buffer(Audio::Buffer*);
private:
explicit SampleWidget(GUI::Widget* parent);
virtual void paint_event(GUI::PaintEvent&) override;

RefPtr<ABuffer> m_buffer;
RefPtr<Audio::Buffer> m_buffer;
};
4 changes: 2 additions & 2 deletions Applications/SoundPlayer/SoundPlayerWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <LibGUI/GMessageBox.h>
#include <LibM/math.h>

SoundPlayerWidget::SoundPlayerWidget(GUI::Window& window, NonnullRefPtr<AClientConnection> connection)
SoundPlayerWidget::SoundPlayerWidget(GUI::Window& window, NonnullRefPtr<Audio::ClientConnection> connection)
: m_window(window)
, m_connection(connection)
, m_manager(connection)
Expand Down Expand Up @@ -124,7 +124,7 @@ void SoundPlayerWidget::open_file(String path)
return;
}

OwnPtr<AWavLoader> loader = make<AWavLoader>(path);
OwnPtr<Audio::WavLoader> loader = make<Audio::WavLoader>(path);
if (loader->has_error()) {
GUI::MessageBox::show(
String::format(
Expand Down
4 changes: 2 additions & 2 deletions Applications/SoundPlayer/SoundPlayerWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SoundPlayerWidget final : public GUI::Widget {
PlaybackManager& manager() { return m_manager; }

private:
explicit SoundPlayerWidget(GUI::Window&, NonnullRefPtr<AClientConnection>);
explicit SoundPlayerWidget(GUI::Window&, NonnullRefPtr<Audio::ClientConnection>);

void update_position(const int position);
void update_ui();
Expand Down Expand Up @@ -77,7 +77,7 @@ class SoundPlayerWidget final : public GUI::Widget {
};

GUI::Window& m_window;
NonnullRefPtr<AClientConnection> m_connection;
NonnullRefPtr<Audio::ClientConnection> m_connection;
PlaybackManager m_manager;
float m_sample_ratio;
RefPtr<GUI::Label> m_status;
Expand Down
2 changes: 1 addition & 1 deletion Applications/SoundPlayer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main(int argc, char** argv)
return 1;
}

auto audio_client = AClientConnection::construct();
auto audio_client = Audio::ClientConnection::construct();
audio_client->handshake();

if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
Expand Down
42 changes: 23 additions & 19 deletions Libraries/LibAudio/ABuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,26 @@
#include <AK/Vector.h>
#include <AK/SharedBuffer.h>

namespace Audio {

// A single sample in an audio buffer.
// Values are floating point, and should range from -1.0 to +1.0
struct ASample {
ASample()
struct Sample {
Sample()
: left(0)
, right(0)
{
}

// For mono
ASample(float left)
Sample(float left)
: left(left)
, right(left)
{
}

// For stereo
ASample(float left, float right)
Sample(float left, float right)
: left(left)
, right(right)
{
Expand All @@ -74,7 +76,7 @@ struct ASample {
right *= pct;
}

ASample& operator+=(const ASample& other)
Sample& operator+=(const Sample& other)
{
left += other.left;
right += other.right;
Expand All @@ -88,9 +90,9 @@ struct ASample {
// Small helper to resample from one playback rate to another
// This isn't really "smart", in that we just insert (or drop) samples.
// Should do better...
class AResampleHelper {
class ResampleHelper {
public:
AResampleHelper(float source, float target);
ResampleHelper(float source, float target);

void process_sample(float sample_l, float sample_r);
bool read_sample(float& next_l, float& next_r);
Expand All @@ -103,34 +105,34 @@ class AResampleHelper {
};

// A buffer of audio samples, normalized to 44100hz.
class ABuffer : public RefCounted<ABuffer> {
class Buffer : public RefCounted<Buffer> {
public:
static RefPtr<ABuffer> from_pcm_data(ByteBuffer& data, AResampleHelper& resampler, int num_channels, int bits_per_sample);
static NonnullRefPtr<ABuffer> create_with_samples(Vector<ASample>&& samples)
static RefPtr<Buffer> from_pcm_data(ByteBuffer& data, ResampleHelper& resampler, int num_channels, int bits_per_sample);
static NonnullRefPtr<Buffer> create_with_samples(Vector<Sample>&& samples)
{
return adopt(*new ABuffer(move(samples)));
return adopt(*new Buffer(move(samples)));
}
static NonnullRefPtr<ABuffer> create_with_shared_buffer(NonnullRefPtr<SharedBuffer>&& buffer, int sample_count)
static NonnullRefPtr<Buffer> create_with_shared_buffer(NonnullRefPtr<SharedBuffer>&& buffer, int sample_count)
{
return adopt(*new ABuffer(move(buffer), sample_count));
return adopt(*new Buffer(move(buffer), sample_count));
}

const ASample* samples() const { return (const ASample*)data(); }
const Sample* samples() const { return (const Sample*)data(); }
int sample_count() const { return m_sample_count; }
const void* data() const { return m_buffer->data(); }
int size_in_bytes() const { return m_sample_count * (int)sizeof(ASample); }
int size_in_bytes() const { return m_sample_count * (int)sizeof(Sample); }
int shared_buffer_id() const { return m_buffer->shared_buffer_id(); }
SharedBuffer& shared_buffer() { return *m_buffer; }

private:
explicit ABuffer(Vector<ASample>&& samples)
: m_buffer(*SharedBuffer::create_with_size(samples.size() * sizeof(ASample))),
explicit Buffer(Vector<Sample>&& samples)
: m_buffer(*SharedBuffer::create_with_size(samples.size() * sizeof(Sample))),
m_sample_count(samples.size())
{
memcpy(m_buffer->data(), samples.data(), samples.size() * sizeof(ASample));
memcpy(m_buffer->data(), samples.data(), samples.size() * sizeof(Sample));
}

explicit ABuffer(NonnullRefPtr<SharedBuffer>&& buffer, int sample_count)
explicit Buffer(NonnullRefPtr<SharedBuffer>&& buffer, int sample_count)
: m_buffer(move(buffer)),
m_sample_count(sample_count)
{
Expand All @@ -139,3 +141,5 @@ class ABuffer : public RefCounted<ABuffer> {
NonnullRefPtr<SharedBuffer> m_buffer;
const int m_sample_count;
};

}
38 changes: 21 additions & 17 deletions Libraries/LibAudio/AClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,88 +28,92 @@
#include <LibAudio/ABuffer.h>
#include <LibAudio/AClientConnection.h>

AClientConnection::AClientConnection()
namespace Audio {

ClientConnection::ClientConnection()
: IPC::ServerConnection<AudioClientEndpoint, AudioServerEndpoint>(*this, "/tmp/portal/audio")
{
}

void AClientConnection::handshake()
void ClientConnection::handshake()
{
auto response = send_sync<AudioServer::Greet>();
set_my_client_id(response->client_id());
}

void AClientConnection::enqueue(const ABuffer& buffer)
void ClientConnection::enqueue(const Buffer& buffer)
{
for (;;) {
const_cast<ABuffer&>(buffer).shared_buffer().share_with(server_pid());
const_cast<Buffer&>(buffer).shared_buffer().share_with(server_pid());
auto response = send_sync<AudioServer::EnqueueBuffer>(buffer.shared_buffer_id(), buffer.sample_count());
if (response->success())
break;
sleep(1);
}
}

bool AClientConnection::try_enqueue(const ABuffer& buffer)
bool ClientConnection::try_enqueue(const Buffer& buffer)
{
const_cast<ABuffer&>(buffer).shared_buffer().share_with(server_pid());
const_cast<Buffer&>(buffer).shared_buffer().share_with(server_pid());
auto response = send_sync<AudioServer::EnqueueBuffer>(buffer.shared_buffer_id(), buffer.sample_count());
return response->success();
}

bool AClientConnection::get_muted()
bool ClientConnection::get_muted()
{
return send_sync<AudioServer::GetMuted>()->muted();
}

void AClientConnection::set_muted(bool muted)
void ClientConnection::set_muted(bool muted)
{
send_sync<AudioServer::SetMuted>(muted);
}

int AClientConnection::get_main_mix_volume()
int ClientConnection::get_main_mix_volume()
{
return send_sync<AudioServer::GetMainMixVolume>()->volume();
}

void AClientConnection::set_main_mix_volume(int volume)
void ClientConnection::set_main_mix_volume(int volume)
{
send_sync<AudioServer::SetMainMixVolume>(volume);
}

int AClientConnection::get_remaining_samples()
int ClientConnection::get_remaining_samples()
{
return send_sync<AudioServer::GetRemainingSamples>()->remaining_samples();
}

int AClientConnection::get_played_samples()
int ClientConnection::get_played_samples()
{
return send_sync<AudioServer::GetPlayedSamples>()->played_samples();
}

void AClientConnection::set_paused(bool paused)
void ClientConnection::set_paused(bool paused)
{
send_sync<AudioServer::SetPaused>(paused);
}

void AClientConnection::clear_buffer(bool paused)
void ClientConnection::clear_buffer(bool paused)
{
send_sync<AudioServer::ClearBuffer>(paused);
}

int AClientConnection::get_playing_buffer()
int ClientConnection::get_playing_buffer()
{
return send_sync<AudioServer::GetPlayingBuffer>()->buffer_id();
}

void AClientConnection::handle(const AudioClient::FinishedPlayingBuffer& message)
void ClientConnection::handle(const AudioClient::FinishedPlayingBuffer& message)
{
if (on_finish_playing_buffer)
on_finish_playing_buffer(message.buffer_id());
}

void AClientConnection::handle(const AudioClient::MutedStateChanged& message)
void ClientConnection::handle(const AudioClient::MutedStateChanged& message)
{
if (on_muted_state_change)
on_muted_state_change(message.muted());
}

}
16 changes: 10 additions & 6 deletions Libraries/LibAudio/AClientConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@
#include <AudioServer/AudioServerEndpoint.h>
#include <LibIPC/IServerConnection.h>

class ABuffer;
namespace Audio {

class AClientConnection : public IPC::ServerConnection<AudioClientEndpoint, AudioServerEndpoint>
class Buffer;

class ClientConnection : public IPC::ServerConnection<AudioClientEndpoint, AudioServerEndpoint>
, public AudioClientEndpoint {
C_OBJECT(AClientConnection)
C_OBJECT(ClientConnection)
public:
AClientConnection();
ClientConnection();

virtual void handshake() override;
void enqueue(const ABuffer&);
bool try_enqueue(const ABuffer&);
void enqueue(const Buffer&);
bool try_enqueue(const Buffer&);

bool get_muted();
void set_muted(bool);
Expand All @@ -62,3 +64,5 @@ class AClientConnection : public IPC::ServerConnection<AudioClientEndpoint, Audi
virtual void handle(const AudioClient::FinishedPlayingBuffer&) override;
virtual void handle(const AudioClient::MutedStateChanged&) override;
};

}
Loading

0 comments on commit 92f7786

Please sign in to comment.