2020-01-18 08:38:21 +00: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.
|
|
|
|
*/
|
|
|
|
|
2020-04-15 14:52:04 +00:00
|
|
|
#include <AK/NumericLimits.h>
|
2020-02-14 21:29:06 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2020-12-02 14:44:45 +00:00
|
|
|
#include <LibAudio/Buffer.h>
|
2020-02-06 14:18:03 +00:00
|
|
|
#include <LibAudio/WavLoader.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <LibCore/IODeviceStreamReader.h>
|
2019-07-13 17:42:03 +00:00
|
|
|
|
2020-02-06 09:40:02 +00:00
|
|
|
namespace Audio {
|
|
|
|
|
2020-12-01 18:55:41 +00:00
|
|
|
WavLoaderPlugin::WavLoaderPlugin(const StringView& path)
|
2020-02-02 11:34:39 +00:00
|
|
|
: m_file(Core::File::construct(path))
|
2019-07-13 17:42:03 +00:00
|
|
|
{
|
2020-02-02 11:34:39 +00:00
|
|
|
if (!m_file->open(Core::IODevice::ReadOnly)) {
|
2020-10-15 11:04:32 +00:00
|
|
|
m_error_string = String::formatted("Can't open file: {}", m_file->error_string());
|
2019-07-27 15:20:41 +00:00
|
|
|
return;
|
2019-07-13 17:42:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:55:41 +00:00
|
|
|
valid = parse_header();
|
|
|
|
if (!valid)
|
2020-02-09 14:53:10 +00:00
|
|
|
return;
|
|
|
|
|
2020-02-06 09:40:02 +00:00
|
|
|
m_resampler = make<ResampleHelper>(m_sample_rate, 44100);
|
2019-07-27 15:20:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
WavLoaderPlugin::WavLoaderPlugin(const ByteBuffer& buffer)
|
|
|
|
{
|
|
|
|
m_stream = make<InputMemoryStream>(buffer);
|
|
|
|
if (!m_stream) {
|
|
|
|
m_error_string = String::formatted("Can't open memory stream");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
valid = parse_header();
|
|
|
|
if (!valid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_resampler = make<ResampleHelper>(m_sample_rate, 44100);
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:55:41 +00:00
|
|
|
bool WavLoaderPlugin::sniff()
|
|
|
|
{
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Buffer> WavLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_input)
|
2019-07-27 15:20:41 +00:00
|
|
|
{
|
2019-07-27 16:54:03 +00:00
|
|
|
#ifdef AWAVLOADER_DEBUG
|
2020-10-15 11:04:32 +00:00
|
|
|
dbgln("Read WAV of format PCM with num_channels {} sample rate {}, bits per sample {}", m_num_channels, m_sample_rate, m_bits_per_sample);
|
2019-07-27 16:54:03 +00:00
|
|
|
#endif
|
2020-12-02 14:44:45 +00:00
|
|
|
size_t samples_to_read = static_cast<int>(max_bytes_to_read_from_input) / (m_num_channels * (m_bits_per_sample / 8));
|
|
|
|
RefPtr<Buffer> buffer;
|
|
|
|
if (m_file) {
|
|
|
|
auto raw_samples = m_file->read(max_bytes_to_read_from_input);
|
|
|
|
if (raw_samples.is_empty())
|
|
|
|
return nullptr;
|
|
|
|
buffer = Buffer::from_pcm_data(raw_samples, *m_resampler, m_num_channels, m_bits_per_sample);
|
|
|
|
} else {
|
|
|
|
buffer = Buffer::from_pcm_stream(*m_stream, *m_resampler, m_num_channels, m_bits_per_sample, samples_to_read);
|
|
|
|
}
|
2020-10-02 13:59:28 +00:00
|
|
|
//Buffer contains normalized samples, but m_loaded_samples should contain the amount of actually loaded samples
|
2020-12-02 14:44:45 +00:00
|
|
|
m_loaded_samples += samples_to_read;
|
2019-11-05 17:34:39 +00:00
|
|
|
m_loaded_samples = min(m_total_samples, m_loaded_samples);
|
2019-07-28 19:52:30 +00:00
|
|
|
return buffer;
|
2019-07-13 17:42:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:55:41 +00:00
|
|
|
void WavLoaderPlugin::seek(const int position)
|
2019-11-04 18:39:17 +00:00
|
|
|
{
|
|
|
|
if (position < 0 || position > m_total_samples)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_loaded_samples = position;
|
2020-12-02 14:44:45 +00:00
|
|
|
size_t byte_position = position * m_num_channels * (m_bits_per_sample / 8);
|
|
|
|
|
|
|
|
if (m_file)
|
|
|
|
m_file->seek(byte_position);
|
|
|
|
else
|
|
|
|
m_stream->seek(byte_position);
|
2019-11-04 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:55:41 +00:00
|
|
|
void WavLoaderPlugin::reset()
|
2019-11-04 18:39:17 +00:00
|
|
|
{
|
|
|
|
seek(0);
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:55:41 +00:00
|
|
|
bool WavLoaderPlugin::parse_header()
|
2019-07-13 17:42:03 +00:00
|
|
|
{
|
2020-12-02 14:44:45 +00:00
|
|
|
OwnPtr<Core::IODeviceStreamReader> file_stream;
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
if (m_file)
|
|
|
|
file_stream = make<Core::IODeviceStreamReader>(*m_file);
|
|
|
|
|
|
|
|
auto read_u8 = [&]() -> u8 {
|
|
|
|
u8 value;
|
|
|
|
if (m_file) {
|
|
|
|
*file_stream >> value;
|
|
|
|
if (file_stream->handle_read_failure()) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto read_u16 = [&]() -> u16 {
|
|
|
|
u16 value;
|
|
|
|
if (m_file) {
|
|
|
|
*file_stream >> value;
|
|
|
|
if (file_stream->handle_read_failure()) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto read_u32 = [&]() -> u32 {
|
|
|
|
u32 value;
|
|
|
|
if (m_file) {
|
|
|
|
*file_stream >> value;
|
|
|
|
if (file_stream->handle_read_failure()) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CHECK_OK(msg) \
|
|
|
|
do { \
|
|
|
|
if (!ok) { \
|
|
|
|
m_error_string = String::formatted("Parsing failed: {}", msg); \
|
|
|
|
return {}; \
|
|
|
|
} \
|
2019-07-13 17:42:03 +00:00
|
|
|
} while (0);
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
u32 riff = read_u32();
|
2019-07-13 17:42:03 +00:00
|
|
|
ok = ok && riff == 0x46464952; // "RIFF"
|
|
|
|
CHECK_OK("RIFF header");
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
u32 sz = read_u32();
|
2019-07-27 18:49:15 +00:00
|
|
|
ok = ok && sz < 1024 * 1024 * 1024; // arbitrary
|
2019-07-13 17:42:03 +00:00
|
|
|
CHECK_OK("File size");
|
2019-07-27 18:49:15 +00:00
|
|
|
ASSERT(sz < 1024 * 1024 * 1024);
|
2019-07-13 17:42:03 +00:00
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
u32 wave = read_u32();
|
2019-07-13 17:42:03 +00:00
|
|
|
ok = ok && wave == 0x45564157; // "WAVE"
|
|
|
|
CHECK_OK("WAVE header");
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
u32 fmt_id = read_u32();
|
2019-07-13 17:42:03 +00:00
|
|
|
ok = ok && fmt_id == 0x20746D66; // "FMT"
|
|
|
|
CHECK_OK("FMT header");
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
u32 fmt_size = read_u32();
|
2019-07-13 17:42:03 +00:00
|
|
|
ok = ok && fmt_size == 16;
|
|
|
|
CHECK_OK("FMT size");
|
2019-07-13 22:28:30 +00:00
|
|
|
ASSERT(fmt_size == 16);
|
2019-07-13 17:42:03 +00:00
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
u16 audio_format = read_u16();
|
2019-11-04 18:39:17 +00:00
|
|
|
CHECK_OK("Audio format"); // incomplete read check
|
2019-07-13 17:42:03 +00:00
|
|
|
ok = ok && audio_format == 1; // WAVE_FORMAT_PCM
|
|
|
|
ASSERT(audio_format == 1);
|
2019-07-13 22:28:30 +00:00
|
|
|
CHECK_OK("Audio format"); // value check
|
2019-07-13 17:42:03 +00:00
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
m_num_channels = read_u16();
|
2019-07-27 15:20:41 +00:00
|
|
|
ok = ok && (m_num_channels == 1 || m_num_channels == 2);
|
2019-07-13 17:42:03 +00:00
|
|
|
CHECK_OK("Channel count");
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
m_sample_rate = read_u32();
|
2019-07-13 17:42:03 +00:00
|
|
|
CHECK_OK("Sample rate");
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
read_u32();
|
2019-07-13 17:42:03 +00:00
|
|
|
CHECK_OK("Byte rate");
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
read_u16();
|
2019-07-13 17:42:03 +00:00
|
|
|
CHECK_OK("Block align");
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
m_bits_per_sample = read_u16();
|
2019-07-13 22:28:30 +00:00
|
|
|
CHECK_OK("Bits per sample"); // incomplete read check
|
2019-07-27 15:20:41 +00:00
|
|
|
ok = ok && (m_bits_per_sample == 8 || m_bits_per_sample == 16 || m_bits_per_sample == 24);
|
|
|
|
ASSERT(m_bits_per_sample == 8 || m_bits_per_sample == 16 || m_bits_per_sample == 24);
|
2019-07-13 22:28:30 +00:00
|
|
|
CHECK_OK("Bits per sample"); // value check
|
2019-07-13 17:42:03 +00:00
|
|
|
|
|
|
|
// Read chunks until we find DATA
|
|
|
|
bool found_data = false;
|
|
|
|
u32 data_sz = 0;
|
2020-06-18 18:00:32 +00:00
|
|
|
u8 search_byte = 0;
|
2019-07-13 22:28:30 +00:00
|
|
|
while (true) {
|
2020-12-02 14:44:45 +00:00
|
|
|
search_byte = read_u8();
|
2020-06-18 18:00:32 +00:00
|
|
|
CHECK_OK("Reading byte searching for data");
|
|
|
|
if (search_byte != 0x64) //D
|
|
|
|
continue;
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
search_byte = read_u8();
|
2020-06-18 18:00:32 +00:00
|
|
|
CHECK_OK("Reading next byte searching for data");
|
|
|
|
if (search_byte != 0x61) //A
|
|
|
|
continue;
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
u16 search_remaining = read_u16();
|
2020-06-18 18:00:32 +00:00
|
|
|
CHECK_OK("Reading remaining bytes searching for data");
|
|
|
|
if (search_remaining != 0x6174) //TA
|
|
|
|
continue;
|
|
|
|
|
2020-12-02 14:44:45 +00:00
|
|
|
data_sz = read_u32();
|
2020-06-18 18:00:32 +00:00
|
|
|
found_data = true;
|
|
|
|
break;
|
2019-07-13 17:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ok = ok && found_data;
|
|
|
|
CHECK_OK("Found no data chunk");
|
2019-07-13 22:28:30 +00:00
|
|
|
ASSERT(found_data);
|
|
|
|
|
2019-07-27 15:20:41 +00:00
|
|
|
ok = ok && data_sz < INT32_MAX;
|
2019-07-13 22:28:30 +00:00
|
|
|
CHECK_OK("Data was too large");
|
|
|
|
|
2019-07-28 19:52:30 +00:00
|
|
|
int bytes_per_sample = (m_bits_per_sample / 8) * m_num_channels;
|
|
|
|
m_total_samples = data_sz / bytes_per_sample;
|
|
|
|
|
2019-07-27 15:20:41 +00:00
|
|
|
return true;
|
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 10:54:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 12:45:10 +00:00
|
|
|
ResampleHelper::ResampleHelper(double source, double target)
|
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 10:54:52 +00:00
|
|
|
: m_ratio(source / target)
|
|
|
|
{
|
2019-07-13 17:42:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 12:45:10 +00:00
|
|
|
void ResampleHelper::process_sample(double sample_l, double sample_r)
|
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 10:54:52 +00:00
|
|
|
{
|
2019-10-16 13:31:01 +00:00
|
|
|
m_last_sample_l = sample_l;
|
|
|
|
m_last_sample_r = sample_r;
|
|
|
|
m_current_ratio += 1;
|
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 10:54:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 12:45:10 +00:00
|
|
|
bool ResampleHelper::read_sample(double& next_l, double& next_r)
|
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 10:54:52 +00:00
|
|
|
{
|
2019-10-16 13:31:01 +00:00
|
|
|
if (m_current_ratio > 0) {
|
|
|
|
m_current_ratio -= m_ratio;
|
|
|
|
next_l = m_last_sample_l;
|
|
|
|
next_r = m_last_sample_r;
|
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 10:54:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|