compress.c 8.65 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3
 * This project's homepage is: http://www.musicpd.org
4
 *
5
 * Compressor logic by
6
 * (c)2003-6 fluffy@beesbuzz.biz
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
 *
21
 */
22 23 24 25 26 27 28

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "compress.h"
29
#include "utils.h"
30 31 32 33 34 35 36 37 38 39 40 41

#ifdef USE_X
#include <X11/Xlib.h>
#include <X11/Xutil.h>

static Display *display;
static Window window;
static Visual *visual;
static int screen;
static GC blackGC, whiteGC, blueGC, yellowGC, dkyellowGC, redGC;
#endif

42
static int *peaks;
43 44 45 46 47 48 49 50 51 52 53 54
static int gainCurrent, gainTarget;

static struct {
	int show_mon;
	int anticlip;
	int target;
	int gainmax;
	int gainsmooth;
	int buckets;
} prefs;

#ifdef USE_X
55
static int mon_init;
56 57 58 59 60
#endif

void CompressCfg(int show_mon, int anticlip, int target, int gainmax,
		 int gainsmooth, int buckets)
{
61
	static int lastsize;
62 63 64 65 66 67 68 69 70

	prefs.show_mon = show_mon;
	prefs.anticlip = anticlip;
	prefs.target = target;
	prefs.gainmax = gainmax;
	prefs.gainsmooth = gainsmooth;
	prefs.buckets = buckets;

	/* Allocate the peak structure */
71
	peaks = xrealloc(peaks, sizeof(int)*prefs.buckets);
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

	if (prefs.buckets > lastsize)
		memset(peaks + lastsize, 0, sizeof(int)*(prefs.buckets
							 - lastsize));
	lastsize = prefs.buckets;

#ifdef USE_X
	/* Configure the monitor window if needed */
	if (show_mon && !mon_init)
	{
		display = XOpenDisplay(getenv("DISPLAY"));

		/* We really shouldn't try to init X if there's no X */
		if (!display)
		{
			fprintf(stderr,
				"X not detected; disabling monitor window\n");
			show_mon = prefs.show_mon = 0;
		}
	}

	if (show_mon && !mon_init)
	{
		XGCValues gcv;
		XColor col;
	
		gainCurrent = gainTarget = (1 << GAINSHIFT);



		screen = DefaultScreen(display);
		visual = DefaultVisual(display, screen);
		window = XCreateSimpleWindow(display,
					     RootWindow(display, screen),
					     0, 0, prefs.buckets, 128 + 8, 0,
					     BlackPixel(display, screen),
					     WhitePixel(display, screen));
		XStoreName(display, window, "AudioCompress monitor");
	
		gcv.foreground = BlackPixel(display, screen);
		blackGC = XCreateGC(display, window, GCForeground, &gcv);
		gcv.foreground = WhitePixel(display, screen);
		whiteGC = XCreateGC(display, window, GCForeground, &gcv);
		col.red = 0;
		col.green = 0;
		col.blue = 65535;
		XAllocColor(display, DefaultColormap(display, screen), &col);
		gcv.foreground = col.pixel;
		blueGC = XCreateGC(display, window, GCForeground, &gcv);
		col.red = 65535;
		col.green = 65535;
		col.blue = 0;
		XAllocColor(display, DefaultColormap(display, screen), &col);
		gcv.foreground = col.pixel;
		yellowGC = XCreateGC(display, window, GCForeground, &gcv);
		col.red = 32767;
		col.green = 32767;
		col.blue = 0;
		XAllocColor(display, DefaultColormap(display, screen), &col);
		gcv.foreground = col.pixel;
		dkyellowGC = XCreateGC(display, window, GCForeground, &gcv);
		col.red = 65535;
		col.green = 0;
		col.blue = 0;
		XAllocColor(display, DefaultColormap(display, screen), &col);
		gcv.foreground = col.pixel;
		redGC = XCreateGC(display, window, GCForeground, &gcv);
		mon_init = 1;
	}

	if (mon_init)
	{
		if (show_mon)
			XMapWindow(display, window);
		else
			XUnmapWindow(display, window);
		XResizeWindow(display, window, prefs.buckets, 128 + 8);
		XFlush(display);
	}
#endif
}

void CompressFree(void)
{
#ifdef USE_X
	if (mon_init)
	{
		XFreeGC(display, blackGC);
		XFreeGC(display, whiteGC);
		XFreeGC(display, blueGC);
		XFreeGC(display, yellowGC);
		XFreeGC(display, dkyellowGC);
		XFreeGC(display, redGC);
		XDestroyWindow(display, window);
		XCloseDisplay(display);
	}
#endif

	if (peaks)
		free(peaks);
}

