annotate libaf/af_volume.c @ 8287:c3eb28f300d0

.align -> .balign patch by Bj«Órn Sandell <biorn@dce.chalmers.se>
author arpi
date Mon, 25 Nov 2002 20:37:12 +0000
parents b56fd4b1738d
children d6f40a06867b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
1 /* This audio filter changes the volume of the sound, and can be used
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
2 when the mixer doesn't support the PCM channel. It can handel
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
3 between 1 and 6 channels. The volume can be adjusted between -60dB
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
4 to +20dB and is set on a per channels basis. The volume can be
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
5 written ad read by AF_CONTROL_VOLUME_SET and AF_CONTROL_VOLUME_GET
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
6 respectivly.
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
7
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
8 The plugin has support for softclipping, it is enabled by
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
9 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
10 can be used to measure the power in the audio stream, both an
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
11 instantanious value and the maximum value can be probed. The
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
12 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
13 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
14 AF_CONTROL_VOLUME_PROBE_GET and AF_CONTROL_VOLUME_PROBE_GET_MAX. The
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
15 probed values are calculated in dB. The control of the volume can
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
16 be turned off by the AF_CONTROL_VOLUME_ON_OFF switch.
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
17 */
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
18
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
19 #include <stdio.h>
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
20 #include <stdlib.h>
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
21
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
22 #include <unistd.h>
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
23 #include <inttypes.h>
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
24 #include <math.h>
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 #include "af.h"
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
27
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
28 // Some limits
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
29 #define NCH AF_NCH // Number of channels
e8832e66babd New features:
anders
parents: 7993
diff changeset
30
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
31 #define MIN_S16 -32650
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
32 #define MAX_S16 32650
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
33
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
34 #define MAX_VOL +40.0
e8832e66babd New features:
anders
parents: 7993
diff changeset
35 #define MIN_VOL -200.0
7745
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 {
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
40 float volume[NCH]; // Volume for each channel
e8832e66babd New features:
anders
parents: 7993
diff changeset
41 float power[NCH]; // Instantaneous power in each channel
e8832e66babd New features:
anders
parents: 7993
diff changeset
42 float maxpower[NCH]; // Maximum power in each channel
e8832e66babd New features:
anders
parents: 7993
diff changeset
43 float alpha; // Forgetting factor for power estimate
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
44 int softclip; // Soft clippng on/off
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
45 int probe; // Probing on/off
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
46 int onoff; // Volume control on/off
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 /* Convert to gain value from dB. Returns AF_OK if of and AF_ERROR if
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
50 fail */
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
51 inline int from_dB(float* in, float* out, float k)
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
52 {
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
53 int i = 0;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
54 // Sanity check
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
55 if(!in || !out)
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
56 return AF_ERROR;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
57
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
58 for(i=0;i<NCH;i++){
e8832e66babd New features:
anders
parents: 7993
diff changeset
59 if(in[i]<MIN_VOL)
e8832e66babd New features:
anders
parents: 7993
diff changeset
60 out[i]=0.0;
e8832e66babd New features:
anders
parents: 7993
diff changeset
61 else
8186
b56fd4b1738d Printing of max volume on exit
anders
parents: 8167
diff changeset
62 out[i]=pow(10.0,clamp(in[i],MIN_VOL,MAX_VOL)/k);
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
63 }
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
64 return AF_OK;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
65 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
66
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
67 /* Convert from gain value to dB. Returns AF_OK if of and AF_ERROR if
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
68 fail */
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
69 inline int to_dB(float* in, float* out, float k)
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
70 {
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
71 int i = 0;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
72 // Sanity check
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
73 if(!in || !out)
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
74 return AF_ERROR;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
75
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
76 for(i=0;i<NCH;i++){
e8832e66babd New features:
anders
parents: 7993
diff changeset
77 if(in[i] == 0.0)
e8832e66babd New features:
anders
parents: 7993
diff changeset
78 out[i]=MIN_VOL;
e8832e66babd New features:
anders
parents: 7993
diff changeset
79 else
e8832e66babd New features:
anders
parents: 7993
diff changeset
80 out[i]=k*log10(clamp(in[i],MIN_VOL,MAX_VOL));
e8832e66babd New features:
anders
parents: 7993
diff changeset
81 }
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
82 return AF_OK;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
83 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
84
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
85 // Initialization and runtime control
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
86 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
87 {
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
88 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
89
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
90 switch(cmd){
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
91 case AF_CONTROL_REINIT:
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
92 // Sanity check
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
93 if(!arg) return AF_ERROR;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
94
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
95 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
96 af->data->nch = ((af_data_t*)arg)->nch;
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
97 af->data->format = AF_FORMAT_SI | AF_FORMAT_LE;
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
98 af->data->bps = 2;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
99
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
100 // Time constant set to 0.1s
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
101 s->alpha = (1.0/0.2)/(2.0*M_PI*(float)((af_data_t*)arg)->rate);
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
102
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
103 // Only AFMT_S16_LE is supported for now
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
104 if(af->data->format != ((af_data_t*)arg)->format ||
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
105 af->data->bps != ((af_data_t*)arg)->bps)
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
106 return AF_FALSE;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
107 return AF_OK;
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
108 case AF_CONTROL_COMMAND_LINE:{
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
109 float v=-10.0;
e8832e66babd New features:
anders
parents: 7993
diff changeset
110 float vol[6];
e8832e66babd New features:
anders
parents: 7993
diff changeset
111 int i;
e8832e66babd New features:
anders
parents: 7993
diff changeset
112 sscanf((char*)arg,"%f:%i:%i:%i", &v,
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
113 &(s->softclip), &(s->probe), &(s->onoff));
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
114 for(i=0;i<NCH;i++) vol[i]=v;
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
115 return from_dB(vol,s->volume,20.0);
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
116 }
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
117 case AF_CONTROL_VOLUME_SET:
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
118 return from_dB((float*)arg,s->volume,20.0);
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
119 case AF_CONTROL_VOLUME_GET:
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
120 return to_dB(s->volume,(float*)arg,20.0);
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
121 case AF_CONTROL_VOLUME_PROBE_GET:
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
122 return to_dB(s->power,(float*)arg,10.0);
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
123 case AF_CONTROL_VOLUME_PROBE_GET_MAX:
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
124 return to_dB(s->maxpower,(float*)arg,10.0);
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
125 case AF_CONTROL_VOLUME_SOFTCLIP:
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
126 s->softclip = (int)arg;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
127 return AF_OK;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
128 case AF_CONTROL_VOLUME_PROBE_ON_OFF:
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
129 s->probe = (int)arg;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
130 return AF_OK;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
131 case AF_CONTROL_VOLUME_ON_OFF:
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
132 s->onoff = (int)arg;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
133 return AF_OK;
8186
b56fd4b1738d Printing of max volume on exit
anders
parents: 8167
diff changeset
134 case AF_CONTROL_PRE_DESTROY:{
b56fd4b1738d Printing of max volume on exit
anders
parents: 8167
diff changeset
135 float m = 0.0;
b56fd4b1738d Printing of max volume on exit
anders
parents: 8167
diff changeset
136 int i;
b56fd4b1738d Printing of max volume on exit
anders
parents: 8167
diff changeset
137 for(i=0;i<NCH;i++)
b56fd4b1738d Printing of max volume on exit
anders
parents: 8167
diff changeset
138 m=max(m,s->maxpower[i]);
b56fd4b1738d Printing of max volume on exit
anders
parents: 8167
diff changeset
139 af_msg(AF_MSG_INFO,"The maximum volume was %0.2fdB \n",10*log10(m));
b56fd4b1738d Printing of max volume on exit
anders
parents: 8167
diff changeset
140 return AF_OK;
b56fd4b1738d Printing of max volume on exit
anders
parents: 8167
diff changeset
141 }
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
142 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
143 return AF_UNKNOWN;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
144 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
145
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
146 // Deallocate memory
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
147 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
148 {
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
149 if(af->data)
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
150 free(af->data);
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
151 if(af->setup)
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
152 free(af->setup);
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
153 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
154
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
155 // Filter data through filter
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
156 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
157 {
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
158 af_data_t* c = data; // Current working data
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
159 af_volume_t* s = (af_volume_t*)af->setup; // Setup for this instance
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
160 int16_t* a = (int16_t*)c->audio; // Audio data
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
161 int len = c->len/2; // Number of samples
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
162 int ch = 0; // Channel counter
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
163 register int nch = c->nch; // Number of channels
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
164 register int i = 0;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
165
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
166 // Probe the data stream
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
167 if(s->probe){
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
168 for(ch = 0; ch < nch ; ch++){
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
169 float alpha = s->alpha;
e8832e66babd New features:
anders
parents: 7993
diff changeset
170 float beta = 1 - alpha;
e8832e66babd New features:
anders
parents: 7993
diff changeset
171 float pow = s->power[ch];
e8832e66babd New features:
anders
parents: 7993
diff changeset
172 float maxpow = s->maxpower[ch];
e8832e66babd New features:
anders
parents: 7993
diff changeset
173 register float t = 0;
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
174 for(i=ch;i<len;i+=nch){
8167
e8832e66babd New features:
anders
parents: 7993
diff changeset
175 t = ((float)a[i])/32768.0;
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
176 t *= t;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
177 // Check maximum power value
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
178 if(t>maxpow)
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
179 maxpow=t;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
180 // Power estimate made using first order AR model
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
181 if(t>pow)
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
182 pow=t;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
183 else
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
184 pow = beta*pow+alpha*t;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
185 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
186 s->power[ch] = pow;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
187 s->maxpower[ch] = maxpow;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
188 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
189 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
190
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
191 // Change the volume.
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
192 if(s->onoff){
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
193 register int sc = s->softclip;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
194 for(ch = 0; ch < nch ; ch++){
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
195 register int vol = (int)(255.0 * s->volume[ch]);
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
196 for(i=ch;i<len;i+=nch){
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
197 register int x;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
198 x=(a[i] * vol) >> 8;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
199 if(sc){
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
200 int64_t t=x*x;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
201 t=(t*x) >> 30;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
202 x = (3*x - (int)t) >> 1;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
203 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
204 a[i]=clamp(x,MIN_S16,MAX_S16);
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
205 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
206 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
207 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
208
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
209 return c;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
210 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
211
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
212 // Allocate memory and set function pointers
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
213 static int open(af_instance_t* af){
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
214 int i = 0;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
215 af->control=control;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
216 af->uninit=uninit;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
217 af->play=play;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
218 af->mul.n=1;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
219 af->mul.d=1;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
220 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
221 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
222 if(af->data == NULL || af->setup == NULL)
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
223 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
224 /* Enable volume control and set initial volume to 0.1 this is a
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
225 safety mesure to ensure that the user doesn't blow his
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
226 speakers. If the user isn't happy with this he can use the
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
227 commandline parameters to set the initial volume */
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
228 ((af_volume_t*)af->setup)->onoff = 1;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
229 for(i=0;i<NCH;i++)
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
230 ((af_volume_t*)af->setup)->volume[i]=0.1;
7745
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
231
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
232 return AF_OK;
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
233 }
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
234
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
235 // Description of this filter
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
236 af_info_t af_info_volume = {
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
237 "Volume control audio filter",
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
238 "volume",
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
239 "Anders",
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
240 "",
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
241 AF_FLAGS_NOT_REENTRANT,
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
242 open
1d3a3dc1f488 Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff changeset
243 };