1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * This file is part of jack_mixer
6 * Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *****************************************************************************/
29 #include "jack_mixer.h"
30 //#define LOG_LEVEL LOG_LEVEL_DEBUG
36 struct list_head scale_siblings
;
45 struct list_head thresholds
;
52 struct scale
* scale_ptr
;
54 scale_ptr
= malloc(sizeof(struct scale
));
55 if (scale_ptr
== NULL
)
60 INIT_LIST_HEAD(&scale_ptr
->thresholds
);
61 scale_ptr
->max_db
= -INFINITY
;
63 LOG_DEBUG("Scale %p created", scale_ptr
);
65 return (jack_mixer_scale_t
)scale_ptr
;
68 #define scale_ptr ((struct scale *)scale)
72 jack_mixer_scale_t scale
)
79 jack_mixer_scale_t scale
,
83 struct threshold
* threshold_ptr
;
85 LOG_DEBUG("Adding threshold (%f dBFS -> %f) to scale %p", db
, scale
, scale_ptr
);
87 threshold_ptr
= malloc(sizeof(struct threshold
));
88 LOG_DEBUG("Threshold %p created ", threshold_ptr
, db
, scale
);
90 if (threshold_ptr
== NULL
)
95 threshold_ptr
->db
= db
;
96 threshold_ptr
->scale
= scale_value
;
98 list_add_tail(&threshold_ptr
->scale_siblings
, &scale_ptr
->thresholds
);
100 if (db
> scale_ptr
->max_db
)
102 scale_ptr
->max_db
= db
;
111 scale_calculate_coefficients(
112 jack_mixer_scale_t scale
)
114 struct threshold
* threshold_ptr
;
115 struct threshold
* prev_ptr
;
116 struct list_head
* node_ptr
;
120 list_for_each(node_ptr
, &scale_ptr
->thresholds
)
122 threshold_ptr
= list_entry(node_ptr
, struct threshold
, scale_siblings
);
124 LOG_DEBUG("Calculating coefficients for threshold %p", threshold_ptr
);
126 if (prev_ptr
!= NULL
)
128 threshold_ptr
->a
= (prev_ptr
->scale
- threshold_ptr
->scale
) / (prev_ptr
->db
- threshold_ptr
->db
);
129 threshold_ptr
->b
= threshold_ptr
->scale
- threshold_ptr
->a
* threshold_ptr
->db
;
130 LOG_DEBUG("%.0f dB - %.0f dB: scale = %f * dB + %f", prev_ptr
->db
, threshold_ptr
->db
, threshold_ptr
->a
, threshold_ptr
->b
);
133 prev_ptr
= threshold_ptr
;
137 /* Convert dBFS value to number in range 0.0-1.0 */
140 jack_mixer_scale_t scale
,
143 struct threshold
* threshold_ptr
;
144 struct threshold
* prev_ptr
;
145 struct list_head
* node_ptr
;
149 list_for_each(node_ptr
, &scale_ptr
->thresholds
)
151 threshold_ptr
= list_entry(node_ptr
, struct threshold
, scale_siblings
);
153 if (db
< threshold_ptr
->db
)
155 LOG_DEBUG("Match at %f dB treshold", threshold_ptr
->db
);
156 if (prev_ptr
== NULL
)
161 return threshold_ptr
->a
* db
+ threshold_ptr
->b
;
164 prev_ptr
= threshold_ptr
;
170 /* Convert number in range 0.0-1.0 to dBFS value */
173 jack_mixer_scale_t scale
,
176 struct threshold
* threshold_ptr
;
177 struct threshold
* prev_ptr
;
178 struct list_head
* node_ptr
;
182 list_for_each(node_ptr
, &scale_ptr
->thresholds
)
184 threshold_ptr
= list_entry(node_ptr
, struct threshold
, scale_siblings
);
186 if (scale_value
<= threshold_ptr
->scale
)
188 if (prev_ptr
== NULL
)
193 return (scale_value
- threshold_ptr
->b
) / threshold_ptr
->a
;
196 prev_ptr
= threshold_ptr
;
199 return scale_ptr
->max_db
;