void CompressDo(void *data, unsigned int length)
{
	int16_t *audio = (int16_t *)data, *ap;
	int peak, pos;
	int i;
	int gr, gf, gn;
	static int pn = -1;
#ifdef STATS
182
	static int clip;
183
#endif
184
	static int clipped;
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

	if (!peaks)
		return;

	if (pn == -1)
	{
		for (i = 0; i < prefs.buckets; i++)
			peaks[i] = 0;
	}
	pn = (pn + 1)%prefs.buckets;

#ifdef DEBUG
	fprintf(stderr, "modifyNative16(0x%08x, %d)\n",(unsigned int)data,
		length);
#endif

	/* Determine peak's value and position */
	peak = 1;
	pos = 0;

#ifdef DEBUG
	fprintf(stderr, "finding peak(b=%d)\n", pn);
#endif

	ap = audio;
	for (i = 0; i < length/2; i++)
	{
		int val = *ap;
		if (val > peak)
		{
			peak = val;
			pos = i;
		} else if (-val > peak)
		{
			peak = -val;
			pos = i;
		}
		ap++;
	}
	peaks[pn] = peak;

	/* Only draw if needed, of course */
#ifdef USE_X
	if (prefs.show_mon)
	{
		/* current amplitude */
		XDrawLine(display, window, whiteGC,
			  pn, 0,
			  pn,
			  127 -
			  (peaks[pn]*gainCurrent >> (GAINSHIFT + 8)));

		/* amplification */
		XDrawLine(display, window, yellowGC,
			  pn,
			  127 - (peaks[pn]*gainCurrent
				 >> (GAINSHIFT + 8)),
			  pn, 127);

		/* peak */
		XDrawLine(display, window, blackGC,
			  pn, 127 - (peaks[pn] >> 8), pn, 127);

		/* clip indicator */
		if (clipped)
			XDrawLine(display, window, redGC,
				  (pn + prefs.buckets - 1)%prefs.buckets,
				  126 - clipped/(length*512),
				  (pn + prefs.buckets - 1)%prefs.buckets,
				  127);
		clipped = 0;

257 258 259 260
		/* target line */
		/* XDrawPoint(display, window, redGC, */
		/*         pn, 127 - TARGET/256); */
		/* amplification edge */
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
		XDrawLine(display, window, dkyellowGC,
			  pn,
			  127 - (peaks[pn]*gainCurrent
				 >> (GAINSHIFT + 8)),
			  pn - 1,
			  127 -
			  (peaks[(pn + prefs.buckets
				  - 1)%prefs.buckets]*gainCurrent
			   >> (GAINSHIFT + 8)));
	}
#endif

	for (i = 0; i < prefs.buckets; i++)
	{
		if (peaks[i] > peak)
		{
			peak = peaks[i];
			pos = 0;
		}
	}

	/* Determine target gain */
	gn = (1 << GAINSHIFT)*prefs.target/peak;

	if (gn <(1 << GAINSHIFT))
		gn = 1 << GAINSHIFT;

	gainTarget = (gainTarget *((1 << prefs.gainsmooth) - 1) + gn)
				      >> prefs.gainsmooth;

	/* Give it an extra insignifigant nudge to counteract possible
	** rounding error
	*/

	if (gn < gainTarget)
		gainTarget--;
	else if (gn > gainTarget)
		gainTarget++;

	if (gainTarget > prefs.gainmax << GAINSHIFT)
		gainTarget = prefs.gainmax << GAINSHIFT;


#ifdef USE_X
	if (prefs.show_mon)
	{
		int x;

		/* peak*gain */
		XDrawPoint(display, window, redGC,
			   pn,
			   127 - (peak*gainCurrent
				  >> (GAINSHIFT + 8)));

315
		/* gain indicator */
316 317 318 319 320 321 322 323 324 325 326 327 328 329
		XFillRectangle(display, window, whiteGC, 0, 128,
			       prefs.buckets, 8);
		x = (gainTarget - (1 << GAINSHIFT))*prefs.buckets
			/ ((prefs.gainmax - 1) << GAINSHIFT);
		XDrawLine(display, window, redGC, x,
			  128, x, 128 + 8);

		x = (gn - (1 << GAINSHIFT))*prefs.buckets
			/ ((prefs.gainmax - 1) << GAINSHIFT);

		XDrawLine(display, window, blackGC,
			  x, 132 - 1,
			  x, 132 + 1);

330
		/* blue peak line */
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
		XDrawLine(display, window, blueGC,
			  0, 127 - (peak >> 8), prefs.buckets,
			  127 - (peak >> 8));
		XFlush(display);
		XDrawLine(display, window, whiteGC,
			  0, 127 - (peak >> 8), prefs.buckets,
			  127 - (peak >> 8));
	}
#endif

	/* See if a peak is going to clip */
	gn = (1 << GAINSHIFT)*32768/peak;

	if (gn < gainTarget)
	{
		gainTarget = gn;

		if (prefs.anticlip)
			pos = 0;

	} else
	{
		/* We're ramping up, so draw it out over the whole frame */
		pos = length;
	}

	/* Determine gain rate necessary to make target */
	if (!pos)
		pos = 1;

	gr = ((gainTarget - gainCurrent) << 16)/pos;

	/* Do the shiznit */
	gf = gainCurrent << 16;

#ifdef STATS
	fprintf(stderr, "\rgain = %2.2f%+.2e ",
		gainCurrent*1.0/(1 << GAINSHIFT),
		(gainTarget - gainCurrent)*1.0/(1 << GAINSHIFT));
#endif

	ap = audio;
	for (i = 0; i < length/2; i++)
	{
		int sample;

		/* Interpolate the gain */
		gainCurrent = gf >> 16;
		if (i < pos)
			gf += gr;
		else if (i == pos)
			gf = gainTarget << 16;

		/* Amplify */
		sample = (*ap)*gainCurrent >> GAINSHIFT;
		if (sample < -32768)
		{
#ifdef STATS
			clip++;
#endif
			clipped += -32768 - sample;
			sample = -32768;
		} else if (sample > 32767)
		{
#ifdef STATS
			clip++;
#endif
			clipped += sample - 32767;
			sample = 32767;
		}
		*ap++ = sample;
	}
#ifdef STATS
	fprintf(stderr, "clip %d b%-3d ", clip, pn);
#endif

#ifdef DEBUG
	fprintf(stderr, "\ndone\n");
#endif
}