Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / common_audio / audio_converter_unittest.cc
1 /*
2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <math.h>
12 #include <algorithm>
13 #include <vector>
14
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "webrtc/common_audio/audio_converter.h"
17 #include "webrtc/common_audio/resampler/push_sinc_resampler.h"
18 #include "webrtc/modules/audio_processing/common.h"
19 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
20
21 namespace webrtc {
22
23 typedef scoped_ptr<ChannelBuffer<float>> ScopedBuffer;
24
25 // Sets the signal value to increase by |data| with every sample.
26 ScopedBuffer CreateBuffer(const std::vector<float>& data, int frames) {
27   const int num_channels = static_cast<int>(data.size());
28   ScopedBuffer sb(new ChannelBuffer<float>(frames, num_channels));
29   for (int i = 0; i < num_channels; ++i)
30     for (int j = 0; j < frames; ++j)
31       sb->channel(i)[j] = data[i] * j;
32   return sb;
33 }
34
35 void VerifyParams(const ChannelBuffer<float>& ref,
36                   const ChannelBuffer<float>& test) {
37   EXPECT_EQ(ref.num_channels(), test.num_channels());
38   EXPECT_EQ(ref.samples_per_channel(), test.samples_per_channel());
39 }
40
41 // Computes the best SNR based on the error between |ref_frame| and
42 // |test_frame|. It searches around |expected_delay| in samples between the
43 // signals to compensate for the resampling delay.
44 float ComputeSNR(const ChannelBuffer<float>& ref,
45                  const ChannelBuffer<float>& test,
46                  int expected_delay) {
47   VerifyParams(ref, test);
48   float best_snr = 0;
49   int best_delay = 0;
50
51   // Search within one sample of the expected delay.
52   for (int delay = std::max(expected_delay - 1, 0);
53        delay <= std::min(expected_delay + 1, ref.samples_per_channel());
54        ++delay) {
55     float mse = 0;
56     float variance = 0;
57     float mean = 0;
58     for (int i = 0; i < ref.num_channels(); ++i) {
59       for (int j = 0; j < ref.samples_per_channel() - delay; ++j) {
60         float error = ref.channel(i)[j] - test.channel(i)[j + delay];
61         mse += error * error;
62         variance += ref.channel(i)[j] * ref.channel(i)[j];
63         mean += ref.channel(i)[j];
64       }
65     }
66     const int length = ref.num_channels() * (ref.samples_per_channel() - delay);
67     mse /= length;
68     variance /= length;
69     mean /= length;
70     variance -= mean * mean;
71     float snr = 100;  // We assign 100 dB to the zero-error case.
72     if (mse > 0)
73       snr = 10 * log10(variance / mse);
74     if (snr > best_snr) {
75       best_snr = snr;
76       best_delay = delay;
77     }
78   }
79   printf("SNR=%.1f dB at delay=%d\n", best_snr, best_delay);
80   return best_snr;
81 }
82
83 // Sets the source to a linearly increasing signal for which we can easily
84 // generate a reference. Runs the AudioConverter and ensures the output has
85 // sufficiently high SNR relative to the reference.
86 void RunAudioConverterTest(int src_channels,
87                            int src_sample_rate_hz,
88                            int dst_channels,
89                            int dst_sample_rate_hz) {
90   const float kSrcLeft = 0.0002f;
91   const float kSrcRight = 0.0001f;
92   const float resampling_factor = (1.f * src_sample_rate_hz) /
93       dst_sample_rate_hz;
94   const float dst_left = resampling_factor * kSrcLeft;
95   const float dst_right = resampling_factor * kSrcRight;
96   const float dst_mono = (dst_left + dst_right) / 2;
97   const int src_frames = src_sample_rate_hz / 100;
98   const int dst_frames = dst_sample_rate_hz / 100;
99
100   std::vector<float> src_data(1, kSrcLeft);
101   if (src_channels == 2)
102     src_data.push_back(kSrcRight);
103   ScopedBuffer src_buffer = CreateBuffer(src_data, src_frames);
104
105   std::vector<float> dst_data(1, 0);
106   std::vector<float> ref_data;
107   if (dst_channels == 1) {
108     if (src_channels == 1)
109       ref_data.push_back(dst_left);
110     else
111       ref_data.push_back(dst_mono);
112   } else {
113     dst_data.push_back(0);
114     ref_data.push_back(dst_left);
115     if (src_channels == 1)
116       ref_data.push_back(dst_left);
117     else
118       ref_data.push_back(dst_right);
119   }
120   ScopedBuffer dst_buffer = CreateBuffer(dst_data, dst_frames);
121   ScopedBuffer ref_buffer = CreateBuffer(ref_data, dst_frames);
122
123   // The sinc resampler has a known delay, which we compute here.
124   const int delay_frames = src_sample_rate_hz == dst_sample_rate_hz ? 0 :
125       PushSincResampler::AlgorithmicDelaySeconds(src_sample_rate_hz) *
126           dst_sample_rate_hz;
127   printf("(%d, %d Hz) -> (%d, %d Hz) ",  // SNR reported on the same line later.
128       src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
129
130   AudioConverter converter(src_channels, src_frames, dst_channels, dst_frames);
131   converter.Convert(src_buffer->channels(), src_channels, src_frames,
132                     dst_channels, dst_frames, dst_buffer->channels());
133
134   EXPECT_LT(43.f,
135             ComputeSNR(*ref_buffer.get(), *dst_buffer.get(), delay_frames));
136 }
137
138 TEST(AudioConverterTest, ConversionsPassSNRThreshold) {
139   const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000};
140   const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates);
141   const int kChannels[] = {1, 2};
142   const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels);
143   for (int src_rate = 0; src_rate < kSampleRatesSize; ++src_rate) {
144     for (int dst_rate = 0; dst_rate < kSampleRatesSize; ++dst_rate) {
145       for (int src_channel = 0; src_channel < kChannelsSize; ++src_channel) {
146         for (int dst_channel = 0; dst_channel < kChannelsSize; ++dst_channel) {
147           RunAudioConverterTest(kChannels[src_channel], kSampleRates[src_rate],
148                                 kChannels[dst_channel], kSampleRates[dst_rate]);
149         }
150       }
151     }
152   }
153 }
154
155 }  // namespace webrtc