Mercurial > mplayer.hg
annotate libaf/af_volume.c @ 8612:a61d1b326beb
It shows not just the progressbar, but progressbar /and/ percentage
for osd levels 2 and 3, and inaddition it adds a new osd level (3)
which also shows total time.
patch by seru <seru@gmx.net>
author | arpi |
---|---|
date | Sat, 28 Dec 2002 14:17:38 +0000 |
parents | d6f40a06867b |
children | 440301fef3fe |
rev | line source |
---|---|
8607 | 1 /*============================================================================= |
2 // | |
3 // This software has been released under the terms of the GNU Public | |
4 // license. See http://www.gnu.org/copyleft/gpl.html for details. | |
5 // | |
6 // Copyright 2002 Anders Johansson ajh@atri.curtin.edu.au | |
7 // | |
8 //============================================================================= | |
9 */ | |
10 | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
11 /* This audio filter changes the volume of the sound, and can be used |
8607 | 12 when the mixer doesn't support the PCM channel. It can handle |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
13 between 1 and 6 channels. The volume can be adjusted between -60dB |
8607 | 14 to +20dB and is set on a per channels basis. The is accessed through |
15 AF_CONTROL_VOLUME_LEVEL. | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
16 |
8607 | 17 The filter has support for soft-clipping, it is enabled by |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
18 AF_CONTROL_VOLUME_SOFTCLIPP. It has also a probing feature which |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
19 can be used to measure the power in the audio stream, both an |
8607 | 20 instantaneous value and the maximum value can be probed. The |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
21 probing is enable by AF_CONTROL_VOLUME_PROBE_ON_OFF and is done on a |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
22 per channel basis. The result from the probing is obtained using |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
23 AF_CONTROL_VOLUME_PROBE_GET and AF_CONTROL_VOLUME_PROBE_GET_MAX. The |
8607 | 24 probed values are calculated in dB. |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
25 */ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
26 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
27 #include <stdio.h> |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
28 #include <stdlib.h> |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
29 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
30 #include <unistd.h> |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
31 #include <inttypes.h> |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
32 #include <math.h> |
8607 | 33 #include <limits.h> |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
34 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
35 #include "af.h" |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
36 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
37 // Data for specific instances of this filter |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
38 typedef struct af_volume_s |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
39 { |
8607 | 40 int enable[AF_NCH]; // Enable/disable / channel |
41 float pow[AF_NCH]; // Estimated power level [dB] | |
42 float max[AF_NCH]; // Max Power level [dB] | |
43 float level[AF_NCH]; // Gain level for each channel | |
44 float time; // Forgetting factor for power estimate | |
45 int soft; // Enable/disable soft clipping | |
46 int fast; // Use fix-point volume control | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
47 }af_volume_t; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
48 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
49 // Initialization and runtime control |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
50 static int control(struct af_instance_s* af, int cmd, void* arg) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
51 { |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
52 af_volume_t* s = (af_volume_t*)af->setup; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
53 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
54 switch(cmd){ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
55 case AF_CONTROL_REINIT: |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
56 // Sanity check |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
57 if(!arg) return AF_ERROR; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
58 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
59 af->data->rate = ((af_data_t*)arg)->rate; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
60 af->data->nch = ((af_data_t*)arg)->nch; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
61 |
8607 | 62 if(s->fast){ |
63 af->data->format = AF_FORMAT_SI | AF_FORMAT_NE; | |
64 af->data->bps = 2; | |
65 } | |
66 else{ | |
67 // Cutoff set to 10Hz for forgetting factor | |
68 float x = 2.0*M_PI*15.0/(float)af->data->rate; | |
69 float t = 2.0-cos(x); | |
70 s->time = 1.0 - (t - sqrt(t*t - 1)); | |
71 af_msg(AF_MSG_DEBUG0,"[volume] Forgetting factor = %0.5f\n",s->time); | |
72 af->data->format = AF_FORMAT_F | AF_FORMAT_NE; | |
73 af->data->bps = 4; | |
74 } | |
75 return af_test_output(af,(af_data_t*)arg); | |
7993
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
76 case AF_CONTROL_COMMAND_LINE:{ |
8167 | 77 float v=-10.0; |
8607 | 78 float vol[AF_NCH]; |
79 int i; | |
80 sscanf((char*)arg,"%f:%i", &v, &s->soft); | |
81 for(i=0;i<AF_NCH;i++) vol[i]=v; | |
82 return control(af,AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET, vol); | |
7993
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
83 } |
8607 | 84 case AF_CONTROL_POST_CREATE: |
85 s->fast = ((af_cfg_t*)arg)->force == AF_INIT_SLOW ? 1 : 0; | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
86 return AF_OK; |
8607 | 87 case AF_CONTROL_VOLUME_ON_OFF | AF_CONTROL_SET: |
88 memcpy(s->enable,(int*)arg,AF_NCH*sizeof(int)); | |
89 return AF_OK; | |
90 case AF_CONTROL_VOLUME_ON_OFF | AF_CONTROL_GET: | |
91 memcpy((int*)arg,s->enable,AF_NCH*sizeof(int)); | |
92 return AF_OK; | |
93 case AF_CONTROL_VOLUME_SOFTCLIP | AF_CONTROL_SET: | |
94 s->soft = *(int*)arg; | |
95 return AF_OK; | |
96 case AF_CONTROL_VOLUME_SOFTCLIP | AF_CONTROL_GET: | |
97 *(int*)arg = s->soft; | |
98 return AF_OK; | |
99 case AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET: | |
100 return af_from_dB(AF_NCH,(float*)arg,s->level,20.0,-200.0,60.0); | |
101 case AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_GET: | |
102 return af_to_dB(AF_NCH,s->level,(float*)arg,20.0); | |
103 case AF_CONTROL_VOLUME_PROBE | AF_CONTROL_GET: | |
104 return af_to_dB(AF_NCH,s->pow,(float*)arg,10.0); | |
105 case AF_CONTROL_VOLUME_PROBE_MAX | AF_CONTROL_GET: | |
106 return af_to_dB(AF_NCH,s->max,(float*)arg,10.0); | |
8186 | 107 case AF_CONTROL_PRE_DESTROY:{ |
108 float m = 0.0; | |
109 int i; | |
8607 | 110 for(i=0;i<AF_NCH;i++) |
111 m=max(m,s->max[i]); | |
8186 | 112 af_msg(AF_MSG_INFO,"The maximum volume was %0.2fdB \n",10*log10(m)); |
113 return AF_OK; | |
114 } | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
115 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
116 return AF_UNKNOWN; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
117 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
118 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
119 // Deallocate memory |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
120 static void uninit(struct af_instance_s* af) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
121 { |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
122 if(af->data) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
123 free(af->data); |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
124 if(af->setup) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
125 free(af->setup); |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
126 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
127 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
128 // Filter data through filter |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
129 static af_data_t* play(struct af_instance_s* af, af_data_t* data) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
130 { |
8607 | 131 af_data_t* c = data; // Current working data |
132 af_volume_t* s = (af_volume_t*)af->setup; // Setup for this instance | |
133 int ch = 0; // Channel counter | |
134 register int nch = c->nch; // Number of channels | |
135 register int i = 0; | |
136 | |
137 // Basic operation volume control only (used on slow machines) | |
138 if(af->data->format == (AF_FORMAT_SI | AF_FORMAT_NE)){ | |
139 int16_t* a = (int16_t*)c->audio; // Audio data | |
140 int len = c->len/2; // Number of samples | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
141 for(ch = 0; ch < nch ; ch++){ |
8607 | 142 if(s->enable[ch]){ |
143 register int vol = (int)(255.0 * s->level[ch]); | |
144 for(i=ch;i<len;i+=nch){ | |
145 register int x = (a[i] * vol) >> 8; | |
146 a[i]=clamp(x,SHRT_MIN,SHRT_MAX); | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
147 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
148 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
149 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
150 } |
8607 | 151 // Machine is fast and data is floating point |
152 else if(af->data->format == (AF_FORMAT_F | AF_FORMAT_NE)){ | |
153 float* a = (float*)c->audio; // Audio data | |
154 int len = c->len/4; // Number of samples | |
155 for(ch = 0; ch < nch ; ch++){ | |
156 // Volume control (fader) | |
157 if(s->enable[ch]){ | |
158 float t = 1.0 - s->time; | |
159 for(i=ch;i<len;i+=nch){ | |
160 register float x = a[i]; | |
161 register float pow = x*x; | |
162 // Check maximum power value | |
163 if(pow > s->max[ch]) | |
164 s->max[ch] = pow; | |
165 // Set volume | |
166 x *= s->level[ch]; | |
167 // Peak meter | |
168 pow = x*x; | |
169 if(pow > s->pow[ch]) | |
170 s->pow[ch] = pow; | |
171 else | |
172 s->pow[ch] = t*s->pow[ch] + pow*s->time; // LP filter | |
173 /* Soft clipping, the sound of a dream, thanks to Jon Wattes | |
174 post to Musicdsp.org */ | |
175 if(s->soft){ | |
176 if (x >= M_PI/2) | |
177 x = 1.0; | |
178 else if(x <= -M_PI/2) | |
179 x = -1.0; | |
180 else | |
181 x = sin(x); | |
182 } | |
183 // Hard clipping | |
184 else | |
185 x=clamp(x,-1.0,1.0); | |
186 a[i] = x; | |
187 } | |
188 } | |
189 } | |
190 } | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
191 return c; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
192 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
193 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
194 // Allocate memory and set function pointers |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
195 static int open(af_instance_t* af){ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
196 int i = 0; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
197 af->control=control; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
198 af->uninit=uninit; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
199 af->play=play; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
200 af->mul.n=1; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
201 af->mul.d=1; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
202 af->data=calloc(1,sizeof(af_data_t)); |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
203 af->setup=calloc(1,sizeof(af_volume_t)); |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
204 if(af->data == NULL || af->setup == NULL) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
205 return AF_ERROR; |
7993
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
206 /* Enable volume control and set initial volume to 0.1 this is a |
8607 | 207 safety measure to ensure that the user doesn't blow his |
7993
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
208 speakers. If the user isn't happy with this he can use the |
8607 | 209 command-line parameters to set the initial volume */ |
210 for(i=0;i<AF_NCH;i++){ | |
211 ((af_volume_t*)af->setup)->enable[i] = 1; | |
212 ((af_volume_t*)af->setup)->level[i] = 0.1; | |
213 } | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
214 return AF_OK; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
215 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
216 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
217 // Description of this filter |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
218 af_info_t af_info_volume = { |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
219 "Volume control audio filter", |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
220 "volume", |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
221 "Anders", |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
222 "", |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
223 AF_FLAGS_NOT_REENTRANT, |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
224 open |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
225 }; |