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

Side by Side Diff: webrtc/common_audio/resampler/push_sinc_resampler_unittest.cc

Issue 1230503003: Update a ton of audio code to use size_t more correctly and in general reduce (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Resync Created 5 years, 3 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
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 void ResampleTest(bool int_format); 51 void ResampleTest(bool int_format);
52 52
53 int input_rate_; 53 int input_rate_;
54 int output_rate_; 54 int output_rate_;
55 double rms_error_; 55 double rms_error_;
56 double low_freq_error_; 56 double low_freq_error_;
57 }; 57 };
58 58
59 class ZeroSource : public SincResamplerCallback { 59 class ZeroSource : public SincResamplerCallback {
60 public: 60 public:
61 void Run(int frames, float* destination) { 61 void Run(size_t frames, float* destination) {
62 std::memset(destination, 0, sizeof(float) * frames); 62 std::memset(destination, 0, sizeof(float) * frames);
63 } 63 }
64 }; 64 };
65 65
66 void PushSincResamplerTest::ResampleBenchmarkTest(bool int_format) { 66 void PushSincResamplerTest::ResampleBenchmarkTest(bool int_format) {
67 const int input_samples = input_rate_ / 100; 67 const size_t input_samples = static_cast<size_t>(input_rate_ / 100);
68 const int output_samples = output_rate_ / 100; 68 const size_t output_samples = static_cast<size_t>(output_rate_ / 100);
69 const int kResampleIterations = 500000; 69 const int kResampleIterations = 500000;
70 70
71 // Source for data to be resampled. 71 // Source for data to be resampled.
72 ZeroSource resampler_source; 72 ZeroSource resampler_source;
73 73
74 rtc::scoped_ptr<float[]> resampled_destination(new float[output_samples]); 74 rtc::scoped_ptr<float[]> resampled_destination(new float[output_samples]);
75 rtc::scoped_ptr<float[]> source(new float[input_samples]); 75 rtc::scoped_ptr<float[]> source(new float[input_samples]);
76 rtc::scoped_ptr<int16_t[]> source_int(new int16_t[input_samples]); 76 rtc::scoped_ptr<int16_t[]> source_int(new int16_t[input_samples]);
77 rtc::scoped_ptr<int16_t[]> destination_int(new int16_t[output_samples]); 77 rtc::scoped_ptr<int16_t[]> destination_int(new int16_t[output_samples]);
78 78
79 resampler_source.Run(input_samples, source.get()); 79 resampler_source.Run(input_samples, source.get());
80 for (int i = 0; i < input_samples; ++i) { 80 for (size_t i = 0; i < input_samples; ++i) {
81 source_int[i] = static_cast<int16_t>(floor(32767 * source[i] + 0.5)); 81 source_int[i] = static_cast<int16_t>(floor(32767 * source[i] + 0.5));
82 } 82 }
83 83
84 printf("Benchmarking %d iterations of %d Hz -> %d Hz:\n", 84 printf("Benchmarking %d iterations of %d Hz -> %d Hz:\n",
85 kResampleIterations, input_rate_, output_rate_); 85 kResampleIterations, input_rate_, output_rate_);
86 const double io_ratio = input_rate_ / static_cast<double>(output_rate_); 86 const double io_ratio = input_rate_ / static_cast<double>(output_rate_);
87 SincResampler sinc_resampler(io_ratio, SincResampler::kDefaultRequestSize, 87 SincResampler sinc_resampler(io_ratio, SincResampler::kDefaultRequestSize,
88 &resampler_source); 88 &resampler_source);
89 TickTime start = TickTime::Now(); 89 TickTime start = TickTime::Now();
90 for (int i = 0; i < kResampleIterations; ++i) { 90 for (int i = 0; i < kResampleIterations; ++i) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 127
128 TEST_P(PushSincResamplerTest, DISABLED_BenchmarkFloat) { 128 TEST_P(PushSincResamplerTest, DISABLED_BenchmarkFloat) {
129 ResampleBenchmarkTest(false); 129 ResampleBenchmarkTest(false);
130 } 130 }
131 131
132 // Tests resampling using a given input and output sample rate. 132 // Tests resampling using a given input and output sample rate.
133 void PushSincResamplerTest::ResampleTest(bool int_format) { 133 void PushSincResamplerTest::ResampleTest(bool int_format) {
134 // Make comparisons using one second of data. 134 // Make comparisons using one second of data.
135 static const double kTestDurationSecs = 1; 135 static const double kTestDurationSecs = 1;
136 // 10 ms blocks. 136 // 10 ms blocks.
137 const int kNumBlocks = kTestDurationSecs * 100; 137 const size_t kNumBlocks = static_cast<size_t>(kTestDurationSecs * 100);
138 const int input_block_size = input_rate_ / 100; 138 const size_t input_block_size = static_cast<size_t>(input_rate_ / 100);
139 const int output_block_size = output_rate_ / 100; 139 const size_t output_block_size = static_cast<size_t>(output_rate_ / 100);
140 const int input_samples = kTestDurationSecs * input_rate_; 140 const size_t input_samples =
141 const int output_samples = kTestDurationSecs * output_rate_; 141 static_cast<size_t>(kTestDurationSecs * input_rate_);
142 const size_t output_samples =
143 static_cast<size_t>(kTestDurationSecs * output_rate_);
142 144
143 // Nyquist frequency for the input sampling rate. 145 // Nyquist frequency for the input sampling rate.
144 const double input_nyquist_freq = 0.5 * input_rate_; 146 const double input_nyquist_freq = 0.5 * input_rate_;
145 147
146 // Source for data to be resampled. 148 // Source for data to be resampled.
147 SinusoidalLinearChirpSource resampler_source( 149 SinusoidalLinearChirpSource resampler_source(
148 input_rate_, input_samples, input_nyquist_freq, 0); 150 input_rate_, input_samples, input_nyquist_freq, 0);
149 151
150 PushSincResampler resampler(input_block_size, output_block_size); 152 PushSincResampler resampler(input_block_size, output_block_size);
151 153
152 // TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to 154 // TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to
153 // allocate these on 32-byte boundaries and ensure they're sized % 32 bytes. 155 // allocate these on 32-byte boundaries and ensure they're sized % 32 bytes.
154 rtc::scoped_ptr<float[]> resampled_destination(new float[output_samples]); 156 rtc::scoped_ptr<float[]> resampled_destination(new float[output_samples]);
155 rtc::scoped_ptr<float[]> pure_destination(new float[output_samples]); 157 rtc::scoped_ptr<float[]> pure_destination(new float[output_samples]);
156 rtc::scoped_ptr<float[]> source(new float[input_samples]); 158 rtc::scoped_ptr<float[]> source(new float[input_samples]);
157 rtc::scoped_ptr<int16_t[]> source_int(new int16_t[input_block_size]); 159 rtc::scoped_ptr<int16_t[]> source_int(new int16_t[input_block_size]);
158 rtc::scoped_ptr<int16_t[]> destination_int(new int16_t[output_block_size]); 160 rtc::scoped_ptr<int16_t[]> destination_int(new int16_t[output_block_size]);
159 161
160 // The sinc resampler has an implicit delay of approximately half the kernel 162 // The sinc resampler has an implicit delay of approximately half the kernel
161 // size at the input sample rate. By moving to a push model, this delay 163 // size at the input sample rate. By moving to a push model, this delay
162 // becomes explicit and is managed by zero-stuffing in PushSincResampler. We 164 // becomes explicit and is managed by zero-stuffing in PushSincResampler. We
163 // deal with it in the test by delaying the "pure" source to match. It must be 165 // deal with it in the test by delaying the "pure" source to match. It must be
164 // checked before the first call to Resample(), because ChunkSize() will 166 // checked before the first call to Resample(), because ChunkSize() will
165 // change afterwards. 167 // change afterwards.
166 const int output_delay_samples = output_block_size - 168 const size_t output_delay_samples = output_block_size -
167 resampler.get_resampler_for_testing()->ChunkSize(); 169 resampler.get_resampler_for_testing()->ChunkSize();
168 170
169 // Generate resampled signal. 171 // Generate resampled signal.
170 // With the PushSincResampler, we produce the signal block-by-10ms-block 172 // With the PushSincResampler, we produce the signal block-by-10ms-block
171 // rather than in a single pass, to exercise how it will be used in WebRTC. 173 // rather than in a single pass, to exercise how it will be used in WebRTC.
172 resampler_source.Run(input_samples, source.get()); 174 resampler_source.Run(input_samples, source.get());
173 if (int_format) { 175 if (int_format) {
174 for (int i = 0; i < kNumBlocks; ++i) { 176 for (size_t i = 0; i < kNumBlocks; ++i) {
175 FloatToS16(&source[i * input_block_size], input_block_size, 177 FloatToS16(&source[i * input_block_size], input_block_size,
176 source_int.get()); 178 source_int.get());
177 EXPECT_EQ(output_block_size, 179 EXPECT_EQ(output_block_size,
178 resampler.Resample(source_int.get(), 180 resampler.Resample(source_int.get(),
179 input_block_size, 181 input_block_size,
180 destination_int.get(), 182 destination_int.get(),
181 output_block_size)); 183 output_block_size));
182 S16ToFloat(destination_int.get(), output_block_size, 184 S16ToFloat(destination_int.get(), output_block_size,
183 &resampled_destination[i * output_block_size]); 185 &resampled_destination[i * output_block_size]);
184 } 186 }
185 } else { 187 } else {
186 for (int i = 0; i < kNumBlocks; ++i) { 188 for (size_t i = 0; i < kNumBlocks; ++i) {
187 EXPECT_EQ( 189 EXPECT_EQ(
188 output_block_size, 190 output_block_size,
189 resampler.Resample(&source[i * input_block_size], 191 resampler.Resample(&source[i * input_block_size],
190 input_block_size, 192 input_block_size,
191 &resampled_destination[i * output_block_size], 193 &resampled_destination[i * output_block_size],
192 output_block_size)); 194 output_block_size));
193 } 195 }
194 } 196 }
195 197
196 // Generate pure signal. 198 // Generate pure signal.
197 SinusoidalLinearChirpSource pure_source( 199 SinusoidalLinearChirpSource pure_source(
198 output_rate_, output_samples, input_nyquist_freq, output_delay_samples); 200 output_rate_, output_samples, input_nyquist_freq, output_delay_samples);
199 pure_source.Run(output_samples, pure_destination.get()); 201 pure_source.Run(output_samples, pure_destination.get());
200 202
201 // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which 203 // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which
202 // we refer to as low and high. 204 // we refer to as low and high.
203 static const double kLowFrequencyNyquistRange = 0.7; 205 static const double kLowFrequencyNyquistRange = 0.7;
204 static const double kHighFrequencyNyquistRange = 0.9; 206 static const double kHighFrequencyNyquistRange = 0.9;
205 207
206 // Calculate Root-Mean-Square-Error and maximum error for the resampling. 208 // Calculate Root-Mean-Square-Error and maximum error for the resampling.
207 double sum_of_squares = 0; 209 double sum_of_squares = 0;
208 double low_freq_max_error = 0; 210 double low_freq_max_error = 0;
209 double high_freq_max_error = 0; 211 double high_freq_max_error = 0;
210 int minimum_rate = std::min(input_rate_, output_rate_); 212 int minimum_rate = std::min(input_rate_, output_rate_);
211 double low_frequency_range = kLowFrequencyNyquistRange * 0.5 * minimum_rate; 213 double low_frequency_range = kLowFrequencyNyquistRange * 0.5 * minimum_rate;
212 double high_frequency_range = kHighFrequencyNyquistRange * 0.5 * minimum_rate; 214 double high_frequency_range = kHighFrequencyNyquistRange * 0.5 * minimum_rate;
213 215
214 for (int i = 0; i < output_samples; ++i) { 216 for (size_t i = 0; i < output_samples; ++i) {
215 double error = fabs(resampled_destination[i] - pure_destination[i]); 217 double error = fabs(resampled_destination[i] - pure_destination[i]);
216 218
217 if (pure_source.Frequency(i) < low_frequency_range) { 219 if (pure_source.Frequency(i) < low_frequency_range) {
218 if (error > low_freq_max_error) 220 if (error > low_freq_max_error)
219 low_freq_max_error = error; 221 low_freq_max_error = error;
220 } else if (pure_source.Frequency(i) < high_frequency_range) { 222 } else if (pure_source.Frequency(i) < high_frequency_range) {
221 if (error > high_freq_max_error) 223 if (error > high_freq_max_error)
222 high_freq_max_error = error; 224 high_freq_max_error = error;
223 } 225 }
224 // TODO(dalecurtis): Sanity check frequencies > kHighFrequencyNyquistRange. 226 // TODO(dalecurtis): Sanity check frequencies > kHighFrequencyNyquistRange.
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 // To 32 kHz 326 // To 32 kHz
325 ::testing::make_tuple(8000, 32000, kResamplingRMSError, -70.30), 327 ::testing::make_tuple(8000, 32000, kResamplingRMSError, -70.30),
326 ::testing::make_tuple(16000, 32000, kResamplingRMSError, -75.51), 328 ::testing::make_tuple(16000, 32000, kResamplingRMSError, -75.51),
327 ::testing::make_tuple(32000, 32000, kResamplingRMSError, -75.51), 329 ::testing::make_tuple(32000, 32000, kResamplingRMSError, -75.51),
328 ::testing::make_tuple(44100, 32000, -16.44, -51.10), 330 ::testing::make_tuple(44100, 32000, -16.44, -51.10),
329 ::testing::make_tuple(48000, 32000, -16.90, -44.03), 331 ::testing::make_tuple(48000, 32000, -16.90, -44.03),
330 ::testing::make_tuple(96000, 32000, -19.61, -18.04), 332 ::testing::make_tuple(96000, 32000, -19.61, -18.04),
331 ::testing::make_tuple(192000, 32000, -21.02, -10.94))); 333 ::testing::make_tuple(192000, 32000, -21.02, -10.94)));
332 334
333 } // namespace webrtc 335 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/common_audio/resampler/push_sinc_resampler.cc ('k') | webrtc/common_audio/resampler/resampler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698