2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 12:54:52 +02:00
|
|
|
#pragma once
|
|
|
|
|
2020-06-12 17:00:03 +02:00
|
|
|
#include "ClientConnection.h"
|
2020-02-14 22:29:06 +01:00
|
|
|
#include <AK/Badge.h>
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 12:54:52 +02:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-07-27 17:20:41 +02:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2019-07-28 21:27:18 +02:00
|
|
|
#include <AK/Queue.h>
|
2019-07-27 17:20:41 +02:00
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
#include <AK/WeakPtr.h>
|
2020-02-06 15:18:03 +01:00
|
|
|
#include <LibAudio/Buffer.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/File.h>
|
2019-08-25 23:51:27 +03:00
|
|
|
#include <LibThread/Lock.h>
|
2019-08-25 19:27:51 +03:00
|
|
|
#include <LibThread/Thread.h>
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 12:54:52 +02:00
|
|
|
|
2020-06-12 17:00:03 +02:00
|
|
|
namespace AudioServer {
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 12:54:52 +02:00
|
|
|
|
2020-06-12 17:00:03 +02:00
|
|
|
class ClientConnection;
|
|
|
|
|
|
|
|
class BufferQueue : public RefCounted<BufferQueue> {
|
2019-07-28 21:27:18 +02:00
|
|
|
public:
|
2020-06-12 17:00:03 +02:00
|
|
|
explicit BufferQueue(ClientConnection&);
|
|
|
|
~BufferQueue() {}
|
2019-07-28 21:27:18 +02:00
|
|
|
|
|
|
|
bool is_full() const { return m_queue.size() >= 3; }
|
2020-02-06 10:40:02 +01:00
|
|
|
void enqueue(NonnullRefPtr<Audio::Buffer>&&);
|
2019-07-28 21:27:18 +02:00
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
bool get_next_sample(Audio::Sample& sample)
|
2019-07-28 21:27:18 +02:00
|
|
|
{
|
2019-11-04 19:39:17 +01:00
|
|
|
if (m_paused)
|
|
|
|
return false;
|
|
|
|
|
2019-07-28 21:27:18 +02:00
|
|
|
while (!m_current && !m_queue.is_empty())
|
|
|
|
m_current = m_queue.dequeue();
|
2019-10-19 19:10:53 +02:00
|
|
|
|
2019-07-28 21:27:18 +02:00
|
|
|
if (!m_current)
|
|
|
|
return false;
|
2019-10-19 19:10:53 +02:00
|
|
|
|
2019-07-28 21:27:18 +02:00
|
|
|
sample = m_current->samples()[m_position++];
|
2019-11-04 19:39:17 +01:00
|
|
|
--m_remaining_samples;
|
|
|
|
++m_played_samples;
|
2019-10-19 19:10:53 +02:00
|
|
|
|
2019-07-28 21:27:18 +02:00
|
|
|
if (m_position >= m_current->sample_count()) {
|
2020-02-28 11:45:19 +01:00
|
|
|
m_client->did_finish_playing_buffer({}, m_current->shbuf_id());
|
2019-07-28 21:27:18 +02:00
|
|
|
m_current = nullptr;
|
|
|
|
m_position = 0;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-12 17:00:03 +02:00
|
|
|
ClientConnection* client() { return m_client.ptr(); }
|
2019-10-19 19:10:53 +02:00
|
|
|
|
2019-11-04 19:39:17 +01:00
|
|
|
void clear(bool paused = false)
|
2019-07-28 21:27:18 +02:00
|
|
|
{
|
|
|
|
m_queue.clear();
|
|
|
|
m_position = 0;
|
2019-11-04 19:39:17 +01:00
|
|
|
m_remaining_samples = 0;
|
|
|
|
m_played_samples = 0;
|
|
|
|
m_current = nullptr;
|
|
|
|
m_paused = paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_paused(bool paused)
|
|
|
|
{
|
|
|
|
m_paused = paused;
|
2019-07-28 21:27:18 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 19:10:53 +02:00
|
|
|
int get_remaining_samples() const { return m_remaining_samples; }
|
2019-11-04 19:39:17 +01:00
|
|
|
int get_played_samples() const { return m_played_samples; }
|
2019-11-23 16:43:21 +01:00
|
|
|
int get_playing_buffer() const
|
|
|
|
{
|
|
|
|
if (m_current)
|
2020-02-28 11:45:19 +01:00
|
|
|
return m_current->shbuf_id();
|
2019-11-04 19:39:17 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2019-10-19 19:10:53 +02:00
|
|
|
|
2019-07-28 21:27:18 +02:00
|
|
|
private:
|
2020-02-06 10:40:02 +01:00
|
|
|
RefPtr<Audio::Buffer> m_current;
|
|
|
|
Queue<NonnullRefPtr<Audio::Buffer>> m_queue;
|
2020-06-12 17:00:03 +02:00
|
|
|
int m_position{ 0 };
|
|
|
|
int m_remaining_samples{ 0 };
|
|
|
|
int m_played_samples{ 0 };
|
|
|
|
bool m_paused{ false };
|
|
|
|
WeakPtr<ClientConnection> m_client;
|
2019-07-28 21:27:18 +02:00
|
|
|
};
|
|
|
|
|
2020-06-12 17:00:03 +02:00
|
|
|
class Mixer : public Core::Object {
|
|
|
|
C_OBJECT(Mixer)
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 12:54:52 +02:00
|
|
|
public:
|
2020-06-12 17:00:03 +02:00
|
|
|
Mixer();
|
|
|
|
virtual ~Mixer() override;
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 12:54:52 +02:00
|
|
|
|
2020-06-12 17:00:03 +02:00
|
|
|
NonnullRefPtr<BufferQueue> create_queue(ClientConnection&);
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 12:54:52 +02:00
|
|
|
|
2019-07-29 19:06:58 +02:00
|
|
|
int main_volume() const { return m_main_volume; }
|
2020-07-21 03:16:48 +02:00
|
|
|
void set_main_volume(int volume);
|
2019-07-29 19:06:58 +02:00
|
|
|
|
2019-11-22 21:44:02 +01:00
|
|
|
bool is_muted() const { return m_muted; }
|
|
|
|
void set_muted(bool);
|
|
|
|
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 12:54:52 +02:00
|
|
|
private:
|
2020-06-12 17:00:03 +02:00
|
|
|
Vector<NonnullRefPtr<BufferQueue>> m_pending_mixing;
|
2019-12-22 21:33:33 +01:00
|
|
|
pthread_mutex_t m_pending_mutex;
|
|
|
|
pthread_cond_t m_pending_cond;
|
2019-07-28 21:27:18 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
RefPtr<Core::File> m_device;
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 12:54:52 +02:00
|
|
|
|
2019-08-25 19:27:51 +03:00
|
|
|
LibThread::Thread m_sound_thread;
|
|
|
|
|
2020-06-12 17:00:03 +02:00
|
|
|
bool m_muted{ false };
|
|
|
|
int m_main_volume{ 100 };
|
2019-07-29 19:06:58 +02:00
|
|
|
|
2020-06-12 17:00:03 +02:00
|
|
|
u8* m_zero_filled_buffer{ nullptr };
|
2019-11-22 21:44:02 +01:00
|
|
|
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 12:54:52 +02:00
|
|
|
void mix();
|
|
|
|
};
|
2020-06-12 17:00:03 +02:00
|
|
|
}
|