• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <gtest/gtest.h>
6 #include <limits.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 
10 extern "C" {
11 #include "linear_resampler.h"
12 }
13 
14 #define BUF_SIZE 2048
15 
16 static uint8_t in_buf[BUF_SIZE];
17 static uint8_t out_buf[BUF_SIZE];
18 
TEST(LinearResampler,ReampleToSlightlyLargerRate)19 TEST(LinearResampler, ReampleToSlightlyLargerRate) {
20   int i, rc;
21   unsigned int count;
22   unsigned int in_offset = 0;
23   unsigned int out_offset = 0;
24   struct linear_resampler* lr;
25 
26   memset(in_buf, 0, BUF_SIZE);
27   memset(out_buf, 0, BUF_SIZE);
28   for (i = 0; i < 100; i++) {
29     *((int16_t*)(in_buf + i * 4)) = i * 10;
30     *((int16_t*)(in_buf + i * 4 + 2)) = i * 20;
31   }
32 
33   lr = linear_resampler_create(2, 4, 48000, 48001);
34 
35   count = 20;
36   rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
37                                  out_buf + 4 * out_offset, 50);
38   EXPECT_EQ(20, rc);
39   EXPECT_EQ(20, count);
40 
41   in_offset += count;
42   out_offset += rc;
43   count = 20;
44   rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
45                                  out_buf + 4 * out_offset, 15);
46   EXPECT_EQ(15, rc);
47   EXPECT_EQ(15, count);
48 
49   /* Assert linear interpotation result. */
50   for (i = 0; i < 34; i++) {
51     EXPECT_GE(*(int16_t*)(in_buf + 4 * i), *(int16_t*)(out_buf + 4 * i));
52     EXPECT_LE(*(int16_t*)(in_buf + 4 * i), *(int16_t*)(out_buf + 4 * (i + 1)));
53   }
54   linear_resampler_destroy(lr);
55 }
56 
TEST(LinearResampler,ResampleIntegerFractionToLarger)57 TEST(LinearResampler, ResampleIntegerFractionToLarger) {
58   int i, rc;
59   unsigned int count;
60   unsigned int in_offset = 0;
61   unsigned int out_offset = 0;
62   struct linear_resampler* lr;
63 
64   memset(in_buf, 0, BUF_SIZE);
65   memset(out_buf, 0, BUF_SIZE);
66   for (i = 0; i < 100; i++) {
67     *((int16_t*)(in_buf + i * 4)) = SHRT_MAX - i;
68     *((int16_t*)(in_buf + i * 4 + 2)) = SHRT_MAX - i * 10;
69   }
70 
71   /* Rate 10 -> 11 */
72   lr = linear_resampler_create(2, 4, 10, 11);
73 
74   count = 5;
75   rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
76                                  out_buf + 4 * out_offset, 10);
77   EXPECT_EQ(5, rc);
78   EXPECT_EQ(5, count);
79 
80   in_offset += count;
81   out_offset += rc;
82   count = 6;
83   /* Assert source rate + 1 frames resample to destination rate + 1
84    * frames. */
85   rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
86                                  out_buf + 4 * out_offset, 10);
87   EXPECT_EQ(7, rc);
88   EXPECT_EQ(6, count);
89 
90   in_offset += count;
91   out_offset += rc;
92   count = 89;
93   rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
94                                  out_buf + 4 * out_offset, 100);
95   EXPECT_EQ(97, rc);
96   EXPECT_EQ(89, count);
97 
98   /* Assert linear interpotation result. */
99   for (i = 0; i < 90; i++) {
100     EXPECT_LE(*(int16_t*)(in_buf + 4 * i), *(int16_t*)(out_buf + 4 * i));
101     EXPECT_LE(*(int16_t*)(in_buf + 4 * i + 2),
102               *(int16_t*)(out_buf + 4 * i + 2));
103   }
104   linear_resampler_destroy(lr);
105 }
106 
TEST(LinearResampler,ResampleIntegerFractionToLess)107 TEST(LinearResampler, ResampleIntegerFractionToLess) {
108   int i, rc;
109   unsigned int count;
110   unsigned int in_offset = 0;
111   unsigned int out_offset = 0;
112   struct linear_resampler* lr;
113 
114   memset(in_buf, 0, BUF_SIZE);
115   memset(out_buf, 0, BUF_SIZE);
116   for (i = 0; i < 100; i++) {
117     *((int16_t*)(in_buf + i * 4)) = SHRT_MIN + i * 10;
118     *((int16_t*)(in_buf + i * 4 + 2)) = SHRT_MIN + i * 20;
119   }
120 
121   /* Rate 10 -> 9 */
122   lr = linear_resampler_create(2, 4, 10, 9);
123 
124   count = 6;
125   rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
126                                  out_buf + 4 * out_offset, 6);
127   EXPECT_EQ(5, rc);
128   EXPECT_EQ(6, count);
129 
130   in_offset += count;
131   out_offset += rc;
132   count = 4;
133 
134   /* Assert source rate frames resample to destination rate frames. */
135   rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
136                                  out_buf + 4 * out_offset, 4);
137   EXPECT_EQ(4, rc);
138   EXPECT_EQ(4, count);
139 
140   in_offset += count;
141   out_offset += rc;
142   count = 90;
143   rc = linear_resampler_resample(lr, in_buf + 4 * in_offset, &count,
144                                  out_buf + 4 * out_offset, 90);
145 
146   /* Assert linear interpotation result. */
147   for (i = 0; i < 90; i++) {
148     EXPECT_LE(*(int16_t*)(in_buf + 4 * i), *(int16_t*)(out_buf + 4 * i));
149     EXPECT_LE(*(int16_t*)(in_buf + 4 * i + 2),
150               *(int16_t*)(out_buf + 4 * i + 2));
151   }
152   linear_resampler_destroy(lr);
153 }
154 
TEST(LinearResampler,ResampleIntegerNoSrcBuffer)155 TEST(LinearResampler, ResampleIntegerNoSrcBuffer) {
156   int rc;
157   unsigned int count;
158   struct linear_resampler* lr;
159 
160   memset(in_buf, 0, BUF_SIZE);
161   memset(out_buf, 0, BUF_SIZE);
162 
163   /* Rate 10 -> 9 */
164   lr = linear_resampler_create(2, 4, 10, 9);
165 
166   count = 0;
167   rc = linear_resampler_resample(lr, in_buf, &count, out_buf, BUF_SIZE);
168   EXPECT_EQ(0, rc);
169   EXPECT_EQ(0, count);
170   linear_resampler_destroy(lr);
171 }
172 
TEST(LinearResampler,ResampleIntegerNoDstBuffer)173 TEST(LinearResampler, ResampleIntegerNoDstBuffer) {
174   int rc;
175   unsigned int count;
176   struct linear_resampler* lr;
177 
178   memset(in_buf, 0, BUF_SIZE);
179   memset(out_buf, 0, BUF_SIZE);
180 
181   /* Rate 10 -> 9 */
182   lr = linear_resampler_create(2, 4, 10, 9);
183 
184   count = BUF_SIZE;
185   rc = linear_resampler_resample(lr, in_buf, &count, out_buf, 0);
186   EXPECT_EQ(0, rc);
187   EXPECT_EQ(0, count);
188   linear_resampler_destroy(lr);
189 }
190 
191 extern "C" {
192 
cras_mix_add_scale_stride(int fmt,uint8_t * dst,uint8_t * src,unsigned int count,unsigned int dst_stride,unsigned int src_stride,float scaler)193 void cras_mix_add_scale_stride(int fmt,
194                                uint8_t* dst,
195                                uint8_t* src,
196                                unsigned int count,
197                                unsigned int dst_stride,
198                                unsigned int src_stride,
199                                float scaler) {
200   unsigned int i;
201 
202   for (i = 0; i < count; i++) {
203     int32_t sum;
204     sum = *(int16_t*)dst + *(int16_t*)src * scaler;
205     if (sum > INT16_MAX)
206       sum = INT16_MAX;
207     else if (sum < INT16_MIN)
208       sum = INT16_MIN;
209     *(int16_t*)dst = sum;
210     dst += dst_stride;
211     src += src_stride;
212   }
213 }
214 
215 }  //  extern "C"
216 
main(int argc,char ** argv)217 int main(int argc, char** argv) {
218   ::testing::InitGoogleTest(&argc, argv);
219   return RUN_ALL_TESTS();
220 }
221