annotate libao2/pl_volnorm.c @ 4958:ca6f6b35baf4

vd_dshow added
author arpi
date Wed, 06 Mar 2002 22:03:46 +0000
parents 511e8d8117e9
children 6d8971d55e40
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 *
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
10 * Sources: some ideas from volnorm for xmms
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
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
16 #include <stdio.h>
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
17 #include <stdlib.h>
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
18 #include <inttypes.h>
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
19 #include <math.h> // for sqrt()
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
20
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
21 #include "audio_out.h"
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
22 #include "audio_plugin.h"
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
23 #include "audio_plugin_internal.h"
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
24 #include "afmt.h"
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
25
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
26 static ao_info_t info = {
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
27 "Volume normalizer",
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
28 "volnorm",
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
29 "pl <p_l@gmx.fr>",
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
30 ""
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
31 };
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
32
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
33 LIBAO_PLUGIN_EXTERN(volnorm)
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
34
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
35 // mul is the value by which the samples are scaled
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
36 // and has to be in [MUL_MIN, MUL_MAX]
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
37 #define MUL_INIT 1.0
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
38 #define MUL_MIN 0.1
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
39 #define MUL_MAX 15.0
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
40 static float mul;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
41
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
42 // "history" value of the filter
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
43 static float lastavg;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
44
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
45 // SMOOTH_* must be in ]0.0, 1.0[
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
46 // The new value accounts for SMOOTH_MUL in the value and history
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
47 #define SMOOTH_MUL 0.06
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
48 #define SMOOTH_LASTAVG 0.06
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
49
4943
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
50 // Some limits
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
51 #define MIN_S16 -32768
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
52 #define MAX_S16 32767
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
53
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
54 // ideal average level
4943
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
55 #define MID_S16 (MAX_S16 * 0.25)
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
56
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
57 // silence level
4943
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
58 #define SIL_S16 (MAX_S16 * 0.02)
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
59
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
60 // local data
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
61 static struct {
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
62 int inuse; // This plugin is in use TRUE, FALSE
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
63 int format; // sample fomat
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
64 } pl_volnorm = {0, 0};
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
65
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
66
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
67 // minimal interface
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
68 static int control(int cmd,int arg){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
69 switch(cmd){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
70 case AOCONTROL_PLUGIN_SET_LEN:
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
71 return CONTROL_OK;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
72 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
73 return CONTROL_UNKNOWN;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
74 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
75
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
76 // minimal interface
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
77 // open & setup audio device
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
78 // return: 1=success 0=fail
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
79 static int init(){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
80 switch(ao_plugin_data.format){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
81 case(AFMT_S16_LE):
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
82 break;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
83 default:
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
84 fprintf(stderr,"[pl_volnorm] Audio format not yet supported.\n");
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
85 return 0;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
86 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
87
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
88 pl_volnorm.format = ao_plugin_data.format;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
89 pl_volnorm.inuse = 1;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
90
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
91 reset();
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
92
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
93 printf("[pl_volnorm] Normalizer plugin in use.\n");
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
94 return 1;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
95 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
96
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
97 // close plugin
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
98 static void uninit(){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
99 pl_volnorm.inuse=0;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
100 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
101
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
102 // empty buffers
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
103 static void reset(){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
104 mul = MUL_INIT;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
105 switch(ao_plugin_data.format) {
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
106 case(AFMT_S16_LE):
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
107 lastavg = MID_S16;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
108 break;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
109 default:
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
110 fprintf(stderr,"[pl_volnorm] internal inconsistency - please bugreport.\n");
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
111 *(char *) 0 = 0;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
112 }
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 // processes 'ao_plugin_data.len' bytes of 'data'
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
116 // called for every block of data
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
117 static int play(){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
118
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
119 switch(pl_volnorm.format){
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
120 case(AFMT_S16_LE): {
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
121
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
122 #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
123
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
124 int16_t* data=(int16_t*)ao_plugin_data.data;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
125 int len=ao_plugin_data.len / 2; // 16 bits samples
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
126
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
127 int32_t i;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
128 register int32_t tmp;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
129 register float curavg;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
130 float newavg;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
131 float neededmul;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
132
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
133 // average of the current samples
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
134 curavg = 0.0;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
135 for (i = 0; i < len ; ++i) {
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
136 tmp = data[i];
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
137 curavg += tmp * tmp;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
138 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
139 curavg = sqrt(curavg / (float) len);
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
140
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
141 if (curavg > SIL_S16) {
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
142 neededmul = MID_S16 / ( curavg * mul);
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
143 mul = (1.0 - SMOOTH_MUL) * mul + SMOOTH_MUL * neededmul;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
144
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
145 // Clamp the mul coefficient
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
146 CLAMP(mul, MUL_MIN, MUL_MAX);
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
147 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
148
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
149 // Scale & clamp the samples
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
150 for (i=0; i < len ; ++i) {
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
151 tmp = data[i] * mul;
4943
511e8d8117e9 proper bsd fix & preventive fix for other archs w/o INT_MAX
pl
parents: 4942
diff changeset
152 CLAMP(tmp, MIN_S16, MAX_S16);
4941
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
153 data[i] = tmp;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
154 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
155
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
156 // Evaluation of newavg (not 100% accurate because of values clamping)
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
157 newavg = mul * curavg;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
158
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
159 #if 0
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
160 printf("time = %d len = %d curavg = %6.0f lastavg = %6.0f newavg = %6.0f\n"
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
161 " needed_m = %2.2f m = %2.2f\n\n",
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
162 time(NULL), len, curavg, lastavg, newavg, neededmul, mul);
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
163 #endif
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
164
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
165 lastavg = (1.0 - SMOOTH_LASTAVG) * lastavg + SMOOTH_LASTAVG * newavg;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
166
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
167 break;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
168 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
169 default:
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
170 return 0;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
171 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
172 return 1;
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
173
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
174 }
9a468a190c4c volume normalizer plugin support
pl
parents:
diff changeset
175