summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_volume_curve.c
blob: 3f7f385d265067ba2a45831d11851e1c7c75ef06 (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
/* Copyright (c) 2012 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.
 */

#include <stddef.h>
#include <stdlib.h>

#include "cras_volume_curve.h"

/* Default to 1dB per tick.
 * Volume = 100 -> 0dB.
 * Volume = 0 -> -100dB. */
static long get_dBFS_default(const struct cras_volume_curve *curve,
			     size_t volume)
{
	/* dB to cut * 100 */
	return (volume - 100) * 100;
}

/* Simple curve with configurable max volume and volume step. */
struct stepped_curve {
	struct cras_volume_curve curve;
	long max_vol;
	long step;
};

static long get_dBFS_step(const struct cras_volume_curve *curve, size_t volume)
{
	const struct stepped_curve *c = (const struct stepped_curve *)curve;
	return c->max_vol - (c->step * (100 - volume));
}

/*
 * Exported Interface.
 */

struct cras_volume_curve *cras_volume_curve_create_default()
{
	struct cras_volume_curve *curve;
	curve = (struct cras_volume_curve *)calloc(1, sizeof(*curve));
	if (curve != NULL)
		curve->get_dBFS = get_dBFS_default;
	return curve;
}

struct cras_volume_curve *cras_volume_curve_create_simple_step(
		long max_volume,
		long volume_step)
{
	struct stepped_curve *curve;
	curve = (struct stepped_curve *)calloc(1, sizeof(*curve));
	if (curve == NULL)
		return NULL;
	curve->curve.get_dBFS = get_dBFS_step;
	curve->max_vol = max_volume;
	curve->step = volume_step;
	return &curve->curve;
}

void cras_volume_curve_destroy(struct cras_volume_curve *curve)
{
	free(curve);
}