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 6 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.
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
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
;
65 case AF_CONTROL_REINIT
:
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
;
77 // Cutoff set to 10Hz for forgetting factor
78 float x
= 2.0*M_PI
*15.0/(float)af
->data
->rate
;
80 s
->time
= 1.0 - (t
- sqrt(t
*t
- 1));
81 af_msg(AF_MSG_DEBUG0
,"[volume] Forgetting factor = %0.5f\n",s
->time
);
82 af
->data
->format
= AF_FORMAT_FLOAT_NE
;
85 return af_test_output(af
,(af_data_t
*)arg
);
86 case AF_CONTROL_COMMAND_LINE
:{
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;
98 case AF_CONTROL_VOLUME_ON_OFF
| AF_CONTROL_SET
:
99 memcpy(s
->enable
,(int*)arg
,AF_NCH
*sizeof(int));
101 case AF_CONTROL_VOLUME_ON_OFF
| AF_CONTROL_GET
:
102 memcpy((int*)arg
,s
->enable
,AF_NCH
*sizeof(int));
104 case AF_CONTROL_VOLUME_SOFTCLIP
| AF_CONTROL_SET
:
105 s
->soft
= *(int*)arg
;
107 case AF_CONTROL_VOLUME_SOFTCLIP
| AF_CONTROL_GET
:
108 *(int*)arg
= s
->soft
;
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
:{
122 for(i
=0;i
<AF_NCH
;i
++)
124 af_to_dB(1, &m
, &m
, 10.0);
125 af_msg(AF_MSG_INFO
,"[volume] The maximum volume was %0.2fdB \n", m
);
134 static void uninit(struct af_instance_s
* af
)
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
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
++){
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)
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
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 */
202 // Allocate memory and set function pointers
203 static int af_open(af_instance_t
* af
){
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
)
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;
221 // Description of this filter
222 af_info_t af_info_volume
= {
223 "Volume control audio filter",
227 AF_FLAGS_NOT_REENTRANT
,