| OLD | NEW |
| 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 |
| 11 #include <cmath> | 11 #include <cmath> |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webrtc/base/arraysize.h" |
| 16 #include "webrtc/base/format_macros.h" | 17 #include "webrtc/base/format_macros.h" |
| 17 #include "webrtc/base/scoped_ptr.h" | 18 #include "webrtc/base/scoped_ptr.h" |
| 18 #include "webrtc/common_audio/audio_converter.h" | 19 #include "webrtc/common_audio/audio_converter.h" |
| 19 #include "webrtc/common_audio/channel_buffer.h" | 20 #include "webrtc/common_audio/channel_buffer.h" |
| 20 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" | 21 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" |
| 21 | 22 |
| 22 namespace webrtc { | 23 namespace webrtc { |
| 23 | 24 |
| 24 typedef rtc::scoped_ptr<ChannelBuffer<float>> ScopedBuffer; | 25 typedef rtc::scoped_ptr<ChannelBuffer<float>> ScopedBuffer; |
| 25 | 26 |
| 26 // Sets the signal value to increase by |data| with every sample. | 27 // Sets the signal value to increase by |data| with every sample. |
| 27 ScopedBuffer CreateBuffer(const std::vector<float>& data, int frames) { | 28 ScopedBuffer CreateBuffer(const std::vector<float>& data, size_t frames) { |
| 28 const int num_channels = static_cast<int>(data.size()); | 29 const int num_channels = static_cast<int>(data.size()); |
| 29 ScopedBuffer sb(new ChannelBuffer<float>(frames, num_channels)); | 30 ScopedBuffer sb(new ChannelBuffer<float>(frames, num_channels)); |
| 30 for (int i = 0; i < num_channels; ++i) | 31 for (int i = 0; i < num_channels; ++i) |
| 31 for (int j = 0; j < frames; ++j) | 32 for (size_t j = 0; j < frames; ++j) |
| 32 sb->channels()[i][j] = data[i] * j; | 33 sb->channels()[i][j] = data[i] * j; |
| 33 return sb; | 34 return sb; |
| 34 } | 35 } |
| 35 | 36 |
| 36 void VerifyParams(const ChannelBuffer<float>& ref, | 37 void VerifyParams(const ChannelBuffer<float>& ref, |
| 37 const ChannelBuffer<float>& test) { | 38 const ChannelBuffer<float>& test) { |
| 38 EXPECT_EQ(ref.num_channels(), test.num_channels()); | 39 EXPECT_EQ(ref.num_channels(), test.num_channels()); |
| 39 EXPECT_EQ(ref.num_frames(), test.num_frames()); | 40 EXPECT_EQ(ref.num_frames(), test.num_frames()); |
| 40 } | 41 } |
| 41 | 42 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 int src_sample_rate_hz, | 90 int src_sample_rate_hz, |
| 90 int dst_channels, | 91 int dst_channels, |
| 91 int dst_sample_rate_hz) { | 92 int dst_sample_rate_hz) { |
| 92 const float kSrcLeft = 0.0002f; | 93 const float kSrcLeft = 0.0002f; |
| 93 const float kSrcRight = 0.0001f; | 94 const float kSrcRight = 0.0001f; |
| 94 const float resampling_factor = (1.f * src_sample_rate_hz) / | 95 const float resampling_factor = (1.f * src_sample_rate_hz) / |
| 95 dst_sample_rate_hz; | 96 dst_sample_rate_hz; |
| 96 const float dst_left = resampling_factor * kSrcLeft; | 97 const float dst_left = resampling_factor * kSrcLeft; |
| 97 const float dst_right = resampling_factor * kSrcRight; | 98 const float dst_right = resampling_factor * kSrcRight; |
| 98 const float dst_mono = (dst_left + dst_right) / 2; | 99 const float dst_mono = (dst_left + dst_right) / 2; |
| 99 const int src_frames = src_sample_rate_hz / 100; | 100 const size_t src_frames = static_cast<size_t>(src_sample_rate_hz / 100); |
| 100 const int dst_frames = dst_sample_rate_hz / 100; | 101 const size_t dst_frames = static_cast<size_t>(dst_sample_rate_hz / 100); |
| 101 | 102 |
| 102 std::vector<float> src_data(1, kSrcLeft); | 103 std::vector<float> src_data(1, kSrcLeft); |
| 103 if (src_channels == 2) | 104 if (src_channels == 2) |
| 104 src_data.push_back(kSrcRight); | 105 src_data.push_back(kSrcRight); |
| 105 ScopedBuffer src_buffer = CreateBuffer(src_data, src_frames); | 106 ScopedBuffer src_buffer = CreateBuffer(src_data, src_frames); |
| 106 | 107 |
| 107 std::vector<float> dst_data(1, 0); | 108 std::vector<float> dst_data(1, 0); |
| 108 std::vector<float> ref_data; | 109 std::vector<float> ref_data; |
| 109 if (dst_channels == 1) { | 110 if (dst_channels == 1) { |
| 110 if (src_channels == 1) | 111 if (src_channels == 1) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 134 src_channels, src_frames, dst_channels, dst_frames); | 135 src_channels, src_frames, dst_channels, dst_frames); |
| 135 converter->Convert(src_buffer->channels(), src_buffer->size(), | 136 converter->Convert(src_buffer->channels(), src_buffer->size(), |
| 136 dst_buffer->channels(), dst_buffer->size()); | 137 dst_buffer->channels(), dst_buffer->size()); |
| 137 | 138 |
| 138 EXPECT_LT(43.f, | 139 EXPECT_LT(43.f, |
| 139 ComputeSNR(*ref_buffer.get(), *dst_buffer.get(), delay_frames)); | 140 ComputeSNR(*ref_buffer.get(), *dst_buffer.get(), delay_frames)); |
| 140 } | 141 } |
| 141 | 142 |
| 142 TEST(AudioConverterTest, ConversionsPassSNRThreshold) { | 143 TEST(AudioConverterTest, ConversionsPassSNRThreshold) { |
| 143 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000}; | 144 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000}; |
| 144 const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates); | |
| 145 const int kChannels[] = {1, 2}; | 145 const int kChannels[] = {1, 2}; |
| 146 const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels); | 146 for (size_t src_rate = 0; src_rate < arraysize(kSampleRates); ++src_rate) { |
| 147 for (int src_rate = 0; src_rate < kSampleRatesSize; ++src_rate) { | 147 for (size_t dst_rate = 0; dst_rate < arraysize(kSampleRates); ++dst_rate) { |
| 148 for (int dst_rate = 0; dst_rate < kSampleRatesSize; ++dst_rate) { | 148 for (size_t src_channel = 0; src_channel < arraysize(kChannels); |
| 149 for (int src_channel = 0; src_channel < kChannelsSize; ++src_channel) { | 149 ++src_channel) { |
| 150 for (int dst_channel = 0; dst_channel < kChannelsSize; ++dst_channel) { | 150 for (size_t dst_channel = 0; dst_channel < arraysize(kChannels); |
| 151 ++dst_channel) { |
| 151 RunAudioConverterTest(kChannels[src_channel], kSampleRates[src_rate], | 152 RunAudioConverterTest(kChannels[src_channel], kSampleRates[src_rate], |
| 152 kChannels[dst_channel], kSampleRates[dst_rate]); | 153 kChannels[dst_channel], kSampleRates[dst_rate]); |
| 153 } | 154 } |
| 154 } | 155 } |
| 155 } | 156 } |
| 156 } | 157 } |
| 157 } | 158 } |
| 158 | 159 |
| 159 } // namespace webrtc | 160 } // namespace webrtc |
| OLD | NEW |