OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 29 matching lines...) Expand all Loading... |
40 PushResampler<int16_t> resampler_; | 40 PushResampler<int16_t> resampler_; |
41 AudioFrame src_frame_; | 41 AudioFrame src_frame_; |
42 AudioFrame dst_frame_; | 42 AudioFrame dst_frame_; |
43 AudioFrame golden_frame_; | 43 AudioFrame golden_frame_; |
44 }; | 44 }; |
45 | 45 |
46 // Sets the signal value to increase by |data| with every sample. Floats are | 46 // Sets the signal value to increase by |data| with every sample. Floats are |
47 // used so non-integer values result in rounding error, but not an accumulating | 47 // used so non-integer values result in rounding error, but not an accumulating |
48 // error. | 48 // error. |
49 void SetMonoFrame(float data, int sample_rate_hz, AudioFrame* frame) { | 49 void SetMonoFrame(float data, int sample_rate_hz, AudioFrame* frame) { |
50 memset(frame->data_, 0, sizeof(frame->data_)); | 50 frame->Mute(); |
51 frame->num_channels_ = 1; | 51 frame->num_channels_ = 1; |
52 frame->sample_rate_hz_ = sample_rate_hz; | 52 frame->sample_rate_hz_ = sample_rate_hz; |
53 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100); | 53 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100); |
| 54 int16_t* frame_data = frame->mutable_data(); |
54 for (size_t i = 0; i < frame->samples_per_channel_; i++) { | 55 for (size_t i = 0; i < frame->samples_per_channel_; i++) { |
55 frame->data_[i] = static_cast<int16_t>(data * i); | 56 frame_data[i] = static_cast<int16_t>(data * i); |
56 } | 57 } |
57 } | 58 } |
58 | 59 |
59 // Keep the existing sample rate. | 60 // Keep the existing sample rate. |
60 void SetMonoFrame(float data, AudioFrame* frame) { | 61 void SetMonoFrame(float data, AudioFrame* frame) { |
61 SetMonoFrame(data, frame->sample_rate_hz_, frame); | 62 SetMonoFrame(data, frame->sample_rate_hz_, frame); |
62 } | 63 } |
63 | 64 |
64 // Sets the signal value to increase by |left| and |right| with every sample in | 65 // Sets the signal value to increase by |left| and |right| with every sample in |
65 // each channel respectively. | 66 // each channel respectively. |
66 void SetStereoFrame(float left, | 67 void SetStereoFrame(float left, |
67 float right, | 68 float right, |
68 int sample_rate_hz, | 69 int sample_rate_hz, |
69 AudioFrame* frame) { | 70 AudioFrame* frame) { |
70 memset(frame->data_, 0, sizeof(frame->data_)); | 71 frame->Mute(); |
71 frame->num_channels_ = 2; | 72 frame->num_channels_ = 2; |
72 frame->sample_rate_hz_ = sample_rate_hz; | 73 frame->sample_rate_hz_ = sample_rate_hz; |
73 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100); | 74 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100); |
| 75 int16_t* frame_data = frame->mutable_data(); |
74 for (size_t i = 0; i < frame->samples_per_channel_; i++) { | 76 for (size_t i = 0; i < frame->samples_per_channel_; i++) { |
75 frame->data_[i * 2] = static_cast<int16_t>(left * i); | 77 frame_data[i * 2] = static_cast<int16_t>(left * i); |
76 frame->data_[i * 2 + 1] = static_cast<int16_t>(right * i); | 78 frame_data[i * 2 + 1] = static_cast<int16_t>(right * i); |
77 } | 79 } |
78 } | 80 } |
79 | 81 |
80 // Keep the existing sample rate. | 82 // Keep the existing sample rate. |
81 void SetStereoFrame(float left, float right, AudioFrame* frame) { | 83 void SetStereoFrame(float left, float right, AudioFrame* frame) { |
82 SetStereoFrame(left, right, frame->sample_rate_hz_, frame); | 84 SetStereoFrame(left, right, frame->sample_rate_hz_, frame); |
83 } | 85 } |
84 | 86 |
85 // Sets the signal value to increase by |ch1|, |ch2|, |ch3|, |ch4| with every | 87 // Sets the signal value to increase by |ch1|, |ch2|, |ch3|, |ch4| with every |
86 // sample in each channel respectively. | 88 // sample in each channel respectively. |
87 void SetQuadFrame(float ch1, | 89 void SetQuadFrame(float ch1, |
88 float ch2, | 90 float ch2, |
89 float ch3, | 91 float ch3, |
90 float ch4, | 92 float ch4, |
91 int sample_rate_hz, | 93 int sample_rate_hz, |
92 AudioFrame* frame) { | 94 AudioFrame* frame) { |
93 memset(frame->data_, 0, sizeof(frame->data_)); | 95 frame->Mute(); |
94 frame->num_channels_ = 4; | 96 frame->num_channels_ = 4; |
95 frame->sample_rate_hz_ = sample_rate_hz; | 97 frame->sample_rate_hz_ = sample_rate_hz; |
96 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100); | 98 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100); |
| 99 int16_t* frame_data = frame->mutable_data(); |
97 for (size_t i = 0; i < frame->samples_per_channel_; i++) { | 100 for (size_t i = 0; i < frame->samples_per_channel_; i++) { |
98 frame->data_[i * 4] = static_cast<int16_t>(ch1 * i); | 101 frame_data[i * 4] = static_cast<int16_t>(ch1 * i); |
99 frame->data_[i * 4 + 1] = static_cast<int16_t>(ch2 * i); | 102 frame_data[i * 4 + 1] = static_cast<int16_t>(ch2 * i); |
100 frame->data_[i * 4 + 2] = static_cast<int16_t>(ch3 * i); | 103 frame_data[i * 4 + 2] = static_cast<int16_t>(ch3 * i); |
101 frame->data_[i * 4 + 3] = static_cast<int16_t>(ch4 * i); | 104 frame_data[i * 4 + 3] = static_cast<int16_t>(ch4 * i); |
102 } | 105 } |
103 } | 106 } |
104 | 107 |
105 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) { | 108 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) { |
106 EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_); | 109 EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_); |
107 EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_); | 110 EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_); |
108 EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_); | 111 EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_); |
109 } | 112 } |
110 | 113 |
111 // Computes the best SNR based on the error between |ref_frame| and | 114 // Computes the best SNR based on the error between |ref_frame| and |
112 // |test_frame|. It allows for up to a |max_delay| in samples between the | 115 // |test_frame|. It allows for up to a |max_delay| in samples between the |
113 // signals to compensate for the resampling delay. | 116 // signals to compensate for the resampling delay. |
114 float ComputeSNR(const AudioFrame& ref_frame, const AudioFrame& test_frame, | 117 float ComputeSNR(const AudioFrame& ref_frame, const AudioFrame& test_frame, |
115 size_t max_delay) { | 118 size_t max_delay) { |
116 VerifyParams(ref_frame, test_frame); | 119 VerifyParams(ref_frame, test_frame); |
117 float best_snr = 0; | 120 float best_snr = 0; |
118 size_t best_delay = 0; | 121 size_t best_delay = 0; |
119 for (size_t delay = 0; delay <= max_delay; delay++) { | 122 for (size_t delay = 0; delay <= max_delay; delay++) { |
120 float mse = 0; | 123 float mse = 0; |
121 float variance = 0; | 124 float variance = 0; |
| 125 const int16_t* ref_frame_data = ref_frame.data(); |
| 126 const int16_t* test_frame_data = test_frame.data(); |
122 for (size_t i = 0; i < ref_frame.samples_per_channel_ * | 127 for (size_t i = 0; i < ref_frame.samples_per_channel_ * |
123 ref_frame.num_channels_ - delay; i++) { | 128 ref_frame.num_channels_ - delay; i++) { |
124 int error = ref_frame.data_[i] - test_frame.data_[i + delay]; | 129 int error = ref_frame_data[i] - test_frame_data[i + delay]; |
125 mse += error * error; | 130 mse += error * error; |
126 variance += ref_frame.data_[i] * ref_frame.data_[i]; | 131 variance += ref_frame_data[i] * ref_frame_data[i]; |
127 } | 132 } |
128 float snr = 100; // We assign 100 dB to the zero-error case. | 133 float snr = 100; // We assign 100 dB to the zero-error case. |
129 if (mse > 0) | 134 if (mse > 0) |
130 snr = 10 * log10(variance / mse); | 135 snr = 10 * log10(variance / mse); |
131 if (snr > best_snr) { | 136 if (snr > best_snr) { |
132 best_snr = snr; | 137 best_snr = snr; |
133 best_delay = delay; | 138 best_delay = delay; |
134 } | 139 } |
135 } | 140 } |
136 printf("SNR=%.1f dB at delay=%" PRIuS "\n", best_snr, best_delay); | 141 printf("SNR=%.1f dB at delay=%" PRIuS "\n", best_snr, best_delay); |
137 return best_snr; | 142 return best_snr; |
138 } | 143 } |
139 | 144 |
140 void VerifyFramesAreEqual(const AudioFrame& ref_frame, | 145 void VerifyFramesAreEqual(const AudioFrame& ref_frame, |
141 const AudioFrame& test_frame) { | 146 const AudioFrame& test_frame) { |
142 VerifyParams(ref_frame, test_frame); | 147 VerifyParams(ref_frame, test_frame); |
| 148 const int16_t* ref_frame_data = ref_frame.data(); |
| 149 const int16_t* test_frame_data = test_frame.data(); |
143 for (size_t i = 0; | 150 for (size_t i = 0; |
144 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) { | 151 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) { |
145 EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]); | 152 EXPECT_EQ(ref_frame_data[i], test_frame_data[i]); |
146 } | 153 } |
147 } | 154 } |
148 | 155 |
149 void UtilityTest::RunResampleTest(int src_channels, | 156 void UtilityTest::RunResampleTest(int src_channels, |
150 int src_sample_rate_hz, | 157 int src_sample_rate_hz, |
151 int dst_channels, | 158 int dst_channels, |
152 int dst_sample_rate_hz) { | 159 int dst_sample_rate_hz) { |
153 PushResampler<int16_t> resampler; // Create a new one with every test. | 160 PushResampler<int16_t> resampler; // Create a new one with every test. |
154 const int16_t kSrcCh1 = 30; // Shouldn't overflow for any used sample rate. | 161 const int16_t kSrcCh1 = 30; // Shouldn't overflow for any used sample rate. |
155 const int16_t kSrcCh2 = 15; | 162 const int16_t kSrcCh2 = 15; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 kDstChannels[dst_channel], kSampleRates[dst_rate]); | 267 kDstChannels[dst_channel], kSampleRates[dst_rate]); |
261 } | 268 } |
262 } | 269 } |
263 } | 270 } |
264 } | 271 } |
265 } | 272 } |
266 | 273 |
267 } // namespace | 274 } // namespace |
268 } // namespace voe | 275 } // namespace voe |
269 } // namespace webrtc | 276 } // namespace webrtc |
OLD | NEW |