diff options
author | pkasting <pkasting@chromium.org> | 2016-01-08 13:50:27 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-08 21:50:32 +0000 |
commit | 25702cb1628941427fa55e528f53483f239ae011 (patch) | |
tree | 508edfcb88a7099815dd335e1ea79ab265463d6a /webrtc/common_audio | |
parent | 5de688ed341fc7b6a558c1d44b489af14646c2e4 (diff) | |
download | webrtc-25702cb1628941427fa55e528f53483f239ae011.tar.gz |
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
Diffstat (limited to 'webrtc/common_audio')
-rw-r--r-- | webrtc/common_audio/audio_converter_unittest.cc | 21 | ||||
-rw-r--r-- | webrtc/common_audio/blocker_unittest.cc | 69 | ||||
-rw-r--r-- | webrtc/common_audio/lapped_transform_unittest.cc | 30 | ||||
-rw-r--r-- | webrtc/common_audio/real_fourier.cc | 2 | ||||
-rw-r--r-- | webrtc/common_audio/real_fourier.h | 2 | ||||
-rw-r--r-- | webrtc/common_audio/real_fourier_unittest.cc | 8 | ||||
-rw-r--r-- | webrtc/common_audio/wav_file.cc | 16 | ||||
-rw-r--r-- | webrtc/common_audio/wav_file.h | 14 | ||||
-rw-r--r-- | webrtc/common_audio/wav_file_unittest.cc | 16 | ||||
-rw-r--r-- | webrtc/common_audio/wav_header.cc | 47 | ||||
-rw-r--r-- | webrtc/common_audio/wav_header.h | 12 | ||||
-rw-r--r-- | webrtc/common_audio/wav_header_unittest.cc | 18 |
12 files changed, 128 insertions, 127 deletions
diff --git a/webrtc/common_audio/audio_converter_unittest.cc b/webrtc/common_audio/audio_converter_unittest.cc index c85b96e285..e373d78b46 100644 --- a/webrtc/common_audio/audio_converter_unittest.cc +++ b/webrtc/common_audio/audio_converter_unittest.cc @@ -13,6 +13,7 @@ #include <vector> #include "testing/gtest/include/gtest/gtest.h" +#include "webrtc/base/arraysize.h" #include "webrtc/base/format_macros.h" #include "webrtc/base/scoped_ptr.h" #include "webrtc/common_audio/audio_converter.h" @@ -24,11 +25,11 @@ namespace webrtc { typedef rtc::scoped_ptr<ChannelBuffer<float>> ScopedBuffer; // Sets the signal value to increase by |data| with every sample. -ScopedBuffer CreateBuffer(const std::vector<float>& data, int frames) { +ScopedBuffer CreateBuffer(const std::vector<float>& data, size_t frames) { const int num_channels = static_cast<int>(data.size()); ScopedBuffer sb(new ChannelBuffer<float>(frames, num_channels)); for (int i = 0; i < num_channels; ++i) - for (int j = 0; j < frames; ++j) + for (size_t j = 0; j < frames; ++j) sb->channels()[i][j] = data[i] * j; return sb; } @@ -96,8 +97,8 @@ void RunAudioConverterTest(int src_channels, const float dst_left = resampling_factor * kSrcLeft; const float dst_right = resampling_factor * kSrcRight; const float dst_mono = (dst_left + dst_right) / 2; - const int src_frames = src_sample_rate_hz / 100; - const int dst_frames = dst_sample_rate_hz / 100; + const size_t src_frames = static_cast<size_t>(src_sample_rate_hz / 100); + const size_t dst_frames = static_cast<size_t>(dst_sample_rate_hz / 100); std::vector<float> src_data(1, kSrcLeft); if (src_channels == 2) @@ -141,13 +142,13 @@ void RunAudioConverterTest(int src_channels, TEST(AudioConverterTest, ConversionsPassSNRThreshold) { const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000}; - const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates); const int kChannels[] = {1, 2}; - const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels); - for (int src_rate = 0; src_rate < kSampleRatesSize; ++src_rate) { - for (int dst_rate = 0; dst_rate < kSampleRatesSize; ++dst_rate) { - for (int src_channel = 0; src_channel < kChannelsSize; ++src_channel) { - for (int dst_channel = 0; dst_channel < kChannelsSize; ++dst_channel) { + for (size_t src_rate = 0; src_rate < arraysize(kSampleRates); ++src_rate) { + for (size_t dst_rate = 0; dst_rate < arraysize(kSampleRates); ++dst_rate) { + for (size_t src_channel = 0; src_channel < arraysize(kChannels); + ++src_channel) { + for (size_t dst_channel = 0; dst_channel < arraysize(kChannels); + ++dst_channel) { RunAudioConverterTest(kChannels[src_channel], kSampleRates[src_rate], kChannels[dst_channel], kSampleRates[dst_rate]); } diff --git a/webrtc/common_audio/blocker_unittest.cc b/webrtc/common_audio/blocker_unittest.cc index 397e269239..065c09ed7f 100644 --- a/webrtc/common_audio/blocker_unittest.cc +++ b/webrtc/common_audio/blocker_unittest.cc @@ -11,6 +11,7 @@ #include "webrtc/common_audio/blocker.h" #include "testing/gtest/include/gtest/gtest.h" +#include "webrtc/base/arraysize.h" namespace { @@ -56,16 +57,16 @@ namespace webrtc { class BlockerTest : public ::testing::Test { protected: void RunTest(Blocker* blocker, - int chunk_size, - int num_frames, + size_t chunk_size, + size_t num_frames, const float* const* input, float* const* input_chunk, float* const* output, float* const* output_chunk, int num_input_channels, int num_output_channels) { - int start = 0; - int end = chunk_size - 1; + size_t start = 0; + size_t end = chunk_size - 1; while (end < num_frames) { CopyTo(input_chunk, 0, start, num_input_channels, chunk_size, input); blocker->ProcessChunk(input_chunk, @@ -75,17 +76,17 @@ class BlockerTest : public ::testing::Test { output_chunk); CopyTo(output, start, 0, num_output_channels, chunk_size, output_chunk); - start = start + chunk_size; - end = end + chunk_size; + start += chunk_size; + end += chunk_size; } } void ValidateSignalEquality(const float* const* expected, const float* const* actual, int num_channels, - int num_frames) { + size_t num_frames) { for (int i = 0; i < num_channels; ++i) { - for (int j = 0; j < num_frames; ++j) { + for (size_t j = 0; j < num_frames; ++j) { EXPECT_FLOAT_EQ(expected[i][j], actual[i][j]); } } @@ -93,10 +94,10 @@ class BlockerTest : public ::testing::Test { void ValidateInitialDelay(const float* const* output, int num_channels, - int num_frames, - int initial_delay) { + size_t num_frames, + size_t initial_delay) { for (int i = 0; i < num_channels; ++i) { - for (int j = 0; j < num_frames; ++j) { + for (size_t j = 0; j < num_frames; ++j) { if (j < initial_delay) { EXPECT_FLOAT_EQ(output[i][j], 0.f); } else { @@ -107,10 +108,10 @@ class BlockerTest : public ::testing::Test { } static void CopyTo(float* const* dst, - int start_index_dst, - int start_index_src, + size_t start_index_dst, + size_t start_index_src, int num_channels, - int num_frames, + size_t num_frames, const float* const* src) { for (int i = 0; i < num_channels; ++i) { memcpy(&dst[i][start_index_dst], @@ -123,10 +124,10 @@ class BlockerTest : public ::testing::Test { TEST_F(BlockerTest, TestBlockerMutuallyPrimeChunkandBlockSize) { const int kNumInputChannels = 3; const int kNumOutputChannels = 2; - const int kNumFrames = 10; - const int kBlockSize = 4; - const int kChunkSize = 5; - const int kShiftAmount = 2; + const size_t kNumFrames = 10; + const size_t kBlockSize = 4; + const size_t kChunkSize = 5; + const size_t kShiftAmount = 2; const float kInput[kNumInputChannels][kNumFrames] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, @@ -176,10 +177,10 @@ TEST_F(BlockerTest, TestBlockerMutuallyPrimeChunkandBlockSize) { TEST_F(BlockerTest, TestBlockerMutuallyPrimeShiftAndBlockSize) { const int kNumInputChannels = 3; const int kNumOutputChannels = 2; - const int kNumFrames = 12; - const int kBlockSize = 4; - const int kChunkSize = 6; - const int kShiftAmount = 3; + const size_t kNumFrames = 12; + const size_t kBlockSize = 4; + const size_t kChunkSize = 6; + const size_t kShiftAmount = 3; const float kInput[kNumInputChannels][kNumFrames] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, @@ -229,10 +230,10 @@ TEST_F(BlockerTest, TestBlockerMutuallyPrimeShiftAndBlockSize) { TEST_F(BlockerTest, TestBlockerNoOverlap) { const int kNumInputChannels = 3; const int kNumOutputChannels = 2; - const int kNumFrames = 12; - const int kBlockSize = 4; - const int kChunkSize = 4; - const int kShiftAmount = 4; + const size_t kNumFrames = 12; + const size_t kBlockSize = 4; + const size_t kChunkSize = 4; + const size_t kShiftAmount = 4; const float kInput[kNumInputChannels][kNumFrames] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, @@ -282,19 +283,19 @@ TEST_F(BlockerTest, TestBlockerNoOverlap) { TEST_F(BlockerTest, InitialDelaysAreMinimum) { const int kNumInputChannels = 3; const int kNumOutputChannels = 2; - const int kNumFrames = 1280; - const int kChunkSize[] = + const size_t kNumFrames = 1280; + const size_t kChunkSize[] = {80, 80, 80, 80, 80, 80, 160, 160, 160, 160, 160, 160}; - const int kBlockSize[] = + const size_t kBlockSize[] = {64, 64, 64, 128, 128, 128, 128, 128, 128, 256, 256, 256}; - const int kShiftAmount[] = + const size_t kShiftAmount[] = {16, 32, 64, 32, 64, 128, 32, 64, 128, 64, 128, 256}; - const int kInitialDelay[] = + const size_t kInitialDelay[] = {48, 48, 48, 112, 112, 112, 96, 96, 96, 224, 224, 224}; float input[kNumInputChannels][kNumFrames]; for (int i = 0; i < kNumInputChannels; ++i) { - for (int j = 0; j < kNumFrames; ++j) { + for (size_t j = 0; j < kNumFrames; ++j) { input[i][j] = i + 1; } } @@ -305,9 +306,9 @@ TEST_F(BlockerTest, InitialDelaysAreMinimum) { CopyBlockerCallback callback; - for (size_t i = 0; i < (sizeof(kChunkSize) / sizeof(*kChunkSize)); ++i) { + for (size_t i = 0; i < arraysize(kChunkSize); ++i) { rtc::scoped_ptr<float[]> window(new float[kBlockSize[i]]); - for (int j = 0; j < kBlockSize[i]; ++j) { + for (size_t j = 0; j < kBlockSize[i]; ++j) { window[j] = 1.f; } diff --git a/webrtc/common_audio/lapped_transform_unittest.cc b/webrtc/common_audio/lapped_transform_unittest.cc index f688cc240a..eb1c80f281 100644 --- a/webrtc/common_audio/lapped_transform_unittest.cc +++ b/webrtc/common_audio/lapped_transform_unittest.cc @@ -36,12 +36,12 @@ class NoopCallback : public webrtc::LappedTransform::Callback { ++block_num_; } - int block_num() { + size_t block_num() { return block_num_; } private: - int block_num_; + size_t block_num_; }; class FftCheckerCallback : public webrtc::LappedTransform::Callback { @@ -69,12 +69,12 @@ class FftCheckerCallback : public webrtc::LappedTransform::Callback { } } - int block_num() { + size_t block_num() { return block_num_; } private: - int block_num_; + size_t block_num_; }; void SetFloatArray(float value, int rows, int cols, float* const* array) { @@ -91,9 +91,9 @@ namespace webrtc { TEST(LappedTransformTest, Windowless) { const int kChannels = 3; - const int kChunkLength = 512; - const int kBlockLength = 64; - const int kShiftAmount = 64; + const size_t kChunkLength = 512; + const size_t kBlockLength = 64; + const size_t kShiftAmount = 64; NoopCallback noop; // Rectangular window. @@ -119,7 +119,7 @@ TEST(LappedTransformTest, Windowless) { trans.ProcessChunk(in_chunk, out_chunk); for (int i = 0; i < kChannels; ++i) { - for (int j = 0; j < kChunkLength; ++j) { + for (size_t j = 0; j < kChunkLength; ++j) { ASSERT_NEAR(out_chunk[i][j], 2.0f, 1e-5f); } } @@ -128,9 +128,9 @@ TEST(LappedTransformTest, Windowless) { } TEST(LappedTransformTest, IdentityProcessor) { - const int kChunkLength = 512; - const int kBlockLength = 64; - const int kShiftAmount = 32; + const size_t kChunkLength = 512; + const size_t kBlockLength = 64; + const size_t kShiftAmount = 32; NoopCallback noop; // Identity window for |overlap = block_size / 2|. @@ -149,7 +149,7 @@ TEST(LappedTransformTest, IdentityProcessor) { trans.ProcessChunk(&in_chunk, &out_chunk); - for (int i = 0; i < kChunkLength; ++i) { + for (size_t i = 0; i < kChunkLength; ++i) { ASSERT_NEAR(out_chunk[i], (i < kBlockLength - kShiftAmount) ? 0.0f : 2.0f, 1e-5f); @@ -159,8 +159,8 @@ TEST(LappedTransformTest, IdentityProcessor) { } TEST(LappedTransformTest, Callbacks) { - const int kChunkLength = 512; - const int kBlockLength = 64; + const size_t kChunkLength = 512; + const size_t kBlockLength = 64; FftCheckerCallback call; // Rectangular window. @@ -183,7 +183,7 @@ TEST(LappedTransformTest, Callbacks) { } TEST(LappedTransformTest, chunk_length) { - const int kBlockLength = 64; + const size_t kBlockLength = 64; FftCheckerCallback call; const float window[kBlockLength] = {}; diff --git a/webrtc/common_audio/real_fourier.cc b/webrtc/common_audio/real_fourier.cc index fef3c60c4c..55ec49cba2 100644 --- a/webrtc/common_audio/real_fourier.cc +++ b/webrtc/common_audio/real_fourier.cc @@ -19,7 +19,7 @@ namespace webrtc { using std::complex; -const int RealFourier::kFftBufferAlignment = 32; +const size_t RealFourier::kFftBufferAlignment = 32; rtc::scoped_ptr<RealFourier> RealFourier::Create(int fft_order) { #if defined(RTC_USE_OPENMAX_DL) diff --git a/webrtc/common_audio/real_fourier.h b/webrtc/common_audio/real_fourier.h index ce3bbff679..0be56a58b0 100644 --- a/webrtc/common_audio/real_fourier.h +++ b/webrtc/common_audio/real_fourier.h @@ -30,7 +30,7 @@ class RealFourier { fft_cplx_scoper; // The alignment required for all input and output buffers, in bytes. - static const int kFftBufferAlignment; + static const size_t kFftBufferAlignment; // Construct a wrapper instance for the given input order, which must be // between 1 and kMaxFftOrder, inclusively. diff --git a/webrtc/common_audio/real_fourier_unittest.cc b/webrtc/common_audio/real_fourier_unittest.cc index 5c8542138b..eb5880ee8a 100644 --- a/webrtc/common_audio/real_fourier_unittest.cc +++ b/webrtc/common_audio/real_fourier_unittest.cc @@ -26,15 +26,15 @@ TEST(RealFourierStaticsTest, AllocatorAlignment) { RealFourier::fft_real_scoper real; real = RealFourier::AllocRealBuffer(3); ASSERT_TRUE(real.get() != nullptr); - int64_t ptr_value = reinterpret_cast<int64_t>(real.get()); - EXPECT_EQ(0, ptr_value % RealFourier::kFftBufferAlignment); + uintptr_t ptr_value = reinterpret_cast<uintptr_t>(real.get()); + EXPECT_EQ(0u, ptr_value % RealFourier::kFftBufferAlignment); } { RealFourier::fft_cplx_scoper cplx; cplx = RealFourier::AllocCplxBuffer(3); ASSERT_TRUE(cplx.get() != nullptr); - int64_t ptr_value = reinterpret_cast<int64_t>(cplx.get()); - EXPECT_EQ(0, ptr_value % RealFourier::kFftBufferAlignment); + uintptr_t ptr_value = reinterpret_cast<uintptr_t>(cplx.get()); + EXPECT_EQ(0u, ptr_value % RealFourier::kFftBufferAlignment); } } diff --git a/webrtc/common_audio/wav_file.cc b/webrtc/common_audio/wav_file.cc index ac11bcdd7b..27d335c543 100644 --- a/webrtc/common_audio/wav_file.cc +++ b/webrtc/common_audio/wav_file.cc @@ -24,7 +24,7 @@ namespace webrtc { // We write 16-bit PCM WAV files. static const WavFormat kWavFormat = kWavFormatPcm; -static const int kBytesPerSample = 2; +static const size_t kBytesPerSample = 2; // Doesn't take ownership of the file handle and won't close it. class ReadableWavFile : public ReadableWav { @@ -52,7 +52,7 @@ WavReader::WavReader(const std::string& filename) ReadableWavFile readable(file_handle_); WavFormat format; - int bytes_per_sample; + size_t bytes_per_sample; RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, &bytes_per_sample, &num_samples_)); num_samples_remaining_ = num_samples_; @@ -69,14 +69,13 @@ size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { #error "Need to convert samples to big-endian when reading from WAV file" #endif // There could be metadata after the audio; ensure we don't read it. - num_samples = std::min(rtc::checked_cast<uint32_t>(num_samples), - num_samples_remaining_); + num_samples = std::min(num_samples, num_samples_remaining_); const size_t read = fread(samples, sizeof(*samples), num_samples, file_handle_); // If we didn't read what was requested, ensure we've reached the EOF. RTC_CHECK(read == num_samples || feof(file_handle_)); RTC_CHECK_LE(read, num_samples_remaining_); - num_samples_remaining_ -= rtc::checked_cast<uint32_t>(read); + num_samples_remaining_ -= read; return read; } @@ -126,9 +125,8 @@ void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) { const size_t written = fwrite(samples, sizeof(*samples), num_samples, file_handle_); RTC_CHECK_EQ(num_samples, written); - num_samples_ += static_cast<uint32_t>(written); - RTC_CHECK(written <= std::numeric_limits<uint32_t>::max() || - num_samples_ >= written); // detect uint32_t overflow + num_samples_ += written; + RTC_CHECK(num_samples_ >= written); // detect size_t overflow } void WavWriter::WriteSamples(const float* samples, size_t num_samples) { @@ -178,6 +176,6 @@ int rtc_WavNumChannels(const rtc_WavWriter* wf) { return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); } -uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf) { +size_t rtc_WavNumSamples(const rtc_WavWriter* wf) { return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); } diff --git a/webrtc/common_audio/wav_file.h b/webrtc/common_audio/wav_file.h index 42b0618e9c..eb2ce1e31d 100644 --- a/webrtc/common_audio/wav_file.h +++ b/webrtc/common_audio/wav_file.h @@ -28,7 +28,7 @@ class WavFile { virtual int sample_rate() const = 0; virtual int num_channels() const = 0; - virtual uint32_t num_samples() const = 0; + virtual size_t num_samples() const = 0; // Returns a human-readable string containing the audio format. std::string FormatAsString() const; @@ -52,13 +52,13 @@ class WavWriter final : public WavFile { int sample_rate() const override { return sample_rate_; } int num_channels() const override { return num_channels_; } - uint32_t num_samples() const override { return num_samples_; } + size_t num_samples() const override { return num_samples_; } private: void Close(); const int sample_rate_; const int num_channels_; - uint32_t num_samples_; // Total number of samples written to file. + size_t num_samples_; // Total number of samples written to file. FILE* file_handle_; // Output file, owned by this class RTC_DISALLOW_COPY_AND_ASSIGN(WavWriter); @@ -80,14 +80,14 @@ class WavReader final : public WavFile { int sample_rate() const override { return sample_rate_; } int num_channels() const override { return num_channels_; } - uint32_t num_samples() const override { return num_samples_; } + size_t num_samples() const override { return num_samples_; } private: void Close(); int sample_rate_; int num_channels_; - uint32_t num_samples_; // Total number of samples in the file. - uint32_t num_samples_remaining_; + size_t num_samples_; // Total number of samples in the file. + size_t num_samples_remaining_; FILE* file_handle_; // Input file, owned by this class. RTC_DISALLOW_COPY_AND_ASSIGN(WavReader); @@ -109,7 +109,7 @@ void rtc_WavWriteSamples(rtc_WavWriter* wf, size_t num_samples); int rtc_WavSampleRate(const rtc_WavWriter* wf); int rtc_WavNumChannels(const rtc_WavWriter* wf); -uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf); +size_t rtc_WavNumSamples(const rtc_WavWriter* wf); #ifdef __cplusplus } // extern "C" diff --git a/webrtc/common_audio/wav_file_unittest.cc b/webrtc/common_audio/wav_file_unittest.cc index 78b0a34de9..3732079f4f 100644 --- a/webrtc/common_audio/wav_file_unittest.cc +++ b/webrtc/common_audio/wav_file_unittest.cc @@ -26,7 +26,7 @@ static const float kSamples[] = {0.0, 10.0, 4e4, -1e9}; // Write a tiny WAV file with the C++ interface and verify the result. TEST(WavWriterTest, CPP) { const std::string outfile = test::OutputPath() + "wavtest1.wav"; - static const uint32_t kNumSamples = 3; + static const size_t kNumSamples = 3; { WavWriter w(outfile, 14099, 1); EXPECT_EQ(14099, w.sample_rate()); @@ -64,10 +64,10 @@ TEST(WavWriterTest, CPP) { 0xff, 0x7f, // third sample: 4e4 (saturated) kMetadata[0], kMetadata[1], }; - static const int kContentSize = + static const size_t kContentSize = kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata); static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); - EXPECT_EQ(size_t(kContentSize), test::GetFileSize(outfile)); + EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); FILE* f = fopen(outfile.c_str(), "rb"); ASSERT_TRUE(f); uint8_t contents[kContentSize]; @@ -95,7 +95,7 @@ TEST(WavWriterTest, C) { EXPECT_EQ(11904, rtc_WavSampleRate(w)); EXPECT_EQ(2, rtc_WavNumChannels(w)); EXPECT_EQ(0u, rtc_WavNumSamples(w)); - static const uint32_t kNumSamples = 4; + static const size_t kNumSamples = 4; rtc_WavWriteSamples(w, &kSamples[0], 2); EXPECT_EQ(2u, rtc_WavNumSamples(w)); rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2); @@ -120,10 +120,10 @@ TEST(WavWriterTest, C) { 0xff, 0x7f, // third sample: 4e4 (saturated) 0, 0x80, // fourth sample: -1e9 (saturated) }; - static const int kContentSize = + static const size_t kContentSize = kWavHeaderSize + kNumSamples * sizeof(int16_t); static_assert(sizeof(kExpectedContents) == kContentSize, "content size"); - EXPECT_EQ(size_t(kContentSize), test::GetFileSize(outfile)); + EXPECT_EQ(kContentSize, test::GetFileSize(outfile)); FILE* f = fopen(outfile.c_str(), "rb"); ASSERT_TRUE(f); uint8_t contents[kContentSize]; @@ -137,9 +137,9 @@ TEST(WavWriterTest, LargeFile) { std::string outfile = test::OutputPath() + "wavtest3.wav"; static const int kSampleRate = 8000; static const int kNumChannels = 2; - static const uint32_t kNumSamples = 3 * kSampleRate * kNumChannels; + static const size_t kNumSamples = 3 * kSampleRate * kNumChannels; float samples[kNumSamples]; - for (uint32_t i = 0; i < kNumSamples; i += kNumChannels) { + for (size_t i = 0; i < kNumSamples; i += kNumChannels) { // A nice periodic beeping sound. static const double kToneHz = 440; const double t = static_cast<double>(i) / (kNumChannels * kSampleRate); diff --git a/webrtc/common_audio/wav_header.cc b/webrtc/common_audio/wav_header.cc index 61cfffe62c..d2aa426414 100644 --- a/webrtc/common_audio/wav_header.cc +++ b/webrtc/common_audio/wav_header.cc @@ -62,12 +62,12 @@ static_assert(sizeof(WavHeader) == kWavHeaderSize, "no padding in header"); bool CheckWavParameters(int num_channels, int sample_rate, WavFormat format, - int bytes_per_sample, - uint32_t num_samples) { + size_t bytes_per_sample, + size_t num_samples) { // num_channels, sample_rate, and bytes_per_sample must be positive, must fit // in their respective fields, and their product must fit in the 32-bit // ByteRate field. - if (num_channels <= 0 || sample_rate <= 0 || bytes_per_sample <= 0) + if (num_channels <= 0 || sample_rate <= 0 || bytes_per_sample == 0) return false; if (static_cast<uint64_t>(sample_rate) > std::numeric_limits<uint32_t>::max()) return false; @@ -99,10 +99,9 @@ bool CheckWavParameters(int num_channels, // The number of bytes in the file, not counting the first ChunkHeader, must // be less than 2^32; otherwise, the ChunkSize field overflows. - const uint32_t max_samples = - (std::numeric_limits<uint32_t>::max() - - (kWavHeaderSize - sizeof(ChunkHeader))) / - bytes_per_sample; + const size_t header_size = kWavHeaderSize - sizeof(ChunkHeader); + const size_t max_samples = + (std::numeric_limits<uint32_t>::max() - header_size) / bytes_per_sample; if (num_samples > max_samples) return false; @@ -132,30 +131,31 @@ static inline std::string ReadFourCC(uint32_t x) { #error "Write be-to-le conversion functions" #endif -static inline uint32_t RiffChunkSize(uint32_t bytes_in_payload) { - return bytes_in_payload + kWavHeaderSize - sizeof(ChunkHeader); +static inline uint32_t RiffChunkSize(size_t bytes_in_payload) { + return static_cast<uint32_t>( + bytes_in_payload + kWavHeaderSize - sizeof(ChunkHeader)); } static inline uint32_t ByteRate(int num_channels, int sample_rate, - int bytes_per_sample) { - return static_cast<uint32_t>(num_channels) * sample_rate * bytes_per_sample; + size_t bytes_per_sample) { + return static_cast<uint32_t>(num_channels * sample_rate * bytes_per_sample); } -static inline uint16_t BlockAlign(int num_channels, int bytes_per_sample) { - return num_channels * bytes_per_sample; +static inline uint16_t BlockAlign(int num_channels, size_t bytes_per_sample) { + return static_cast<uint16_t>(num_channels * bytes_per_sample); } void WriteWavHeader(uint8_t* buf, int num_channels, int sample_rate, WavFormat format, - int bytes_per_sample, - uint32_t num_samples) { + size_t bytes_per_sample, + size_t num_samples) { RTC_CHECK(CheckWavParameters(num_channels, sample_rate, format, bytes_per_sample, num_samples)); WavHeader header; - const uint32_t bytes_in_payload = bytes_per_sample * num_samples; + const size_t bytes_in_payload = bytes_per_sample * num_samples; WriteFourCC(&header.riff.header.ID, 'R', 'I', 'F', 'F'); WriteLE32(&header.riff.header.Size, RiffChunkSize(bytes_in_payload)); @@ -164,15 +164,16 @@ void WriteWavHeader(uint8_t* buf, WriteFourCC(&header.fmt.header.ID, 'f', 'm', 't', ' '); WriteLE32(&header.fmt.header.Size, kFmtSubchunkSize); WriteLE16(&header.fmt.AudioFormat, format); - WriteLE16(&header.fmt.NumChannels, num_channels); + WriteLE16(&header.fmt.NumChannels, static_cast<uint16_t>(num_channels)); WriteLE32(&header.fmt.SampleRate, sample_rate); WriteLE32(&header.fmt.ByteRate, ByteRate(num_channels, sample_rate, bytes_per_sample)); WriteLE16(&header.fmt.BlockAlign, BlockAlign(num_channels, bytes_per_sample)); - WriteLE16(&header.fmt.BitsPerSample, 8 * bytes_per_sample); + WriteLE16(&header.fmt.BitsPerSample, + static_cast<uint16_t>(8 * bytes_per_sample)); WriteFourCC(&header.data.header.ID, 'd', 'a', 't', 'a'); - WriteLE32(&header.data.header.Size, bytes_in_payload); + WriteLE32(&header.data.header.Size, static_cast<uint32_t>(bytes_in_payload)); // Do an extra copy rather than writing everything to buf directly, since buf // might not be correctly aligned. @@ -183,8 +184,8 @@ bool ReadWavHeader(ReadableWav* readable, int* num_channels, int* sample_rate, WavFormat* format, - int* bytes_per_sample, - uint32_t* num_samples) { + size_t* bytes_per_sample, + size_t* num_samples) { WavHeader header; if (readable->Read(&header, kWavHeaderSize - sizeof(header.data)) != kWavHeaderSize - sizeof(header.data)) @@ -210,8 +211,8 @@ bool ReadWavHeader(ReadableWav* readable, *num_channels = ReadLE16(header.fmt.NumChannels); *sample_rate = ReadLE32(header.fmt.SampleRate); *bytes_per_sample = ReadLE16(header.fmt.BitsPerSample) / 8; - const uint32_t bytes_in_payload = ReadLE32(header.data.header.Size); - if (*bytes_per_sample <= 0) + const size_t bytes_in_payload = ReadLE32(header.data.header.Size); + if (*bytes_per_sample == 0) return false; *num_samples = bytes_in_payload / *bytes_per_sample; diff --git a/webrtc/common_audio/wav_header.h b/webrtc/common_audio/wav_header.h index 1a0fd7c81d..65b7792992 100644 --- a/webrtc/common_audio/wav_header.h +++ b/webrtc/common_audio/wav_header.h @@ -35,8 +35,8 @@ enum WavFormat { bool CheckWavParameters(int num_channels, int sample_rate, WavFormat format, - int bytes_per_sample, - uint32_t num_samples); + size_t bytes_per_sample, + size_t num_samples); // Write a kWavHeaderSize bytes long WAV header to buf. The payload that // follows the header is supposed to have the specified number of interleaved @@ -46,8 +46,8 @@ void WriteWavHeader(uint8_t* buf, int num_channels, int sample_rate, WavFormat format, - int bytes_per_sample, - uint32_t num_samples); + size_t bytes_per_sample, + size_t num_samples); // Read a WAV header from an implemented ReadableWav and parse the values into // the provided output parameters. ReadableWav is used because the header can @@ -56,8 +56,8 @@ bool ReadWavHeader(ReadableWav* readable, int* num_channels, int* sample_rate, WavFormat* format, - int* bytes_per_sample, - uint32_t* num_samples); + size_t* bytes_per_sample, + size_t* num_samples); } // namespace webrtc diff --git a/webrtc/common_audio/wav_header_unittest.cc b/webrtc/common_audio/wav_header_unittest.cc index e03cb303aa..226d788204 100644 --- a/webrtc/common_audio/wav_header_unittest.cc +++ b/webrtc/common_audio/wav_header_unittest.cc @@ -70,7 +70,7 @@ TEST(WavHeaderTest, CheckWavParameters) { // Try some really stupid values for one parameter at a time. EXPECT_TRUE(CheckWavParameters(1, 8000, kWavFormatPcm, 1, 0)); EXPECT_FALSE(CheckWavParameters(0, 8000, kWavFormatPcm, 1, 0)); - EXPECT_FALSE(CheckWavParameters(-1, 8000, kWavFormatPcm, 1, 0)); + EXPECT_FALSE(CheckWavParameters(0x10000, 8000, kWavFormatPcm, 1, 0)); EXPECT_FALSE(CheckWavParameters(1, 0, kWavFormatPcm, 1, 0)); EXPECT_FALSE(CheckWavParameters(1, 8000, WavFormat(0), 1, 0)); EXPECT_FALSE(CheckWavParameters(1, 8000, kWavFormatPcm, 0, 0)); @@ -94,8 +94,8 @@ TEST(WavHeaderTest, ReadWavHeaderWithErrors) { int num_channels = 0; int sample_rate = 0; WavFormat format = kWavFormatPcm; - int bytes_per_sample = 0; - uint32_t num_samples = 0; + size_t bytes_per_sample = 0; + size_t num_samples = 0; // Test a few ways the header can be invalid. We start with the valid header // used in WriteAndReadWavHeader, and invalidate one field per test. The @@ -271,8 +271,8 @@ TEST(WavHeaderTest, WriteAndReadWavHeader) { int num_channels = 0; int sample_rate = 0; WavFormat format = kWavFormatPcm; - int bytes_per_sample = 0; - uint32_t num_samples = 0; + size_t bytes_per_sample = 0; + size_t num_samples = 0; ReadableWavBuffer r(buf + 4, sizeof(buf) - 8); EXPECT_TRUE( ReadWavHeader(&r, &num_channels, &sample_rate, &format, @@ -280,7 +280,7 @@ TEST(WavHeaderTest, WriteAndReadWavHeader) { EXPECT_EQ(17, num_channels); EXPECT_EQ(12345, sample_rate); EXPECT_EQ(kWavFormatALaw, format); - EXPECT_EQ(1, bytes_per_sample); + EXPECT_EQ(1u, bytes_per_sample); EXPECT_EQ(123457689u, num_samples); } @@ -307,8 +307,8 @@ TEST(WavHeaderTest, ReadAtypicalWavHeader) { int num_channels = 0; int sample_rate = 0; WavFormat format = kWavFormatPcm; - int bytes_per_sample = 0; - uint32_t num_samples = 0; + size_t bytes_per_sample = 0; + size_t num_samples = 0; ReadableWavBuffer r(kBuf, sizeof(kBuf)); EXPECT_TRUE( ReadWavHeader(&r, &num_channels, &sample_rate, &format, @@ -316,7 +316,7 @@ TEST(WavHeaderTest, ReadAtypicalWavHeader) { EXPECT_EQ(17, num_channels); EXPECT_EQ(12345, sample_rate); EXPECT_EQ(kWavFormatALaw, format); - EXPECT_EQ(1, bytes_per_sample); + EXPECT_EQ(1u, bytes_per_sample); EXPECT_EQ(123457689u, num_samples); } |