File: scale.c

package info (click to toggle)
jack-mixer 9-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,812 kB
  • sloc: sh: 10,127; ansic: 3,113; python: 2,172; makefile: 106
file content (199 lines) | stat: -rw-r--r-- 4,521 bytes parent folder | download
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*****************************************************************************
 *
 *   This file is part of jack_mixer
 *    
 *   Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
 *    
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; version 2 of the License
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *****************************************************************************/

#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>

#include "jack_mixer.h"
//#define LOG_LEVEL LOG_LEVEL_DEBUG
#include "log.h"
#include "list.h"

struct threshold
{
  struct list_head scale_siblings;
  double db;
  double scale;
  double a;
  double b;
};

struct scale
{
  struct list_head thresholds;
  double max_db;
};

jack_mixer_scale_t
scale_create()
{
  struct scale * scale_ptr;

  scale_ptr = malloc(sizeof(struct scale));
  if (scale_ptr == NULL)
  {
    return NULL;
  }

  INIT_LIST_HEAD(&scale_ptr->thresholds);
  scale_ptr->max_db = -INFINITY;

  LOG_DEBUG("Scale %p created", scale_ptr);

  return (jack_mixer_scale_t)scale_ptr;
}

#define scale_ptr ((struct scale *)scale)

void
scale_destroy(
  jack_mixer_scale_t scale)
{
  free(scale_ptr);
}

bool
scale_add_threshold(
  jack_mixer_scale_t scale,
  float db,
  float scale_value)
{
  struct threshold * threshold_ptr;

  LOG_DEBUG("Adding threshold (%f dBFS -> %f) to scale %p", db, scale, scale_ptr);
  LOG_DEBUG("Threshold %p created ", threshold_ptr, db, scale);

  threshold_ptr = malloc(sizeof(struct threshold));
  if (threshold_ptr == NULL)
  {
    return false;
  }

  threshold_ptr->db = db;
  threshold_ptr->scale = scale_value;

  list_add_tail(&threshold_ptr->scale_siblings, &scale_ptr->thresholds);

  if (db > scale_ptr->max_db)
  {
    scale_ptr->max_db = db;
  }

  return true;
}

#undef threshold_ptr

void
scale_calculate_coefficients(
  jack_mixer_scale_t scale)
{
  struct threshold * threshold_ptr;
  struct threshold * prev_ptr;
  struct list_head * node_ptr;

  prev_ptr = NULL;

  list_for_each(node_ptr, &scale_ptr->thresholds)
  {
    threshold_ptr = list_entry(node_ptr, struct threshold, scale_siblings);

    LOG_DEBUG("Calculating coefficients for threshold %p", threshold_ptr);

    if (prev_ptr != NULL)
    {
      threshold_ptr->a = (prev_ptr->scale - threshold_ptr->scale) / (prev_ptr->db - threshold_ptr->db);
      threshold_ptr->b = threshold_ptr->scale - threshold_ptr->a * threshold_ptr->db;
      LOG_DEBUG("%.0f dB - %.0f dB: scale = %f * dB + %f", prev_ptr->db, threshold_ptr->db, threshold_ptr->a, threshold_ptr->b);
    }

    prev_ptr = threshold_ptr;
  }
}

/* Convert dBFS value to number in range 0.0-1.0 */
double
scale_db_to_scale(
  jack_mixer_scale_t scale,
  double db)
{
  struct threshold * threshold_ptr;
  struct threshold * prev_ptr;
  struct list_head * node_ptr;

  prev_ptr = NULL;

  list_for_each(node_ptr, &scale_ptr->thresholds)
  {
    threshold_ptr = list_entry(node_ptr, struct threshold, scale_siblings);

    if (db < threshold_ptr->db)
    {
      LOG_DEBUG("Match at %f dB treshold", threshold_ptr->db);
      if (prev_ptr == NULL)
      {
        return 0.0;
      }

      return threshold_ptr->a * db + threshold_ptr->b;
    }

    prev_ptr = threshold_ptr;
  }

  return 1.0;
}

/* Convert number in range 0.0-1.0 to dBFS value */
double
scale_scale_to_db(
  jack_mixer_scale_t scale,
  double scale_value)
{
  struct threshold * threshold_ptr;
  struct threshold * prev_ptr;
  struct list_head * node_ptr;

  prev_ptr = NULL;

  list_for_each(node_ptr, &scale_ptr->thresholds)
  {
    threshold_ptr = list_entry(node_ptr, struct threshold, scale_siblings);

    if (scale_value <= threshold_ptr->scale)
    {
      if (prev_ptr == NULL)
      {
        return -INFINITY;
      }

      return (scale_value - threshold_ptr->b) / threshold_ptr->a;
    }

    prev_ptr = threshold_ptr;
  }

  return scale_ptr->max_db;
}