encoder/lame: eliminate the audio_format field

This commit is contained in:
Max Kellermann 2022-07-12 12:05:10 +02:00
parent c266fb7758
commit 3e2de560ca
2 changed files with 14 additions and 18 deletions

View File

@ -31,18 +31,16 @@
#include <stdexcept> #include <stdexcept>
class LameEncoder final : public Encoder { class LameEncoder final : public Encoder {
const AudioFormat audio_format;
lame_global_flags *const gfp; lame_global_flags *const gfp;
ReusableArray<std::byte, 32768> output_buffer; ReusableArray<std::byte, 32768> output_buffer;
std::span<const std::byte> output{}; std::span<const std::byte> output{};
public: public:
LameEncoder(const AudioFormat _audio_format, static constexpr unsigned CHANNELS = 2;
lame_global_flags *_gfp) noexcept
:Encoder(false), explicit LameEncoder(lame_global_flags *_gfp) noexcept
audio_format(_audio_format), gfp(_gfp) {} :Encoder(false), gfp(_gfp) {}
~LameEncoder() noexcept override; ~LameEncoder() noexcept override;
@ -144,7 +142,7 @@ Encoder *
PreparedLameEncoder::Open(AudioFormat &audio_format) PreparedLameEncoder::Open(AudioFormat &audio_format)
{ {
audio_format.format = SampleFormat::S16; audio_format.format = SampleFormat::S16;
audio_format.channels = 2; audio_format.channels = LameEncoder::CHANNELS;
auto gfp = lame_init(); auto gfp = lame_init();
if (gfp == nullptr) if (gfp == nullptr)
@ -157,7 +155,7 @@ PreparedLameEncoder::Open(AudioFormat &audio_format)
throw; throw;
} }
return new LameEncoder(audio_format, gfp); return new LameEncoder(gfp);
} }
LameEncoder::~LameEncoder() noexcept LameEncoder::~LameEncoder() noexcept
@ -173,7 +171,7 @@ LameEncoder::Write(std::span<const std::byte> _src)
assert(output.empty()); assert(output.empty());
const std::size_t num_samples = src.size(); const std::size_t num_samples = src.size();
const std::size_t num_frames = num_samples / audio_format.channels; const std::size_t num_frames = num_samples / CHANNELS;
/* worst-case formula according to LAME documentation */ /* worst-case formula according to LAME documentation */
const std::size_t output_buffer_size = 5 * num_samples / 4 + 7200; const std::size_t output_buffer_size = 5 * num_samples / 4 + 7200;

View File

@ -32,8 +32,6 @@
#include <stdexcept> #include <stdexcept>
class TwolameEncoder final : public Encoder { class TwolameEncoder final : public Encoder {
const AudioFormat audio_format;
twolame_options *options; twolame_options *options;
std::byte output_buffer[32768]; std::byte output_buffer[32768];
@ -46,10 +44,10 @@ class TwolameEncoder final : public Encoder {
bool flush = false; bool flush = false;
public: public:
TwolameEncoder(const AudioFormat _audio_format, static constexpr unsigned CHANNELS = 2;
twolame_options *_options)
:Encoder(false), explicit TwolameEncoder(twolame_options *_options) noexcept
audio_format(_audio_format), options(_options) {} :Encoder(false), options(_options) {}
~TwolameEncoder() noexcept override; ~TwolameEncoder() noexcept override;
TwolameEncoder(const TwolameEncoder &) = delete; TwolameEncoder(const TwolameEncoder &) = delete;
@ -162,7 +160,7 @@ Encoder *
PreparedTwolameEncoder::Open(AudioFormat &audio_format) PreparedTwolameEncoder::Open(AudioFormat &audio_format)
{ {
audio_format.format = SampleFormat::S16; audio_format.format = SampleFormat::S16;
audio_format.channels = 2; audio_format.channels = TwolameEncoder::CHANNELS;
auto options = twolame_init(); auto options = twolame_init();
if (options == nullptr) if (options == nullptr)
@ -176,7 +174,7 @@ PreparedTwolameEncoder::Open(AudioFormat &audio_format)
throw; throw;
} }
return new TwolameEncoder(audio_format, options); return new TwolameEncoder(options);
} }
TwolameEncoder::~TwolameEncoder() noexcept TwolameEncoder::~TwolameEncoder() noexcept
@ -191,7 +189,7 @@ TwolameEncoder::Write(std::span<const std::byte> _src)
assert(fill == 0); assert(fill == 0);
const std::size_t num_frames = src.size() / audio_format.channels; const std::size_t num_frames = src.size() / CHANNELS;
int bytes_out = twolame_encode_buffer_interleaved(options, int bytes_out = twolame_encode_buffer_interleaved(options,
src.data(), num_frames, src.data(), num_frames,