annotate libaf/af_pan.c @ 24147:2f9f0e7fe015

Remove redundant variable declarations.
author diego
date Sat, 25 Aug 2007 12:12:31 +0000
parents 63d9b7032bf3
children b2402b4f0afa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
1 /*=============================================================================
d6f40a06867b Changes includes:
anders
parents:
diff changeset
2 //
13602
14090f7300a8 The full name of the GPL is GNU General Public License.
diego
parents: 12008
diff changeset
3 // This software has been released under the terms of the GNU General Public
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
d6f40a06867b Changes includes:
anders
parents:
diff changeset
5 //
d6f40a06867b Changes includes:
anders
parents:
diff changeset
6 // Copyright 2002 Anders Johansson ajh@atri.curtin.edu.au
d6f40a06867b Changes includes:
anders
parents:
diff changeset
7 //
d6f40a06867b Changes includes:
anders
parents:
diff changeset
8 //=============================================================================
d6f40a06867b Changes includes:
anders
parents:
diff changeset
9 */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
10
d6f40a06867b Changes includes:
anders
parents:
diff changeset
11 /* */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
12
d6f40a06867b Changes includes:
anders
parents:
diff changeset
13 #include <stdio.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
14 #include <stdlib.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
15
d6f40a06867b Changes includes:
anders
parents:
diff changeset
16 #include <inttypes.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
17 #include <math.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
18 #include <limits.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
19
d6f40a06867b Changes includes:
anders
parents:
diff changeset
20 #include "af.h"
d6f40a06867b Changes includes:
anders
parents:
diff changeset
21
d6f40a06867b Changes includes:
anders
parents:
diff changeset
22 // Data for specific instances of this filter
d6f40a06867b Changes includes:
anders
parents:
diff changeset
23 typedef struct af_pan_s
d6f40a06867b Changes includes:
anders
parents:
diff changeset
24 {
23532
a1eb547cf52e Avoid zero output for pan filter; zero output now means same # of channels
zuxy
parents: 23531
diff changeset
25 int nch; // Number of output channels; zero means same as input
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
26 float level[AF_NCH][AF_NCH]; // Gain level for each channel
d6f40a06867b Changes includes:
anders
parents:
diff changeset
27 }af_pan_t;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
28
d6f40a06867b Changes includes:
anders
parents:
diff changeset
29 // Initialization and runtime control
d6f40a06867b Changes includes:
anders
parents:
diff changeset
30 static int control(struct af_instance_s* af, int cmd, void* arg)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
31 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
32 af_pan_t* s = af->setup;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
33
d6f40a06867b Changes includes:
anders
parents:
diff changeset
34 switch(cmd){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
35 case AF_CONTROL_REINIT:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
36 // Sanity check
d6f40a06867b Changes includes:
anders
parents:
diff changeset
37 if(!arg) return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
38
d6f40a06867b Changes includes:
anders
parents:
diff changeset
39 af->data->rate = ((af_data_t*)arg)->rate;
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 13602
diff changeset
40 af->data->format = AF_FORMAT_FLOAT_NE;
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
41 af->data->bps = 4;
23532
a1eb547cf52e Avoid zero output for pan filter; zero output now means same # of channels
zuxy
parents: 23531
diff changeset
42 af->data->nch = s->nch ? s->nch: ((af_data_t*)arg)->nch;
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
43 af->mul.n = af->data->nch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
44 af->mul.d = ((af_data_t*)arg)->nch;
14433
95bb94a930a3 always cancel down fractions (frac_t) to avoid overflows and playback
reimar
parents: 14245
diff changeset
45 af_frac_cancel(&af->mul);
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
46
d6f40a06867b Changes includes:
anders
parents:
diff changeset
47 if((af->data->format != ((af_data_t*)arg)->format) ||
d6f40a06867b Changes includes:
anders
parents:
diff changeset
48 (af->data->bps != ((af_data_t*)arg)->bps)){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
49 ((af_data_t*)arg)->format = af->data->format;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
50 ((af_data_t*)arg)->bps = af->data->bps;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
51 return AF_FALSE;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
52 }
23532
a1eb547cf52e Avoid zero output for pan filter; zero output now means same # of channels
zuxy
parents: 23531
diff changeset
53 return AF_OK;
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
54 case AF_CONTROL_COMMAND_LINE:{
d6f40a06867b Changes includes:
anders
parents:
diff changeset
55 int nch = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
56 int n = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
57 char* cp = NULL;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
58 int j,k;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
59 // Read number of outputs
d6f40a06867b Changes includes:
anders
parents:
diff changeset
60 sscanf((char*)arg,"%i%n", &nch,&n);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
61 if(AF_OK != control(af,AF_CONTROL_PAN_NOUT | AF_CONTROL_SET, &nch))
d6f40a06867b Changes includes:
anders
parents:
diff changeset
62 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
63
d6f40a06867b Changes includes:
anders
parents:
diff changeset
64 // Read pan values
d6f40a06867b Changes includes:
anders
parents:
diff changeset
65 cp = &((char*)arg)[n];
d6f40a06867b Changes includes:
anders
parents:
diff changeset
66 j = 0; k = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
67 while((*cp == ':') && (k < AF_NCH)){
16493
851d10933f27 Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents: 14433
diff changeset
68 sscanf(cp, ":%f%n" , &s->level[j][k], &n);
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
69 af_msg(AF_MSG_VERBOSE,"[pan] Pan level from channel %i to"
16493
851d10933f27 Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents: 14433
diff changeset
70 " channel %i = %f\n",k,j,s->level[j][k]);
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
71 cp =&cp[n];
d6f40a06867b Changes includes:
anders
parents:
diff changeset
72 j++;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
73 if(j>=nch){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
74 j = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
75 k++;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
76 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
77 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
78 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
79 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
80 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_SET:{
d6f40a06867b Changes includes:
anders
parents:
diff changeset
81 int i;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
82 int ch = ((af_control_ext_t*)arg)->ch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
83 float* level = ((af_control_ext_t*)arg)->arg;
16493
851d10933f27 Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents: 14433
diff changeset
84 if (ch >= AF_NCH)
851d10933f27 Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents: 14433
diff changeset
85 return AF_FALSE;
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
86 for(i=0;i<AF_NCH;i++)
16493
851d10933f27 Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents: 14433
diff changeset
87 s->level[ch][i] = level[i];
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
88 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
89 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
90 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_GET:{
d6f40a06867b Changes includes:
anders
parents:
diff changeset
91 int i;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
92 int ch = ((af_control_ext_t*)arg)->ch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
93 float* level = ((af_control_ext_t*)arg)->arg;
16493
851d10933f27 Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents: 14433
diff changeset
94 if (ch >= AF_NCH)
851d10933f27 Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents: 14433
diff changeset
95 return AF_FALSE;
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
96 for(i=0;i<AF_NCH;i++)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
97 level[i] = s->level[ch][i];
d6f40a06867b Changes includes:
anders
parents:
diff changeset
98 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
99 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
100 case AF_CONTROL_PAN_NOUT | AF_CONTROL_SET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
101 // Reinit must be called after this function has been called
d6f40a06867b Changes includes:
anders
parents:
diff changeset
102
d6f40a06867b Changes includes:
anders
parents:
diff changeset
103 // Sanity check
12008
8631b43c9d82 10l - replace 6 with AF_NCH
henry
parents: 8674
diff changeset
104 if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH){
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
105 af_msg(AF_MSG_ERROR,"[pan] The number of output channels must be"
d6f40a06867b Changes includes:
anders
parents:
diff changeset
106 " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
107 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
108 }
23532
a1eb547cf52e Avoid zero output for pan filter; zero output now means same # of channels
zuxy
parents: 23531
diff changeset
109 s->nch=((int*)arg)[0];
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
110 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
111 case AF_CONTROL_PAN_NOUT | AF_CONTROL_GET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
112 *(int*)arg = af->data->nch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
113 return AF_OK;
23551
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
114 case AF_CONTROL_PAN_BALANCE | AF_CONTROL_SET:{
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
115 float val = *(float*)arg;
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
116 if (s->nch)
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
117 return AF_ERROR;
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
118 if (af->data->nch >= 2) {
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
119 s->level[0][0] = min(1.f, 1.f - val);
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
120 s->level[0][1] = max(0.f, val);
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
121 s->level[1][0] = max(0.f, -val);
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
122 s->level[1][1] = min(1.f, 1.f + val);
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
123 }
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
124 return AF_OK;
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
125 }
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
126 case AF_CONTROL_PAN_BALANCE | AF_CONTROL_GET:
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
127 if (s->nch)
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
128 return AF_ERROR;
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
129 *(float*)arg = s->level[0][1] - s->level[1][0];
63d9b7032bf3 Add AF_CONTROL_PAN_BALANCE control
zuxy
parents: 23533
diff changeset
130 return AF_OK;
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
131 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
132 return AF_UNKNOWN;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
133 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
134
d6f40a06867b Changes includes:
anders
parents:
diff changeset
135 // Deallocate memory
d6f40a06867b Changes includes:
anders
parents:
diff changeset
136 static void uninit(struct af_instance_s* af)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
137 {
22175
105f32787336 Fix nonsense tests ("if (af->data->audio)" before "if (af->data)").
uau
parents: 16493
diff changeset
138 if(af->data)
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8607
diff changeset
139 free(af->data->audio);
22175
105f32787336 Fix nonsense tests ("if (af->data->audio)" before "if (af->data)").
uau
parents: 16493
diff changeset
140 free(af->data);
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
141 if(af->setup)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
142 free(af->setup);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
143 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
144
d6f40a06867b Changes includes:
anders
parents:
diff changeset
145 // Filter data through filter
d6f40a06867b Changes includes:
anders
parents:
diff changeset
146 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
147 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
148 af_data_t* c = data; // Current working data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
149 af_data_t* l = af->data; // Local data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
150 af_pan_t* s = af->setup; // Setup for this instance
d6f40a06867b Changes includes:
anders
parents:
diff changeset
151 float* in = c->audio; // Input audio data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
152 float* out = NULL; // Output audio data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
153 float* end = in+c->len/4; // End of loop
d6f40a06867b Changes includes:
anders
parents:
diff changeset
154 int nchi = c->nch; // Number of input channels
d6f40a06867b Changes includes:
anders
parents:
diff changeset
155 int ncho = l->nch; // Number of output channels
d6f40a06867b Changes includes:
anders
parents:
diff changeset
156 register int j,k;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
157
d6f40a06867b Changes includes:
anders
parents:
diff changeset
158 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
d6f40a06867b Changes includes:
anders
parents:
diff changeset
159 return NULL;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
160
d6f40a06867b Changes includes:
anders
parents:
diff changeset
161 out = l->audio;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
162 // Execute panning
d6f40a06867b Changes includes:
anders
parents:
diff changeset
163 // FIXME: Too slow
d6f40a06867b Changes includes:
anders
parents:
diff changeset
164 while(in < end){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
165 for(j=0;j<ncho;j++){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
166 register float x = 0.0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
167 register float* tin = in;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
168 for(k=0;k<nchi;k++)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
169 x += tin[k] * s->level[j][k];
d6f40a06867b Changes includes:
anders
parents:
diff changeset
170 out[j] = x;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
171 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
172 out+= ncho;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
173 in+= nchi;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
174 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
175
d6f40a06867b Changes includes:
anders
parents:
diff changeset
176 // Set output data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
177 c->audio = l->audio;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
178 c->len = (c->len*af->mul.n)/af->mul.d;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
179 c->nch = l->nch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
180
d6f40a06867b Changes includes:
anders
parents:
diff changeset
181 return c;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
182 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
183
d6f40a06867b Changes includes:
anders
parents:
diff changeset
184 // Allocate memory and set function pointers
22746
fd6f824ef894 Rename open to af_open so as not to conflict with a previous header definition.
diego
parents: 22175
diff changeset
185 static int af_open(af_instance_t* af){
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
186 af->control=control;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
187 af->uninit=uninit;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
188 af->play=play;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
189 af->mul.n=1;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
190 af->mul.d=1;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
191 af->data=calloc(1,sizeof(af_data_t));
d6f40a06867b Changes includes:
anders
parents:
diff changeset
192 af->setup=calloc(1,sizeof(af_pan_t));
d6f40a06867b Changes includes:
anders
parents:
diff changeset
193 if(af->data == NULL || af->setup == NULL)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
194 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
195 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
196 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
197
d6f40a06867b Changes includes:
anders
parents:
diff changeset
198 // Description of this filter
d6f40a06867b Changes includes:
anders
parents:
diff changeset
199 af_info_t af_info_pan = {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
200 "Panning audio filter",
d6f40a06867b Changes includes:
anders
parents:
diff changeset
201 "pan",
d6f40a06867b Changes includes:
anders
parents:
diff changeset
202 "Anders",
d6f40a06867b Changes includes:
anders
parents:
diff changeset
203 "",
23531
bd9e74cd4d3d Make pan reentrant. Multiple pans in chain work fine.
zuxy
parents: 22748
diff changeset
204 AF_FLAGS_REENTRANT,
22746
fd6f824ef894 Rename open to af_open so as not to conflict with a previous header definition.
diego
parents: 22175
diff changeset
205 af_open
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
206 };