Mercurial > mplayer.hg
annotate libaf/af_volume.c @ 8016:892c73ce9ba3
I ported mplayer to the GNU system (also known as GNU/Hurd),
it was just a matter of adding it to the makefiles and other misc fix.
Patch by Robert Millan <zeratul2@wanadoo.es>
author | arpi |
---|---|
date | Fri, 01 Nov 2002 00:05:56 +0000 |
parents | ea0680d87f3f |
children | e8832e66babd |
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 "../config.h" |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
27 #include "../mp_msg.h" |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
28 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
29 #include "af.h" |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
30 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
31 // Some limits |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
32 #define MIN_S16 -32650 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
33 #define MAX_S16 32650 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
34 |
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
|
35 #define MAX_VOL +20.0 |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
36 #define MIN_VOL -60.0 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
37 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
38 // Number of channels |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
39 #define NCH 6 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
40 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
41 #include "../libao2/afmt.h" |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
42 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
43 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
44 // Data for specific instances of this filter |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
45 typedef struct af_volume_s |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
46 { |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
47 double volume[NCH]; // Volume for each channel |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
48 double power[NCH]; // Instantaneous power in each channel |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
49 double maxpower[NCH]; // Maximum power in each channel |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
50 double alpha; // Forgetting factor for power estimate |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
51 int softclip; // Soft clippng on/off |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
52 int probe; // Probing on/off |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
53 int onoff; // Volume control on/off |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
54 }af_volume_t; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
55 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
56 /* 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
|
57 fail */ |
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
|
58 inline int from_dB(double* in, double* out, double k) |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
59 { |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
60 int i = 0; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
61 // Sanity check |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
62 if(!in || !out) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
63 return AF_ERROR; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
64 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
65 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
|
66 out[i]=pow(10.0,clamp(in[i],MIN_VOL,MAX_VOL)/k); |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
67 return AF_OK; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
68 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
69 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
70 /* 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
|
71 fail */ |
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
|
72 inline int to_dB(double* in, double* out, double k) |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
73 { |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
74 int i = 0; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
75 // Sanity check |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
76 if(!in || !out) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
77 return AF_ERROR; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
78 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
79 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
|
80 out[i]=k*log10(clamp(in[i],MIN_VOL,MAX_VOL)); |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
81 return AF_OK; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
82 } |
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 // Initialization and runtime control |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
85 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
|
86 { |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
87 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
|
88 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
89 switch(cmd){ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
90 case AF_CONTROL_REINIT: |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
91 // Sanity check |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
92 if(!arg) return AF_ERROR; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
93 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
94 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
|
95 af->data->nch = ((af_data_t*)arg)->nch; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
96 af->data->format = AFMT_S16_LE; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
97 af->data->bps = 2; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
98 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
99 // Time constant set to 0.1s |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
100 s->alpha = (1.0/0.2)/(2.0*M_PI*(double)((af_data_t*)arg)->rate); |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
101 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
102 // 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
|
103 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
|
104 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
|
105 return AF_FALSE; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
106 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
|
107 case AF_CONTROL_COMMAND_LINE:{ |
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 double vol[6]={-10.0,-10.0,-10.0,-10.0,-10.0,-10.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
|
109 sscanf((char*)arg,"%lf:%lf:%lf:%lf:%lf:%lf:%i:%i:%i", |
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
|
110 &vol[0], &vol[1], &vol[2], &vol[3], &vol[4], &vol[5], |
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
|
111 &(s->softclip), &(s->probe), &(s->onoff)); |
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
|
112 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
|
113 } |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
114 case AF_CONTROL_VOLUME_SET: |
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((double*)arg,s->volume,20.0); |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
116 case AF_CONTROL_VOLUME_GET: |
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
|
117 return to_dB(s->volume,(double*)arg,20.0); |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
118 case AF_CONTROL_VOLUME_PROBE_GET: |
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
|
119 return to_dB(s->power,(double*)arg,10.0); |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
120 case AF_CONTROL_VOLUME_PROBE_GET_MAX: |
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
|
121 return to_dB(s->maxpower,(double*)arg,10.0); |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
122 case AF_CONTROL_VOLUME_SOFTCLIP: |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
123 s->softclip = (int)arg; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
124 return AF_OK; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
125 case AF_CONTROL_VOLUME_PROBE_ON_OFF: |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
126 s->probe = (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_ON_OFF: |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
129 s->onoff = (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 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
132 return AF_UNKNOWN; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
133 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
134 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
135 // Deallocate memory |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
136 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
|
137 { |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
138 if(af->data) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
139 free(af->data); |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
140 if(af->setup) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
141 free(af->setup); |
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 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
144 // Filter data through filter |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
145 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
|
146 { |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 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
|
151 int ch = 0; // Channel counter |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
152 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
|
153 register int i = 0; |
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 // Probe the data stream |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
156 if(s->probe){ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
157 for(ch = 0; ch < nch ; ch++){ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
158 double alpha = s->alpha; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
159 double beta = 1 - alpha; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
160 double pow = s->power[ch]; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
161 double maxpow = s->maxpower[ch]; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
162 register double t = 0; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
163 for(i=ch;i<len;i+=nch){ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
164 t = ((double)a[i])/32768.0; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
165 t *= t; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
166 // Check maximum power value |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
167 if(t>maxpow) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
168 maxpow=t; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
169 // 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
|
170 if(t>pow) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
171 pow=t; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
172 else |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
173 pow = beta*pow+alpha*t; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
174 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
175 s->power[ch] = pow; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
176 s->maxpower[ch] = maxpow; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
177 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
178 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
179 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
180 // Change the volume. |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
181 if(s->onoff){ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
182 register int sc = s->softclip; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
183 for(ch = 0; ch < nch ; ch++){ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
184 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
|
185 for(i=ch;i<len;i+=nch){ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
186 register int x; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
187 x=(a[i] * vol) >> 8; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
188 if(sc){ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
189 int64_t t=x*x; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
190 t=(t*x) >> 30; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
191 x = (3*x - (int)t) >> 1; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
192 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
193 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
|
194 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
195 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
196 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
197 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
198 return c; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
199 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
200 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
201 // Allocate memory and set function pointers |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
202 static int open(af_instance_t* af){ |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
203 int i = 0; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
204 af->control=control; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
205 af->uninit=uninit; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
206 af->play=play; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
207 af->mul.n=1; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
208 af->mul.d=1; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
209 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
|
210 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
|
211 if(af->data == NULL || af->setup == NULL) |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
212 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
|
213 /* 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
|
214 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
|
215 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
|
216 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
|
217 ((af_volume_t*)af->setup)->onoff = 1; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
218 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
|
219 ((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
|
220 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
221 return AF_OK; |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
222 } |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
223 |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
224 // Description of this filter |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
225 af_info_t af_info_volume = { |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
226 "Volume control audio filter", |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
227 "volume", |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
228 "Anders", |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
229 "", |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
230 AF_FLAGS_NOT_REENTRANT, |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
231 open |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
diff
changeset
|
232 }; |