1 #include "stdio.h"
2 #ifndef mips
3 #include "stdlib.h"
4 #endif
5 #include "xlisp.h"
6 #include "sound.h"
7 
8 #include "falloc.h"
9 #include "cext.h"
10 #include "biquadfilt.h"
11 
12 void biquadfilt_free(snd_susp_type a_susp);
13 
14 
15 typedef struct biquadfilt_susp_struct {
16     snd_susp_node susp;
17     int64_t terminate_cnt;
18     boolean logically_stopped;
19     sound_type s;
20     int s_cnt;
21     sample_block_values_type s_ptr;
22 
23     double z1;
24     double z2;
25     double b0;
26     double b1;
27     double b2;
28     double a1;
29     double a2;
30 } biquadfilt_susp_node, *biquadfilt_susp_type;
31 
32 
biquadfilt_n_fetch(snd_susp_type a_susp,snd_list_type snd_list)33 void biquadfilt_n_fetch(snd_susp_type a_susp, snd_list_type snd_list)
34 {
35     biquadfilt_susp_type susp = (biquadfilt_susp_type) a_susp;
36     int cnt = 0; /* how many samples computed */
37     int togo;
38     int n;
39     sample_block_type out;
40     register sample_block_values_type out_ptr;
41 
42     register sample_block_values_type out_ptr_reg;
43 
44     register double z1_reg;
45     register double z2_reg;
46     register double b0_reg;
47     register double b1_reg;
48     register double b2_reg;
49     register double a1_reg;
50     register double a2_reg;
51     register sample_block_values_type s_ptr_reg;
52     falloc_sample_block(out, "biquadfilt_n_fetch");
53     out_ptr = out->samples;
54     snd_list->block = out;
55 
56     while (cnt < max_sample_block_len) { /* outer loop */
57         /* first compute how many samples to generate in inner loop: */
58         /* don't overflow the output sample block: */
59         togo = max_sample_block_len - cnt;
60 
61         /* don't run past the s input sample block: */
62         susp_check_term_log_samples(s, s_ptr, s_cnt);
63         togo = min(togo, susp->s_cnt);
64 
65         /* don't run past terminate time */
66         if (susp->terminate_cnt != UNKNOWN &&
67             susp->terminate_cnt <= susp->susp.current + cnt + togo) {
68             togo = (int) (susp->terminate_cnt - (susp->susp.current + cnt));
69             if (togo < 0) togo = 0;  /* avoids rounding errros */
70             if (togo == 0) break;
71         }
72 
73 
74         /* don't run past logical stop time */
75         if (!susp->logically_stopped && susp->susp.log_stop_cnt != UNKNOWN) {
76             int64_t to_stop = susp->susp.log_stop_cnt - (susp->susp.current + cnt);
77             /* break if to_stop == 0 (we're at the logical stop)
78              * AND cnt > 0 (we're not at the beginning of the
79              * output block).
80              */
81             if (to_stop < 0) to_stop = 0; /* avoids rounding errors */
82             if (to_stop < togo) {
83                 if (to_stop == 0) {
84                     if (cnt) {
85                         togo = 0;
86                         break;
87                     } else /* keep togo as is: since cnt == 0, we
88                             * can set the logical stop flag on this
89                             * output block
90                             */
91                         susp->logically_stopped = true;
92                 } else /* limit togo so we can start a new
93                         * block at the LST
94                         */
95                     togo = (int) to_stop;
96             }
97         }
98 
99         n = togo;
100         z1_reg = susp->z1;
101         z2_reg = susp->z2;
102         b0_reg = susp->b0;
103         b1_reg = susp->b1;
104         b2_reg = susp->b2;
105         a1_reg = susp->a1;
106         a2_reg = susp->a2;
107         s_ptr_reg = susp->s_ptr;
108         out_ptr_reg = out_ptr;
109         if (n) do { /* the inner sample computation loop */
110             double z0;            z0 = *s_ptr_reg++ + a1_reg*z1_reg + a2_reg*z2_reg;
111             *out_ptr_reg++ = (sample_type) (z0*b0_reg + z1_reg*b1_reg + z2_reg*b2_reg);
112             z2_reg = z1_reg; z1_reg = z0;
113         } while (--n); /* inner loop */
114 
115         susp->z1 = z1_reg;
116         susp->z2 = z2_reg;
117         /* using s_ptr_reg is a bad idea on RS/6000: */
118         susp->s_ptr += togo;
119         out_ptr += togo;
120         susp_took(s_cnt, togo);
121         cnt += togo;
122     } /* outer loop */
123 
124     /* test for termination */
125     if (togo == 0 && cnt == 0) {
126         snd_list_terminate(snd_list);
127     } else {
128         snd_list->block_len = cnt;
129         susp->susp.current += cnt;
130     }
131     /* test for logical stop */
132     if (susp->logically_stopped) {
133         snd_list->logically_stopped = true;
134     } else if (susp->susp.log_stop_cnt == susp->susp.current) {
135         susp->logically_stopped = true;
136     }
137 } /* biquadfilt_n_fetch */
138 
139 
biquadfilt_toss_fetch(snd_susp_type a_susp,snd_list_type snd_list)140 void biquadfilt_toss_fetch(snd_susp_type a_susp, snd_list_type snd_list)
141 {
142     biquadfilt_susp_type susp = (biquadfilt_susp_type) a_susp;
143     time_type final_time = susp->susp.t0;
144     int n;
145 
146     /* fetch samples from s up to final_time for this block of zeros */
147     while ((ROUNDBIG((final_time - susp->s->t0) * susp->s->sr)) >=
148            susp->s->current)
149         susp_get_samples(s, s_ptr, s_cnt);
150     /* convert to normal processing when we hit final_count */
151     /* we want each signal positioned at final_time */
152     n = (int) ROUNDBIG((final_time - susp->s->t0) * susp->s->sr -
153          (susp->s->current - susp->s_cnt));
154     susp->s_ptr += n;
155     susp_took(s_cnt, n);
156     susp->susp.fetch = susp->susp.keep_fetch;
157     (*(susp->susp.fetch))(a_susp, snd_list);
158 }
159 
160 
biquadfilt_mark(snd_susp_type a_susp)161 void biquadfilt_mark(snd_susp_type a_susp)
162 {
163     biquadfilt_susp_type susp = (biquadfilt_susp_type) a_susp;
164     sound_xlmark(susp->s);
165 }
166 
167 
biquadfilt_free(snd_susp_type a_susp)168 void biquadfilt_free(snd_susp_type a_susp)
169 {
170     biquadfilt_susp_type susp = (biquadfilt_susp_type) a_susp;
171     sound_unref(susp->s);
172     ffree_generic(susp, sizeof(biquadfilt_susp_node), "biquadfilt_free");
173 }
174 
175 
biquadfilt_print_tree(snd_susp_type a_susp,int n)176 void biquadfilt_print_tree(snd_susp_type a_susp, int n)
177 {
178     biquadfilt_susp_type susp = (biquadfilt_susp_type) a_susp;
179     indent(n);
180     stdputstr("s:");
181     sound_print_tree_1(susp->s, n);
182 }
183 
184 
snd_make_biquadfilt(sound_type s,double b0,double b1,double b2,double a1,double a2,double z1init,double z2init)185 sound_type snd_make_biquadfilt(sound_type s, double b0, double b1, double b2, double a1, double a2, double z1init, double z2init)
186 {
187     register biquadfilt_susp_type susp;
188     rate_type sr = s->sr;
189     time_type t0 = s->t0;
190     sample_type scale_factor = 1.0F;
191     time_type t0_min = t0;
192     /* combine scale factors of linear inputs (S) */
193     scale_factor *= s->scale;
194     s->scale = 1.0F;
195 
196     /* try to push scale_factor back to a low sr input */
197     if (s->sr < sr) { s->scale = scale_factor; scale_factor = 1.0F; }
198 
199     falloc_generic(susp, biquadfilt_susp_node, "snd_make_biquadfilt");
200     susp->z1 = z1init;
201     susp->z2 = z2init;
202     susp->b0 = b0;
203     susp->b1 = b1;
204     susp->b2 = b2;
205     susp->a1 = a1;
206     susp->a2 = a2;
207     susp->susp.fetch = biquadfilt_n_fetch;
208     susp->terminate_cnt = UNKNOWN;
209     /* handle unequal start times, if any */
210     if (t0 < s->t0) sound_prepend_zeros(s, t0);
211     /* minimum start time over all inputs: */
212     t0_min = min(s->t0, t0);
213     /* how many samples to toss before t0: */
214     susp->susp.toss_cnt = (long) ((t0 - t0_min) * sr + 0.5);
215     if (susp->susp.toss_cnt > 0) {
216         susp->susp.keep_fetch = susp->susp.fetch;
217         susp->susp.fetch = biquadfilt_toss_fetch;
218     }
219 
220     /* initialize susp state */
221     susp->susp.free = biquadfilt_free;
222     susp->susp.sr = sr;
223     susp->susp.t0 = t0;
224     susp->susp.mark = biquadfilt_mark;
225     susp->susp.print_tree = biquadfilt_print_tree;
226     susp->susp.name = "biquadfilt";
227     susp->logically_stopped = false;
228     susp->susp.log_stop_cnt = logical_stop_cnt_cvt(s);
229     susp->susp.current = 0;
230     susp->s = s;
231     susp->s_cnt = 0;
232     return sound_create((snd_susp_type)susp, t0, sr, scale_factor);
233 }
234 
235 
snd_biquadfilt(sound_type s,double b0,double b1,double b2,double a1,double a2,double z1init,double z2init)236 sound_type snd_biquadfilt(sound_type s, double b0, double b1, double b2, double a1, double a2, double z1init, double z2init)
237 {
238     sound_type s_copy = sound_copy(s);
239     return snd_make_biquadfilt(s_copy, b0, b1, b2, a1, a2, z1init, z2init);
240 }
241