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.
|
|
|
|
|
*/
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 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; }
|
|
|
|
|
virtual u16 bits_per_sample() override { return m_bits_per_sample; }
|
|
|
|
|
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 };
|
|
|
|
|
u16 m_bits_per_sample { 0 };
|
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
|
|
|
|
|
|
|
|
}
|