File: volumeleveler.cpp

package info (click to toggle)
vlevel 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: cpp: 1,429; makefile: 106; sh: 7
file content (270 lines) | stat: -rw-r--r-- 7,431 bytes parent folder | download | duplicates (5)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// This file is part of VLevel, a dynamic volume normalizer.
//
// Copyright 2003 Tom Felker <tcfelker@mtco.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either version 2.1 of
// the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA

// volumeleveler.cpp - defines the VolumeLeveler class

#include <sys/types.h>
#include <assert.h>
#include <math.h>

#include "volumeleveler.h"

using namespace std;

VolumeLeveler::VolumeLeveler(size_t l, size_t c,  value_t s, value_t m)
{
	bufs = 0;
	SetSamplesAndChannels(l, c);
	SetStrength(s);
	SetMaxMultiplier(m);
}

VolumeLeveler::~VolumeLeveler()
{
	for(size_t ch = 0; ch < channels; ++ch)
		delete [] bufs[ch];
	delete [] bufs;
}

void VolumeLeveler::SetStrength(value_t s)
{
	strength = s;
}

void VolumeLeveler::SetMaxMultiplier(value_t m)
{
	if(m <= 0) m = HUGE_VAL;
	max_multiplier = m;
}

void VolumeLeveler::SetSamplesAndChannels(size_t s, size_t c)
{
	assert(s > 1 && c > 0);
	
	if(bufs) {
		for(size_t ch = 0; ch < channels; ++ch)
			delete [] bufs[ch];
		delete [] bufs;
	}
	
	bufs = new value_t*[c];
	
	for(size_t ch = 0; ch < c; ++ch)
		bufs[ch] = new value_t[s];
	
	samples = s;
	channels = c;
	Flush();
}

void VolumeLeveler::Flush()
{
	for(size_t ch = 0; ch < channels; ++ch)
		for(size_t i = 0; i < samples; ++i)
			bufs[ch][i] = 0;
	
	silence = samples;
	pos = max_slope_pos = 0;
	max_slope = max_slope_val = avg_amp = 0;
}

value_t VolumeLeveler::GetMultiplier()
{
	value_t multiplier = pow(avg_amp, -strength);
	if(multiplier > max_multiplier) multiplier = max_multiplier;
	return multiplier;
}

size_t VolumeLeveler::Exchange(value_t **in_bufs, value_t **out_bufs, size_t in_samples)
{
	switch(channels) {
		//case 1:
		//  Exchange_1(in_bufs, out_bufs, in_samples);
		//  break;
		//case 2:
		//  Exchange_2(in_bufs, out_bufs, in_samples);
		//  break;
	default:
		Exchange_n(in_bufs, out_bufs, in_samples);
	}
	
	if(silence >= in_samples) {
		silence -= in_samples;
		return in_samples;
	} else {
		size_t returned_silence = silence;
		silence = 0;
		return returned_silence;
	}
}

