3631
|
1 /*=============================================================================
|
|
2 //
|
4049
|
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.
|
3631
|
5 //
|
|
6 // Copyright 2001 Anders Johansson ajh@atri.curtin.edu.au
|
|
7 //
|
|
8 //=============================================================================
|
|
9 */
|
|
10
|
|
11 /* This audio output plugin changes the sample rate. The output
|
|
12 samplerate from this plugin is specified by using the switch
|
|
13 `fout=F' where F is the desired output sample frequency
|
|
14 */
|
|
15
|
|
16 #define PLUGIN
|
|
17
|
|
18 #include <stdio.h>
|
|
19 #include <stdlib.h>
|
|
20 #include <unistd.h>
|
|
21 #include <inttypes.h>
|
|
22
|
|
23 #include "audio_out.h"
|
|
24 #include "audio_plugin.h"
|
|
25 #include "audio_plugin_internal.h"
|
|
26 #include "afmt.h"
|
|
27 //#include "../config.h"
|
|
28
|
|
29 static ao_info_t info =
|
|
30 {
|
|
31 "Sample frequency conversion audio plugin",
|
|
32 "resample",
|
|
33 "Anders",
|
|
34 ""
|
|
35 };
|
|
36
|
|
37 LIBAO_PLUGIN_EXTERN(resample)
|
|
38
|
|
39 #define min(a,b) (((a) < (b)) ? (a) : (b))
|
|
40 #define max(a,b) (((a) > (b)) ? (a) : (b))
|
|
41
|
|
42 /* Below definition selects the length of each poly phase component.
|
|
43 Valid definitions are L4 and L8, where the number denotes the
|
|
44 length of the filter. This definition affects the computational
|
|
45 complexity (see play()), the performance (see filter.h) and the
|
|
46 memory usage. For now the filterlenght is choosen to 4 and without
|
|
47 assembly optimization if no SSE is present.
|
|
48 */
|
|
49 #ifdef HAVE_SSE
|
|
50 #define L8 1 // Filter bank type
|
|
51 #define W W8 // Filter bank parameters
|
|
52 #define L 8 // Filter length
|
|
53 #else
|
|
54 #define L4 1
|
|
55 #define W W4
|
|
56 #define L 4
|
|
57 #endif
|
|
58
|
|
59 #define CH 6 // Max number of channels
|
|
60 #define UP 128 /* Up sampling factor. Increasing this value will
|
|
61 improve frequency accuracy. Think about the L1
|
|
62 cashing of filter parameters - how big can it be? */
|
|
63
|
|
64 #include "fir.h"
|
|
65 #include "filter.h"
|
|
66
|
|
67 // local data
|
|
68 typedef struct pl_resample_s
|
|
69 {
|
|
70 int16_t* data; // Data buffer
|
|
71 int16_t* w; // Current filter weights
|
|
72 uint16_t dn; // Down sampling factor
|
|
73 uint16_t up; // Up sampling factor
|
|
74 int channels; // Number of channels
|
|
75 int len; // Lenght of buffer
|
|
76 int bypass; // Bypass this plugin
|
|
77 int16_t ws[UP*L]; // List of all available filters
|
|
78 int16_t xs[CH][L*2]; // Circular buffers
|
|
79 } pl_resample_t;
|
|
80
|
|
81 static pl_resample_t pl_resample = {NULL,NULL,1,1,1,0,0,W};
|
|
82
|
|
83 // to set/get/query special features/parameters
|
|
84 static int control(int cmd,int arg){
|
|
85 switch(cmd){
|
|
86 case AOCONTROL_PLUGIN_SET_LEN:
|
|
87 if(pl_resample.data)
|
|
88 free(pl_resample.data);
|
|
89 pl_resample.len = ao_plugin_data.len;
|
|
90 pl_resample.data=(int16_t*)malloc(pl_resample.len);
|
|
91 if(!pl_resample.data)
|
|
92 return CONTROL_ERROR;
|
|
93 ao_plugin_data.len = (int)((double)ao_plugin_data.len *
|
|
94 ((double)pl_resample.up)/
|
|
95 ((double)pl_resample.dn));
|
|
96 return CONTROL_OK;
|
|
97 }
|
|
98 return -1;
|
|
99 }
|
|
100
|
|
101 // open & setup audio device
|
|
102 // return: 1=success 0=fail
|
|
103 static int init(){
|
|
104 int fin=ao_plugin_data.rate;
|
|
105 int fout=ao_plugin_cfg.pl_resample_fout;
|
|
106 pl_resample.w=pl_resample.ws;
|
|
107 pl_resample.up=UP;
|
|
108
|
|
109 // Sheck input format
|
|
110 if(ao_plugin_data.format != AFMT_S16_LE){
|
|
111 fprintf(stderr,"[pl_resample] Input audio format not yet suported. \n");
|
|
112 return 0;
|
|
113 }
|
|
114 // Sanity check and calculate down sampling factor
|
|
115 if((float)max(fin,fout)/(float)min(fin,fout) > 10){
|
|
116 fprintf(stderr,"[pl_resample] The difference between fin and fout is too large.\n");
|
|
117 return 0;
|
|
118 }
|
|
119 pl_resample.dn=(int)(0.5+((float)(fin*pl_resample.up))/((float)fout));
|
|
120 if(pl_resample.dn == pl_resample.up){
|
|
121 fprintf(stderr,"[pl_resample] Fin is too close to fout no conversion is needed.\n");
|
|
122 pl_resample.bypass=1;
|
|
123 return 1;
|
|
124 }
|
|
125 pl_resample.channels=ao_plugin_data.channels;
|
|
126 if(ao_plugin_data.channels>CH){
|
|
127 fprintf(stderr,"[pl_resample] Too many channels, max is 6.\n");
|
|
128 return 0;
|
|
129 }
|
|
130
|
|
131 // Tell the world what we are up to
|
|
132 printf("[pl_resample] Up=%i, Down=%i, True fout=%f\n",
|
|
133 pl_resample.up,pl_resample.dn,
|
|
134 ((float)fin*pl_resample.up)/((float)pl_resample.dn));
|
|
135
|
|
136 // This plugin changes buffersize and adds some delay
|
|
137 ao_plugin_data.sz_mult/=((float)pl_resample.up)/((float)pl_resample.dn);
|
|
138 ao_plugin_data.delay_fix-= ((float)L/2) * (1/fout);
|
|
139 ao_plugin_data.rate=fout;
|
|
140 return 1;
|
|
141 }
|
|
142
|
|
143 // close plugin
|
|
144 static void uninit(){
|
|
145 if(pl_resample.data)
|
|
146 free(pl_resample.data);
|
|
147 pl_resample.data=NULL;
|
|
148 }
|
|
149
|
|
150 // empty buffers
|
|
151 static void reset(){
|
|
152 }
|
|
153
|
|
154 // processes 'ao_plugin_data.len' bytes of 'data'
|
|
155 // called for every block of data
|
|
156 // FIXME: this routine needs to be optimized (it is probably possible to do a lot here)
|
|
157 static int play(){
|
|
158 static uint16_t pwi = 0; // Index for w
|
|
159 static uint16_t pxi = 0; // Index for circular queue
|
|
160 static uint16_t pi = 1; // Number of new samples to put in x queue
|
|
161
|
|
162 uint16_t ci = pl_resample.channels; // Index for channels
|
|
163 uint16_t len = 0; // Number of output samples
|
|
164 uint16_t nch = pl_resample.channels; // Number of channels
|
|
165 uint16_t inc = pl_resample.dn/pl_resample.up;
|
|
166 uint16_t level = pl_resample.dn%pl_resample.up;
|
|
167 uint16_t up = pl_resample.up;
|
|
168 uint16_t dn = pl_resample.dn;
|
|
169
|
|
170 register uint16_t i,wi,xi; // Temporary indexes
|
|
171
|
|
172 if(pl_resample.bypass)
|
|
173 return 1;
|
|
174
|
|
175 // Index current channel
|
|
176 while(ci--){
|
|
177 // Temporary pointers
|
|
178 register int16_t* x = pl_resample.xs[ci];
|
|
179 register int16_t* in = ((int16_t*)ao_plugin_data.data)+ci;
|
|
180 register int16_t* out = pl_resample.data+ci;
|
|
181 // Block loop end
|
|
182 register int16_t* end = in+ao_plugin_data.len/2;
|
|
183 i = pi; wi = pwi; xi = pxi;
|
|
184
|
|
185 LOAD_QUE(x);
|
|
186 if(0!=i) goto L1;
|
|
187 while(in < end){
|
|
188 // Update wi to point at the correct polyphase component
|
|
189 wi=(wi+dn)%up;
|
|
190
|
|
191 /* Update circular buffer x. This loop will be updated 0 or 1 time
|
|
192 for upsamling and inc or inc + 1 times for downsampling */
|
|
193 if(wi<level) goto L3;
|
|
194 if(0==i) goto L2;
|
|
195 L1: i--;
|
|
196 L3: UPDATE_QUE(in);
|
|
197 in+=nch;
|
|
198 if(in >= end) goto L2;
|
|
199 if(i) goto L1;
|
|
200 L2: if(i) goto L5;
|
|
201 i=inc;
|
|
202
|
|
203 /* Get the correct polyphase component and the correct startpoint
|
|
204 in the circular bufer and run the FIR filter */
|
|
205 FIR((&x[xi]),(&pl_resample.w[wi*L]),out);
|
|
206 len++;
|
|
207 out+=nch;
|
|
208 }
|
|
209 L5:
|
|
210 SAVE_QUE(x);
|
|
211 }
|
|
212
|
|
213 // Save values that needs to be kept for next time
|
|
214 pwi = wi;
|
|
215 pxi = xi;
|
|
216 pi = i;
|
|
217 // Set new data
|
|
218 ao_plugin_data.len=len*2;
|
|
219 ao_plugin_data.data=pl_resample.data;
|
|
220 return 1;
|
|
221 }
|
|
222
|
|
223
|
|
224
|
|
225
|
|
226
|