1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-17 21:25:07 +00:00
serenity/Userland/Libraries/LibAudio/WavWriter.h
kleines Filmröllchen cd2e890304 LibAudio: Handle all integer PCM sample formats "correctly" in WavWriter
WavWriter needs a TON of modernization work, but for now this commit
just tackles two FIXMEs by converting samples correctly into all
supported integer PCM formats. The supported formats are only signed
16-bit and unsigned 8-bit for now, but can be expanded later. At least
we don't produce horrible speaker-destroying noise when writing any
other format.
2023-06-22 21:45:54 +02:00

53 lines
1.6 KiB
C++

/*
* Copyright (c) 2020, William McPherson <willmcpherson2@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedString.h>
#include <AK/Noncopyable.h>
#include <AK/RefPtr.h>
#include <AK/StringView.h>
#include <LibAudio/Sample.h>
#include <LibAudio/SampleFormats.h>
#include <LibCore/File.h>
#include <LibCore/Forward.h>
namespace Audio {
class WavWriter {
AK_MAKE_NONCOPYABLE(WavWriter);
AK_MAKE_NONMOVABLE(WavWriter);
public:
static ErrorOr<NonnullOwnPtr<WavWriter>> create_from_file(StringView path, int sample_rate = 44100, u16 num_channels = 2, PcmSampleFormat sample_format = PcmSampleFormat::Int16);
WavWriter(int sample_rate = 44100, u16 num_channels = 2, PcmSampleFormat sample_format = PcmSampleFormat::Int16);
~WavWriter();
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; }
PcmSampleFormat sample_format() const { return m_sample_format; }
Core::File& file() const { return *m_file; }
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_sample_format(PcmSampleFormat sample_format) { m_sample_format = sample_format; }
private:
ErrorOr<void> write_header();
OwnPtr<Core::File> m_file;
bool m_finalized { false };
u32 m_sample_rate;
u16 m_num_channels;
PcmSampleFormat m_sample_format;
u32 m_data_sz { 0 };
};
}