8607
|
1 /*=============================================================================
|
|
2 //
|
|
3 // This software has been released under the terms of the GNU Public
|
|
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;
|
|
40 af->data->format = AF_FORMAT_F | AF_FORMAT_NE;
|
|
41 af->data->bps = 4;
|
|
42 af->mul.n = af->data->nch;
|
|
43 af->mul.d = ((af_data_t*)arg)->nch;
|
|
44
|
|
45 if((af->data->format != ((af_data_t*)arg)->format) ||
|
|
46 (af->data->bps != ((af_data_t*)arg)->bps)){
|
|
47 ((af_data_t*)arg)->format = af->data->format;
|
|
48 ((af_data_t*)arg)->bps = af->data->bps;
|
|
49 return AF_FALSE;
|
|
50 }
|
|
51 return control(af,AF_CONTROL_PAN_NOUT | AF_CONTROL_SET, &af->data->nch);
|
|
52 case AF_CONTROL_COMMAND_LINE:{
|
|
53 int nch = 0;
|
|
54 int n = 0;
|
|
55 char* cp = NULL;
|
|
56 int j,k;
|
|
57 // Read number of outputs
|
|
58 sscanf((char*)arg,"%i%n", &nch,&n);
|
|
59 if(AF_OK != control(af,AF_CONTROL_PAN_NOUT | AF_CONTROL_SET, &nch))
|
|
60 return AF_ERROR;
|
|
61
|
|
62 // Read pan values
|
|
63 cp = &((char*)arg)[n];
|
|
64 j = 0; k = 0;
|
|
65 while((*cp == ':') && (k < AF_NCH)){
|
|
66 sscanf(cp, ":%f%n" , &s->level[k][j], &n);
|
|
67 s->level[k][j] = clamp(s->level[k][j],0.0,1.0);
|
|
68 af_msg(AF_MSG_VERBOSE,"[pan] Pan level from channel %i to"
|
|
69 " channel %i = %f\n",j,k,s->level[k][j]);
|
|
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;
|
|
83 for(i=0;i<AF_NCH;i++)
|
|
84 s->level[ch][i] = clamp(level[i],0.0,1.0);
|
|
85 return AF_OK;
|
|
86 }
|
|
87 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_GET:{
|
|
88 int i;
|
|
89 int ch = ((af_control_ext_t*)arg)->ch;
|
|
90 float* level = ((af_control_ext_t*)arg)->arg;
|
|
91 for(i=0;i<AF_NCH;i++)
|
|
92 level[i] = s->level[ch][i];
|
|
93 return AF_OK;
|
|
94 }
|
|
95 case AF_CONTROL_PAN_NOUT | AF_CONTROL_SET:
|
|
96 // Reinit must be called after this function has been called
|
|
97
|
|
98 // Sanity check
|
|
99 if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > 6){
|
|
100 af_msg(AF_MSG_ERROR,"[pan] The number of output channels must be"
|
|
101 " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]);
|
|
102 return AF_ERROR;
|
|
103 }
|
|
104 af->data->nch=((int*)arg)[0];
|
|
105 return AF_OK;
|
|
106 case AF_CONTROL_PAN_NOUT | AF_CONTROL_GET:
|
|
107 *(int*)arg = af->data->nch;
|
|
108 return AF_OK;
|
|
109 }
|
|
110 return AF_UNKNOWN;
|
|
111 }
|
|
112
|
|
113 // Deallocate memory
|
|
114 static void uninit(struct af_instance_s* af)
|
|
115 {
|
|
116 if(af->data)
|
|
117 free(af->data);
|
|
118 if(af->setup)
|
|
119 free(af->setup);
|
|
120 }
|
|
121
|
|
122 // Filter data through filter
|
|
123 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
|
|
124 {
|
|
125 af_data_t* c = data; // Current working data
|
|
126 af_data_t* l = af->data; // Local data
|
|
127 af_pan_t* s = af->setup; // Setup for this instance
|
|
128 float* in = c->audio; // Input audio data
|
|
129 float* out = NULL; // Output audio data
|
|
130 float* end = in+c->len/4; // End of loop
|
|
131 int nchi = c->nch; // Number of input channels
|
|
132 int ncho = l->nch; // Number of output channels
|
|
133 register int j,k;
|
|
134
|
|
135 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
|
|
136 return NULL;
|
|
137
|
|
138 out = l->audio;
|
|
139 // Execute panning
|
|
140 // FIXME: Too slow
|
|
141 while(in < end){
|
|
142 for(j=0;j<ncho;j++){
|
|
143 register float x = 0.0;
|
|
144 register float* tin = in;
|
|
145 for(k=0;k<nchi;k++)
|
|
146 x += tin[k] * s->level[j][k];
|
|
147 out[j] = x;
|
|
148 }
|
|
149 out+= ncho;
|
|
150 in+= nchi;
|
|
151 }
|
|
152
|
|
153 // Set output data
|
|
154 c->audio = l->audio;
|
|
155 c->len = (c->len*af->mul.n)/af->mul.d;
|
|
156 c->nch = l->nch;
|
|
157
|
|
158 return c;
|
|
159 }
|
|
160
|
|
161 // Allocate memory and set function pointers
|
|
162 static int open(af_instance_t* af){
|
|
163 af->control=control;
|
|
164 af->uninit=uninit;
|
|
165 af->play=play;
|
|
166 af->mul.n=1;
|
|
167 af->mul.d=1;
|
|
168 af->data=calloc(1,sizeof(af_data_t));
|
|
169 af->setup=calloc(1,sizeof(af_pan_t));
|
|
170 if(af->data == NULL || af->setup == NULL)
|
|
171 return AF_ERROR;
|
|
172 // Set initial pan to pass-through.
|
|
173 return AF_OK;
|
|
174 }
|
|
175
|
|
176 // Description of this filter
|
|
177 af_info_t af_info_pan = {
|
|
178 "Panning audio filter",
|
|
179 "pan",
|
|
180 "Anders",
|
|
181 "",
|
|
182 AF_FLAGS_NOT_REENTRANT,
|
|
183 open
|
|
184 };
|