annotate Plugins/Effect/audiocompress/compress.c @ 1112:73631b1e8611 trunk

[svn] - new tunings, please test
author nenolod
date Tue, 30 May 2006 21:55:51 -0700
parents d78fa5f0b680
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
937
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
1 /* compress.c
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
2 ** Compressor logic
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
3 */
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
4
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
5 #include <stdio.h>
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
6 #include <stdlib.h>
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
7 #include <string.h>
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
8 #include <sys/types.h>
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
9
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
10 #include "config.h"
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
11 #include "compress.h"
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
12
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
13 static int *peaks = NULL;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
14 static int gainCurrent, gainTarget;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
15
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
16 static struct {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
17 int anticlip;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
18 int target;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
19 int gainmax;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
20 int gainsmooth;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
21 int buckets;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
22 } prefs;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
23
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
24 void CompressCfg(int anticlip, int target, int gainmax,
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
25 int gainsmooth, int buckets)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
26 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
27 static int lastsize = 0;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
28
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
29 prefs.anticlip = anticlip;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
30 prefs.target = target;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
31 prefs.gainmax = gainmax;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
32 prefs.gainsmooth = gainsmooth;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
33 prefs.buckets = buckets;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
34
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
35 /* Allocate the peak structure */
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
36 peaks = realloc(peaks, sizeof(int)*prefs.buckets);
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
37
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
38 if (prefs.buckets > lastsize)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
39 memset(peaks + lastsize, 0, sizeof(int)*(prefs.buckets
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
40 - lastsize));
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
41 lastsize = prefs.buckets;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
42 }
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
43
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
44 void CompressFree(void)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
45 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
46 if (peaks)
939
d78fa5f0b680 [svn] prevent audiocompress from fulfilling its suicidal tendencies
giacomo
parents: 937
diff changeset
47 {
937
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
48 free(peaks);
939
d78fa5f0b680 [svn] prevent audiocompress from fulfilling its suicidal tendencies
giacomo
parents: 937
diff changeset
49 peaks = NULL;
d78fa5f0b680 [svn] prevent audiocompress from fulfilling its suicidal tendencies
giacomo
parents: 937
diff changeset
50 }
937
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
51 }
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
52
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
53 void CompressDo(void *data, unsigned int length)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
54 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
55 int16_t *audio = (int16_t *)data, *ap;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
56 int peak, pos;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
57 int i;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
58 int gr, gf, gn;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
59 static int pn = -1;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
60 #ifdef STATS
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
61 static int clip = 0;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
62 #endif
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
63 static int clipped = 0;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
64
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
65 if (!peaks)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
66 return;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
67
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
68 if (pn == -1)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
69 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
70 for (i = 0; i < prefs.buckets; i++)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
71 peaks[i] = 0;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
72 }
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
73 pn = (pn + 1)%prefs.buckets;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
74
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
75 #ifdef DEBUG
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
76 fprintf(stderr, "modifyNative16(0x%08x, %d)\n",(unsigned int)data,
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
77 length);
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
78 #endif
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
79
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
80 /* Determine peak's value and position */
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
81 peak = 1;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
82 pos = 0;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
83
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
84 #ifdef DEBUG
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
85 fprintf(stderr, "finding peak(b=%d)\n", pn);
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
86 #endif
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
87
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
88 ap = audio;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
89 for (i = 0; i < length/2; i++)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
90 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
91 int val = *ap;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
92 if (val > peak)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
93 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
94 peak = val;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
95 pos = i;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
96 } else if (-val > peak)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
97 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
98 peak = -val;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
99 pos = i;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
100 }
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
101 ap++;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
102 }
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
103 peaks[pn] = peak;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
104
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
105 for (i = 0; i < prefs.buckets; i++)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
106 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
107 if (peaks[i] > peak)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
108 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
109 peak = peaks[i];
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
110 pos = 0;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
111 }
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
112 }
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
113
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
114 /* Determine target gain */
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
115 gn = (1 << GAINSHIFT)*prefs.target/peak;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
116
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
117 if (gn <(1 << GAINSHIFT))
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
118 gn = 1 << GAINSHIFT;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
119
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
120 gainTarget = (gainTarget *((1 << prefs.gainsmooth) - 1) + gn)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
121 >> prefs.gainsmooth;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
122
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
123 /* Give it an extra insignifigant nudge to counteract possible
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
124 ** rounding error
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
125 */
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
126
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
127 if (gn < gainTarget)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
128 gainTarget--;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
129 else if (gn > gainTarget)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
130 gainTarget++;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
131
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
132 if (gainTarget > prefs.gainmax << GAINSHIFT)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
133 gainTarget = prefs.gainmax << GAINSHIFT;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
134
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
135
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
136 /* See if a peak is going to clip */
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
137 gn = (1 << GAINSHIFT)*32768/peak;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
138
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
139 if (gn < gainTarget)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
140 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
141 gainTarget = gn;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
142
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
143 if (prefs.anticlip)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
144 pos = 0;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
145
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
146 } else
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
147 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
148 /* We're ramping up, so draw it out over the whole frame */
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
149 pos = length;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
150 }
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
151
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
152 /* Determine gain rate necessary to make target */
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
153 if (!pos)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
154 pos = 1;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
155
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
156 gr = ((gainTarget - gainCurrent) << 16)/pos;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
157
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
158 /* Do the shiznit */
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
159 gf = gainCurrent << 16;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
160
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
161 #ifdef STATS
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
162 fprintf(stderr, "\rgain = %2.2f%+.2e ",
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
163 gainCurrent*1.0/(1 << GAINSHIFT),
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
164 (gainTarget - gainCurrent)*1.0/(1 << GAINSHIFT));
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
165 #endif
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
166
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
167 ap = audio;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
168 for (i = 0; i < length/2; i++)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
169 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
170 int sample;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
171
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
172 /* Interpolate the gain */
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
173 gainCurrent = gf >> 16;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
174 if (i < pos)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
175 gf += gr;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
176 else if (i == pos)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
177 gf = gainTarget << 16;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
178
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
179 /* Amplify */
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
180 sample = (*ap)*gainCurrent >> GAINSHIFT;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
181 if (sample < -32768)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
182 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
183 #ifdef STATS
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
184 clip++;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
185 #endif
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
186 clipped += -32768 - sample;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
187 sample = -32768;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
188 } else if (sample > 32767)
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
189 {
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
190 #ifdef STATS
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
191 clip++;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
192 #endif
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
193 clipped += sample - 32767;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
194 sample = 32767;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
195 }
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
196 *ap++ = sample;
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
197 }
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
198 #ifdef STATS
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
199 fprintf(stderr, "clip %d b%-3d ", clip, pn);
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
200 #endif
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
201
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
202 #ifdef DEBUG
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
203 fprintf(stderr, "\ndone\n");
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
204 #endif
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
205 }
6158ee7d8b06 [svn] Port AudioCompress (AGC) effect plugin from XMMS, as requested by Junics on SorceryNet.
chainsaw
parents:
diff changeset
206