rpm: Remove MEncoder from rpm packaging
[mplayer/glamo.git] / libaf / af_volume.c
blob768f67c5f3a7d5b8fbd22a8d0cd1760362b1e983
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 free(af->data);
137 free(af->setup);
140 // Filter data through filter
141 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
143 af_data_t* c = data; // Current working data
144 af_volume_t* s = (af_volume_t*)af->setup; // Setup for this instance
145 int ch = 0; // Channel counter
146 register int nch = c->nch; // Number of channels
147 register int i = 0;
149 // Basic operation volume control only (used on slow machines)
150 if(af->data->format == (AF_FORMAT_S16_NE)){
151 int16_t* a = (int16_t*)c->audio; // Audio data
152 int len = c->len/2; // Number of samples
153 for(ch = 0; ch < nch ; ch++){
154 if(s->enable[ch]){
155 register int vol = (int)(255.0 * s->level[ch]);
156 for(i=ch;i<len;i+=nch){
157 register int x = (a[i] * vol) >> 8;
158 a[i]=clamp(x,SHRT_MIN,SHRT_MAX);
163 // Machine is fast and data is floating point
164 else if(af->data->format == (AF_FORMAT_FLOAT_NE)){
165 float* a = (float*)c->audio; // Audio data
166 int len = c->len/4; // Number of samples
167 for(ch = 0; ch < nch ; ch++){
168 // Volume control (fader)
169 if(s->enable[ch]){
170 float t = 1.0 - s->time;
171 for(i=ch;i<len;i+=nch){
172 register float x = a[i];
173 register float pow = x*x;
174 // Check maximum power value
175 if(pow > s->max[ch])
176 s->max[ch] = pow;
177 // Set volume
178 x *= s->level[ch];
179 // Peak meter
180 pow = x*x;
181 if(pow > s->pow[ch])
182 s->pow[ch] = pow;
183 else
184 s->pow[ch] = t*s->pow[ch] + pow*s->time; // LP filter
185 /* Soft clipping, the sound of a dream, thanks to Jon Wattes
186 post to Musicdsp.org */
187 if(s->soft)
188 x=af_softclip(x);
189 // Hard clipping
190 else
191 x=clamp(x,-1.0,1.0);
192 a[i] = x;
197 return c;
200 // Allocate memory and set function pointers
201 static int af_open(af_instance_t* af){
202 int i = 0;
203 af->control=control;
204 af->uninit=uninit;
205 af->play=play;
206 af->mul=1;
207 af->data=calloc(1,sizeof(af_data_t));
208 af->setup=calloc(1,sizeof(af_volume_t));
209 if(af->data == NULL || af->setup == NULL)
210 return AF_ERROR;
211 // Enable volume control and set initial volume to 0dB.
212 for(i=0;i<AF_NCH;i++){
213 ((af_volume_t*)af->setup)->enable[i] = 1;
214 ((af_volume_t*)af->setup)->level[i] = 1.0;
216 return AF_OK;
219 // Description of this filter
220 af_info_t af_info_volume = {
221 "Volume control audio filter",
222 "volume",
223 "Anders",
225 AF_FLAGS_NOT_REENTRANT,
226 af_open