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-14 00:28:30 +02:00
|
|
|
#include <AK/BufferStream.h>
|
2020-02-06 15:18:03 +01:00
|
|
|
#include <LibAudio/WavLoader.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <LibCore/IODeviceStreamReader.h>
|
2019-07-14 00:28:30 +02:00
|
|
|
#include <limits>
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
namespace Audio {
|
|
|
|
|
|
|
|
WavLoader::WavLoader(const StringView& path)
|
2020-02-02 12:34:39 +01:00
|
|
|
: m_file(Core::File::construct(path))
|
2019-07-13 19:42:03 +02:00
|
|
|
{
|
2020-02-02 12:34:39 +01:00
|
|
|
if (!m_file->open(Core::IODevice::ReadOnly)) {
|
2019-09-21 20:50:06 +02:00
|
|
|
m_error_string = String::format("Can't open file: %s", m_file->error_string());
|
2019-07-27 17:20:41 +02:00
|
|
|
return;
|
2019-07-13 19:42:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-27 17:20:41 +02:00
|
|
|
parse_header();
|
2020-02-06 10:40:02 +01:00
|
|
|
m_resampler = make<ResampleHelper>(m_sample_rate, 44100);
|
2019-07-27 17:20:41 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
RefPtr<Buffer> WavLoader::get_more_samples(size_t max_bytes_to_read_from_input)
|
2019-07-27 17:20:41 +02:00
|
|
|
{
|
2019-07-27 18:54:03 +02:00
|
|
|
#ifdef AWAVLOADER_DEBUG
|
2019-07-27 17:20:41 +02:00
|
|
|
dbgprintf("Read WAV of format PCM with num_channels %u sample rate %u, bits per sample %u\n", m_num_channels, m_sample_rate, m_bits_per_sample);
|
2019-07-27 18:54:03 +02:00
|
|
|
#endif
|
2019-07-27 17:20:41 +02:00
|
|
|
|
2019-09-21 20:50:06 +02:00
|
|
|
auto raw_samples = m_file->read(max_bytes_to_read_from_input);
|
2019-07-27 18:54:03 +02:00
|
|
|
if (raw_samples.is_empty())
|
|
|
|
return nullptr;
|
2019-07-28 21:52:30 +02:00
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
auto buffer = Buffer::from_pcm_data(raw_samples, *m_resampler, m_num_channels, m_bits_per_sample);
|
2019-11-05 18:34:39 +01:00
|
|
|
//Buffer contains normalized samples, but m_loaded_samples should containt the ammount of actually loaded samples
|
|
|
|
m_loaded_samples += static_cast<int>(max_bytes_to_read_from_input) / (m_num_channels * (m_bits_per_sample / 8));
|
|
|
|
m_loaded_samples = min(m_total_samples, m_loaded_samples);
|
2019-07-28 21:52:30 +02:00
|
|
|
return buffer;
|
2019-07-13 19:42:03 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
void WavLoader::seek(const int position)
|
2019-11-04 19:39:17 +01:00
|
|
|
{
|
|
|
|
if (position < 0 || position > m_total_samples)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_loaded_samples = position;
|
|
|
|
m_file->seek(position * m_num_channels * (m_bits_per_sample / 8));
|
|
|
|
}
|
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
void WavLoader::reset()
|
2019-11-04 19:39:17 +01:00
|
|
|
{
|
|
|
|
seek(0);
|
|
|
|
}
|
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
bool WavLoader::parse_header()
|
2019-07-13 19:42:03 +02:00
|
|
|
{
|
2020-02-02 12:34:39 +01:00
|
|
|
Core::IODeviceStreamReader stream(*m_file);
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2019-07-27 14:30:09 +02:00
|
|
|
#define CHECK_OK(msg) \
|
|
|
|
do { \
|
|
|
|
ASSERT(ok); \
|
|
|
|
if (stream.handle_read_failure()) { \
|
2019-07-14 00:28:30 +02:00
|
|
|
m_error_string = String::format("Premature stream EOF at %s", msg); \
|
2019-07-27 14:30:09 +02:00
|
|
|
return {}; \
|
|
|
|
} \
|
|
|
|
if (!ok) { \
|
|
|
|
m_error_string = String::format("Parsing failed: %s", msg); \
|
|
|
|
return {}; \
|
|
|
|
} else { \
|
|
|
|
dbgprintf("%s is OK!\n", msg); \
|
|
|
|
} \
|
2019-07-13 19:42:03 +02:00
|
|
|
} while (0);
|
|
|
|
|
|
|
|
bool ok = true;
|
2019-07-27 14:30:09 +02:00
|
|
|
u32 riff;
|
|
|
|
stream >> riff;
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && riff == 0x46464952; // "RIFF"
|
|
|
|
CHECK_OK("RIFF header");
|
|
|
|
|
2019-07-27 14:30:09 +02:00
|
|
|
u32 sz;
|
|
|
|
stream >> sz;
|
2019-07-27 20:49:15 +02:00
|
|
|
ok = ok && sz < 1024 * 1024 * 1024; // arbitrary
|
2019-07-13 19:42:03 +02:00
|
|
|
CHECK_OK("File size");
|
2019-07-27 20:49:15 +02:00
|
|
|
ASSERT(sz < 1024 * 1024 * 1024);
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2019-07-27 14:30:09 +02:00
|
|
|
u32 wave;
|
|
|
|
stream >> wave;
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && wave == 0x45564157; // "WAVE"
|
|
|
|
CHECK_OK("WAVE header");
|
|
|
|
|
2019-07-27 14:30:09 +02:00
|
|
|
u32 fmt_id;
|
|
|
|
stream >> fmt_id;
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && fmt_id == 0x20746D66; // "FMT"
|
|
|
|
CHECK_OK("FMT header");
|
|
|
|
|
2019-07-27 14:30:09 +02:00
|
|
|
u32 fmt_size;
|
|
|
|
stream >> fmt_size;
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && fmt_size == 16;
|
|
|
|
CHECK_OK("FMT size");
|
2019-07-14 00:28:30 +02:00
|
|
|
ASSERT(fmt_size == 16);
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2019-07-27 14:30:09 +02:00
|
|
|
u16 audio_format;
|
|
|
|
stream >> audio_format;
|
2019-11-04 19:39:17 +01:00
|
|
|
CHECK_OK("Audio format"); // incomplete read check
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && audio_format == 1; // WAVE_FORMAT_PCM
|
|
|
|
ASSERT(audio_format == 1);
|
2019-07-14 00:28:30 +02:00
|
|
|
CHECK_OK("Audio format"); // value check
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2019-07-27 17:20:41 +02:00
|
|
|
stream >> m_num_channels;
|
|
|
|
ok = ok && (m_num_channels == 1 || m_num_channels == 2);
|
2019-07-13 19:42:03 +02:00
|
|
|
CHECK_OK("Channel count");
|
|
|
|
|
2019-07-27 17:20:41 +02:00
|
|
|
stream >> m_sample_rate;
|
2019-07-13 19:42:03 +02:00
|
|
|
CHECK_OK("Sample rate");
|
|
|
|
|
2019-07-27 14:30:09 +02:00
|
|
|
u32 byte_rate;
|
|
|
|
stream >> byte_rate;
|
2019-07-13 19:42:03 +02:00
|
|
|
CHECK_OK("Byte rate");
|
|
|
|
|
2019-07-27 14:30:09 +02:00
|
|
|
u16 block_align;
|
|
|
|
stream >> block_align;
|
2019-07-13 19:42:03 +02:00
|
|
|
CHECK_OK("Block align");
|
|
|
|
|
2019-07-27 17:20:41 +02:00
|
|
|
stream >> m_bits_per_sample;
|
2019-07-14 00:28:30 +02:00
|
|
|
CHECK_OK("Bits per sample"); // incomplete read check
|
2019-07-27 17:20:41 +02: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-14 00:28:30 +02:00
|
|
|
CHECK_OK("Bits per sample"); // value check
|
2019-07-13 19:42:03 +02:00
|
|
|
|
|
|
|
// Read chunks until we find DATA
|
|
|
|
bool found_data = false;
|
|
|
|
u32 data_sz = 0;
|
2019-07-14 00:28:30 +02:00
|
|
|
while (true) {
|
2019-07-27 14:30:09 +02:00
|
|
|
u32 chunk_id;
|
|
|
|
stream >> chunk_id;
|
2019-07-14 00:28:30 +02:00
|
|
|
CHECK_OK("Reading chunk ID searching for data");
|
|
|
|
stream >> data_sz;
|
|
|
|
CHECK_OK("Reading chunk size searching for data");
|
2019-07-13 19:42:03 +02:00
|
|
|
if (chunk_id == 0x61746164) { // DATA
|
|
|
|
found_data = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = ok && found_data;
|
|
|
|
CHECK_OK("Found no data chunk");
|
2019-07-14 00:28:30 +02:00
|
|
|
ASSERT(found_data);
|
|
|
|
|
2019-07-27 17:20:41 +02:00
|
|
|
ok = ok && data_sz < INT32_MAX;
|
2019-07-14 00:28:30 +02:00
|
|
|
CHECK_OK("Data was too large");
|
|
|
|
|
2019-07-28 21:52:30 +02:00
|
|
|
int bytes_per_sample = (m_bits_per_sample / 8) * m_num_channels;
|
|
|
|
m_total_samples = data_sz / bytes_per_sample;
|
|
|
|
|
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
|
|
|
// Just make sure we're good before we read the data...
|
2019-07-14 00:28:30 +02:00
|
|
|
ASSERT(!stream.handle_read_failure());
|
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-27 17:20:41 +02: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 12:54:52 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
ResampleHelper::ResampleHelper(float source, float 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 12:54:52 +02:00
|
|
|
: m_ratio(source / target)
|
|
|
|
{
|
2019-07-13 19:42:03 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
void ResampleHelper::process_sample(float sample_l, float 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 12:54:52 +02:00
|
|
|
{
|
2019-10-16 15:31:01 +02: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 12:54:52 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
bool ResampleHelper::read_sample(float& next_l, float& 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 12:54:52 +02:00
|
|
|
{
|
2019-10-16 15:31:01 +02: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 12:54:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:39:17 +01:00
|
|
|
template<typename SampleReader>
|
2020-02-06 10:40:02 +01:00
|
|
|
static void read_samples_from_stream(BufferStream& stream, SampleReader read_sample, Vector<Sample>& samples, ResampleHelper& resampler, int num_channels)
|
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
|
|
|
{
|
|
|
|
float norm_l = 0;
|
|
|
|
float norm_r = 0;
|
2019-10-16 15:31:01 +02: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
|
|
|
switch (num_channels) {
|
|
|
|
case 1:
|
2019-11-04 19:39:17 +01:00
|
|
|
for (;;) {
|
2019-10-16 15:31:01 +02:00
|
|
|
while (resampler.read_sample(norm_l, norm_r)) {
|
2020-02-06 10:40:02 +01:00
|
|
|
samples.append(Sample(norm_l));
|
2019-10-16 15:31:01 +02:00
|
|
|
}
|
|
|
|
norm_l = read_sample(stream);
|
|
|
|
|
|
|
|
if (stream.handle_read_failure()) {
|
|
|
|
break;
|
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-10-16 15:31:01 +02:00
|
|
|
resampler.process_sample(norm_l, norm_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 12:54:52 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
2019-11-04 19:39:17 +01:00
|
|
|
for (;;) {
|
2019-10-16 15:31:01 +02:00
|
|
|
while (resampler.read_sample(norm_l, norm_r)) {
|
2020-02-06 10:40:02 +01:00
|
|
|
samples.append(Sample(norm_l, norm_r));
|
2019-10-16 15:31:01 +02:00
|
|
|
}
|
|
|
|
norm_l = read_sample(stream);
|
|
|
|
norm_r = read_sample(stream);
|
|
|
|
|
|
|
|
if (stream.handle_read_failure()) {
|
|
|
|
break;
|
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-10-16 15:31:01 +02:00
|
|
|
resampler.process_sample(norm_l, norm_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 12:54:52 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:32:54 +02:00
|
|
|
static float read_norm_sample_24(BufferStream& stream)
|
2019-07-18 13:24:01 +02:00
|
|
|
{
|
2019-07-18 13:32:54 +02:00
|
|
|
u8 byte = 0;
|
|
|
|
stream >> byte;
|
|
|
|
u32 sample1 = byte;
|
|
|
|
stream >> byte;
|
|
|
|
u32 sample2 = byte;
|
|
|
|
stream >> byte;
|
|
|
|
u32 sample3 = byte;
|
|
|
|
|
|
|
|
i32 value = 0;
|
|
|
|
value = sample1 << 8;
|
|
|
|
value |= (sample2 << 16);
|
|
|
|
value |= (sample3 << 24);
|
|
|
|
return float(value) / std::numeric_limits<i32>::max();
|
|
|
|
}
|
2019-07-18 13:24:01 +02:00
|
|
|
|
2019-07-18 13:32:54 +02:00
|
|
|
static float read_norm_sample_16(BufferStream& stream)
|
|
|
|
{
|
|
|
|
i16 sample = 0;
|
|
|
|
stream >> sample;
|
|
|
|
return float(sample) / std::numeric_limits<i16>::max();
|
|
|
|
}
|
2019-07-18 13:24:01 +02:00
|
|
|
|
2019-07-18 13:32:54 +02:00
|
|
|
static float read_norm_sample_8(BufferStream& stream)
|
|
|
|
{
|
|
|
|
u8 sample = 0;
|
|
|
|
stream >> sample;
|
|
|
|
return float(sample) / std::numeric_limits<u8>::max();
|
2019-07-18 13:24:01 +02: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
|
|
|
// ### can't const this because BufferStream is non-const
|
|
|
|
// perhaps we need a reading class separate from the writing one, that can be
|
|
|
|
// entirely consted.
|
2020-02-06 10:40:02 +01:00
|
|
|
RefPtr<Buffer> Buffer::from_pcm_data(ByteBuffer& data, ResampleHelper& resampler, int num_channels, int bits_per_sample)
|
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
|
|
|
{
|
|
|
|
BufferStream stream(data);
|
2020-02-06 10:40:02 +01:00
|
|
|
Vector<Sample> fdata;
|
2019-11-04 19:39:17 +01:00
|
|
|
fdata.ensure_capacity(data.size() / (bits_per_sample / 8));
|
2019-07-28 21:54:34 +02:00
|
|
|
#ifdef AWAVLOADER_DEBUG
|
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
|
|
|
dbg() << "Reading " << bits_per_sample << " bits and " << num_channels << " channels, total bytes: " << data.size();
|
2019-07-28 21:54:34 +02:00
|
|
|
#endif
|
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
|
|
|
|
|
|
|
switch (bits_per_sample) {
|
|
|
|
case 8:
|
2019-10-16 15:31:01 +02:00
|
|
|
read_samples_from_stream(stream, read_norm_sample_8, fdata, resampler, num_channels);
|
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
|
|
|
break;
|
|
|
|
case 16:
|
2019-10-16 15:31:01 +02:00
|
|
|
read_samples_from_stream(stream, read_norm_sample_16, fdata, resampler, num_channels);
|
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
|
|
|
break;
|
2019-07-18 13:24:01 +02:00
|
|
|
case 24:
|
2019-10-16 15:31:01 +02:00
|
|
|
read_samples_from_stream(stream, read_norm_sample_24, fdata, resampler, num_channels);
|
2019-07-18 13:24:01 +02:00
|
|
|
break;
|
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
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should handle this in a better way above, but for now --
|
|
|
|
// just make sure we're good. Worst case we just write some 0s where they
|
|
|
|
// don't belong.
|
|
|
|
ASSERT(!stream.handle_read_failure());
|
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
return Buffer::create_with_samples(move(fdata));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|