2020-02-06 17:33:12 +11:00
|
|
|
/*
|
2020-12-25 12:15:16 +01:00
|
|
|
* Copyright (c) 2020, William McPherson <willmcpherson2@gmail.com>
|
2020-02-06 17:33:12 +11:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-06 17:33:12 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-02-08 21:08:01 +01:00
|
|
|
#include <AK/DeprecatedString.h>
|
2021-11-15 22:27:28 +01:00
|
|
|
#include <AK/Noncopyable.h>
|
2023-02-08 21:08:01 +01:00
|
|
|
#include <AK/RefPtr.h>
|
2020-02-06 17:33:12 +11:00
|
|
|
#include <AK/StringView.h>
|
2022-07-13 12:44:19 +02:00
|
|
|
#include <LibAudio/Sample.h>
|
2023-03-27 00:37:17 +00:00
|
|
|
#include <LibCore/File.h>
|
2023-02-08 21:08:01 +01:00
|
|
|
#include <LibCore/Forward.h>
|
2020-02-06 17:33:12 +11:00
|
|
|
|
|
|
|
|
namespace Audio {
|
|
|
|
|
|
|
|
|
|
class WavWriter {
|
2021-11-15 22:27:28 +01:00
|
|
|
AK_MAKE_NONCOPYABLE(WavWriter);
|
|
|
|
|
AK_MAKE_NONMOVABLE(WavWriter);
|
|
|
|
|
|
2020-02-06 17:33:12 +11:00
|
|
|
public:
|
2023-03-27 00:37:17 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<WavWriter>> create_from_file(StringView path, int sample_rate = 44100, u16 num_channels = 2, u16 bits_per_sample = 16);
|
2021-11-15 22:27:28 +01:00
|
|
|
WavWriter(int sample_rate = 44100, u16 num_channels = 2, u16 bits_per_sample = 16);
|
2020-02-06 17:33:12 +11:00
|
|
|
~WavWriter();
|
|
|
|
|
|
2023-03-27 00:37:17 +00:00
|
|
|
ErrorOr<void> write_samples(Span<Sample> samples);
|
2020-02-06 17:33:12 +11:00
|
|
|
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; }
|
2023-03-27 00:37:17 +00:00
|
|
|
Core::File& file() const { return *m_file; }
|
2020-02-06 17:33:12 +11:00
|
|
|
|
2023-03-27 00:37:17 +00:00
|
|
|
ErrorOr<void> set_file(StringView path);
|
2020-02-06 17:33:12 +11:00
|
|
|
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; }
|
|
|
|
|
|
|
|
|
|
private:
|
2023-03-27 00:37:17 +00:00
|
|
|
ErrorOr<void> write_header();
|
|
|
|
|
OwnPtr<Core::File> m_file;
|
2020-02-06 17:33:12 +11:00
|
|
|
bool m_finalized { false };
|
|
|
|
|
|
|
|
|
|
u32 m_sample_rate;
|
|
|
|
|
u16 m_num_channels;
|
|
|
|
|
u16 m_bits_per_sample;
|
|
|
|
|
u32 m_data_sz { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|