summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_ramp.h
blob: 077e9bea64ac5ad37b199091d40cf7e1311a9e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* Copyright 2016 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef CRAS_RAMP_H_
#define CRAS_RAMP_H_

#include "cras_iodev.h"

struct cras_ramp;

/*
 * Infomation telling user how to do ramping.
 * action CRAS_RAMP_ACTION_NONE: No scale should be applied.
 * action CRAS_RAMP_ACTION_PARTIAL: scale sample by sample starting from scaler
 *                                  and increase increment for each sample.
 * action CRAS_RAMP_ACTION_INVALID: There is an error in cras_ramp.
 */
enum CRAS_RAMP_ACTION_TYPE {
	CRAS_RAMP_ACTION_NONE,
	CRAS_RAMP_ACTION_PARTIAL,
	CRAS_RAMP_ACTION_INVALID,
};

/*
 * Struct to hold current ramping action for user.
 * Members:
 *   type: See CRAS_RAMP_ACTION_TYPE.
 *   scaler: The initial scaler to be applied.
 *   increment: The scaler increment that should be added to scaler for every
 *              frame.
 */
struct cras_ramp_action {
	enum CRAS_RAMP_ACTION_TYPE type;
	float scaler;
	float increment;
	float target;
};

typedef void (*cras_ramp_cb)(void *arg);

/* Creates a ramp. */
struct cras_ramp *cras_ramp_create();

/* Destroys a ramp. */
void cras_ramp_destroy(struct cras_ramp *ramp);

/* Ramps the scaler between from and to for duration_frames frames.
 * Args:
 *   ramp[in]: The ramp struct to start.
 *   mute_ramp[in]: Is this ramp a mute->unmute or unmute->mute ramp.
 *   from[in]: The scaler value to ramp from.
 *   to[in]: The scaler value to ramp to.
 *   duration_frames[in]: Ramp duration in frames.
 *   cb[in]: The callback function to call after ramping is done. User can set
 *           cb to turn off speaker/headphone switch after ramping down
 *           is done.
 *   cb_data[in]: The data passed to callback function.
 * Returns:
 *   0 on success; negative error code on failure.
 */
int cras_ramp_start(struct cras_ramp *ramp, int mute_ramp, float from, float to,
		    int duration_frames, cras_ramp_cb cb, void *cb_data);

/* Convenience wrappers for cras_ramp_start */
static inline int cras_mute_ramp_start(struct cras_ramp *ramp, float from,
				       float to, int duration_frames,
				       cras_ramp_cb cb, void *cb_data)
{
	return cras_ramp_start(ramp, 1, from, to, duration_frames, cb, cb_data);
}

static inline int cras_volume_ramp_start(struct cras_ramp *ramp, float from,
					 float to, int duration_frames,
					 cras_ramp_cb cb, void *cb_data)
{
	return cras_ramp_start(ramp, 0, from, to, duration_frames, cb, cb_data);
}

/* Resets ramp and cancels current ramping. */
int cras_ramp_reset(struct cras_ramp *ramp);

/* Gets current ramp action. */
struct cras_ramp_action
cras_ramp_get_current_action(const struct cras_ramp *ramp);

/* Updates number of samples that went through ramping. */
int cras_ramp_update_ramped_frames(struct cras_ramp *ramp, int num_frames);

#endif /* CRAS_RAMP_H_ */