annotate libao2/pl_volnorm.c @ 8448:1564db05e39d

sync
author nicolas
date Sat, 14 Dec 2002 13:11:08 +0000
parents 48deec5d2050
children 46d21c0f36aa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
1 /* Normalizer plugin
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
2 *
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
3 * Limitations:
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
4 * - only AFMT_S16_LE supported
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
5 * - no parameters yet => tweak the values by editing the #defines
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
6 *
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
7 * License: GPLv2
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
8 * Author: pl <p_l@gmx.fr> (c) 2002 and beyond...
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
9 *
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
10 * Sources: some ideas from volnorm plugin for xmms
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
11 *
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
12 * */
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
13
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
14 #define PLUGIN
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
15
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
16 /* Values for AVG:
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
17 * 1: uses a 1 value memory and coefficients new=a*old+b*cur (with a+b=1)
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
18 *
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
19 * 2: uses several samples to smooth the variations (standard weighted mean
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
20 * on past samples)
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
21 *
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
22 * */
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
23 #define AVG 1
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
24
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
25 #include <stdio.h>
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
26 #include <stdlib.h>
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
27 #include <inttypes.h>
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
28 #include <math.h> // for sqrt()
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
29
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
30 #include "audio_out.h"
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
31 #include "audio_plugin.h"
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
32 #include "audio_plugin_internal.h"
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
33 #include "afmt.h"
8218
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
34 #include "../config.h"
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
35
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
36 static ao_info_t info = {
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
37 "Volume normalizer",
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
38 "volnorm",
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
39 "pl <p_l@gmx.fr>",
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
40 ""
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
41 };
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
42
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
43 LIBAO_PLUGIN_EXTERN(volnorm)
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
44
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
45 // mul is the value by which the samples are scaled
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
46 // and has to be in [MUL_MIN, MUL_MAX]
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
47 #define MUL_INIT 1.0
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
48 #define MUL_MIN 0.1
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
49 #define MUL_MAX 5.0
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
50 static float mul;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
51
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
52
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
53 #if AVG==1
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
54 // "history" value of the filter
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
55 static float lastavg;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
56
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
57 // SMOOTH_* must be in ]0.0, 1.0[
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
58 // The new value accounts for SMOOTH_MUL in the value and history
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
59 #define SMOOTH_MUL 0.06
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
60 #define SMOOTH_LASTAVG 0.06
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
61
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
62
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
63 #elif AVG==2
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
64 // Size of the memory array
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
65 // FIXME: should depend on the frequency of the data (should be a few seconds)
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
66 #define NSAMPLES 128
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
67
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
68 // Indicates where to write (in 0..NSAMPLES-1)
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
69 static int idx;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
70 // The array
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
71 static struct {
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
72 float avg; // average level of the sample
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
73 int32_t len; // sample size (weight)
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
74 } mem[NSAMPLES];
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
75
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
76 // If summing all the mem[].len is lower than MIN_SAMPLE_SIZE bytes, then we
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
77 // choose to ignore the computed value as it's not significant enough
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
78 // FIXME: should depend on the frequency of the data (0.5s maybe)
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
79 #define MIN_SAMPLE_SIZE 32000
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
80
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
81 #else
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
82 // Kab00m !
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
83 #error "Unknown AVG"
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
84 #endif
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
85
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
86
4943
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
87 // Some limits
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
88 #define MIN_S16 -32768
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
89 #define MAX_S16 32767
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
90
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
91 // "Ideal" level
4943
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
92 #define MID_S16 (MAX_S16 * 0.25)
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
93
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
94 // Silence level
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
95 // FIXME: should be relative to the level of the samples
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
96 #define SIL_S16 (MAX_S16 * 0.01)
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
97
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
98
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
99 // Local data
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
100 static struct {
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
101 int inuse; // This plugin is in use TRUE, FALSE
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
102 int format; // sample fomat
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
103 } pl_volnorm = {0, 0};
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
104
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
105
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
106 // minimal interface
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
107 static int control(int cmd,int arg){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
108 switch(cmd){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
109 case AOCONTROL_PLUGIN_SET_LEN:
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
110 return CONTROL_OK;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
111 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
112 return CONTROL_UNKNOWN;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
113 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
114
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
115 // minimal interface
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
116 // open & setup audio device
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
117 // return: 1=success 0=fail
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
118 static int init(){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
119 switch(ao_plugin_data.format){
8218
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
120 #ifndef WORDS_BIGENDIAN
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
121 case(AFMT_S16_LE):
8218
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
122 #else
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
123 case(AFMT_S16_BE):
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
124 #endif
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
125 break;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
126 default:
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
127 fprintf(stderr,"[pl_volnorm] Audio format not yet supported.\n");
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
128 return 0;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
129 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
130
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
131 pl_volnorm.format = ao_plugin_data.format;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
132 pl_volnorm.inuse = 1;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
133
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
134 reset();
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
135
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
136 printf("[pl_volnorm] Normalizer plugin in use.\n");
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
137 return 1;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
138 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
139
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
140 // close plugin
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
141 static void uninit(){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
142 pl_volnorm.inuse=0;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
143 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
144
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
145 // empty buffers
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
146 static void reset(){
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
147 int i;
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
148 mul = MUL_INIT;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
149 switch(ao_plugin_data.format) {
8218
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
150 #ifndef WORDS_BIGENDIAN
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
151 case(AFMT_S16_LE):
8218
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
152 #else
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
153 case(AFMT_S16_BE):
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
154 #endif
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
155 #if AVG==1
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
156 lastavg = MID_S16;
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
157 #elif AVG==2
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
158 for(i=0; i < NSAMPLES; ++i) {
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
159 mem[i].len = 0;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
160 mem[i].avg = 0;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
161 }
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
162 idx = 0;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
163 #endif
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
164
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
165 break;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
166 default:
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
167 fprintf(stderr,"[pl_volnorm] internal inconsistency - bugreport !\n");
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
168 *(char *) 0 = 0;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
169 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
170 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
171
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
172 // processes 'ao_plugin_data.len' bytes of 'data'
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
173 // called for every block of data
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
174 static int play(){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
175
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
176 switch(pl_volnorm.format){
8218
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
177 #ifndef WORDS_BIGENDIAN
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
178 case(AFMT_S16_LE): {
8218
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
179 #else
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
180 case(AFMT_S16_BE): {
48deec5d2050 volnorm for bigendian
colin
parents: 5025
diff changeset
181 #endif
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
182 #define CLAMP(x,m,M) do { if ((x)<(m)) (x) = (m); else if ((x)>(M)) (x) = (M); } while(0)
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
183
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
184 int16_t* data=(int16_t*)ao_plugin_data.data;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
185 int len=ao_plugin_data.len / 2; // 16 bits samples
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
186
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
187 int32_t i, tmp;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
188 float curavg, newavg;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
189
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
190 #if AVG==1
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
191 float neededmul;
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
192 #elif AVG==2
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
193 float avg;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
194 int32_t totallen;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
195 #endif
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
196
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
197 // Evaluate current samples average level
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
198 curavg = 0.0;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
199 for (i = 0; i < len ; ++i) {
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
200 tmp = data[i];
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
201 curavg += tmp * tmp;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
202 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
203 curavg = sqrt(curavg / (float) len);
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
204
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
205 // Evaluate an adequate 'mul' coefficient based on previous state, current
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
206 // samples level, etc
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
207 #if AVG==1
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
208 if (curavg > SIL_S16) {
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
209 neededmul = MID_S16 / ( curavg * mul);
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
210 mul = (1.0 - SMOOTH_MUL) * mul + SMOOTH_MUL * neededmul;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
211
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
212 // Clamp the mul coefficient
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
213 CLAMP(mul, MUL_MIN, MUL_MAX);
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
214 }
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
215 #elif AVG==2
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
216 avg = 0.0;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
217 totallen = 0;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
218
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
219 for (i = 0; i < NSAMPLES; ++i) {
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
220 avg += mem[i].avg * (float) mem[i].len;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
221 totallen += mem[i].len;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
222 }
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
223
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
224 if (totallen > MIN_SAMPLE_SIZE) {
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
225 avg /= (float) totallen;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
226 if (avg >= SIL_S16) {
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
227 mul = (float) MID_S16 / avg;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
228 CLAMP(mul, MUL_MIN, MUL_MAX);
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
229 }
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
230 }
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
231 #endif
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
232
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
233 // Scale & clamp the samples
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
234 for (i = 0; i < len ; ++i) {
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
235 tmp = mul * data[i];
4943
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
236 CLAMP(tmp, MIN_S16, MAX_S16);
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
237 data[i] = tmp;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
238 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
239
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
240 // Evaluation of newavg (not 100% accurate because of values clamping)
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
241 newavg = mul * curavg;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
242
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
243 // Stores computed values for future smoothing
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
244 #if AVG==1
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
245 lastavg = (1.0 - SMOOTH_LASTAVG) * lastavg + SMOOTH_LASTAVG * newavg;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
246 //printf("\rmul=%02.1f ", mul);
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
247 #elif AVG==2
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
248 mem[idx].len = len;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
249 mem[idx].avg = newavg;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
250 idx = (idx + 1) % NSAMPLES;
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
251 //printf("\rmul=%02.1f (%04dKiB) ", mul, totallen/1024);
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
252 #endif
5025
6d8971d55e40 new smoothing method ('#define AVG 2' to enable'n'test it)
pl
parents: 4943
diff changeset
253 //fflush(stdout);
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
254
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
255 break;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
256 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
257 default:
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
258 return 0;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
259 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
260 return 1;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
261
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
262 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
263