void VolumeLeveler::Exchange_n(value_t **in_bufs, value_t **out_bufs, size_t in_samples)
{
	// for each user_pos in user_buf
	for(size_t user_pos = 0; user_pos < in_samples; ++user_pos) {
		
		// compute multiplier
		value_t multiplier = pow(avg_amp, -strength);
		
		// if avg_amp <= 0, then the above line sets multiplier to Inf, so
		// samples will scale to Inf or NaN.  This causes a tick on the
		// first sample after a Flush() unless max_multiplier is not Inf.
		// hopefully this fix isn't too slow.
		if(unlikely(avg_amp <= 0)) multiplier = 0;

		// untested!
		// The advantage of using floats is that you can be
		// sloppy with going over 1.  Since we have this nifty
		// average_amp calculation, let's apply it to limit
		// the audio to varying normally below 1.  Again,
		// hopefully this won't slow things down too much.
		if(unlikely(avg_amp > 1)) multiplier = 1 / avg_amp;
		
		// limit multiplier to max_multiplier.  max_multiplier can be Inf
		// to disable this.
		if(unlikely(multiplier > max_multiplier)) multiplier = max_multiplier;
		
		// swap buf[pos] with user_buf[user_pos], scaling user[buf] by
		// multiplier and finding max of the new sample
		value_t new_val = 0;
		for(size_t ch = 0; ch < channels; ++ch) {
			value_t in = in_bufs[ch][user_pos];
			out_bufs[ch][user_pos] = bufs[ch][pos] * multiplier;
			bufs[ch][pos] = in;
			if(VLEVEL_ABS(in) > new_val) new_val = VLEVEL_ABS(in);
		}
		
		pos = (pos + 1) % samples; // now pos is the oldest, new one is pos-1
		
		avg_amp += max_slope;
		
		if(unlikely(pos == max_slope_pos)) {
			// recompute (this is expensive)
			max_slope = -HUGE_VAL;
			for(size_t i = 1; i < samples; ++i) {
				value_t sample_val = 0;
				for(size_t ch = 0; ch < channels; ++ch) {
					value_t ch_val = VLEVEL_ABS(bufs[ch][(pos + i) % samples]);
					if(ch_val > sample_val) sample_val = ch_val;
				}
				value_t slope = (sample_val - avg_amp) / i;
				// must be >=, otherwise clipping causes excessive computation
				// TODO: maybe optimize - just save i, then compute slope, pos, and val only once later.
				// maybe unlikely()
				if(unlikely(slope >= max_slope)) { 
					max_slope_pos = (pos + i) % samples;
					max_slope = slope;
					max_slope_val = sample_val;
				}
			}
		} else {
			// only chance of higher slope is the new sample
			
			// recomputing max_slope isn't really necessary...
			max_slope = (max_slope_val - avg_amp) / ((max_slope_pos - pos + samples) % samples);
			// ...but it doesn't take long and has a small effect.

			value_t slope = (new_val - avg_amp) / (samples - 1);

			// probably needs to be >= for same reason as above
			// maybe unlikely()
			if(unlikely(slope >= max_slope)) {
				max_slope_pos = (pos - 1) % samples;
				max_slope = slope;
				max_slope_val = new_val;
			}
		}
	}
}

// this code has been proven correct, but not tested much.  ;-)
void ToValues(char *in, value_t *out, size_t values,
              size_t bits_per_value, bool has_sign)
{
	switch(bits_per_value) {
    case 32:
		if(has_sign) {
			for(size_t i = 0; i < values; ++i)
				out[i] = ((value_t)((int32_t *)in)[i]) / 2147483648;
		} else {
			for(size_t i = 0; i < values; ++i)
				out[i] = (((value_t)((u_int32_t *)in)[i]) - 2147483648) / 2147483648;
		}
		break;
	case 16:
		if(has_sign) {
			for(size_t i = 0; i < values; ++i)
				out[i] = ((value_t)((int16_t *)in)[i]) / 32768;
		} else {
			for(size_t i = 0; i < values; ++i)
				out[i] = (((value_t)((u_int16_t *)in)[i]) - 32768) / 32768;
		}
		break;
	case 8:
		if(has_sign) {
			for(size_t i = 0; i < values; ++i)
				out[i] = ((value_t)((int8_t *)in)[i]) / 128;
		} else {
			for(size_t i = 0; i < values; ++i)
				out[i] = (((value_t)((u_int8_t *)in)[i]) - 128) / 128;
		}
		break;
	default:
		assert(false);
	}
}


// note: no clipping, just wrap.  I don't know how badly clipping will perform.
void FromValues(value_t *in, char *out, size_t values,
                size_t bits_per_value, bool has_sign)
{
	switch(bits_per_value) {
	case 32:
		if(has_sign) {
			for(size_t i = 0; i < values; ++i)
				((int32_t *)out)[i] = (int32_t)(in[i] * 2147483647);
		} else {
    	for(size_t i = 0; i < values; ++i)
				((u_int32_t *)out)[i] = (u_int32_t)((in[i] * 2147483647) + 2147483647);
		}
		break;
	case 16:
		if(has_sign) {
			for(size_t i = 0; i < values; ++i)
				((int16_t *)out)[i] = (int16_t)(in[i] * 32767);
		} else {
    	for(size_t i = 0; i < values; ++i)
				((u_int16_t *)out)[i] = (u_int16_t)((in[i] * 32767) + 32767);
		}
		break;
	case 8:
		if(has_sign) {
			for(size_t i = 0; i < values; ++i)
				((int8_t *)out)[i] = (int8_t)(in[i] * 127);
		} else {
      for(size_t i = 0; i < values; ++i)
				((u_int8_t *)out)[i] = (u_int8_t)((in[i] * 127) + 127);
		}
		break;
	default:
		assert(false);
	}
}