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 "allpoles.h"
11
12 void allpoles_free(snd_susp_type a_susp);
13
14
15 typedef struct allpoles_susp_struct {
16 snd_susp_node susp;
17 int64_t terminate_cnt;
18 boolean logically_stopped;
19 sound_type x_snd;
20 int x_snd_cnt;
21 sample_block_values_type x_snd_ptr;
22
23 long ak_len;
24 LVAL ak_array;
25 double gain;
26 double *ak_coefs;
27 double *zk_buf;
28 long index;
29 } allpoles_susp_node, *allpoles_susp_type;
30
31
allpoles_s_fetch(snd_susp_type a_susp,snd_list_type snd_list)32 void allpoles_s_fetch(snd_susp_type a_susp, snd_list_type snd_list)
33 {
34 allpoles_susp_type susp = (allpoles_susp_type) a_susp;
35 int cnt = 0; /* how many samples computed */
36 int togo;
37 int n;
38 sample_block_type out;
39 register sample_block_values_type out_ptr;
40
41 register sample_block_values_type out_ptr_reg;
42
43 register long ak_len_reg;
44 register double gain_reg;
45 register double * ak_coefs_reg;
46 register double * zk_buf_reg;
47 register long index_reg;
48 register sample_type x_snd_scale_reg = susp->x_snd->scale;
49 register sample_block_values_type x_snd_ptr_reg;
50 falloc_sample_block(out, "allpoles_s_fetch");
51 out_ptr = out->samples;
52 snd_list->block = out;
53
54 while (cnt < max_sample_block_len) { /* outer loop */
55 /* first compute how many samples to generate in inner loop: */
56 /* don't overflow the output sample block: */
57 togo = max_sample_block_len - cnt;
58
59 /* don't run past the x_snd input sample block: */
60 susp_check_term_log_samples(x_snd, x_snd_ptr, x_snd_cnt);
61 togo = min(togo, susp->x_snd_cnt);
62
63 /* don't run past terminate time */
64 if (susp->terminate_cnt != UNKNOWN &&
65 susp->terminate_cnt <= susp->susp.current + cnt + togo) {
66 togo = (int) (susp->terminate_cnt - (susp->susp.current + cnt));
67 if (togo < 0) togo = 0; /* avoids rounding errros */
68 if (togo == 0) break;
69 }
70
71
72 /* don't run past logical stop time */
73 if (!susp->logically_stopped && susp->susp.log_stop_cnt != UNKNOWN) {
74 int64_t to_stop = susp->susp.log_stop_cnt - (susp->susp.current + cnt);
75 /* break if to_stop == 0 (we're at the logical stop)
76 * AND cnt > 0 (we're not at the beginning of the
77 * output block).
78 */
79 if (to_stop < 0) to_stop = 0; /* avoids rounding errors */
80 if (to_stop < togo) {
81 if (to_stop == 0) {
82 if (cnt) {
83 togo = 0;
84 break;
85 } else /* keep togo as is: since cnt == 0, we
86 * can set the logical stop flag on this
87 * output block
88 */
89 susp->logically_stopped = true;
90 } else /* limit togo so we can start a new
91 * block at the LST
92 */
93 togo = (int) to_stop;
94 }
95 }
96
97
98 if (susp->ak_array == NULL) {
99 togo = 0; /* indicate termination */
100 break; /* we're done */
101 }
102 else if (!vectorp(susp->ak_array))
103 xlerror("array expected", susp->ak_array);
104 else if (susp->ak_coefs == NULL)
105 {
106 long i;
107 susp->ak_len = getsize(susp->ak_array);
108 if (susp->ak_len < 1) xlerror("array has not elements", susp->ak_array);
109 susp->ak_coefs = (double *) calloc(susp->ak_len, sizeof(double));
110 susp->zk_buf = (double *) calloc(susp->ak_len, sizeof(double));
111
112 /* at this point we have a new array and a place to put ak coefs */
113 for(i=0; i < susp->ak_len; i++) {
114 LVAL elem = getelement(susp->ak_array,i);
115 if (ntype(elem) != FLONUM) {
116 xlerror("flonum expected", elem);
117 }
118 susp->ak_coefs[i] = getflonum(elem);
119 }
120
121 }
122
123 n = togo;
124 ak_len_reg = susp->ak_len;
125 gain_reg = susp->gain;
126 ak_coefs_reg = susp->ak_coefs;
127 zk_buf_reg = susp->zk_buf;
128 index_reg = susp->index;
129 x_snd_ptr_reg = susp->x_snd_ptr;
130 out_ptr_reg = out_ptr;
131 if (n) do { /* the inner sample computation loop */
132 double z0; long xi; long xj; z0 = (x_snd_scale_reg * *x_snd_ptr_reg++)*gain_reg;
133 for (xi=0; xi < ak_len_reg ; xi++) {
134 xj = index_reg + xi; if (xj >= ak_len_reg) xj -= ak_len_reg;
135 z0 += ak_coefs_reg[xi] * zk_buf_reg[xj];
136 }
137 zk_buf_reg[index_reg] = z0;
138 index_reg++; if (index_reg == ak_len_reg) index_reg = 0;
139 *out_ptr_reg++ = (sample_type) z0;
140 } while (--n); /* inner loop */
141
142 susp->zk_buf = zk_buf_reg;
143 susp->index = index_reg;
144 /* using x_snd_ptr_reg is a bad idea on RS/6000: */
145 susp->x_snd_ptr += togo;
146 out_ptr += togo;
147 susp_took(x_snd_cnt, togo);
148 cnt += togo;
149 } /* outer loop */
150
151 /* test for termination */
152 if (togo == 0 && cnt == 0) {
153 snd_list_terminate(snd_list);
154 } else {
155 snd_list->block_len = cnt;
156 susp->susp.current += cnt;
157 }
158 /* test for logical stop */
159 if (susp->logically_stopped) {
160 snd_list->logically_stopped = true;
161 } else if (susp->susp.log_stop_cnt == susp->susp.current) {
162 susp->logically_stopped = true;
163 }
164 } /* allpoles_s_fetch */
165
166
allpoles_toss_fetch(snd_susp_type a_susp,snd_list_type snd_list)167 void allpoles_toss_fetch(snd_susp_type a_susp, snd_list_type snd_list)
168 {
169 allpoles_susp_type susp = (allpoles_susp_type) a_susp;
170 time_type final_time = susp->susp.t0;
171 int n;
172
173 /* fetch samples from x_snd up to final_time for this block of zeros */
174 while ((ROUNDBIG((final_time - susp->x_snd->t0) * susp->x_snd->sr)) >=
175 susp->x_snd->current)
176 susp_get_samples(x_snd, x_snd_ptr, x_snd_cnt);
177 /* convert to normal processing when we hit final_count */
178 /* we want each signal positioned at final_time */
179 n = (int) ROUNDBIG((final_time - susp->x_snd->t0) * susp->x_snd->sr -
180 (susp->x_snd->current - susp->x_snd_cnt));
181 susp->x_snd_ptr += n;
182 susp_took(x_snd_cnt, n);
183 susp->susp.fetch = susp->susp.keep_fetch;
184 (*(susp->susp.fetch))(a_susp, snd_list);
185 }
186
187
allpoles_mark(snd_susp_type a_susp)188 void allpoles_mark(snd_susp_type a_susp)
189 {
190 allpoles_susp_type susp = (allpoles_susp_type) a_susp;
191 if (susp->ak_array) mark(susp->ak_array);
192 sound_xlmark(susp->x_snd);
193 }
194
195
allpoles_free(snd_susp_type a_susp)196 void allpoles_free(snd_susp_type a_susp)
197 {
198 allpoles_susp_type susp = (allpoles_susp_type) a_susp;
199
200 free(susp->zk_buf);
201 free(susp->ak_coefs);
202 susp->ak_array = NULL; /* free array */
203 sound_unref(susp->x_snd);
204 ffree_generic(susp, sizeof(allpoles_susp_node), "allpoles_free");
205 }
206
207
allpoles_print_tree(snd_susp_type a_susp,int n)208 void allpoles_print_tree(snd_susp_type a_susp, int n)
209 {
210 allpoles_susp_type susp = (allpoles_susp_type) a_susp;
211 indent(n);
212 stdputstr("x_snd:");
213 sound_print_tree_1(susp->x_snd, n);
214 }
215
216
snd_make_allpoles(sound_type x_snd,LVAL ak_array,double gain)217 sound_type snd_make_allpoles(sound_type x_snd, LVAL ak_array, double gain)
218 {
219 register allpoles_susp_type susp;
220 rate_type sr = x_snd->sr;
221 time_type t0 = x_snd->t0;
222 sample_type scale_factor = 1.0F;
223 time_type t0_min = t0;
224 falloc_generic(susp, allpoles_susp_node, "snd_make_allpoles");
225 susp->ak_len = 0;
226 susp->ak_array = ak_array;
227 susp->gain = gain;
228 susp->ak_coefs = NULL;
229 susp->zk_buf = NULL;
230 susp->index = 0;
231 susp->susp.fetch = allpoles_s_fetch;
232 susp->terminate_cnt = UNKNOWN;
233 /* handle unequal start times, if any */
234 if (t0 < x_snd->t0) sound_prepend_zeros(x_snd, t0);
235 /* minimum start time over all inputs: */
236 t0_min = min(x_snd->t0, t0);
237 /* how many samples to toss before t0: */
238 susp->susp.toss_cnt = (long) ((t0 - t0_min) * sr + 0.5);
239 if (susp->susp.toss_cnt > 0) {
240 susp->susp.keep_fetch = susp->susp.fetch;
241 susp->susp.fetch = allpoles_toss_fetch;
242 }
243
244 /* initialize susp state */
245 susp->susp.free = allpoles_free;
246 susp->susp.sr = sr;
247 susp->susp.t0 = t0;
248 susp->susp.mark = allpoles_mark;
249 susp->susp.print_tree = allpoles_print_tree;
250 susp->susp.name = "allpoles";
251 susp->logically_stopped = false;
252 susp->susp.log_stop_cnt = logical_stop_cnt_cvt(x_snd);
253 susp->susp.current = 0;
254 susp->x_snd = x_snd;
255 susp->x_snd_cnt = 0;
256 return sound_create((snd_susp_type)susp, t0, sr, scale_factor);
257 }
258
259
snd_allpoles(sound_type x_snd,LVAL ak_array,double gain)260 sound_type snd_allpoles(sound_type x_snd, LVAL ak_array, double gain)
261 {
262 sound_type x_snd_copy = sound_copy(x_snd);
263 return snd_make_allpoles(x_snd_copy, ak_array, gain);
264 }
265