Mercurial > mplayer.hg
annotate libaf/af_pan.c @ 23537:0755e053255d
update wishlist
author | compn |
---|---|
date | Fri, 15 Jun 2007 11:59:14 +0000 |
parents | cdc8f1aeb2e2 |
children | 63d9b7032bf3 |
rev | line source |
---|---|
8607 | 1 /*============================================================================= |
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 | 4 // license. See http://www.gnu.org/copyleft/gpl.html for details. |
5 // | |
6 // Copyright 2002 Anders Johansson ajh@atri.curtin.edu.au | |
7 // | |
8 //============================================================================= | |
9 */ | |
10 | |
11 /* */ | |
12 | |
13 #include <stdio.h> | |
14 #include <stdlib.h> | |
15 | |
16 #include <inttypes.h> | |
17 #include <math.h> | |
18 #include <limits.h> | |
19 | |
20 #include "af.h" | |
21 | |
22 // Data for specific instances of this filter | |
23 typedef struct af_pan_s | |
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 | 26 float level[AF_NCH][AF_NCH]; // Gain level for each channel |
27 }af_pan_t; | |
28 | |
29 // Initialization and runtime control | |
30 static int control(struct af_instance_s* af, int cmd, void* arg) | |
31 { | |
32 af_pan_t* s = af->setup; | |
33 | |
34 switch(cmd){ | |
35 case AF_CONTROL_REINIT: | |
36 // Sanity check | |
37 if(!arg) return AF_ERROR; | |
38 | |
39 af->data->rate = ((af_data_t*)arg)->rate; | |
14245 | 40 af->data->format = AF_FORMAT_FLOAT_NE; |
8607 | 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 | 43 af->mul.n = af->data->nch; |
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 | 46 |
47 if((af->data->format != ((af_data_t*)arg)->format) || | |
48 (af->data->bps != ((af_data_t*)arg)->bps)){ | |
49 ((af_data_t*)arg)->format = af->data->format; | |
50 ((af_data_t*)arg)->bps = af->data->bps; | |
51 return AF_FALSE; | |
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 | 54 case AF_CONTROL_COMMAND_LINE:{ |
55 int nch = 0; | |
56 int n = 0; | |
57 char* cp = NULL; | |
58 int j,k; | |
59 // Read number of outputs | |
60 sscanf((char*)arg,"%i%n", &nch,&n); | |
61 if(AF_OK != control(af,AF_CONTROL_PAN_NOUT | AF_CONTROL_SET, &nch)) | |
62 return AF_ERROR; | |
63 | |
64 // Read pan values | |
65 cp = &((char*)arg)[n]; | |
66 j = 0; k = 0; | |
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 | 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 | 71 cp =&cp[n]; |
72 j++; | |
73 if(j>=nch){ | |
74 j = 0; | |
75 k++; | |
76 } | |
77 } | |
78 return AF_OK; | |
79 } | |
80 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_SET:{ | |
81 int i; | |
82 int ch = ((af_control_ext_t*)arg)->ch; | |
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 | 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 | 88 return AF_OK; |
89 } | |
90 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_GET:{ | |
91 int i; | |
92 int ch = ((af_control_ext_t*)arg)->ch; | |
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 | 96 for(i=0;i<AF_NCH;i++) |
97 level[i] = s->level[ch][i]; | |
98 return AF_OK; | |
99 } | |
100 case AF_CONTROL_PAN_NOUT | AF_CONTROL_SET: | |
101 // Reinit must be called after this function has been called | |
102 | |
103 // Sanity check | |
12008 | 104 if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH){ |
8607 | 105 af_msg(AF_MSG_ERROR,"[pan] The number of output channels must be" |
106 " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]); | |
107 return AF_ERROR; | |
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 | 110 return AF_OK; |
111 case AF_CONTROL_PAN_NOUT | AF_CONTROL_GET: | |
112 *(int*)arg = af->data->nch; | |
113 return AF_OK; | |
114 } | |
115 return AF_UNKNOWN; | |
116 } | |
117 | |
118 // Deallocate memory | |
119 static void uninit(struct af_instance_s* af) | |
120 { | |
22175
105f32787336
Fix nonsense tests ("if (af->data->audio)" before "if (af->data)").
uau
parents:
16493
diff
changeset
|
121 if(af->data) |
8674
93212da0032e
10l memory leak + bug fixes in ms to sample time conversion
anders
parents:
8607
diff
changeset
|
122 free(af->data->audio); |
22175
105f32787336
Fix nonsense tests ("if (af->data->audio)" before "if (af->data)").
uau
parents:
16493
diff
changeset
|
123 free(af->data); |
8607 | 124 if(af->setup) |
125 free(af->setup); | |
126 } | |
127 | |
128 // Filter data through filter | |
129 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
130 { | |
131 af_data_t* c = data; // Current working data | |
132 af_data_t* l = af->data; // Local data | |
133 af_pan_t* s = af->setup; // Setup for this instance | |
134 float* in = c->audio; // Input audio data | |
135 float* out = NULL; // Output audio data | |
136 float* end = in+c->len/4; // End of loop | |
137 int nchi = c->nch; // Number of input channels | |
138 int ncho = l->nch; // Number of output channels | |
139 register int j,k; | |
140 | |
141 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
142 return NULL; | |
143 | |
144 out = l->audio; | |
145 // Execute panning | |
146 // FIXME: Too slow | |
147 while(in < end){ | |
148 for(j=0;j<ncho;j++){ | |
149 register float x = 0.0; | |
150 register float* tin = in; | |
151 for(k=0;k<nchi;k++) | |
152 x += tin[k] * s->level[j][k]; | |
153 out[j] = x; | |
154 } | |
155 out+= ncho; | |
156 in+= nchi; | |
157 } | |
158 | |
159 // Set output data | |
160 c->audio = l->audio; | |
161 c->len = (c->len*af->mul.n)/af->mul.d; | |
162 c->nch = l->nch; | |
163 | |
164 return c; | |
165 } | |
166 | |
167 // 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
|
168 static int af_open(af_instance_t* af){ |
8607 | 169 af->control=control; |
170 af->uninit=uninit; | |
171 af->play=play; | |
172 af->mul.n=1; | |
173 af->mul.d=1; | |
174 af->data=calloc(1,sizeof(af_data_t)); | |
175 af->setup=calloc(1,sizeof(af_pan_t)); | |
176 if(af->data == NULL || af->setup == NULL) | |
177 return AF_ERROR; | |
178 return AF_OK; | |
179 } | |
180 | |
181 // Description of this filter | |
182 af_info_t af_info_pan = { | |
183 "Panning audio filter", | |
184 "pan", | |
185 "Anders", | |
186 "", | |
23531
bd9e74cd4d3d
Make pan reentrant. Multiple pans in chain work fine.
zuxy
parents:
22748
diff
changeset
|
187 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
|
188 af_open |
8607 | 189 }; |