annotate libaf/af_resample.c @ 8401:1b2fc92980d9

Runtime SSE detection for NEtBSD, patch by Nick Hudson <skrll at netbsd.org>
author atmos4
date Sat, 07 Dec 2002 21:04:54 +0000
parents e8832e66babd
children fb88ccbc5ccc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
1 /*=============================================================================
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
2 //
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
3 // This software has been released under the terms of the GNU Public
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
5 //
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
6 // Copyright 2002 Anders Johansson ajh@atri.curtin.edu.au
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
7 //
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
8 //=============================================================================
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
9 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
10
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
11 /* This audio filter changes the sample rate. */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
12 #include <stdio.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
13 #include <stdlib.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
14 #include <unistd.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
15 #include <inttypes.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
16
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
17 #include "af.h"
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
18 #include "dsp.h"
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
19
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
20 /* Below definition selects the length of each poly phase component.
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
21 Valid definitions are L8 and L16, where the number denotes the
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
22 length of the filter. This definition affects the computational
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
23 complexity (see play()), the performance (see filter.h) and the
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
24 memory usage. The filterlenght is choosen to 8 if the machine is
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
25 slow and to 16 if the machine is fast and has MMX.
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
26 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
27
7588
arpi
parents: 7587
diff changeset
28 #if !defined(HAVE_SSE) && !defined(HAVE_3DNOW) // This machine is slow
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
29
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
30 #define L 8 // Filter length
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
31 // Unrolled loop to speed up execution
7602
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
32 #define FIR(x,w,y) \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
33 (y[0]) = ( w[0]*x[0]+w[1]*x[1]+w[2]*x[2]+w[3]*x[3] \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
34 + w[4]*x[4]+w[5]*x[5]+w[6]*x[6]+w[7]*x[7] ) >> 16
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
35
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
36 #else /* Fast machine */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
37
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
38 #define L 16
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
39 // Unrolled loop to speed up execution
7602
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
40 #define FIR(x,w,y) \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
41 y[0] = ( w[0] *x[0] +w[1] *x[1] +w[2] *x[2] +w[3] *x[3] \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
42 + w[4] *x[4] +w[5] *x[5] +w[6] *x[6] +w[7] *x[7] \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
43 + w[8] *x[8] +w[9] *x[9] +w[10]*x[10]+w[11]*x[11] \
2c8dcba3d8bb optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents: 7588
diff changeset
44 + w[12]*x[12]+w[13]*x[13]+w[14]*x[14]+w[15]*x[15] ) >> 16
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
45
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
46 #endif /* Fast machine */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
47
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
48 // Macro to add data to circular que
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
49 #define ADDQUE(xi,xq,in)\
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
50 xq[xi]=xq[xi+L]=(*in);\
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
51 xi=(--xi)&(L-1);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
52
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
53
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
54
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
55 // local data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
56 typedef struct af_resample_s
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
57 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
58 int16_t* w; // Current filter weights
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
59 int16_t** xq; // Circular buffers
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
60 uint32_t xi; // Index for circular buffers
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
61 uint32_t wi; // Index for w
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
62 uint32_t i; // Number of new samples to put in x queue
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
63 uint32_t dn; // Down sampling factor
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
64 uint32_t up; // Up sampling factor
7998
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
65 int sloppy; // Enable sloppy resampling to reduce memory usage
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
66 int fast; // Enable linear interpolation instead of filtering
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
67 } af_resample_t;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
68
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
69 // Euclids algorithm for calculating Greatest Common Divisor GCD(a,b)
7894
3aba91eb5c1f made gcd() static
arpi
parents: 7719
diff changeset
70 static inline int gcd(register int a, register int b)
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
71 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
72 register int r = min(a,b);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
73 a=max(a,b);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
74 b=r;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
75
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
76 r=a%b;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
77 while(r!=0){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
78 a=b;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
79 b=r;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
80 r=a%b;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
81 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
82 return b;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
83 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
84
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
85 static int upsample(af_data_t* c,af_data_t* l, af_resample_t* s)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
86 {
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
87 uint32_t ci = l->nch; // Index for channels
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
88 uint32_t len = 0; // Number of input samples
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
89 uint32_t nch = l->nch; // Number of channels
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
90 uint32_t inc = s->up/s->dn;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
91 uint32_t level = s->up%s->dn;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
92 uint32_t up = s->up;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
93 uint32_t dn = s->dn;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
94
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
95 register int16_t* w = s->w;
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
96 register uint32_t wi = 0;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
97 register uint32_t xi = 0;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
98
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
99 // Index current channel
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
100 while(ci--){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
101 // Temporary pointers
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
102 register int16_t* x = s->xq[ci];
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
103 register int16_t* in = ((int16_t*)c->audio)+ci;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
104 register int16_t* out = ((int16_t*)l->audio)+ci;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
105 int16_t* end = in+c->len/2; // Block loop end
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
106 wi = s->wi; xi = s->xi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
107
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
108 while(in < end){
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
109 register uint32_t i = inc;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
110 if(wi<level) i++;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
111
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
112 ADDQUE(xi,x,in);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
113 in+=nch;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
114 while(i--){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
115 // Run the FIR filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
116 FIR((&x[xi]),(&w[wi*L]),out);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
117 len++; out+=nch;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
118 // Update wi to point at the correct polyphase component
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
119 wi=(wi+dn)%up;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
120 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
121 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
122 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
123 // Save values that needs to be kept for next time
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
124 s->wi = wi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
125 s->xi = xi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
126 return len;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
127 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
128
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
129 static int downsample(af_data_t* c,af_data_t* l, af_resample_t* s)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
130 {
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
131 uint32_t ci = l->nch; // Index for channels
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
132 uint32_t len = 0; // Number of output samples
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
133 uint32_t nch = l->nch; // Number of channels
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
134 uint32_t inc = s->dn/s->up;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
135 uint32_t level = s->dn%s->up;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
136 uint32_t up = s->up;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
137 uint32_t dn = s->dn;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
138
7587
arpi
parents: 7580
diff changeset
139 register int32_t i = 0;
7580
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
140 register uint32_t wi = 0;
255039c14525 Changing to 32 bit aritmetics for counters
anders
parents: 7571
diff changeset
141 register uint32_t xi = 0;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
142
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
143 // Index current channel
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
144 while(ci--){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
145 // Temporary pointers
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
146 register int16_t* x = s->xq[ci];
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
147 register int16_t* in = ((int16_t*)c->audio)+ci;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
148 register int16_t* out = ((int16_t*)l->audio)+ci;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
149 register int16_t* end = in+c->len/2; // Block loop end
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
150 i = s->i; wi = s->wi; xi = s->xi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
151
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
152 while(in < end){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
153
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
154 ADDQUE(xi,x,in);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
155 in+=nch;
7587
arpi
parents: 7580
diff changeset
156 if((--i)<=0){
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
157 // Run the FIR filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
158 FIR((&x[xi]),(&s->w[wi*L]),out);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
159 len++; out+=nch;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
160
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
161 // Update wi to point at the correct polyphase component
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
162 wi=(wi+dn)%up;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
163
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
164 // Insert i number of new samples in queue
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
165 i = inc;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
166 if(wi<level) i++;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
167 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
168 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
169 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
170 // Save values that needs to be kept for next time
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
171 s->wi = wi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
172 s->xi = xi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
173 s->i = i;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
174
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
175 return len;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
176 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
177
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
178 // Initialization and runtime control
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
179 static int control(struct af_instance_s* af, int cmd, void* arg)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
180 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
181 switch(cmd){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
182 case AF_CONTROL_REINIT:{
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
183 af_resample_t* s = (af_resample_t*)af->setup;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
184 af_data_t* n = (af_data_t*)arg; // New configureation
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
185 int i,d = 0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
186 int rv = AF_OK;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
187
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
188 // Make sure this filter isn't redundant
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
189 if(af->data->rate == n->rate)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
190 return AF_DETACH;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
191
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
192 // Create space for circular bufers (if nesessary)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
193 if(af->data->nch != n->nch){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
194 // First free the old ones
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
195 if(s->xq){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
196 for(i=1;i<af->data->nch;i++)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
197 if(s->xq[i])
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
198 free(s->xq[i]);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
199 free(s->xq);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
200 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
201 // ... then create new
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
202 s->xq = malloc(n->nch*sizeof(int16_t*));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
203 for(i=0;i<n->nch;i++)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
204 s->xq[i] = malloc(2*L*sizeof(int16_t));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
205 s->xi = 0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
206 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
207
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
208 // Set parameters
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
209 af->data->nch = n->nch;
8167
e8832e66babd New features:
anders
parents: 8071
diff changeset
210 af->data->format = AF_FORMAT_NE | AF_FORMAT_SI;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
211 af->data->bps = 2;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
212 if(af->data->format != n->format || af->data->bps != n->bps)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
213 rv = AF_FALSE;
8167
e8832e66babd New features:
anders
parents: 8071
diff changeset
214 n->format = AF_FORMAT_NE | AF_FORMAT_SI;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
215 n->bps = 2;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
216
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
217 // Calculate up and down sampling factors
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
218 d=gcd(af->data->rate,n->rate);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
219
7998
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
220 // If sloppy resampling is enabled limit the upsampling factor
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
221 if(s->sloppy && (af->data->rate/d > 5000)){
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
222 int up=af->data->rate/2;
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
223 int dn=n->rate/2;
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
224 int m=2;
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
225 while(af->data->rate/(d*m) > 5000){
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
226 d=gcd(up,dn);
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
227 up/=2; dn/=2; m*=2;
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
228 }
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
229 d*=m;
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
230 }
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
231
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
232 // Check if the the design needs to be redone
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
233 if(s->up != af->data->rate/d || s->dn != n->rate/d){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
234 float* w;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
235 float* wt;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
236 float fc;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
237 int j;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
238 s->up = af->data->rate/d;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
239 s->dn = n->rate/d;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
240
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
241 // Calculate cuttof frequency for filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
242 fc = 1/(float)(max(s->up,s->dn));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
243 // Allocate space for polyphase filter bank and protptype filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
244 w = malloc(sizeof(float) * s->up *L);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
245 if(NULL != s->w)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
246 free(s->w);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
247 s->w = malloc(L*s->up*sizeof(int16_t));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
248
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
249 // Design prototype filter type using Kaiser window with beta = 10
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
250 if(NULL == w || NULL == s->w ||
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
251 -1 == design_fir(s->up*L, w, &fc, LP|KAISER , 10.0)){
8167
e8832e66babd New features:
anders
parents: 8071
diff changeset
252 af_msg(AF_MSG_ERROR,"[resample] Unable to design prototype filter.\n");
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
253 return AF_ERROR;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
254 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
255 // Copy data from prototype to polyphase filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
256 wt=w;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
257 for(j=0;j<L;j++){//Columns
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
258 for(i=0;i<s->up;i++){//Rows
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
259 float t=(float)s->up*32767.0*(*wt);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
260 s->w[i*L+j] = (int16_t)((t>=0.0)?(t+0.5):(t-0.5));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
261 wt++;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
262 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
263 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
264 free(w);
8167
e8832e66babd New features:
anders
parents: 8071
diff changeset
265 af_msg(AF_MSG_VERBOSE,"[resample] New filter designed up: %i down: %i\n", s->up, s->dn);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
266 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
267
7665
fbd5445cc853 Adding function for calculating the delay caused by the filters
anders
parents: 7616
diff changeset
268 // Set multiplier and delay
fbd5445cc853 Adding function for calculating the delay caused by the filters
anders
parents: 7616
diff changeset
269 af->delay = (double)(1000*L/2)/((double)n->rate);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
270 af->mul.n = s->up;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
271 af->mul.d = s->dn;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
272 return rv;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
273 }
7998
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
274 case AF_CONTROL_COMMAND_LINE:{
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
275 af_resample_t* s = (af_resample_t*)af->setup;
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
276 int rate=0;
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
277 sscanf((char*)arg,"%i:%i:%i",&rate,&(s->sloppy), &(s->fast));
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
278 return af->control(af,AF_CONTROL_RESAMPLE,&rate);
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7894
diff changeset
279 }
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
280 case AF_CONTROL_RESAMPLE:
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
281 // Reinit must be called after this function has been called
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
282
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
283 // Sanity check
7616
d09c125a88fa Changing frequency limit for resampling
anders
parents: 7615
diff changeset
284 if(((int*)arg)[0] < 8000 || ((int*)arg)[0] > 192000){
8167
e8832e66babd New features:
anders
parents: 8071
diff changeset
285 af_msg(AF_MSG_ERROR,"[resample] The output sample frequency must be between 8kHz and 192kHz. Current value is %i \n",((int*)arg)[0]);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
286 return AF_ERROR;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
287 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
288
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
289 af->data->rate=((int*)arg)[0];
8167
e8832e66babd New features:
anders
parents: 8071
diff changeset
290 af_msg(AF_MSG_VERBOSE,"[resample] Changing sample rate to %iHz\n",af->data->rate);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
291 return AF_OK;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
292 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
293 return AF_UNKNOWN;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
294 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
295
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
296 // Deallocate memory
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
297 static void uninit(struct af_instance_s* af)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
298 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
299 if(af->data)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
300 free(af->data);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
301 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
302
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
303 // Filter data through filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
304 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
305 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
306 int len = 0; // Length of output data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
307 af_data_t* c = data; // Current working data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
308 af_data_t* l = af->data; // Local data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
309 af_resample_t* s = (af_resample_t*)af->setup;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
310
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
311 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
312 return NULL;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
313
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
314 // Run resampling
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
315 if(s->up>s->dn)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
316 len = upsample(c,l,s);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
317 else
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
318 len = downsample(c,l,s);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
319
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
320 // Set output data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
321 c->audio = l->audio;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
322 c->len = len*2;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
323 c->rate = l->rate;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
324
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
325 return c;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
326 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
327
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
328 // Allocate memory and set function pointers
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
329 static int open(af_instance_t* af){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
330 af->control=control;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
331 af->uninit=uninit;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
332 af->play=play;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
333 af->mul.n=1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
334 af->mul.d=1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
335 af->data=calloc(1,sizeof(af_data_t));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
336 af->setup=calloc(1,sizeof(af_resample_t));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
337 if(af->data == NULL || af->setup == NULL)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
338 return AF_ERROR;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
339 return AF_OK;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
340 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
341
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
342 // Description of this plugin
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
343 af_info_t af_info_resample = {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
344 "Sample frequency conversion",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
345 "resample",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
346 "Anders",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
347 "",
7615
c67328dd459a Adding Support for non-reentrant audio filters
anders
parents: 7602
diff changeset
348 AF_FLAGS_REENTRANT,
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
349 open
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
350 };
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
351