Mercurial > mplayer.hg
annotate libaf/af_pan.c @ 15700:a398c21242bf
toolame/twolame typo, 10l
author | nicodvb |
---|---|
date | Thu, 09 Jun 2005 06:58:41 +0000 |
parents | 95bb94a930a3 |
children | 851d10933f27 |
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)){ | |
67 sscanf(cp, ":%f%n" , &s->level[k][j], &n); | |
68 s->level[k][j] = clamp(s->level[k][j],0.0,1.0); | |
69 af_msg(AF_MSG_VERBOSE,"[pan] Pan level from channel %i to" | |
70 " channel %i = %f\n",j,k,s->level[k][j]); | |
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; | |
84 for(i=0;i<AF_NCH;i++) | |
85 s->level[ch][i] = clamp(level[i],0.0,1.0); | |
86 return AF_OK; | |
87 } | |
88 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_GET:{ | |
89 int i; | |
90 int ch = ((af_control_ext_t*)arg)->ch; | |
91 float* level = ((af_control_ext_t*)arg)->arg; | |
92 for(i=0;i<AF_NCH;i++) | |
93 level[i] = s->level[ch][i]; | |
94 return AF_OK; | |
95 } | |
96 case AF_CONTROL_PAN_NOUT | AF_CONTROL_SET: | |
97 // Reinit must be called after this function has been called | |
98 | |
99 // Sanity check | |
12008 | 100 if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH){ |
8607 | 101 af_msg(AF_MSG_ERROR,"[pan] The number of output channels must be" |
102 " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]); | |
103 return AF_ERROR; | |
104 } | |
105 af->data->nch=((int*)arg)[0]; | |
106 return AF_OK; | |
107 case AF_CONTROL_PAN_NOUT | AF_CONTROL_GET: | |
108 *(int*)arg = af->data->nch; | |
109 return AF_OK; | |
110 } | |
111 return AF_UNKNOWN; | |
112 } | |
113 | |
114 // Deallocate memory | |
115 static void uninit(struct af_instance_s* af) | |
116 { | |
8674
93212da0032e
10l memory leak + bug fixes in ms to sample time conversion
anders
parents:
8607
diff
changeset
|
117 if(af->data->audio) |
93212da0032e
10l memory leak + bug fixes in ms to sample time conversion
anders
parents:
8607
diff
changeset
|
118 free(af->data->audio); |
8607 | 119 if(af->data) |
120 free(af->data); | |
121 if(af->setup) | |
122 free(af->setup); | |
123 } | |
124 | |
125 // Filter data through filter | |
126 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
127 { | |
128 af_data_t* c = data; // Current working data | |
129 af_data_t* l = af->data; // Local data | |
130 af_pan_t* s = af->setup; // Setup for this instance | |
131 float* in = c->audio; // Input audio data | |
132 float* out = NULL; // Output audio data | |
133 float* end = in+c->len/4; // End of loop | |
134 int nchi = c->nch; // Number of input channels | |
135 int ncho = l->nch; // Number of output channels | |
136 register int j,k; | |
137 | |
138 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
139 return NULL; | |
140 | |
141 out = l->audio; | |
142 // Execute panning | |
143 // FIXME: Too slow | |
144 while(in < end){ | |
145 for(j=0;j<ncho;j++){ | |
146 register float x = 0.0; | |
147 register float* tin = in; | |
148 for(k=0;k<nchi;k++) | |
149 x += tin[k] * s->level[j][k]; | |
150 out[j] = x; | |
151 } | |
152 out+= ncho; | |
153 in+= nchi; | |
154 } | |
155 | |
156 // Set output data | |
157 c->audio = l->audio; | |
158 c->len = (c->len*af->mul.n)/af->mul.d; | |
159 c->nch = l->nch; | |
160 | |
161 return c; | |
162 } | |
163 | |
164 // Allocate memory and set function pointers | |
165 static int open(af_instance_t* af){ | |
166 af->control=control; | |
167 af->uninit=uninit; | |
168 af->play=play; | |
169 af->mul.n=1; | |
170 af->mul.d=1; | |
171 af->data=calloc(1,sizeof(af_data_t)); | |
172 af->setup=calloc(1,sizeof(af_pan_t)); | |
173 if(af->data == NULL || af->setup == NULL) | |
174 return AF_ERROR; | |
175 // Set initial pan to pass-through. | |
176 return AF_OK; | |
177 } | |
178 | |
179 // Description of this filter | |
180 af_info_t af_info_pan = { | |
181 "Panning audio filter", | |
182 "pan", | |
183 "Anders", | |
184 "", | |
185 AF_FLAGS_NOT_REENTRANT, | |
186 open | |
187 }; |