• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 <stdio.h>
7 
8 extern "C" {
9 #include "cras_ramp.c"
10 }
11 
12 static int callback_called;
13 static void* callback_arg;
14 
ResetStubData()15 void ResetStubData() {
16   callback_called = 0;
17   callback_arg = NULL;
18 }
19 
20 namespace {
21 
TEST(RampTestSuite,Init)22 TEST(RampTestSuite, Init) {
23   struct cras_ramp* ramp;
24   struct cras_ramp_action action;
25 
26   ResetStubData();
27 
28   ramp = cras_ramp_create();
29   action = cras_ramp_get_current_action(ramp);
30 
31   EXPECT_EQ(action.type, CRAS_RAMP_ACTION_NONE);
32   EXPECT_FLOAT_EQ(1.0, action.scaler);
33   EXPECT_FLOAT_EQ(0.0, action.increment);
34   EXPECT_FLOAT_EQ(1.0, action.target);
35 
36   cras_ramp_destroy(ramp);
37 }
38 
TEST(RampTestSuite,RampUpInitialIncrement)39 TEST(RampTestSuite, RampUpInitialIncrement) {
40   float from = 0.0;
41   float to = 1.0;
42   int duration_frames = 48000;
43   float increment = 1.0 / 48000;
44   cras_ramp* ramp;
45   cras_ramp_action action;
46 
47   ResetStubData();
48 
49   ramp = cras_ramp_create();
50   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
51 
52   action = cras_ramp_get_current_action(ramp);
53 
54   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
55   EXPECT_FLOAT_EQ(0.0, action.scaler);
56   EXPECT_FLOAT_EQ(increment, action.increment);
57   EXPECT_FLOAT_EQ(to, action.target);
58 
59   cras_ramp_destroy(ramp);
60 }
61 
TEST(RampTestSuite,RampUpUpdateRampedFrames)62 TEST(RampTestSuite, RampUpUpdateRampedFrames) {
63   float from = 0.0;
64   float to = 1.0;
65   int duration_frames = 48000;
66   float increment = 1.0 / 48000;
67   int rc;
68   int ramped_frames = 512;
69   struct cras_ramp* ramp;
70   struct cras_ramp_action action;
71   float scaler = increment * ramped_frames;
72 
73   ResetStubData();
74 
75   ramp = cras_ramp_create();
76   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
77 
78   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
79 
80   action = cras_ramp_get_current_action(ramp);
81 
82   EXPECT_EQ(rc, 0);
83   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
84   EXPECT_FLOAT_EQ(scaler, action.scaler);
85   EXPECT_FLOAT_EQ(increment, action.increment);
86   EXPECT_FLOAT_EQ(to, action.target);
87 
88   cras_ramp_destroy(ramp);
89 }
90 
TEST(RampTestSuite,RampUpPassedRamp)91 TEST(RampTestSuite, RampUpPassedRamp) {
92   float from = 0.0;
93   float to = 1.0;
94   int duration_frames = 48000;
95   int rc;
96   int ramped_frames = 48000;
97   struct cras_ramp* ramp;
98   struct cras_ramp_action action;
99 
100   ResetStubData();
101 
102   ramp = cras_ramp_create();
103   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
104 
105   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
106 
107   action = cras_ramp_get_current_action(ramp);
108 
109   EXPECT_EQ(0, rc);
110   EXPECT_EQ(CRAS_RAMP_ACTION_NONE, action.type);
111   EXPECT_FLOAT_EQ(1.0, action.scaler);
112   EXPECT_FLOAT_EQ(0.0, action.increment);
113   EXPECT_FLOAT_EQ(1.0, action.target);
114 
115   cras_ramp_destroy(ramp);
116 }
117 
TEST(RampTestSuite,RampUpWhileHalfWayRampDown)118 TEST(RampTestSuite, RampUpWhileHalfWayRampDown) {
119   float from;
120   float to;
121   int duration_frames = 48000;
122   int rc;
123   int ramped_frames = 24000;
124   struct cras_ramp* ramp;
125   struct cras_ramp_action action;
126   float down_increment = -1.0 / 48000;
127   float up_increment;
128   float scaler;
129 
130   ResetStubData();
131 
132   ramp = cras_ramp_create();
133   from = 1.0;
134   to = 0.0;
135   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
136 
137   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
138 
139   // Get expected current scaler.
140   scaler = 1 + down_increment * ramped_frames;
141   // The increment will be calculated by ramping to 1 starting from scaler.
142   up_increment = (1 - scaler) / 48000;
143 
144   from = 0.0;
145   to = 1.0;
146   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
147 
148   action = cras_ramp_get_current_action(ramp);
149 
150   EXPECT_EQ(0, rc);
151   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
152   EXPECT_FLOAT_EQ(scaler, action.scaler);
153   EXPECT_FLOAT_EQ(up_increment, action.increment);
154   EXPECT_FLOAT_EQ(to, action.target);
155 
156   cras_ramp_destroy(ramp);
157 }
158 
TEST(RampTestSuite,RampUpWhileHalfWayRampUp)159 TEST(RampTestSuite, RampUpWhileHalfWayRampUp) {
160   float from = 0.0;
161   float to = 1.0;
162   int duration_frames = 48000;
163   int rc;
164   int ramped_frames = 24000;
165   struct cras_ramp* ramp;
166   struct cras_ramp_action action;
167   float first_increment = 1.0 / 48000;
168   float second_increment;
169   float scaler;
170 
171   ResetStubData();
172 
173   ramp = cras_ramp_create();
174   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
175 
176   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
177 
178   // Get expected current scaler.
179   scaler = first_increment * ramped_frames;
180   // The increment will be calculated by ramping to 1 starting from scaler.
181   second_increment = (1 - scaler) / 48000;
182 
183   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
184 
185   action = cras_ramp_get_current_action(ramp);
186 
187   EXPECT_EQ(0, rc);
188   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
189   EXPECT_FLOAT_EQ(scaler, action.scaler);
190   EXPECT_FLOAT_EQ(second_increment, action.increment);
191   EXPECT_FLOAT_EQ(to, action.target);
192 
193   cras_ramp_destroy(ramp);
194 }
195 
TEST(RampTestSuite,RampDownInitialIncrement)196 TEST(RampTestSuite, RampDownInitialIncrement) {
197   float from = 1.0;
198   float to = 0.0;
199   int duration_frames = 48000;
200   float increment = -1.0 / 48000;
201   cras_ramp* ramp;
202   cras_ramp_action action;
203 
204   ResetStubData();
205 
206   ramp = cras_ramp_create();
207   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
208 
209   action = cras_ramp_get_current_action(ramp);
210 
211   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
212   EXPECT_FLOAT_EQ(1.0, action.scaler);
213   EXPECT_FLOAT_EQ(increment, action.increment);
214   EXPECT_FLOAT_EQ(to, action.target);
215 
216   cras_ramp_destroy(ramp);
217 }
218 
TEST(RampTestSuite,RampDownUpdateRampedFrames)219 TEST(RampTestSuite, RampDownUpdateRampedFrames) {
220   float from = 1.0;
221   float to = 0.0;
222   int duration_frames = 48000;
223   float increment = -1.0 / 48000;
224   int rc;
225   int ramped_frames = 512;
226   struct cras_ramp* ramp;
227   struct cras_ramp_action action;
228   float scaler = 1 + increment * ramped_frames;
229 
230   ResetStubData();
231 
232   ramp = cras_ramp_create();
233   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
234 
235   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
236 
237   action = cras_ramp_get_current_action(ramp);
238 
239   EXPECT_EQ(rc, 0);
240   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
241   EXPECT_FLOAT_EQ(scaler, action.scaler);
242   EXPECT_FLOAT_EQ(increment, action.increment);
243   EXPECT_FLOAT_EQ(to, action.target);
244 
245   cras_ramp_destroy(ramp);
246 }
247 
TEST(RampTestSuite,RampDownPassedRamp)248 TEST(RampTestSuite, RampDownPassedRamp) {
249   float from = 1.0;
250   float to = 0.0;
251   int duration_frames = 48000;
252   int rc;
253   int ramped_frames = 48000;
254   struct cras_ramp* ramp;
255   struct cras_ramp_action action;
256 
257   ResetStubData();
258 
259   ramp = cras_ramp_create();
260   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
261 
262   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
263 
264   action = cras_ramp_get_current_action(ramp);
265 
266   EXPECT_EQ(0, rc);
267   EXPECT_EQ(CRAS_RAMP_ACTION_NONE, action.type);
268   EXPECT_FLOAT_EQ(1.0, action.scaler);
269   EXPECT_FLOAT_EQ(0.0, action.increment);
270   EXPECT_FLOAT_EQ(1.0, action.target);
271 
272   cras_ramp_destroy(ramp);
273 }
274 
TEST(RampTestSuite,RampDownWhileHalfWayRampUp)275 TEST(RampTestSuite, RampDownWhileHalfWayRampUp) {
276   float from;
277   float to;
278   int duration_frames = 48000;
279   int rc;
280   int ramped_frames = 24000;
281   struct cras_ramp* ramp;
282   struct cras_ramp_action action;
283   float up_increment = 1.0 / 48000;
284   float down_increment;
285   float scaler;
286 
287   ResetStubData();
288 
289   ramp = cras_ramp_create();
290   // Ramp up first.
291   from = 0.0;
292   to = 1.0;
293   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
294 
295   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
296 
297   // Get expected current scaler.
298   scaler = up_increment * ramped_frames;
299   // The increment will be calculated by ramping to 0 starting from scaler.
300   down_increment = -scaler / duration_frames;
301 
302   // Ramp down will start from current scaler.
303   from = 1.0;
304   to = 0.0;
305   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
306 
307   action = cras_ramp_get_current_action(ramp);
308 
309   EXPECT_EQ(0, rc);
310   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
311   EXPECT_FLOAT_EQ(scaler, action.scaler);
312   EXPECT_FLOAT_EQ(down_increment, action.increment);
313   EXPECT_FLOAT_EQ(to, action.target);
314 
315   cras_ramp_destroy(ramp);
316 }
317 
TEST(RampTestSuite,RampDownWhileHalfWayRampDown)318 TEST(RampTestSuite, RampDownWhileHalfWayRampDown) {
319   float from = 1.0;
320   float to = 0.0;
321   int duration_frames = 48000;
322   int rc;
323   int ramped_frames = 24000;
324   struct cras_ramp* ramp;
325   struct cras_ramp_action action;
326   float down_increment = -1.0 / 48000;
327   float second_down_increment;
328   float scaler;
329 
330   ResetStubData();
331 
332   ramp = cras_ramp_create();
333   // Ramp down.
334   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
335 
336   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
337 
338   // Get expected current scaler.
339   scaler = 1 + down_increment * ramped_frames;
340   // The increment will be calculated by ramping to 0 starting from scaler.
341   second_down_increment = -scaler / duration_frames;
342 
343   // Ramp down starting from current scaler.
344   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
345 
346   action = cras_ramp_get_current_action(ramp);
347 
348   EXPECT_EQ(0, rc);
349   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
350   EXPECT_FLOAT_EQ(scaler, action.scaler);
351   EXPECT_FLOAT_EQ(second_down_increment, action.increment);
352   EXPECT_FLOAT_EQ(to, action.target);
353 
354   cras_ramp_destroy(ramp);
355 }
356 
TEST(RampTestSuite,PartialRamp)357 TEST(RampTestSuite, PartialRamp) {
358   float from_one = 0.75;
359   float to_one = 0.4;
360   float from_two = 0.6;
361   float to_two = 0.9;
362   int duration_frames = 1200;
363   int rc;
364   int ramped_frames = 600;
365   struct cras_ramp* ramp;
366   struct cras_ramp_action action;
367   float down_increment = (to_one - from_one) / duration_frames;
368   float up_increment;
369   float scaler;
370 
371   ResetStubData();
372 
373   ramp = cras_ramp_create();
374   // Ramp down.
375   cras_volume_ramp_start(ramp, from_one, to_one, duration_frames, NULL, NULL);
376 
377   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
378 
379   scaler = from_one + ramped_frames * down_increment;
380   action = cras_ramp_get_current_action(ramp);
381   EXPECT_EQ(0, rc);
382   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
383   EXPECT_FLOAT_EQ(scaler, action.scaler);
384   EXPECT_FLOAT_EQ(down_increment, action.increment);
385   EXPECT_FLOAT_EQ(to_one, action.target);
386 
387   // Ramp up starting from current scaler.
388   cras_volume_ramp_start(ramp, from_two, to_two, duration_frames, NULL, NULL);
389 
390   // we start by multiplying by previous scaler
391   scaler = scaler * from_two;
392   action = cras_ramp_get_current_action(ramp);
393   up_increment = (to_two - scaler) / duration_frames;
394   EXPECT_EQ(0, rc);
395   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
396   EXPECT_FLOAT_EQ(scaler, action.scaler);
397   EXPECT_FLOAT_EQ(up_increment, action.increment);
398   EXPECT_FLOAT_EQ(to_two, action.target);
399 
400   cras_ramp_destroy(ramp);
401 }
402 
ramp_callback(void * arg)403 void ramp_callback(void* arg) {
404   callback_called++;
405   callback_arg = arg;
406 }
407 
TEST(RampTestSuite,RampUpPassedRampCallback)408 TEST(RampTestSuite, RampUpPassedRampCallback) {
409   float from = 0.0;
410   float to = 1.0;
411   int duration_frames = 48000;
412   int rc;
413   int ramped_frames = 48000;
414   struct cras_ramp* ramp;
415   struct cras_ramp_action action;
416   void* cb_data = reinterpret_cast<void*>(0x123);
417 
418   ResetStubData();
419 
420   ramp = cras_ramp_create();
421   cras_mute_ramp_start(ramp, from, to, duration_frames, ramp_callback, cb_data);
422 
423   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
424 
425   action = cras_ramp_get_current_action(ramp);
426 
427   EXPECT_EQ(0, rc);
428   EXPECT_EQ(CRAS_RAMP_ACTION_NONE, action.type);
429   EXPECT_FLOAT_EQ(1.0, action.scaler);
430   EXPECT_FLOAT_EQ(0.0, action.increment);
431   EXPECT_FLOAT_EQ(1.0, action.target);
432   EXPECT_EQ(1, callback_called);
433   EXPECT_EQ(cb_data, callback_arg);
434 
435   cras_ramp_destroy(ramp);
436 }
437 
TEST(RampTestSuite,RampDownPassedRampCallback)438 TEST(RampTestSuite, RampDownPassedRampCallback) {
439   float from = 1.0;
440   float to = 0.0;
441   int duration_frames = 48000;
442   int rc;
443   int ramped_frames = 48000;
444   struct cras_ramp* ramp;
445   struct cras_ramp_action action;
446   void* cb_data = reinterpret_cast<void*>(0x123);
447 
448   ResetStubData();
449 
450   ramp = cras_ramp_create();
451   cras_mute_ramp_start(ramp, from, to, duration_frames, ramp_callback, cb_data);
452 
453   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
454 
455   action = cras_ramp_get_current_action(ramp);
456 
457   EXPECT_EQ(0, rc);
458   EXPECT_EQ(CRAS_RAMP_ACTION_NONE, action.type);
459   EXPECT_FLOAT_EQ(1.0, action.scaler);
460   EXPECT_FLOAT_EQ(0.0, action.increment);
461   EXPECT_FLOAT_EQ(1.0, action.target);
462   EXPECT_EQ(1, callback_called);
463   EXPECT_EQ(cb_data, callback_arg);
464 
465   cras_ramp_destroy(ramp);
466 }
467 
468 }  // namespace
469 
main(int argc,char ** argv)470 int main(int argc, char** argv) {
471   ::testing::InitGoogleTest(&argc, argv);
472   int rc = RUN_ALL_TESTS();
473 
474   return rc;
475 }
476