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
6 #ifndef CRAS_RAMP_H_
7 #define CRAS_RAMP_H_
8
9 #include "cras_iodev.h"
10
11 struct cras_ramp;
12
13 /*
14 * Infomation telling user how to do ramping.
15 * action CRAS_RAMP_ACTION_NONE: No scale should be applied.
16 * action CRAS_RAMP_ACTION_PARTIAL: scale sample by sample starting from scaler
17 * and increase increment for each sample.
18 * action CRAS_RAMP_ACTION_INVALID: There is an error in cras_ramp.
19 */
20 enum CRAS_RAMP_ACTION_TYPE {
21 CRAS_RAMP_ACTION_NONE,
22 CRAS_RAMP_ACTION_PARTIAL,
23 CRAS_RAMP_ACTION_INVALID,
24 };
25
26 /*
27 * Struct to hold current ramping action for user.
28 * Members:
29 * type: See CRAS_RAMP_ACTION_TYPE.
30 * scaler: The initial scaler to be applied.
31 * increment: The scaler increment that should be added to scaler for every
32 * frame.
33 */
34 struct cras_ramp_action {
35 enum CRAS_RAMP_ACTION_TYPE type;
36 float scaler;
37 float increment;
38 float target;
39 };
40
41 typedef void (*cras_ramp_cb)(void *arg);
42
43 /* Creates a ramp. */
44 struct cras_ramp *cras_ramp_create();
45
46 /* Destroys a ramp. */
47 void cras_ramp_destroy(struct cras_ramp *ramp);
48
49 /* Ramps the scaler between from and to for duration_frames frames.
50 * Args:
51 * ramp[in]: The ramp struct to start.
52 * mute_ramp[in]: Is this ramp a mute->unmute or unmute->mute ramp.
53 * from[in]: The scaler value to ramp from.
54 * to[in]: The scaler value to ramp to.
55 * duration_frames[in]: Ramp duration in frames.
56 * cb[in]: The callback function to call after ramping is done. User can set
57 * cb to turn off speaker/headphone switch after ramping down
58 * is done.
59 * cb_data[in]: The data passed to callback function.
60 * Returns:
61 * 0 on success; negative error code on failure.
62 */
63 int cras_ramp_start(struct cras_ramp *ramp, int mute_ramp, float from, float to,
64 int duration_frames, cras_ramp_cb cb, void *cb_data);
65
66 /* Convenience wrappers for cras_ramp_start */
cras_mute_ramp_start(struct cras_ramp * ramp,float from,float to,int duration_frames,cras_ramp_cb cb,void * cb_data)67 static inline int cras_mute_ramp_start(struct cras_ramp *ramp, float from,
68 float to, int duration_frames,
69 cras_ramp_cb cb, void *cb_data)
70 {
71 return cras_ramp_start(ramp, 1, from, to, duration_frames, cb, cb_data);
72 }
73
cras_volume_ramp_start(struct cras_ramp * ramp,float from,float to,int duration_frames,cras_ramp_cb cb,void * cb_data)74 static inline int cras_volume_ramp_start(struct cras_ramp *ramp, float from,
75 float to, int duration_frames,
76 cras_ramp_cb cb, void *cb_data)
77 {
78 return cras_ramp_start(ramp, 0, from, to, duration_frames, cb, cb_data);
79 }
80
81 /* Resets ramp and cancels current ramping. */
82 int cras_ramp_reset(struct cras_ramp *ramp);
83
84 /* Gets current ramp action. */
85 struct cras_ramp_action
86 cras_ramp_get_current_action(const struct cras_ramp *ramp);
87
88 /* Updates number of samples that went through ramping. */
89 int cras_ramp_update_ramped_frames(struct cras_ramp *ramp, int num_frames);
90
91 #endif /* CRAS_RAMP_H_ */
92