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 "instrclar.h"
11 
12 void clarinet_free(snd_susp_type a_susp);
13 
14 
15 typedef struct clarinet_susp_struct {
16     snd_susp_node susp;
17     int64_t terminate_cnt;
18     sound_type breath_env;
19     int breath_env_cnt;
20     sample_block_values_type breath_env_ptr;
21 
22     struct instr *clar;
23     int temp_ret_value;
24     float breath_scale;
25 } clarinet_susp_node, *clarinet_susp_type;
26 
27 #include "instr.h"
28 #include "upsample.h"
29 
30 
clarinet_n_fetch(snd_susp_type a_susp,snd_list_type snd_list)31 void clarinet_n_fetch(snd_susp_type a_susp, snd_list_type snd_list)
32 {
33     clarinet_susp_type susp = (clarinet_susp_type) a_susp;
34     int cnt = 0; /* how many samples computed */
35     int togo;
36     int n;
37     sample_block_type out;
38     register sample_block_values_type out_ptr;
39 
40     register sample_block_values_type out_ptr_reg;
41 
42     register struct instr * clar_reg;
43     register float breath_scale_reg;
44     register sample_block_values_type breath_env_ptr_reg;
45     falloc_sample_block(out, "clarinet_n_fetch");
46     out_ptr = out->samples;
47     snd_list->block = out;
48 
49     while (cnt < max_sample_block_len) { /* outer loop */
50         /* first compute how many samples to generate in inner loop: */
51         /* don't overflow the output sample block: */
52         togo = max_sample_block_len - cnt;
53 
54         /* don't run past the breath_env input sample block: */
55         susp_check_term_samples(breath_env, breath_env_ptr, breath_env_cnt);
56         togo = min(togo, susp->breath_env_cnt);
57 
58         /* don't run past terminate time */
59         if (susp->terminate_cnt != UNKNOWN &&
60             susp->terminate_cnt <= susp->susp.current + cnt + togo) {
61             togo = (int) (susp->terminate_cnt - (susp->susp.current + cnt));
62             if (togo < 0) togo = 0;  /* avoids rounding errros */
63             if (togo == 0) break;
64         }
65 
66         n = togo;
67         clar_reg = susp->clar;
68         breath_scale_reg = susp->breath_scale;
69         breath_env_ptr_reg = susp->breath_env_ptr;
70         out_ptr_reg = out_ptr;
71         if (n) do { /* the inner sample computation loop */
72             controlChange(clar_reg, 128, breath_scale_reg * *breath_env_ptr_reg++);
73             *out_ptr_reg++ = (sample_type) tick(clar_reg);
74         } while (--n); /* inner loop */
75 
76         susp->clar = clar_reg;
77         /* using breath_env_ptr_reg is a bad idea on RS/6000: */
78         susp->breath_env_ptr += togo;
79         out_ptr += togo;
80         susp_took(breath_env_cnt, togo);
81         cnt += togo;
82     } /* outer loop */
83 
84     /* test for termination */
85     if (togo == 0 && cnt == 0) {
86         snd_list_terminate(snd_list);
87     } else {
88         snd_list->block_len = cnt;
89         susp->susp.current += cnt;
90     }
91 } /* clarinet_n_fetch */
92 
93 
clarinet_toss_fetch(snd_susp_type a_susp,snd_list_type snd_list)94 void clarinet_toss_fetch(snd_susp_type a_susp, snd_list_type snd_list)
95 {
96     clarinet_susp_type susp = (clarinet_susp_type) a_susp;
97     time_type final_time = susp->susp.t0;
98     int n;
99 
100     /* fetch samples from breath_env up to final_time for this block of zeros */
101     while ((ROUNDBIG((final_time - susp->breath_env->t0) * susp->breath_env->sr)) >=
102            susp->breath_env->current)
103         susp_get_samples(breath_env, breath_env_ptr, breath_env_cnt);
104     /* convert to normal processing when we hit final_count */
105     /* we want each signal positioned at final_time */
106     n = (int) ROUNDBIG((final_time - susp->breath_env->t0) * susp->breath_env->sr -
107          (susp->breath_env->current - susp->breath_env_cnt));
108     susp->breath_env_ptr += n;
109     susp_took(breath_env_cnt, n);
110     susp->susp.fetch = susp->susp.keep_fetch;
111     (*(susp->susp.fetch))(a_susp, snd_list);
112 }
113 
114 
clarinet_mark(snd_susp_type a_susp)115 void clarinet_mark(snd_susp_type a_susp)
116 {
117     clarinet_susp_type susp = (clarinet_susp_type) a_susp;
118     sound_xlmark(susp->breath_env);
119 }
120 
121 
clarinet_free(snd_susp_type a_susp)122 void clarinet_free(snd_susp_type a_susp)
123 {
124     clarinet_susp_type susp = (clarinet_susp_type) a_susp;
125     deleteInstrument(susp->clar);
126     sound_unref(susp->breath_env);
127     ffree_generic(susp, sizeof(clarinet_susp_node), "clarinet_free");
128 }
129 
130 
clarinet_print_tree(snd_susp_type a_susp,int n)131 void clarinet_print_tree(snd_susp_type a_susp, int n)
132 {
133     clarinet_susp_type susp = (clarinet_susp_type) a_susp;
134     indent(n);
135     stdputstr("breath_env:");
136     sound_print_tree_1(susp->breath_env, n);
137 }
138 
139 
snd_make_clarinet(double freq,sound_type breath_env,rate_type sr)140 sound_type snd_make_clarinet(double freq, sound_type breath_env, rate_type sr)
141 {
142     register clarinet_susp_type susp;
143     /* sr specified as input parameter */
144     time_type t0 = breath_env->t0;
145     sample_type scale_factor = 1.0F;
146     time_type t0_min = t0;
147     falloc_generic(susp, clarinet_susp_node, "snd_make_clarinet");
148     susp->clar = initInstrument(CLARINET, ROUND32(sr));
149     controlChange(susp->clar, 1, 0.0);;
150     susp->temp_ret_value = noteOn(susp->clar, freq, 1.0);
151     susp->breath_scale = breath_env->scale * CLAR_CONTROL_CHANGE_CONST;
152 
153     /* make sure no sample rate is too high */
154     if (breath_env->sr > sr) {
155         sound_unref(breath_env);
156         snd_badsr();
157     } else if (breath_env->sr < sr) breath_env = snd_make_up(sr, breath_env);
158     susp->susp.fetch = clarinet_n_fetch;
159     susp->terminate_cnt = UNKNOWN;
160     /* handle unequal start times, if any */
161     if (t0 < breath_env->t0) sound_prepend_zeros(breath_env, t0);
162     /* minimum start time over all inputs: */
163     t0_min = min(breath_env->t0, t0);
164     /* how many samples to toss before t0: */
165     susp->susp.toss_cnt = (long) ((t0 - t0_min) * sr + 0.5);
166     if (susp->susp.toss_cnt > 0) {
167         susp->susp.keep_fetch = susp->susp.fetch;
168         susp->susp.fetch = clarinet_toss_fetch;
169     }
170 
171     /* initialize susp state */
172     susp->susp.free = clarinet_free;
173     susp->susp.sr = sr;
174     susp->susp.t0 = t0;
175     susp->susp.mark = clarinet_mark;
176     susp->susp.print_tree = clarinet_print_tree;
177     susp->susp.name = "clarinet";
178     susp->susp.log_stop_cnt = UNKNOWN;
179     susp->susp.current = 0;
180     susp->breath_env = breath_env;
181     susp->breath_env_cnt = 0;
182     return sound_create((snd_susp_type)susp, t0, sr, scale_factor);
183 }
184 
185 
snd_clarinet(double freq,sound_type breath_env,rate_type sr)186 sound_type snd_clarinet(double freq, sound_type breath_env, rate_type sr)
187 {
188     sound_type breath_env_copy = sound_copy(breath_env);
189     return snd_make_clarinet(freq, breath_env_copy, sr);
190 }
191