aboutsummaryrefslogtreecommitdiffstats
path: root/src/normalize.c
blob: 8c48f13bdbbc7285809fb8552532264e6e7bc897 (plain) (blame)
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
/* the Music Player Daemon (MPD)
 * (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
 * This project's homepage is: http://www.musicpd.org
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <math.h>
#include <limits.h>

#include "conf.h"
#include "normalize.h"
#include "playlist.h"

/* silence level, apparently this is Wrong (tm) */
#define SILENCE_LEVEL (SHRT_MAX * 0.01)
/* not sure what this is :) */
#define MID           (SHRT_MAX * 0.25)

#define MUL_MIN 0.1
#define MUL_MAX 5.0
#define NSAMPLES 128
#define MIN_SAMPLE_SIZE 32000

#define clamp(a,min,max) (((a)>(max))?(max):(((a)<(min))?(min):(a)))

void normalizeData(char *buffer, int bufferSize, AudioFormat *format)
{
	static float multiplier = 1.0;
	static int current_id = 0;
	float average = 0.0;
	static int old_song = 0;
	int new_song = 0;
	int total_length = 0;
	int temp = 0;
	int i = 0;
	float root_mean_square = 0.0; /* the rms of the data */
	mpd_sint16 *data = (mpd_sint16 *) buffer; /* the audio data */
	int length = bufferSize / 2; /* the number of samples */
	static struct {
		float avg; /* average sample 'level' */
		int len;   /* sample size (used to weigh sample) */
	} mem[NSAMPLES];

	/* operate only on 16 bit, 2 channel audio */
	if (format->bits != 16 && format->channels != 2) return;

	/* calculate the root mean square of the data */
	for (i = 0; i < length; i++)
		root_mean_square += (float)(data[i] * data[i]);

	root_mean_square = sqrt(root_mean_square / (float)length);

	/* reset the multiplier if the song has changed */
	if (old_song != (new_song = getPlaylistCurrentSong())) {
		old_song = new_song;
		/* re-zero 'mem' */
		for (i = 0; i < NSAMPLES; i++) {
			mem[i].avg = 0.0;
			mem[i].len = 0;
		}
		current_id = 0;
	}

	/* and now do magic tricks */
	for (i = 0; i < NSAMPLES; i++) {
		average += mem[i].avg * (float)mem[i].len;
		total_length += mem[i].len;
	}

	if (total_length > MIN_SAMPLE_SIZE) {
		average /= (float) total_length;
		if (average >= SILENCE_LEVEL) {
			multiplier = MID / average;
			/* clamp multiplier */
			multiplier = clamp(multiplier, MUL_MIN, MUL_MAX);
		}
	}

	/* scale and clamp the samples */
	for (i = 0; i < length; i++) {
		temp = data[i] * multiplier;
		data[i] = clamp(temp, SHRT_MIN, SHRT_MAX);
	}

	mem[current_id].len = bufferSize / 2;
	mem[current_id].avg = multiplier * root_mean_square;
	current_id = (current_id + 1) % NSAMPLES; /* increment current_id */
}