x11_common: Get window dimensions also for -wid 0
[mplayer/glamo.git] / libaf / af_volume.c
blobe523333c9123a4c9ba868a6c1b48215062ccabeb
1 /*
2 * Copyright (C)2002 Anders Johansson ajh@atri.curtin.edu.au
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /* This audio filter changes the volume of the sound, and can be used
22 when the mixer doesn't support the PCM channel. It can handle
23 between 1 and AF_NCH channels. The volume can be adjusted between -60dB
24 to +20dB and is set on a per channels basis. The is accessed through
25 AF_CONTROL_VOLUME_LEVEL.
27 The filter has support for soft-clipping, it is enabled by
28 AF_CONTROL_VOLUME_SOFTCLIPP. It has also a probing feature which
29 can be used to measure the power in the audio stream, both an
30 instantaneous value and the maximum value can be probed. The
31 probing is enable by AF_CONTROL_VOLUME_PROBE_ON_OFF and is done on a
32 per channel basis. The result from the probing is obtained using
33 AF_CONTROL_VOLUME_PROBE_GET and AF_CONTROL_VOLUME_PROBE_GET_MAX. The
34 probed values are calculated in dB.
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include <inttypes.h>
42 #include <math.h>
43 #include <limits.h>
45 #include "af.h"
47 // Data for specific instances of this filter
48 typedef struct af_volume_s
50 int enable[AF_NCH]; // Enable/disable / channel
51 float pow[AF_NCH]; // Estimated power level [dB]
52 float max[AF_NCH]; // Max Power level [dB]
53 float level[AF_NCH]; // Gain level for each channel
54 float time; // Forgetting factor for power estimate
55 int soft; // Enable/disable soft clipping
56 int fast; // Use fix-point volume control
57 }af_volume_t;
59 // Initialization and runtime control
60 static int control(struct af_instance_s* af, int cmd, void* arg)
62 af_volume_t* s = (af_volume_t*)af->setup;
64 switch(cmd){
65 case AF_CONTROL_REINIT:
66 // Sanity check
67 if(!arg) return AF_ERROR;
69 af->data->rate = ((af_data_t*)arg)->rate;
70 af->data->nch = ((af_data_t*)arg)->nch;
72 if(s->fast && (((af_data_t*)arg)->format != (AF_FORMAT_FLOAT_NE))){
73 af->data->format = AF_FORMAT_S16_NE;
74 af->data->bps = 2;
76 else{
77 // Cutoff set to 10Hz for forgetting factor
78 float x = 2.0*M_PI*15.0/(float)af->data->rate;
79 float t = 2.0-cos(x);
80 s->time = 1.0 - (t - sqrt(t*t - 1));
81 mp_msg(MSGT_AFILTER, MSGL_DBG2, "[volume] Forgetting factor = %0.5f\n",s->time);
82 af->data->format = AF_FORMAT_FLOAT_NE;
83 af->data->bps = 4;
85 return af_test_output(af,(af_data_t*)arg);
86 case AF_CONTROL_COMMAND_LINE:{
87 float v=0.0;
88 float vol[AF_NCH];
89 int i;
90 sscanf((char*)arg,"%f:%i", &v, &s->soft);
91 for(i=0;i<AF_NCH;i++) vol[i]=v;
92 return control(af,AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET, vol);
94 case AF_CONTROL_POST_CREATE:
95 s->fast = ((((af_cfg_t*)arg)->force & AF_INIT_FORMAT_MASK) ==
96 AF_INIT_FLOAT) ? 0 : 1;
97 return AF_OK;
98 case AF_CONTROL_VOLUME_ON_OFF | AF_CONTROL_SET:
99 memcpy(s->enable,(int*)arg,AF_NCH*sizeof(int));
100 return AF_OK;
101 case AF_CONTROL_VOLUME_ON_OFF | AF_CONTROL_GET:
102 memcpy((int*)arg,s->enable,AF_NCH*sizeof(int));
103 return AF_OK;
104 case AF_CONTROL_VOLUME_SOFTCLIP | AF_CONTROL_SET:
105 s->soft = *(int*)arg;
106 return AF_OK;
107 case AF_CONTROL_VOLUME_SOFTCLIP | AF_CONTROL_GET:
108 *(int*)arg = s->soft;
109 return AF_OK;
110 case AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET:
111 return af_from_dB(AF_NCH,(float*)arg,s->level,20.0,-200.0,60.0);
112 case AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_GET:
113 return af_to_dB(AF_NCH,s->level,(float*)arg,20.0);
114 case AF_CONTROL_VOLUME_PROBE | AF_CONTROL_GET:
115 return af_to_dB(AF_NCH,s->pow,(float*)arg,10.0);
116 case AF_CONTROL_VOLUME_PROBE_MAX | AF_CONTROL_GET:
117 return af_to_dB(AF_NCH,s->max,(float*)arg,10.0);
118 case AF_CONTROL_PRE_DESTROY:{
119 float m = 0.0;
120 int i;
121 if(!s->fast){
122 for(i=0;i<AF_NCH;i++)
123 m=max(m,s->max[i]);
124 af_to_dB(1, &m, &m, 10.0);
125 mp_msg(MSGT_AFILTER, MSGL_INFO, "[volume] The maximum volume was %0.2fdB \n", m);
127 return AF_OK;
130 return AF_UNKNOWN;
133 // Deallocate memory
134 static void uninit(struct af_instance_s* af)
136 if(af->data)
137 free(af->data);
138 if(af->setup)
139 free(af->setup);
142 // Filter data through filter
143 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
145 af_data_t* c = data; // Current working data
146 af_volume_t* s = (af_volume_t*)af->setup; // Setup for this instance
147 int ch = 0; // Channel counter
148 register int nch = c->nch; // Number of channels
149 register int i = 0;
151 // Basic operation volume control only (used on slow machines)
152 if(af->data->format == (AF_FORMAT_S16_NE)){
153 int16_t* a = (int16_t*)c->audio; // Audio data
154 int len = c->len/2; // Number of samples
155 for(ch = 0; ch < nch ; ch++){
156 if(s->enable[ch]){
157 register int vol = (int)(255.0 * s->level[ch]);
158 for(i=ch;i<len;i+=nch){
159 register int x = (a[i] * vol) >> 8;
160 a[i]=clamp(x,SHRT_MIN,SHRT_MAX);
165 // Machine is fast and data is floating point
166 else if(af->data->format == (AF_FORMAT_FLOAT_NE)){
167 float* a = (float*)c->audio; // Audio data
168 int len = c->len/4; // Number of samples
169 for(ch = 0; ch < nch ; ch++){
170 // Volume control (fader)
171 if(s->enable[ch]){
172 float t = 1.0 - s->time;
173 for(i=ch;i<len;i+=nch){
174 register float x = a[i];
175 register float pow = x*x;
176 // Check maximum power value
177 if(pow > s->max[ch])
178 s->max[ch] = pow;
179 // Set volume
180 x *= s->level[ch];
181 // Peak meter
182 pow = x*x;
183 if(pow > s->pow[ch])
184 s->pow[ch] = pow;
185 else
186 s->pow[ch] = t*s->pow[ch] + pow*s->time; // LP filter
187 /* Soft clipping, the sound of a dream, thanks to Jon Wattes
188 post to Musicdsp.org */
189 if(s->soft)
190 x=af_softclip(x);
191 // Hard clipping
192 else
193 x=clamp(x,-1.0,1.0);
194 a[i] = x;
199 return c;
202 // Allocate memory and set function pointers
203 static int af_open(af_instance_t* af){
204 int i = 0;
205 af->control=control;
206 af->uninit=uninit;
207 af->play=play;
208 af->mul=1;
209 af->data=calloc(1,sizeof(af_data_t));
210 af->setup=calloc(1,sizeof(af_volume_t));
211 if(af->data == NULL || af->setup == NULL)
212 return AF_ERROR;
213 // Enable volume control and set initial volume to 0dB.
214 for(i=0;i<AF_NCH;i++){
215 ((af_volume_t*)af->setup)->enable[i] = 1;
216 ((af_volume_t*)af->setup)->level[i] = 1.0;
218 return AF_OK;
221 // Description of this filter
222 af_info_t af_info_volume = {
223 "Volume control audio filter",
224 "volume",
225 "Anders",
227 AF_FLAGS_NOT_REENTRANT,
228 af_open