summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Userland/Applications/Piano/AudioPlayerLoop.cpp18
-rw-r--r--Userland/Applications/Piano/AudioPlayerLoop.h2
-rw-r--r--Userland/Applications/Piano/main.cpp8
-rw-r--r--Userland/Libraries/LibAudio/WavWriter.cpp72
-rw-r--r--Userland/Libraries/LibAudio/WavWriter.h20
5 files changed, 61 insertions, 59 deletions
diff --git a/Userland/Applications/Piano/AudioPlayerLoop.cpp b/Userland/Applications/Piano/AudioPlayerLoop.cpp
index 2a4d820410..82dd16edef 100644
--- a/Userland/Applications/Piano/AudioPlayerLoop.cpp
+++ b/Userland/Applications/Piano/AudioPlayerLoop.cpp
@@ -93,14 +93,14 @@ intptr_t AudioPlayerLoop::pipeline_thread_main()
// The track manager guards against allocations itself.
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.
- if (result.is_error()) [[unlikely]] {
+ if (auto result = send_audio_to_server(); result.is_error()) [[unlikely]] {
dbgln("Error in audio pipeline: {}", result.error());
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();
return static_cast<intptr_t>(0);
@@ -131,28 +131,32 @@ ErrorOr<void> AudioPlayerLoop::send_audio_to_server()
return {};
}
-void AudioPlayerLoop::write_wav_if_needed()
+ErrorOr<void> AudioPlayerLoop::write_wav_if_needed()
{
bool _true = true;
if (m_need_to_write_wav.compare_exchange_strong(_true, false)) {
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.set_should_loop(false);
do {
// 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_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());
// FIXME: Make sure that the new TrackManager APIs aren't as bad.
m_wav_percent_written.store(100);
m_track_manager.reset();
m_track_manager.set_should_loop(true);
wav_writer.finalize();
- });
+
+ return {};
+ }));
m_audio_client->async_start_playback();
}
+
+ return {};
}
void AudioPlayerLoop::toggle_paused()
diff --git a/Userland/Applications/Piano/AudioPlayerLoop.h b/Userland/Applications/Piano/AudioPlayerLoop.h
index 3a6713d5d3..c037af218b 100644
--- a/Userland/Applications/Piano/AudioPlayerLoop.h
+++ b/Userland/Applications/Piano/AudioPlayerLoop.h
@@ -34,7 +34,7 @@ private:
intptr_t pipeline_thread_main();
ErrorOr<void> send_audio_to_server();
- void write_wav_if_needed();
+ ErrorOr<void> write_wav_if_needed();
TrackManager& m_track_manager;
FixedArray<DSP::Sample> m_buffer;
diff --git a/Userland/Applications/Piano/main.cpp b/Userland/Applications/Piano/main.cpp
index 954d876a5a..ac15d9a81c 100644
--- a/Userland/Applications/Piano/main.cpp
+++ b/Userland/Applications/Piano/main.cpp
@@ -65,11 +65,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
return;
DeprecatedString error;
wav_writer.with_locked([&](auto& wav_writer) {
- wav_writer.set_file(save_path.value());
- if (wav_writer.has_error()) {
- error = DeprecatedString::formatted("Failed to export WAV file: {}", wav_writer.error_string());
- wav_writer.clear_error();
- }
+ auto error_or_void = wav_writer.set_file(save_path.value());
+ if (error_or_void.is_error())
+ error = DeprecatedString::formatted("Failed to export WAV file: {}", error_or_void.error());
});
if (!error.is_empty()) {
GUI::MessageBox::show_error(window, error);
diff --git a/Userland/Libraries/LibAudio/WavWriter.cpp b/Userland/Libraries/LibAudio/WavWriter.cpp
index bbba9b15e8..896ec15719 100644
--- a/Userland/Libraries/LibAudio/WavWriter.cpp
+++ b/Userland/Libraries/LibAudio/WavWriter.cpp
@@ -1,20 +1,19 @@
/*
* Copyright (c) 2020, William McPherson <willmcpherson2@gmail.com>
+ * Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibAudio/WavWriter.h>
-#include <LibCore/DeprecatedFile.h>
namespace Audio {
-WavWriter::WavWriter(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)
+ErrorOr<NonnullOwnPtr<WavWriter>> WavWriter::create_from_file(StringView path, int sample_rate, u16 num_channels, u16 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)
@@ -30,18 +29,15 @@ WavWriter::~WavWriter()
finalize();
}
-void WavWriter::set_file(StringView path)
+ErrorOr<void> WavWriter::set_file(StringView path)
{
- m_file = Core::DeprecatedFile::construct(path);
- if (!m_file->open(Core::OpenMode::ReadWrite)) {
- m_error_string = DeprecatedString::formatted("Can't open file: {}", m_file->error_string());
- return;
- }
- m_file->seek(44);
+ m_file = TRY(Core::File::open(path, Core::File::OpenMode::ReadWrite));
+ TRY(m_file->seek(44, SeekMode::SetPosition));
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);
@@ -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 right = static_cast<i16>(sample.right * static_cast<float>(1 << m_bits_per_sample));
// FIXME: This ignores endianness.
- m_file->write(bit_cast<u8 const*>(&left), sizeof(u16));
- m_file->write(bit_cast<u8 const*>(&right), sizeof(u16));
+ TRY(m_file->write_value(left));
+ TRY(m_file->write_value(right));
}
+
+ return {};
}
void WavWriter::finalize()
{
VERIFY(!m_finalized);
m_finalized = true;
- if (m_file) {
- m_file->seek(0);
- write_header();
+
+ if (m_file->is_open()) {
+ 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_data_sz = 0;
}
-void WavWriter::write_header()
+ErrorOr<void> WavWriter::write_header()
{
// "RIFF"
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)
u32 sz = m_data_sz + (44 - 4 - 4);
- m_file->write(reinterpret_cast<u8*>(&sz), sizeof(sz));
+ TRY(m_file->write_value(sz));
// "WAVE"
static u32 wave = 0x45564157;
- m_file->write(reinterpret_cast<u8*>(&wave), sizeof(wave));
+ TRY(m_file->write_value(wave));
// "fmt "
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
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
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);
- 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);
- 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"
static u32 chunk_id = 0x61746164;
- m_file->write(reinterpret_cast<u8*>(&chunk_id), sizeof(chunk_id));
+ TRY(m_file->write_value(chunk_id));
+
+ TRY(m_file->write_value(m_data_sz));
- m_file->write(reinterpret_cast<u8*>(&m_data_sz), sizeof(m_data_sz));
+ return {};
}
}
diff --git a/Userland/Libraries/LibAudio/WavWriter.h b/Userland/Libraries/LibAudio/WavWriter.h
index dbfbd5457e..8548d2f777 100644
--- a/Userland/Libraries/LibAudio/WavWriter.h
+++ b/Userland/Libraries/LibAudio/WavWriter.h
@@ -11,7 +11,7 @@
#include <AK/RefPtr.h>
#include <AK/StringView.h>
#include <LibAudio/Sample.h>
-#include <LibCore/DeprecatedFile.h>
+#include <LibCore/File.h>
#include <LibCore/Forward.h>
namespace Audio {
@@ -21,32 +21,26 @@ class WavWriter {
AK_MAKE_NONMOVABLE(WavWriter);
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();
- bool has_error() const { return !m_error_string.is_null(); }
- char const* error_string() const { return m_error_string.characters(); }
-
- void write_samples(Span<Sample> samples);
+ ErrorOr<void> write_samples(Span<Sample> samples);
void finalize(); // You can finalize manually or let the destructor do it.
u32 sample_rate() const { return m_sample_rate; }
u16 num_channels() const { return m_num_channels; }
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_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 clear_error() { m_error_string = DeprecatedString(); }
-
private:
- void write_header();
- RefPtr<Core::DeprecatedFile> m_file;
- DeprecatedString m_error_string;
+ ErrorOr<void> write_header();
+ OwnPtr<Core::File> m_file;
bool m_finalized { false };
u32 m_sample_rate;