• 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,NullWontCrash)318 TEST(RampTestSuite, NullWontCrash) {
319   float from;
320   float to;
321   int duration_frames = 48000;
322   int rc = 0;
323   struct cras_ramp* ramp = NULL;
324 
325   ResetStubData();
326 
327   from = 0.0;
328   to = 1.0;
329   rc = cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
330 
331   EXPECT_EQ(-EINVAL, rc);
332 }
333 
TEST(RampTestSuite,RampDownWhileHalfWayRampDown)334 TEST(RampTestSuite, RampDownWhileHalfWayRampDown) {
335   float from = 1.0;
336   float to = 0.0;
337   int duration_frames = 48000;
338   int rc;
339   int ramped_frames = 24000;
340   struct cras_ramp* ramp;
341   struct cras_ramp_action action;
342   float down_increment = -1.0 / 48000;
343   float second_down_increment;
344   float scaler;
345 
346   ResetStubData();
347 
348   ramp = cras_ramp_create();
349   // Ramp down.
350   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
351 
352   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
353 
354   // Get expected current scaler.
355   scaler = 1 + down_increment * ramped_frames;
356   // The increment will be calculated by ramping to 0 starting from scaler.
357   second_down_increment = -scaler / duration_frames;
358 
359   // Ramp down starting from current scaler.
360   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
361 
362   action = cras_ramp_get_current_action(ramp);
363 
364   EXPECT_EQ(0, rc);
365   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
366   EXPECT_FLOAT_EQ(scaler, action.scaler);
367   EXPECT_FLOAT_EQ(second_down_increment, action.increment);
368   EXPECT_FLOAT_EQ(to, action.target);
369 
370   cras_ramp_destroy(ramp);
371 }
372 
TEST(RampTestSuite,MuteRamp)373 TEST(RampTestSuite, MuteRamp) {
374   float from = 0.0;
375   float to = 0.0;
376   int duration_frames = 48000;
377   struct cras_ramp* ramp;
378   struct cras_ramp_action action;
379 
380   ResetStubData();
381 
382   ramp = cras_ramp_create();
383   cras_mute_ramp_start(ramp, from, to, duration_frames, NULL, NULL);
384 
385   action = cras_ramp_get_current_action(ramp);
386 
387   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
388   EXPECT_FLOAT_EQ(0.0, action.scaler);
389   EXPECT_FLOAT_EQ(0.0, action.increment);
390   EXPECT_FLOAT_EQ(0.0, action.target);
391 
392   cras_ramp_destroy(ramp);
393 }
394 
TEST(RampTestSuite,PartialRamp)395 TEST(RampTestSuite, PartialRamp) {
396   float from_one = 0.75;
397   float to_one = 0.4;
398   float from_two = 0.6;
399   float to_two = 0.9;
400   int duration_frames = 1200;
401   int rc;
402   int ramped_frames = 600;
403   struct cras_ramp* ramp;
404   struct cras_ramp_action action;
405   float down_increment = (to_one - from_one) / duration_frames;
406   float up_increment;
407   float scaler;
408 
409   ResetStubData();
410 
411   ramp = cras_ramp_create();
412   // Ramp down.
413   cras_volume_ramp_start(ramp, from_one, to_one, duration_frames, NULL, NULL);
414 
415   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
416 
417   scaler = from_one + ramped_frames * down_increment;
418   action = cras_ramp_get_current_action(ramp);
419   EXPECT_EQ(0, rc);
420   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
421   EXPECT_FLOAT_EQ(scaler, action.scaler);
422   EXPECT_FLOAT_EQ(down_increment, action.increment);
423   EXPECT_FLOAT_EQ(to_one, action.target);
424 
425   // Ramp up starting from current scaler.
426   cras_volume_ramp_start(ramp, from_two, to_two, duration_frames, NULL, NULL);
427 
428   // we start by multiplying by previous scaler
429   scaler = scaler * from_two;
430   action = cras_ramp_get_current_action(ramp);
431   up_increment = (to_two - scaler) / duration_frames;
432   EXPECT_EQ(0, rc);
433   EXPECT_EQ(CRAS_RAMP_ACTION_PARTIAL, action.type);
434   EXPECT_FLOAT_EQ(scaler, action.scaler);
435   EXPECT_FLOAT_EQ(up_increment, action.increment);
436   EXPECT_FLOAT_EQ(to_two, action.target);
437 
438   cras_ramp_destroy(ramp);
439 }
440 
ramp_callback(void * arg)441 void ramp_callback(void* arg) {
442   callback_called++;
443   callback_arg = arg;
444 }
445 
TEST(RampTestSuite,RampUpPassedRampCallback)446 TEST(RampTestSuite, RampUpPassedRampCallback) {
447   float from = 0.0;
448   float to = 1.0;
449   int duration_frames = 48000;
450   int rc;
451   int ramped_frames = 48000;
452   struct cras_ramp* ramp;
453   struct cras_ramp_action action;
454   void* cb_data = reinterpret_cast<void*>(0x123);
455 
456   ResetStubData();
457 
458   ramp = cras_ramp_create();
459   cras_mute_ramp_start(ramp, from, to, duration_frames, ramp_callback, cb_data);
460 
461   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
462 
463   action = cras_ramp_get_current_action(ramp);
464 
465   EXPECT_EQ(0, rc);
466   EXPECT_EQ(CRAS_RAMP_ACTION_NONE, action.type);
467   EXPECT_FLOAT_EQ(1.0, action.scaler);
468   EXPECT_FLOAT_EQ(0.0, action.increment);
469   EXPECT_FLOAT_EQ(1.0, action.target);
470   EXPECT_EQ(1, callback_called);
471   EXPECT_EQ(cb_data, callback_arg);
472 
473   cras_ramp_destroy(ramp);
474 }
475 
TEST(RampTestSuite,RampDownPassedRampCallback)476 TEST(RampTestSuite, RampDownPassedRampCallback) {
477   float from = 1.0;
478   float to = 0.0;
479   int duration_frames = 48000;
480   int rc;
481   int ramped_frames = 48000;
482   struct cras_ramp* ramp;
483   struct cras_ramp_action action;
484   void* cb_data = reinterpret_cast<void*>(0x123);
485 
486   ResetStubData();
487 
488   ramp = cras_ramp_create();
489   cras_mute_ramp_start(ramp, from, to, duration_frames, ramp_callback, cb_data);
490 
491   rc = cras_ramp_update_ramped_frames(ramp, ramped_frames);
492 
493   action = cras_ramp_get_current_action(ramp);
494 
495   EXPECT_EQ(0, rc);
496   EXPECT_EQ(CRAS_RAMP_ACTION_NONE, action.type);
497   EXPECT_FLOAT_EQ(1.0, action.scaler);
498   EXPECT_FLOAT_EQ(0.0, action.increment);
499   EXPECT_FLOAT_EQ(1.0, action.target);
500   EXPECT_EQ(1, callback_called);
501   EXPECT_EQ(cb_data, callback_arg);
502 
503   cras_ramp_destroy(ramp);
504 }
505 
506 }  // namespace
507 
main(int argc,char ** argv)508 int main(int argc, char** argv) {
509   ::testing::InitGoogleTest(&argc, argv);
510   int rc = RUN_ALL_TESTS();
511 
512   return rc;
513 }
514