Mercurial > mplayer.hg
annotate libaf/af_pan.c @ 22345:a50a835635c7
grammar/spelling
author | diego |
---|---|
date | Tue, 27 Feb 2007 17:06:36 +0000 |
parents | 105f32787336 |
children | fd6f824ef894 |
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 <unistd.h> | |
17 #include <inttypes.h> | |
18 #include <math.h> | |
19 #include <limits.h> | |
20 | |
21 #include "af.h" | |
22 | |
23 // Data for specific instances of this filter | |
24 typedef struct af_pan_s | |
25 { | |
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; |
42 af->mul.n = af->data->nch; | |
43 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
|
44 af_frac_cancel(&af->mul); |
8607 | 45 |
46 if((af->data->format != ((af_data_t*)arg)->format) || | |
47 (af->data->bps != ((af_data_t*)arg)->bps)){ | |
48 ((af_data_t*)arg)->format = af->data->format; | |
49 ((af_data_t*)arg)->bps = af->data->bps; | |
50 return AF_FALSE; | |
51 } | |
52 return control(af,AF_CONTROL_PAN_NOUT | AF_CONTROL_SET, &af->data->nch); | |
53 case AF_CONTROL_COMMAND_LINE:{ | |
54 int nch = 0; | |
55 int n = 0; | |
56 char* cp = NULL; | |
57 int j,k; | |
58 // Read number of outputs | |
59 sscanf((char*)arg,"%i%n", &nch,&n); | |
60 if(AF_OK != control(af,AF_CONTROL_PAN_NOUT | AF_CONTROL_SET, &nch)) | |
61 return AF_ERROR; | |
62 | |
63 // Read pan values | |
64 cp = &((char*)arg)[n]; | |
65 j = 0; k = 0; | |
66 while((*cp == ':') && (k < AF_NCH)){ | |
16493
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
67 sscanf(cp, ":%f%n" , &s->level[j][k], &n); |
8607 | 68 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
|
69 " channel %i = %f\n",k,j,s->level[j][k]); |
8607 | 70 cp =&cp[n]; |
71 j++; | |
72 if(j>=nch){ | |
73 j = 0; | |
74 k++; | |
75 } | |
76 } | |
77 return AF_OK; | |
78 } | |
79 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_SET:{ | |
80 int i; | |
81 int ch = ((af_control_ext_t*)arg)->ch; | |
82 float* level = ((af_control_ext_t*)arg)->arg; | |
16493
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
83 if (ch >= AF_NCH) |
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
84 return AF_FALSE; |
8607 | 85 for(i=0;i<AF_NCH;i++) |
16493
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
86 s->level[ch][i] = level[i]; |
8607 | 87 return AF_OK; |
88 } | |
89 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_GET:{ | |
90 int i; | |
91 int ch = ((af_control_ext_t*)arg)->ch; | |
92 float* level = ((af_control_ext_t*)arg)->arg; | |
16493
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
93 if (ch >= AF_NCH) |
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
94 return AF_FALSE; |
8607 | 95 for(i=0;i<AF_NCH;i++) |
96 level[i] = s->level[ch][i]; | |
97 return AF_OK; | |
98 } | |
99 case AF_CONTROL_PAN_NOUT | AF_CONTROL_SET: | |
100 // Reinit must be called after this function has been called | |
101 | |
102 // Sanity check | |
12008 | 103 if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH){ |
8607 | 104 af_msg(AF_MSG_ERROR,"[pan] The number of output channels must be" |
105 " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]); | |
106 return AF_ERROR; | |
107 } | |
108 af->data->nch=((int*)arg)[0]; | |
109 return AF_OK; | |
110 case AF_CONTROL_PAN_NOUT | AF_CONTROL_GET: | |
111 *(int*)arg = af->data->nch; | |
112 return AF_OK; | |
113 } | |
114 return AF_UNKNOWN; | |
115 } | |
116 | |
117 // Deallocate memory | |
118 static void uninit(struct af_instance_s* af) | |
119 { | |
22175
105f32787336
Fix nonsense tests ("if (af->data->audio)" before "if (af->data)").
uau
parents:
16493
diff
changeset
|
120 if(af->data) |
8674
93212da0032e
10l memory leak + bug fixes in ms to sample time conversion
anders
parents:
8607
diff
changeset
|
121 free(af->data->audio); |
22175
105f32787336
Fix nonsense tests ("if (af->data->audio)" before "if (af->data)").
uau
parents:
16493
diff
changeset
|
122 free(af->data); |
8607 | 123 if(af->setup) |
124 free(af->setup); | |
125 } | |
126 | |
127 // Filter data through filter | |
128 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
129 { | |
130 af_data_t* c = data; // Current working data | |
131 af_data_t* l = af->data; // Local data | |
132 af_pan_t* s = af->setup; // Setup for this instance | |
133 float* in = c->audio; // Input audio data | |
134 float* out = NULL; // Output audio data | |
135 float* end = in+c->len/4; // End of loop | |
136 int nchi = c->nch; // Number of input channels | |
137 int ncho = l->nch; // Number of output channels | |
138 register int j,k; | |
139 | |
140 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
141 return NULL; | |
142 | |
143 out = l->audio; | |
144 // Execute panning | |
145 // FIXME: Too slow | |
146 while(in < end){ | |
147 for(j=0;j<ncho;j++){ | |
148 register float x = 0.0; | |
149 register float* tin = in; | |
150 for(k=0;k<nchi;k++) | |
151 x += tin[k] * s->level[j][k]; | |
152 out[j] = x; | |
153 } | |
154 out+= ncho; | |
155 in+= nchi; | |
156 } | |
157 | |
158 // Set output data | |
159 c->audio = l->audio; | |
160 c->len = (c->len*af->mul.n)/af->mul.d; | |
161 c->nch = l->nch; | |
162 | |
163 return c; | |
164 } | |
165 | |
166 // Allocate memory and set function pointers | |
167 static int open(af_instance_t* af){ | |
168 af->control=control; | |
169 af->uninit=uninit; | |
170 af->play=play; | |
171 af->mul.n=1; | |
172 af->mul.d=1; | |
173 af->data=calloc(1,sizeof(af_data_t)); | |
174 af->setup=calloc(1,sizeof(af_pan_t)); | |
175 if(af->data == NULL || af->setup == NULL) | |
176 return AF_ERROR; | |
177 // Set initial pan to pass-through. | |
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 "", | |
187 AF_FLAGS_NOT_REENTRANT, | |
188 open | |
189 }; |