mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:07:45 +00:00
Piano+LibAudio: Port to Core::File
This commit is contained in:
parent
fb8d4b7032
commit
7734eba03f
5 changed files with 61 additions and 59 deletions
|
@ -93,14 +93,14 @@ intptr_t AudioPlayerLoop::pipeline_thread_main()
|
||||||
// The track manager guards against allocations itself.
|
// The track manager guards against allocations itself.
|
||||||
m_track_manager.fill_buffer(m_buffer);
|
m_track_manager.fill_buffer(m_buffer);
|
||||||
|
|
||||||
auto result = send_audio_to_server();
|
|
||||||
// Tolerate errors in the audio pipeline; we don't want this thread to crash the program. This might likely happen with OOM.
|
// Tolerate errors in the audio pipeline; we don't want this thread to crash the program. This might likely happen with OOM.
|
||||||
if (result.is_error()) [[unlikely]] {
|
if (auto result = send_audio_to_server(); result.is_error()) [[unlikely]] {
|
||||||
dbgln("Error in audio pipeline: {}", result.error());
|
dbgln("Error in audio pipeline: {}", result.error());
|
||||||
m_track_manager.reset();
|
m_track_manager.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
write_wav_if_needed();
|
if (auto result = write_wav_if_needed(); result.is_error()) [[unlikely]]
|
||||||
|
dbgln("Error writing WAV: {}", result.error());
|
||||||
}
|
}
|
||||||
m_audio_client->async_pause_playback();
|
m_audio_client->async_pause_playback();
|
||||||
return static_cast<intptr_t>(0);
|
return static_cast<intptr_t>(0);
|
||||||
|
@ -131,28 +131,32 @@ ErrorOr<void> AudioPlayerLoop::send_audio_to_server()
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerLoop::write_wav_if_needed()
|
ErrorOr<void> AudioPlayerLoop::write_wav_if_needed()
|
||||||
{
|
{
|
||||||
bool _true = true;
|
bool _true = true;
|
||||||
if (m_need_to_write_wav.compare_exchange_strong(_true, false)) {
|
if (m_need_to_write_wav.compare_exchange_strong(_true, false)) {
|
||||||
m_audio_client->async_pause_playback();
|
m_audio_client->async_pause_playback();
|
||||||
m_wav_writer.with_locked([this](auto& wav_writer) {
|
TRY(m_wav_writer.with_locked([this](auto& wav_writer) -> ErrorOr<void> {
|
||||||
m_track_manager.reset();
|
m_track_manager.reset();
|
||||||
m_track_manager.set_should_loop(false);
|
m_track_manager.set_should_loop(false);
|
||||||
do {
|
do {
|
||||||
// FIXME: This progress detection is crude, but it works for now.
|
// FIXME: This progress detection is crude, but it works for now.
|
||||||
m_wav_percent_written.store(static_cast<int>(static_cast<float>(m_track_manager.transport()->time()) / roll_length * 100.0f));
|
m_wav_percent_written.store(static_cast<int>(static_cast<float>(m_track_manager.transport()->time()) / roll_length * 100.0f));
|
||||||
m_track_manager.fill_buffer(m_buffer);
|
m_track_manager.fill_buffer(m_buffer);
|
||||||
wav_writer.write_samples(m_buffer.span());
|
TRY(wav_writer.write_samples(m_buffer.span()));
|
||||||
} while (m_track_manager.transport()->time());
|
} while (m_track_manager.transport()->time());
|
||||||
// FIXME: Make sure that the new TrackManager APIs aren't as bad.
|
// FIXME: Make sure that the new TrackManager APIs aren't as bad.
|
||||||
m_wav_percent_written.store(100);
|
m_wav_percent_written.store(100);
|
||||||
m_track_manager.reset();
|
m_track_manager.reset();
|
||||||
m_track_manager.set_should_loop(true);
|
m_track_manager.set_should_loop(true);
|
||||||
wav_writer.finalize();
|
wav_writer.finalize();
|
||||||
});
|
|
||||||
|
return {};
|
||||||
|
}));
|
||||||
m_audio_client->async_start_playback();
|
m_audio_client->async_start_playback();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerLoop::toggle_paused()
|
void AudioPlayerLoop::toggle_paused()
|
||||||
|
|
|
@ -34,7 +34,7 @@ private:
|
||||||
|
|
||||||
intptr_t pipeline_thread_main();
|
intptr_t pipeline_thread_main();
|
||||||
ErrorOr<void> send_audio_to_server();
|
ErrorOr<void> send_audio_to_server();
|
||||||
void write_wav_if_needed();
|
ErrorOr<void> write_wav_if_needed();
|
||||||
|
|
||||||
TrackManager& m_track_manager;
|
TrackManager& m_track_manager;
|
||||||
FixedArray<DSP::Sample> m_buffer;
|
FixedArray<DSP::Sample> m_buffer;
|
||||||
|
|
|
@ -65,11 +65,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
return;
|
return;
|
||||||
DeprecatedString error;
|
DeprecatedString error;
|
||||||
wav_writer.with_locked([&](auto& wav_writer) {
|
wav_writer.with_locked([&](auto& wav_writer) {
|
||||||
wav_writer.set_file(save_path.value());
|
auto error_or_void = wav_writer.set_file(save_path.value());
|
||||||
if (wav_writer.has_error()) {
|
if (error_or_void.is_error())
|
||||||
error = DeprecatedString::formatted("Failed to export WAV file: {}", wav_writer.error_string());
|
error = DeprecatedString::formatted("Failed to export WAV file: {}", error_or_void.error());
|
||||||
wav_writer.clear_error();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
if (!error.is_empty()) {
|
if (!error.is_empty()) {
|
||||||
GUI::MessageBox::show_error(window, error);
|
GUI::MessageBox::show_error(window, error);
|
||||||
|
|
|
@ -1,20 +1,19 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, William McPherson <willmcpherson2@gmail.com>
|
* Copyright (c) 2020, William McPherson <willmcpherson2@gmail.com>
|
||||||
|
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibAudio/WavWriter.h>
|
#include <LibAudio/WavWriter.h>
|
||||||
#include <LibCore/DeprecatedFile.h>
|
|
||||||
|
|
||||||
namespace Audio {
|
namespace Audio {
|
||||||
|
|
||||||
WavWriter::WavWriter(StringView path, int sample_rate, u16 num_channels, u16 bits_per_sample)
|
ErrorOr<NonnullOwnPtr<WavWriter>> WavWriter::create_from_file(StringView path, int sample_rate, u16 num_channels, u16 bits_per_sample)
|
||||||
: m_sample_rate(sample_rate)
|
|
||||||
, m_num_channels(num_channels)
|
|
||||||
, m_bits_per_sample(bits_per_sample)
|
|
||||||
{
|
{
|
||||||
set_file(path);
|
auto wav_writer = TRY(adopt_nonnull_own_or_enomem(new (nothrow) WavWriter(sample_rate, num_channels, bits_per_sample)));
|
||||||
|
TRY(wav_writer->set_file(path));
|
||||||
|
return wav_writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
WavWriter::WavWriter(int sample_rate, u16 num_channels, u16 bits_per_sample)
|
WavWriter::WavWriter(int sample_rate, u16 num_channels, u16 bits_per_sample)
|
||||||
|
@ -30,18 +29,15 @@ WavWriter::~WavWriter()
|
||||||
finalize();
|
finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WavWriter::set_file(StringView path)
|
ErrorOr<void> WavWriter::set_file(StringView path)
|
||||||
{
|
{
|
||||||
m_file = Core::DeprecatedFile::construct(path);
|
m_file = TRY(Core::File::open(path, Core::File::OpenMode::ReadWrite));
|
||||||
if (!m_file->open(Core::OpenMode::ReadWrite)) {
|
TRY(m_file->seek(44, SeekMode::SetPosition));
|
||||||
m_error_string = DeprecatedString::formatted("Can't open file: {}", m_file->error_string());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_file->seek(44);
|
|
||||||
m_finalized = false;
|
m_finalized = false;
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void WavWriter::write_samples(Span<Sample> samples)
|
ErrorOr<void> WavWriter::write_samples(Span<Sample> samples)
|
||||||
{
|
{
|
||||||
m_data_sz += samples.size() * sizeof(Sample);
|
m_data_sz += samples.size() * sizeof(Sample);
|
||||||
|
|
||||||
|
@ -50,66 +46,76 @@ void WavWriter::write_samples(Span<Sample> samples)
|
||||||
u16 left = static_cast<i16>(sample.left * static_cast<float>(1 << m_bits_per_sample));
|
u16 left = static_cast<i16>(sample.left * static_cast<float>(1 << m_bits_per_sample));
|
||||||
u16 right = static_cast<i16>(sample.right * static_cast<float>(1 << m_bits_per_sample));
|
u16 right = static_cast<i16>(sample.right * static_cast<float>(1 << m_bits_per_sample));
|
||||||
// FIXME: This ignores endianness.
|
// FIXME: This ignores endianness.
|
||||||
m_file->write(bit_cast<u8 const*>(&left), sizeof(u16));
|
TRY(m_file->write_value(left));
|
||||||
m_file->write(bit_cast<u8 const*>(&right), sizeof(u16));
|
TRY(m_file->write_value(right));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void WavWriter::finalize()
|
void WavWriter::finalize()
|
||||||
{
|
{
|
||||||
VERIFY(!m_finalized);
|
VERIFY(!m_finalized);
|
||||||
m_finalized = true;
|
m_finalized = true;
|
||||||
if (m_file) {
|
|
||||||
m_file->seek(0);
|
if (m_file->is_open()) {
|
||||||
write_header();
|
auto result = [&]() -> ErrorOr<void> {
|
||||||
|
TRY(m_file->seek(0, SeekMode::SetPosition));
|
||||||
|
return TRY(write_header());
|
||||||
|
}();
|
||||||
|
|
||||||
|
if (result.is_error())
|
||||||
|
dbgln("Failed to finalize WavWriter: {}", result.error());
|
||||||
m_file->close();
|
m_file->close();
|
||||||
}
|
}
|
||||||
m_data_sz = 0;
|
m_data_sz = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WavWriter::write_header()
|
ErrorOr<void> WavWriter::write_header()
|
||||||
{
|
{
|
||||||
// "RIFF"
|
// "RIFF"
|
||||||
static u32 riff = 0x46464952;
|
static u32 riff = 0x46464952;
|
||||||
m_file->write(reinterpret_cast<u8*>(&riff), sizeof(riff));
|
TRY(m_file->write_value(riff));
|
||||||
|
|
||||||
// Size of data + (size of header - previous field - this field)
|
// Size of data + (size of header - previous field - this field)
|
||||||
u32 sz = m_data_sz + (44 - 4 - 4);
|
u32 sz = m_data_sz + (44 - 4 - 4);
|
||||||
m_file->write(reinterpret_cast<u8*>(&sz), sizeof(sz));
|
TRY(m_file->write_value(sz));
|
||||||
|
|
||||||
// "WAVE"
|
// "WAVE"
|
||||||
static u32 wave = 0x45564157;
|
static u32 wave = 0x45564157;
|
||||||
m_file->write(reinterpret_cast<u8*>(&wave), sizeof(wave));
|
TRY(m_file->write_value(wave));
|
||||||
|
|
||||||
// "fmt "
|
// "fmt "
|
||||||
static u32 fmt_id = 0x20746D66;
|
static u32 fmt_id = 0x20746D66;
|
||||||
m_file->write(reinterpret_cast<u8*>(&fmt_id), sizeof(fmt_id));
|
TRY(m_file->write_value(fmt_id));
|
||||||
|
|
||||||
// Size of the next 6 fields
|
// Size of the next 6 fields
|
||||||
static u32 fmt_size = 16;
|
static u32 fmt_size = 16;
|
||||||
m_file->write(reinterpret_cast<u8*>(&fmt_size), sizeof(fmt_size));
|
TRY(m_file->write_value(fmt_size));
|
||||||
|
|
||||||
// 1 for PCM
|
// 1 for PCM
|
||||||
static u16 audio_format = 1;
|
static u16 audio_format = 1;
|
||||||
m_file->write(reinterpret_cast<u8*>(&audio_format), sizeof(audio_format));
|
TRY(m_file->write_value(audio_format));
|
||||||
|
|
||||||
m_file->write(reinterpret_cast<u8*>(&m_num_channels), sizeof(m_num_channels));
|
TRY(m_file->write_value(m_num_channels));
|
||||||
|
|
||||||
m_file->write(reinterpret_cast<u8*>(&m_sample_rate), sizeof(m_sample_rate));
|
TRY(m_file->write_value(m_sample_rate));
|
||||||
|
|
||||||
u32 byte_rate = m_sample_rate * m_num_channels * (m_bits_per_sample / 8);
|
u32 byte_rate = m_sample_rate * m_num_channels * (m_bits_per_sample / 8);
|
||||||
m_file->write(reinterpret_cast<u8*>(&byte_rate), sizeof(byte_rate));
|
TRY(m_file->write_value(byte_rate));
|
||||||
|
|
||||||
u16 block_align = m_num_channels * (m_bits_per_sample / 8);
|
u16 block_align = m_num_channels * (m_bits_per_sample / 8);
|
||||||
m_file->write(reinterpret_cast<u8*>(&block_align), sizeof(block_align));
|
TRY(m_file->write_value(block_align));
|
||||||
|
|
||||||
m_file->write(reinterpret_cast<u8*>(&m_bits_per_sample), sizeof(m_bits_per_sample));
|
TRY(m_file->write_value(m_bits_per_sample));
|
||||||
|
|
||||||
// "data"
|
// "data"
|
||||||
static u32 chunk_id = 0x61746164;
|
static u32 chunk_id = 0x61746164;
|
||||||
m_file->write(reinterpret_cast<u8*>(&chunk_id), sizeof(chunk_id));
|
TRY(m_file->write_value(chunk_id));
|
||||||
|
|
||||||
m_file->write(reinterpret_cast<u8*>(&m_data_sz), sizeof(m_data_sz));
|
TRY(m_file->write_value(m_data_sz));
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
#include <AK/RefPtr.h>
|
#include <AK/RefPtr.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <LibAudio/Sample.h>
|
#include <LibAudio/Sample.h>
|
||||||
#include <LibCore/DeprecatedFile.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/Forward.h>
|
#include <LibCore/Forward.h>
|
||||||
|
|
||||||
namespace Audio {
|
namespace Audio {
|
||||||
|
@ -21,32 +21,26 @@ class WavWriter {
|
||||||
AK_MAKE_NONMOVABLE(WavWriter);
|
AK_MAKE_NONMOVABLE(WavWriter);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
WavWriter(StringView path, int sample_rate = 44100, u16 num_channels = 2, u16 bits_per_sample = 16);
|
static ErrorOr<NonnullOwnPtr<WavWriter>> create_from_file(StringView path, int sample_rate = 44100, u16 num_channels = 2, u16 bits_per_sample = 16);
|
||||||
WavWriter(int sample_rate = 44100, u16 num_channels = 2, u16 bits_per_sample = 16);
|
WavWriter(int sample_rate = 44100, u16 num_channels = 2, u16 bits_per_sample = 16);
|
||||||
~WavWriter();
|
~WavWriter();
|
||||||
|
|
||||||
bool has_error() const { return !m_error_string.is_null(); }
|
ErrorOr<void> write_samples(Span<Sample> samples);
|
||||||
char const* error_string() const { return m_error_string.characters(); }
|
|
||||||
|
|
||||||
void write_samples(Span<Sample> samples);
|
|
||||||
void finalize(); // You can finalize manually or let the destructor do it.
|
void finalize(); // You can finalize manually or let the destructor do it.
|
||||||
|
|
||||||
u32 sample_rate() const { return m_sample_rate; }
|
u32 sample_rate() const { return m_sample_rate; }
|
||||||
u16 num_channels() const { return m_num_channels; }
|
u16 num_channels() const { return m_num_channels; }
|
||||||
u16 bits_per_sample() const { return m_bits_per_sample; }
|
u16 bits_per_sample() const { return m_bits_per_sample; }
|
||||||
RefPtr<Core::DeprecatedFile> file() const { return m_file; }
|
Core::File& file() const { return *m_file; }
|
||||||
|
|
||||||
void set_file(StringView path);
|
ErrorOr<void> set_file(StringView path);
|
||||||
void set_num_channels(int num_channels) { m_num_channels = num_channels; }
|
void set_num_channels(int num_channels) { m_num_channels = num_channels; }
|
||||||
void set_sample_rate(int sample_rate) { m_sample_rate = sample_rate; }
|
void set_sample_rate(int sample_rate) { m_sample_rate = sample_rate; }
|
||||||
void set_bits_per_sample(int bits_per_sample) { m_bits_per_sample = bits_per_sample; }
|
void set_bits_per_sample(int bits_per_sample) { m_bits_per_sample = bits_per_sample; }
|
||||||
|
|
||||||
void clear_error() { m_error_string = DeprecatedString(); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void write_header();
|
ErrorOr<void> write_header();
|
||||||
RefPtr<Core::DeprecatedFile> m_file;
|
OwnPtr<Core::File> m_file;
|
||||||
DeprecatedString m_error_string;
|
|
||||||
bool m_finalized { false };
|
bool m_finalized { false };
|
||||||
|
|
||||||
u32 m_sample_rate;
|
u32 m_sample_rate;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue