Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(964)

Side by Side Diff: webrtc/common_audio/wav_file_unittest.cc

Issue 1316523002: Convert channel counts to size_t. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Fix compile Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/common_audio/wav_file.cc ('k') | webrtc/common_audio/wav_header.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 12 matching lines...) Expand all
23 23
24 static const float kSamples[] = {0.0, 10.0, 4e4, -1e9}; 24 static const float kSamples[] = {0.0, 10.0, 4e4, -1e9};
25 25
26 // Write a tiny WAV file with the C++ interface and verify the result. 26 // Write a tiny WAV file with the C++ interface and verify the result.
27 TEST(WavWriterTest, CPP) { 27 TEST(WavWriterTest, CPP) {
28 const std::string outfile = test::OutputPath() + "wavtest1.wav"; 28 const std::string outfile = test::OutputPath() + "wavtest1.wav";
29 static const size_t kNumSamples = 3; 29 static const size_t kNumSamples = 3;
30 { 30 {
31 WavWriter w(outfile, 14099, 1); 31 WavWriter w(outfile, 14099, 1);
32 EXPECT_EQ(14099, w.sample_rate()); 32 EXPECT_EQ(14099, w.sample_rate());
33 EXPECT_EQ(1, w.num_channels()); 33 EXPECT_EQ(1u, w.num_channels());
34 EXPECT_EQ(0u, w.num_samples()); 34 EXPECT_EQ(0u, w.num_samples());
35 w.WriteSamples(kSamples, kNumSamples); 35 w.WriteSamples(kSamples, kNumSamples);
36 EXPECT_EQ(kNumSamples, w.num_samples()); 36 EXPECT_EQ(kNumSamples, w.num_samples());
37 } 37 }
38 // Write some extra "metadata" to the file that should be silently ignored 38 // Write some extra "metadata" to the file that should be silently ignored
39 // by WavReader. We don't use WavWriter directly for this because it doesn't 39 // by WavReader. We don't use WavWriter directly for this because it doesn't
40 // support metadata. 40 // support metadata.
41 static const uint8_t kMetadata[] = {101, 202}; 41 static const uint8_t kMetadata[] = {101, 202};
42 { 42 {
43 FILE* f = fopen(outfile.c_str(), "ab"); 43 FILE* f = fopen(outfile.c_str(), "ab");
(...skipping 27 matching lines...) Expand all
71 FILE* f = fopen(outfile.c_str(), "rb"); 71 FILE* f = fopen(outfile.c_str(), "rb");
72 ASSERT_TRUE(f); 72 ASSERT_TRUE(f);
73 uint8_t contents[kContentSize]; 73 uint8_t contents[kContentSize];
74 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); 74 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
75 EXPECT_EQ(0, fclose(f)); 75 EXPECT_EQ(0, fclose(f));
76 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); 76 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
77 77
78 { 78 {
79 WavReader r(outfile); 79 WavReader r(outfile);
80 EXPECT_EQ(14099, r.sample_rate()); 80 EXPECT_EQ(14099, r.sample_rate());
81 EXPECT_EQ(1, r.num_channels()); 81 EXPECT_EQ(1u, r.num_channels());
82 EXPECT_EQ(kNumSamples, r.num_samples()); 82 EXPECT_EQ(kNumSamples, r.num_samples());
83 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0}; 83 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
84 float samples[kNumSamples]; 84 float samples[kNumSamples];
85 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples)); 85 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
86 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples))); 86 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
87 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples)); 87 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
88 } 88 }
89 } 89 }
90 90
91 // Write a tiny WAV file with the C interface and verify the result. 91 // Write a tiny WAV file with the C interface and verify the result.
92 TEST(WavWriterTest, C) { 92 TEST(WavWriterTest, C) {
93 const std::string outfile = test::OutputPath() + "wavtest2.wav"; 93 const std::string outfile = test::OutputPath() + "wavtest2.wav";
94 rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2); 94 rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2);
95 EXPECT_EQ(11904, rtc_WavSampleRate(w)); 95 EXPECT_EQ(11904, rtc_WavSampleRate(w));
96 EXPECT_EQ(2, rtc_WavNumChannels(w)); 96 EXPECT_EQ(2u, rtc_WavNumChannels(w));
97 EXPECT_EQ(0u, rtc_WavNumSamples(w)); 97 EXPECT_EQ(0u, rtc_WavNumSamples(w));
98 static const size_t kNumSamples = 4; 98 static const size_t kNumSamples = 4;
99 rtc_WavWriteSamples(w, &kSamples[0], 2); 99 rtc_WavWriteSamples(w, &kSamples[0], 2);
100 EXPECT_EQ(2u, rtc_WavNumSamples(w)); 100 EXPECT_EQ(2u, rtc_WavNumSamples(w));
101 rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2); 101 rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2);
102 EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w)); 102 EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w));
103 rtc_WavClose(w); 103 rtc_WavClose(w);
104 static const uint8_t kExpectedContents[] = { 104 static const uint8_t kExpectedContents[] = {
105 'R', 'I', 'F', 'F', 105 'R', 'I', 'F', 'F',
106 44, 0, 0, 0, // size of whole file - 8: 8 + 44 - 8 106 44, 0, 0, 0, // size of whole file - 8: 8 + 44 - 8
(...skipping 22 matching lines...) Expand all
129 uint8_t contents[kContentSize]; 129 uint8_t contents[kContentSize];
130 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); 130 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
131 EXPECT_EQ(0, fclose(f)); 131 EXPECT_EQ(0, fclose(f));
132 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); 132 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
133 } 133 }
134 134
135 // Write a larger WAV file. You can listen to this file to sanity-check it. 135 // Write a larger WAV file. You can listen to this file to sanity-check it.
136 TEST(WavWriterTest, LargeFile) { 136 TEST(WavWriterTest, LargeFile) {
137 std::string outfile = test::OutputPath() + "wavtest3.wav"; 137 std::string outfile = test::OutputPath() + "wavtest3.wav";
138 static const int kSampleRate = 8000; 138 static const int kSampleRate = 8000;
139 static const int kNumChannels = 2; 139 static const size_t kNumChannels = 2;
140 static const size_t kNumSamples = 3 * kSampleRate * kNumChannels; 140 static const size_t kNumSamples = 3 * kSampleRate * kNumChannels;
141 float samples[kNumSamples]; 141 float samples[kNumSamples];
142 for (size_t i = 0; i < kNumSamples; i += kNumChannels) { 142 for (size_t i = 0; i < kNumSamples; i += kNumChannels) {
143 // A nice periodic beeping sound. 143 // A nice periodic beeping sound.
144 static const double kToneHz = 440; 144 static const double kToneHz = 440;
145 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate); 145 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate);
146 const double x = 146 const double x =
147 std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI); 147 std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI);
148 samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x; 148 samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x;
149 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x; 149 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x;
(...skipping 18 matching lines...) Expand all
168 float read_samples[kNumSamples]; 168 float read_samples[kNumSamples];
169 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples)); 169 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
170 for (size_t i = 0; i < kNumSamples; ++i) 170 for (size_t i = 0; i < kNumSamples; ++i)
171 EXPECT_NEAR(samples[i], read_samples[i], 1); 171 EXPECT_NEAR(samples[i], read_samples[i], 1);
172 172
173 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples)); 173 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples));
174 } 174 }
175 } 175 }
176 176
177 } // namespace webrtc 177 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/common_audio/wav_file.cc ('k') | webrtc/common_audio/wav_header.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698