Read new settings before the dialog is destroyed
[jack_mixer.git] / scale.c
blob5efba192241fdee66075ac4d497260a407aaeb98
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * This file is part of jack_mixer
5 *
6 * Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
7 *
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 *****************************************************************************/
23 #include <stddef.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #include <assert.h>
29 #include "jack_mixer.h"
30 //#define LOG_LEVEL LOG_LEVEL_DEBUG
31 #include "log.h"
32 #include "list.h"
34 struct threshold
36 struct list_head scale_siblings;
37 double db;
38 double scale;
39 double a;
40 double b;
43 struct scale
45 struct list_head thresholds;
46 double max_db;
49 jack_mixer_scale_t
50 scale_create()
52 struct scale * scale_ptr;
54 scale_ptr = malloc(sizeof(struct scale));
55 if (scale_ptr == NULL)
57 return 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)
70 void
71 scale_destroy(
72 jack_mixer_scale_t scale)
74 free(scale_ptr);
77 bool
78 scale_add_threshold(
79 jack_mixer_scale_t scale,
80 float db,
81 float scale_value)
83 struct threshold * threshold_ptr;
85 LOG_DEBUG("Adding threshold (%f dBFS -> %f) to scale %p", db, scale, scale_ptr);
86 LOG_DEBUG("Threshold %p created ", threshold_ptr, db, scale);
88 threshold_ptr = malloc(sizeof(struct threshold));
89 if (threshold_ptr == NULL)
91 return false;
94 threshold_ptr->db = db;
95 threshold_ptr->scale = scale_value;
97 list_add_tail(&threshold_ptr->scale_siblings, &scale_ptr->thresholds);
99 if (db > scale_ptr->max_db)
101 scale_ptr->max_db = db;
104 return true;
107 #undef threshold_ptr
109 void
110 scale_calculate_coefficients(
111 jack_mixer_scale_t scale)
113 struct threshold * threshold_ptr;
114 struct threshold * prev_ptr;
115 struct list_head * node_ptr;
117 prev_ptr = NULL;
119 list_for_each(node_ptr, &scale_ptr->thresholds)
121 threshold_ptr = list_entry(node_ptr, struct threshold, scale_siblings);
123 LOG_DEBUG("Calculating coefficients for threshold %p", threshold_ptr);
125 if (prev_ptr != NULL)
127 threshold_ptr->a = (prev_ptr->scale - threshold_ptr->scale) / (prev_ptr->db - threshold_ptr->db);
128 threshold_ptr->b = threshold_ptr->scale - threshold_ptr->a * threshold_ptr->db;
129 LOG_DEBUG("%.0f dB - %.0f dB: scale = %f * dB + %f", prev_ptr->db, threshold_ptr->db, threshold_ptr->a, threshold_ptr->b);
132 prev_ptr = threshold_ptr;
136 /* Convert dBFS value to number in range 0.0-1.0 */
137 double
138 scale_db_to_scale(
139 jack_mixer_scale_t scale,
140 double db)
142 struct threshold * threshold_ptr;
143 struct threshold * prev_ptr;
144 struct list_head * node_ptr;
146 prev_ptr = NULL;
148 list_for_each(node_ptr, &scale_ptr->thresholds)
150 threshold_ptr = list_entry(node_ptr, struct threshold, scale_siblings);
152 if (db < threshold_ptr->db)
154 LOG_DEBUG("Match at %f dB treshold", threshold_ptr->db);
155 if (prev_ptr == NULL)
157 return 0.0;
160 return threshold_ptr->a * db + threshold_ptr->b;
163 prev_ptr = threshold_ptr;
166 return 1.0;
169 /* Convert number in range 0.0-1.0 to dBFS value */
170 double
171 scale_scale_to_db(
172 jack_mixer_scale_t scale,
173 double scale_value)
175 struct threshold * threshold_ptr;
176 struct threshold * prev_ptr;
177 struct list_head * node_ptr;
179 prev_ptr = NULL;
181 list_for_each(node_ptr, &scale_ptr->thresholds)
183 threshold_ptr = list_entry(node_ptr, struct threshold, scale_siblings);
185 if (scale_value <= threshold_ptr->scale)
187 if (prev_ptr == NULL)
189 return -INFINITY;
192 return (scale_value - threshold_ptr->b) / threshold_ptr->a;
195 prev_ptr = threshold_ptr;
198 return scale_ptr->max_db;