osd: convert OSD font to OpenType
[mplayer.git] / libaf / af_volume.c
blob4e6a3b40f69462e806bceb8329ad76f12ff86bb2
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 register int nch = c->nch; // Number of channels
146 register int i = 0;
148 // Basic operation volume control only (used on slow machines)
149 if(af->data->format == (AF_FORMAT_S16_NE)){
150 int16_t* a = (int16_t*)c->audio; // Audio data
151 int len = c->len/2; // Number of samples
152 for (int ch = 0; ch < nch; ch++) {
153 int vol = 256.0 * s->level[ch];
154 if (s->enable[ch] && vol != 256) {
155 for(i=ch;i<len;i+=nch){
156 register int x = (a[i] * vol) >> 8;
157 a[i]=clamp(x,SHRT_MIN,SHRT_MAX);
162 // Machine is fast and data is floating point
163 else if(af->data->format == (AF_FORMAT_FLOAT_NE)){
164 float* a = (float*)c->audio; // Audio data
165 int len = c->len/4; // Number of samples
166 for (int ch = 0; ch < nch; ch++) {
167 // Volume control (fader)
168 if(s->enable[ch]){
169 float t = 1.0 - s->time;
170 for(i=ch;i<len;i+=nch){
171 register float x = a[i];
172 register float pow = x*x;
173 // Check maximum power value
174 if(pow > s->max[ch])
175 s->max[ch] = pow;
176 // Set volume
177 x *= s->level[ch];
178 // Peak meter
179 pow = x*x;
180 if(pow > s->pow[ch])
181 s->pow[ch] = pow;
182 else
183 s->pow[ch] = t*s->pow[ch] + pow*s->time; // LP filter
184 /* Soft clipping, the sound of a dream, thanks to Jon Wattes
185 post to Musicdsp.org */
186 if(s->soft)
187 x=af_softclip(x);
188 // Hard clipping
189 else
190 x=clamp(x,-1.0,1.0);
191 a[i] = x;
196 return c;
199 // Allocate memory and set function pointers
200 static int af_open(af_instance_t* af){
201 int i = 0;
202 af->control=control;
203 af->uninit=uninit;
204 af->play=play;
205 af->mul=1;
206 af->data=calloc(1,sizeof(af_data_t));
207 af->setup=calloc(1,sizeof(af_volume_t));
208 if(af->data == NULL || af->setup == NULL)
209 return AF_ERROR;
210 // Enable volume control and set initial volume to 0dB.
211 for(i=0;i<AF_NCH;i++){
212 ((af_volume_t*)af->setup)->enable[i] = 1;
213 ((af_volume_t*)af->setup)->level[i] = 1.0;
215 return AF_OK;
218 // Description of this filter
219 af_info_t af_info_volume = {
220 "Volume control audio filter",
221 "volume",
222 "Anders",
224 AF_FLAGS_NOT_REENTRANT,
225 af_open