Skip to content

Commit

Permalink
Adjust WavWriter::WriteSamples to take in an std::vector.
Browse files Browse the repository at this point in the history
  - The new interface is harder to misuse and is simpler for how it was being used in practice.

PiperOrigin-RevId: 627442563
  • Loading branch information
jwcullen committed Apr 24, 2024
1 parent 9f22540 commit 0a93ec7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion iamf/cli/adm_to_user_metadata/adm/wav_file_splicer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void AbortAllWavWriters(

void FlushToWavWriter(std::vector<uint8_t>& 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();
}

Expand Down
2 changes: 1 addition & 1 deletion iamf/cli/audio_frame_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
<< ".";
Expand Down
15 changes: 7 additions & 8 deletions iamf/cli/tests/wav_writer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@ TEST(WavWriterTest, InvalidBitDepthFailsAtWriting) {

// `WriteSamples()` returning `false` means writing failed.
std::vector<uint8_t> samples(10, 0);
EXPECT_FALSE(wav_writer.WriteSamples(samples.data(), samples.size()));
EXPECT_FALSE(wav_writer.WriteSamples(samples));
}

TEST(WavWriterTest, WriteEmptySamplesSucceeds) {
WavWriter wav_writer(GetTestWavPath(), kNumChannels, kSampleRateHz,
kBitDepth24);

std::vector<uint8_t> empty_samples(0);
EXPECT_TRUE(
wav_writer.WriteSamples(empty_samples.data(), empty_samples.size()));
EXPECT_TRUE(wav_writer.WriteSamples(empty_samples));
}

TEST(WavWriterTest, WriteIntegerSamplesSucceeds) {
Expand All @@ -70,7 +69,7 @@ TEST(WavWriterTest, WriteIntegerSamplesSucceeds) {

// Bit depth = 16, and writing 6 bytes = 48 bits = 3 samples succeeds.
std::vector<uint8_t> samples(6, 0);
EXPECT_TRUE(wav_writer.WriteSamples(samples.data(), samples.size()));
EXPECT_TRUE(wav_writer.WriteSamples(samples));
}

TEST(WavWriterTest, WriteNonIntegerNumberOfSamplesFails) {
Expand All @@ -79,7 +78,7 @@ TEST(WavWriterTest, WriteNonIntegerNumberOfSamplesFails) {

// Bit depth = 16, and writing 3 bytes = 24 bits = 1.5 samples fails.
std::vector<uint8_t> samples(3, 0);
EXPECT_FALSE(wav_writer.WriteSamples(samples.data(), samples.size()));
EXPECT_FALSE(wav_writer.WriteSamples(samples));
}

TEST(WavWriterTest, Write24BitSamplesSucceeds) {
Expand All @@ -88,7 +87,7 @@ TEST(WavWriterTest, Write24BitSamplesSucceeds) {

// Bit depth = 24, and writing 6 bytes = 48 bits = 2 samples succeeds.
std::vector<uint8_t> samples(6, 0);
EXPECT_TRUE(wav_writer.WriteSamples(samples.data(), samples.size()));
EXPECT_TRUE(wav_writer.WriteSamples(samples));
}

TEST(WavWriterTest, Write32BitSamplesSucceeds) {
Expand All @@ -97,7 +96,7 @@ TEST(WavWriterTest, Write32BitSamplesSucceeds) {

// Bit depth = 32, and writing 8 bytes = 64 bits = 2 samples succeeds.
std::vector<uint8_t> 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) {
Expand Down Expand Up @@ -133,7 +132,7 @@ TEST(WavWriterTest, MovingSucceeds) {

// Expect the new `wav_writer` can write samples.
std::vector<uint8_t> 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();
Expand Down
4 changes: 3 additions & 1 deletion iamf/cli/wav_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>& buffer) {
if (file_ == nullptr) {
return false;
}

const auto buffer_size = buffer.size();

if (buffer_size == 0) {
// Nothing to write.
return true;
Expand Down
6 changes: 3 additions & 3 deletions iamf/cli/wav_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>

namespace iamf_tools {

Expand Down Expand Up @@ -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<uint8_t>& buffer);

/*\!brief Aborts the write process and deletes the wav file.*/
void Abort();
Expand Down

0 comments on commit 0a93ec7

Please sign in to comment.