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 "osc.h"
11
12 void osc_free(snd_susp_type a_susp);
13
14
15 typedef struct osc_susp_struct {
16 snd_susp_node susp;
17 int64_t terminate_cnt;
18
19 double ph_incr;
20 table_type the_table;
21 sample_type *table_ptr;
22 double table_len;
23 double phase;
24 } osc_susp_node, *osc_susp_type;
25
26
osc__fetch(snd_susp_type a_susp,snd_list_type snd_list)27 void osc__fetch(snd_susp_type a_susp, snd_list_type snd_list)
28 {
29 osc_susp_type susp = (osc_susp_type) a_susp;
30 int cnt = 0; /* how many samples computed */
31 int togo;
32 int n;
33 sample_block_type out;
34 register sample_block_values_type out_ptr;
35
36 register sample_block_values_type out_ptr_reg;
37
38 register double ph_incr_reg;
39 register sample_type * table_ptr_reg;
40 register double table_len_reg;
41 register double phase_reg;
42 falloc_sample_block(out, "osc__fetch");
43 out_ptr = out->samples;
44 snd_list->block = out;
45
46 while (cnt < max_sample_block_len) { /* outer loop */
47 /* first compute how many samples to generate in inner loop: */
48 /* don't overflow the output sample block: */
49 togo = max_sample_block_len - cnt;
50
51 /* don't run past terminate time */
52 if (susp->terminate_cnt != UNKNOWN &&
53 susp->terminate_cnt <= susp->susp.current + cnt + togo) {
54 togo = (int) (susp->terminate_cnt - (susp->susp.current + cnt));
55 if (togo < 0) togo = 0; /* avoids rounding errros */
56 if (togo == 0) break;
57 }
58
59 n = togo;
60 ph_incr_reg = susp->ph_incr;
61 table_ptr_reg = susp->table_ptr;
62 table_len_reg = susp->table_len;
63 phase_reg = susp->phase;
64 out_ptr_reg = out_ptr;
65 if (n) do { /* the inner sample computation loop */
66 long table_index = (long) phase_reg;
67 double x1 = table_ptr_reg[table_index];
68 *out_ptr_reg++ = (sample_type) (x1 + (phase_reg - table_index) *
69 (table_ptr_reg[table_index + 1] - x1));
70 phase_reg += ph_incr_reg;
71 while (phase_reg >= table_len_reg) phase_reg -= table_len_reg;
72 } while (--n); /* inner loop */
73
74 susp->phase = phase_reg;
75 out_ptr += togo;
76 cnt += togo;
77 } /* outer loop */
78
79 /* test for termination */
80 if (togo == 0 && cnt == 0) {
81 snd_list_terminate(snd_list);
82 } else {
83 snd_list->block_len = cnt;
84 susp->susp.current += cnt;
85 }
86 } /* osc__fetch */
87
88
osc_free(snd_susp_type a_susp)89 void osc_free(snd_susp_type a_susp)
90 {
91 osc_susp_type susp = (osc_susp_type) a_susp;
92 table_unref(susp->the_table);
93 ffree_generic(susp, sizeof(osc_susp_node), "osc_free");
94 }
95
96
osc_print_tree(snd_susp_type a_susp,int n)97 void osc_print_tree(snd_susp_type a_susp, int n)
98 {
99 }
100
101
snd_make_osc(sound_type input,double step,rate_type sr,double hz,time_type t0,time_type d,double phase)102 sound_type snd_make_osc(sound_type input, double step, rate_type sr, double hz, time_type t0, time_type d, double phase)
103 {
104 register osc_susp_type susp;
105 /* sr specified as input parameter */
106 /* t0 specified as input parameter */
107 sample_type scale_factor = 1.0F;
108 falloc_generic(susp, osc_susp_node, "snd_make_osc");
109 susp->ph_incr = 0;
110 susp->the_table = sound_to_table(input);
111 susp->table_ptr = susp->the_table->samples;
112 susp->table_len = susp->the_table->length;
113 susp->phase = compute_phase(phase, step, (long) susp->table_len,
114 input->sr, sr, hz, &susp->ph_incr);
115 susp->susp.fetch = osc__fetch;
116
117 susp->terminate_cnt = check_terminate_cnt(ROUNDBIG((d) * sr));
118 /* initialize susp state */
119 susp->susp.free = osc_free;
120 susp->susp.sr = sr;
121 susp->susp.t0 = t0;
122 susp->susp.mark = NULL;
123 susp->susp.print_tree = osc_print_tree;
124 susp->susp.name = "osc";
125 susp->susp.log_stop_cnt = UNKNOWN;
126 susp->susp.current = 0;
127 return sound_create((snd_susp_type)susp, t0, sr, scale_factor);
128 }
129
130
snd_osc(sound_type input,double step,rate_type sr,double hz,time_type t0,time_type d,double phase)131 sound_type snd_osc(sound_type input, double step, rate_type sr, double hz, time_type t0, time_type d, double phase)
132 {
133 return snd_make_osc(input, step, sr, hz, t0, d, phase);
134 }
135