summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorCameron Youell <cameronyouell@gmail.com>2023-03-27 00:37:17 +0000
committerAndrew Kaster <andrewdkaster@gmail.com>2023-04-09 20:58:54 -0600
commit7734eba03f8ded9005d036f48e6a5f4cc99025e7 (patch)
tree53f0bea4b262761bb86c571e72ad52638b49ad35 /Userland/Libraries
parentfb8d4b703247a4edee13e8459caa7d1bd541deb1 (diff)
downloadserenity-7734eba03f8ded9005d036f48e6a5f4cc99025e7.zip
Piano+LibAudio: Port to `Core::File`
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibAudio/WavWriter.cpp72
-rw-r--r--Userland/Libraries/LibAudio/WavWriter.h20
2 files changed, 46 insertions, 46 deletions
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;