2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-04-26 17:13:04 +02:00
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-07-13 19:42:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-12-02 15:44:45 +01:00
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
|
#include <AK/MemoryStream.h>
|
2020-02-27 00:53:35 +11:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-07-13 19:42:03 +02:00
|
|
|
#include <AK/RefPtr.h>
|
2020-02-06 10:40:02 +01:00
|
|
|
#include <AK/String.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/StringView.h>
|
2020-02-06 15:18:03 +01:00
|
|
|
#include <LibAudio/Buffer.h>
|
2020-12-01 19:55:41 +01:00
|
|
|
#include <LibAudio/Loader.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/File.h>
|
2019-07-27 14:30:09 +02:00
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
namespace Audio {
|
|
|
|
|
class Buffer;
|
|
|
|
|
|
2021-04-26 17:13:04 +02:00
|
|
|
// defines for handling the WAV header data
|
|
|
|
|
#define WAVE_FORMAT_PCM 0x0001 // PCM
|
|
|
|
|
#define WAVE_FORMAT_IEEE_FLOAT 0x0003 // IEEE float
|
|
|
|
|
#define WAVE_FORMAT_ALAW 0x0006 // 8-bit ITU-T G.711 A-law
|
|
|
|
|
#define WAVE_FORMAT_MULAW 0x0007 // 8-bit ITU-T G.711 µ-law
|
|
|
|
|
#define WAVE_FORMAT_EXTENSIBLE 0xFFFE // Determined by SubFormat
|
|
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
// Parses a WAV file and produces an Audio::Buffer.
|
2020-12-01 19:55:41 +01:00
|
|
|
class WavLoaderPlugin : public LoaderPlugin {
|
2019-07-13 19:42:03 +02:00
|
|
|
public:
|
2020-12-02 15:44:45 +01:00
|
|
|
WavLoaderPlugin(const StringView& path);
|
|
|
|
|
WavLoaderPlugin(const ByteBuffer& buffer);
|
2019-09-04 20:13:32 +02:00
|
|
|
|
2020-12-01 19:55:41 +01:00
|
|
|
virtual bool sniff() override;
|
2019-07-27 14:30:09 +02:00
|
|
|
|
2020-12-01 19:55:41 +01:00
|
|
|
virtual bool has_error() override { return !m_error_string.is_null(); }
|
|
|
|
|
virtual const char* error_string() override { return m_error_string.characters(); }
|
2019-07-27 17:20:41 +02:00
|
|
|
|
2020-12-01 19:55:41 +01:00
|
|
|
virtual RefPtr<Buffer> get_more_samples(size_t max_bytes_to_read_from_input = 128 * KiB) override;
|
2019-11-04 19:39:17 +01:00
|
|
|
|
2020-12-01 19:55:41 +01:00
|
|
|
virtual void reset() override;
|
|
|
|
|
virtual void seek(const int position) override;
|
|
|
|
|
|
|
|
|
|
virtual int loaded_samples() override { return m_loaded_samples; }
|
|
|
|
|
virtual int total_samples() override { return m_total_samples; }
|
|
|
|
|
virtual u32 sample_rate() override { return m_sample_rate; }
|
|
|
|
|
virtual u16 num_channels() override { return m_num_channels; }
|
2021-04-26 17:13:04 +02:00
|
|
|
virtual PcmSampleFormat pcm_format() override { return m_sample_format; }
|
2020-12-01 19:55:41 +01:00
|
|
|
virtual RefPtr<Core::File> file() override { return m_file; }
|
2019-07-28 21:52:30 +02:00
|
|
|
|
2019-07-13 19:42:03 +02:00
|
|
|
private:
|
2019-07-27 17:20:41 +02:00
|
|
|
bool parse_header();
|
2020-12-01 19:55:41 +01:00
|
|
|
|
|
|
|
|
bool valid { false };
|
2020-02-02 12:34:39 +01:00
|
|
|
RefPtr<Core::File> m_file;
|
2020-12-02 15:44:45 +01:00
|
|
|
OwnPtr<InputMemoryStream> m_stream;
|
2019-07-13 19:42:03 +02:00
|
|
|
String m_error_string;
|
2020-02-06 10:40:02 +01:00
|
|
|
OwnPtr<ResampleHelper> m_resampler;
|
2019-07-27 17:20:41 +02:00
|
|
|
|
|
|
|
|
u32 m_sample_rate { 0 };
|
|
|
|
|
u16 m_num_channels { 0 };
|
2021-04-26 17:13:04 +02:00
|
|
|
PcmSampleFormat m_sample_format;
|
2019-07-28 21:52:30 +02:00
|
|
|
|
|
|
|
|
int m_loaded_samples { 0 };
|
|
|
|
|
int m_total_samples { 0 };
|
2019-07-13 19:42:03 +02:00
|
|
|
};
|
2020-02-06 10:40:02 +01:00
|
|
|
|
|
|
|
|
}
|