summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_volume_curve.c
blob: a86612208d5e468e17c6a54b1ecdc1977f6cedcb (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
/* 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 <sys/param.h>

#include "cras_util.h"
#include "cras_volume_curve.h"

/* 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 * (MAX_VOLUME - volume));
}

/* Curve that has each step explicitly called out by value. */
struct explicit_curve {
	struct cras_volume_curve curve;
	long dB_values[NUM_VOLUME_STEPS];
};

static long get_dBFS_explicit(const struct cras_volume_curve *curve,
			      size_t volume)
{
	const struct explicit_curve *c = (const struct explicit_curve *)curve;

	/* Limit volume to (0, MAX_VOLUME). */
	volume = MIN(MAX_VOLUME, MAX(0, volume));
	return c->dB_values[volume];
}

/*
 * Exported Interface.
 */

struct cras_volume_curve *cras_volume_curve_create_default()
{
	/* Default to max volume of 0dBFS, and a step of 0.5dBFS. */
	return cras_volume_curve_create_simple_step(0, 50);
}

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;
}

struct cras_volume_curve *
cras_volume_curve_create_explicit(long dB_values[NUM_VOLUME_STEPS])
{
	struct explicit_curve *curve;
	curve = (struct explicit_curve *)calloc(1, sizeof(*curve));
	if (curve == NULL)
		return NULL;
	curve->curve.get_dBFS = get_dBFS_explicit;
	memcpy(curve->dB_values, dB_values, sizeof(curve->dB_values));
	return &curve->curve;
}

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