From 0a93ec7173169ab8843446ecc7b436d943c818aa Mon Sep 17 00:00:00 2001 From: jwcullen Date: Tue, 23 Apr 2024 14:00:21 -0400 Subject: [PATCH] Adjust `WavWriter::WriteSamples` to take in an `std::vector`. - The new interface is harder to misuse and is simpler for how it was being used in practice. PiperOrigin-RevId: 627442563 --- .../adm_to_user_metadata/adm/wav_file_splicer.cc | 2 +- iamf/cli/audio_frame_decoder.cc | 2 +- iamf/cli/tests/wav_writer_test.cc | 15 +++++++-------- iamf/cli/wav_writer.cc | 4 +++- iamf/cli/wav_writer.h | 6 +++--- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/iamf/cli/adm_to_user_metadata/adm/wav_file_splicer.cc b/iamf/cli/adm_to_user_metadata/adm/wav_file_splicer.cc index 35d743e8..5bc1f904 100644 --- a/iamf/cli/adm_to_user_metadata/adm/wav_file_splicer.cc +++ b/iamf/cli/adm_to_user_metadata/adm/wav_file_splicer.cc @@ -61,7 +61,7 @@ void AbortAllWavWriters( void FlushToWavWriter(std::vector& samples_to_flush, WavWriter& wav_writer) { - wav_writer.WriteSamples(samples_to_flush.data(), samples_to_flush.size()); + wav_writer.WriteSamples(samples_to_flush); samples_to_flush.clear(); } diff --git a/iamf/cli/audio_frame_decoder.cc b/iamf/cli/audio_frame_decoder.cc index 58d31199..9ce73ca2 100644 --- a/iamf/cli/audio_frame_decoder.cc +++ b/iamf/cli/audio_frame_decoder.cc @@ -167,7 +167,7 @@ absl::Status WriteInterlacedSamplesToWav( /*big_endian=*/false, buffer)); // Write the raw PCM to a ".wav" file. - if (!wav_writer.WriteSamples(buffer.data(), buffer.size())) { + if (!wav_writer.WriteSamples(buffer)) { LOG_FIRST_N(WARNING, 1) << "Failed to write to wav file for substream ID: " << substream_id << "."; diff --git a/iamf/cli/tests/wav_writer_test.cc b/iamf/cli/tests/wav_writer_test.cc index ce65af19..d97a0898 100644 --- a/iamf/cli/tests/wav_writer_test.cc +++ b/iamf/cli/tests/wav_writer_test.cc @@ -52,7 +52,7 @@ TEST(WavWriterTest, InvalidBitDepthFailsAtWriting) { // `WriteSamples()` returning `false` means writing failed. std::vector samples(10, 0); - EXPECT_FALSE(wav_writer.WriteSamples(samples.data(), samples.size())); + EXPECT_FALSE(wav_writer.WriteSamples(samples)); } TEST(WavWriterTest, WriteEmptySamplesSucceeds) { @@ -60,8 +60,7 @@ TEST(WavWriterTest, WriteEmptySamplesSucceeds) { kBitDepth24); std::vector empty_samples(0); - EXPECT_TRUE( - wav_writer.WriteSamples(empty_samples.data(), empty_samples.size())); + EXPECT_TRUE(wav_writer.WriteSamples(empty_samples)); } TEST(WavWriterTest, WriteIntegerSamplesSucceeds) { @@ -70,7 +69,7 @@ TEST(WavWriterTest, WriteIntegerSamplesSucceeds) { // Bit depth = 16, and writing 6 bytes = 48 bits = 3 samples succeeds. std::vector samples(6, 0); - EXPECT_TRUE(wav_writer.WriteSamples(samples.data(), samples.size())); + EXPECT_TRUE(wav_writer.WriteSamples(samples)); } TEST(WavWriterTest, WriteNonIntegerNumberOfSamplesFails) { @@ -79,7 +78,7 @@ TEST(WavWriterTest, WriteNonIntegerNumberOfSamplesFails) { // Bit depth = 16, and writing 3 bytes = 24 bits = 1.5 samples fails. std::vector samples(3, 0); - EXPECT_FALSE(wav_writer.WriteSamples(samples.data(), samples.size())); + EXPECT_FALSE(wav_writer.WriteSamples(samples)); } TEST(WavWriterTest, Write24BitSamplesSucceeds) { @@ -88,7 +87,7 @@ TEST(WavWriterTest, Write24BitSamplesSucceeds) { // Bit depth = 24, and writing 6 bytes = 48 bits = 2 samples succeeds. std::vector samples(6, 0); - EXPECT_TRUE(wav_writer.WriteSamples(samples.data(), samples.size())); + EXPECT_TRUE(wav_writer.WriteSamples(samples)); } TEST(WavWriterTest, Write32BitSamplesSucceeds) { @@ -97,7 +96,7 @@ TEST(WavWriterTest, Write32BitSamplesSucceeds) { // Bit depth = 32, and writing 8 bytes = 64 bits = 2 samples succeeds. std::vector samples = {1, 0, 0, 0, 2, 0, 0, 0}; - EXPECT_TRUE(wav_writer.WriteSamples(samples.data(), samples.size())); + EXPECT_TRUE(wav_writer.WriteSamples(samples)); } TEST(WavWriterTest, OutputFileExists) { @@ -133,7 +132,7 @@ TEST(WavWriterTest, MovingSucceeds) { // Expect the new `wav_writer` can write samples. std::vector samples(10, 0); - EXPECT_TRUE(new_wav_writer.WriteSamples(samples.data(), samples.size())); + EXPECT_TRUE(new_wav_writer.WriteSamples(samples)); // Expect that aborting after moving deletes the output file. new_wav_writer.Abort(); diff --git a/iamf/cli/wav_writer.cc b/iamf/cli/wav_writer.cc index f53f09b2..fef82426 100644 --- a/iamf/cli/wav_writer.cc +++ b/iamf/cli/wav_writer.cc @@ -76,11 +76,13 @@ WavWriter::WavWriter(WavWriter&& original) } // Write samples for all channels. -bool WavWriter::WriteSamples(uint8_t* buffer, size_t buffer_size) { +bool WavWriter::WriteSamples(const std::vector& buffer) { if (file_ == nullptr) { return false; } + const auto buffer_size = buffer.size(); + if (buffer_size == 0) { // Nothing to write. return true; diff --git a/iamf/cli/wav_writer.h b/iamf/cli/wav_writer.h index f0c5f7ac..6c6fd84d 100644 --- a/iamf/cli/wav_writer.h +++ b/iamf/cli/wav_writer.h @@ -17,6 +17,7 @@ #include #include #include +#include namespace iamf_tools { @@ -53,14 +54,13 @@ class WavWriter { * * There must be an integer number of samples and the number of samples % * `num_channels()` must equal 0. The number of samples is implicitly - * calculated by `buffer_size` / (bit_depth / 8). + * calculated by `buffer.size()` / (bit_depth / 8). * * \param buffer Buffer of raw input PCM with channels interlaced and no * padding. - * \param buffer_size Size of the buffer in bytes. * \return `true` on success. `false` on failure. */ - bool WriteSamples(uint8_t* buffer, size_t buffer_size); + bool WriteSamples(const std::vector& buffer); /*\!brief Aborts the write process and deletes the wav file.*/ void Abort();