1 /*
2     oscbnk.h:
3 
4     Copyright (C) 2002, 2005 Istvan Varga
5 
6     This file is part of Csound.
7 
8     The Csound Library is free software; you can redistribute it
9     and/or modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation; either
11     version 2.1 of the License, or (at your option) any later version.
12 
13     Csound is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public
19     License along with Csound; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21     02110-1301 USA
22 */
23 
24 #ifndef CSOUND_OSCBNK_H
25 #define CSOUND_OSCBNK_H
26 
27 #include "stdopcod.h"
28 
29 /*
30 #ifdef  B64BIT
31 #define MAXLEN     0x40000000
32 #define FMAXLEN    ((MYFLT)(MAXLEN))
33 #define PHMASK     0x3fffffff
34 #else
35 #define MAXLEN     0x1000000L
36 #define FMAXLEN    ((MYFLT)(MAXLEN))
37 #define PHMASK     0x0FFFFFFL
38 #endif
39 */
40 
41 
42 /* ---- oscbnk, grain2, and grain3 - written by Istvan Varga, 2001 ---- */
43 
44 #define OSCBNK_PHSMAX   0x80000000UL    /* max. phase   */
45 #define OSCBNK_PHSMSK   0x7FFFFFFFUL    /* phase mask   */
46 #define OSCBNK_RNDPHS   0               /* 31 bit rand -> phase bit shift */
47 
48 /* convert floating point phase value to integer */
49 
50 #define OSCBNK_PHS2INT(x)                                                     \
51     ((uint32) MYFLT2LRND((x) * (MYFLT) OSCBNK_PHSMAX) & OSCBNK_PHSMSK)
52 
53 /* oscbnk types */
54 
55 typedef struct {
56         uint32  LFO1phs;                /* LFO 1 phase                  */
57         MYFLT   LFO1frq;                /* LFO 1 frequency (0-1)        */
58         uint32  LFO2phs;                /* LFO 2 phase                  */
59         MYFLT   LFO2frq;                /* LFO 2 frequency (0-1)        */
60         uint32  osc_phs;                /* main oscillator phase        */
61         MYFLT   osc_phm;                /* phase mod.                   */
62         MYFLT   osc_frq, osc_amp;       /* osc. freq. / sr, amplitude   */
63         MYFLT   xnm1, xnm2, ynm1, ynm2; /* EQ tmp data                  */
64         MYFLT   a1, a2, b0, b1, b2;     /* EQ coeffs saved for interp.  */
65 } OSCBNK_OSC;
66 
67 typedef struct {
68         OPDS    h;
69         MYFLT   *args[27];              /* opcode args (see manual)     */
70         int32_t     init_k;                 /* 1st k-cycle (0: no, 1: yes)  */
71         int32_t     nr_osc;                 /* number of oscillators        */
72         int32   seed;                   /* random seed                  */
73         int32_t     ilfomode, ieqmode;      /* LFO and EQ mode              */
74         int32_t     eq_interp;              /* enable filter coeff. interp. */
75         MYFLT   frq_scl;                /* constants for calculating    */
76         MYFLT   lf1_scl, lf1_ofs;       /* k-rate parameters            */
77         MYFLT   lf2_scl, lf2_ofs;
78         MYFLT   eqo_scl, eqo_ofs;
79         MYFLT   eql_scl, eql_ofs;
80         MYFLT   eqq_scl, eqq_ofs;
81         MYFLT   *l1t, l1t_pfrac;        /* LFO 1 ftable                 */
82         uint32   l1t_mask, l1t_lobits;
83         MYFLT   *l2t, l2t_pfrac;        /* LFO 2 ftable                 */
84         uint32   l2t_mask, l2t_lobits;
85         MYFLT   *eqft;                  /* EQ frequency table           */
86         int32    eqft_len;
87         MYFLT   *eqlt;                  /* EQ level table               */
88         int32    eqlt_len;
89         MYFLT   *eqqt;                  /* EQ Q table                   */
90         int32    eqqt_len;
91         MYFLT   *tabl;                  /* parameter input table        */
92         int32    tabl_len;               /* (optional)                   */
93         MYFLT   *outft;                 /* parameter output table       */
94         int32    outft_len;              /* (optional)                   */
95         int32    tabl_cnt;               /* current param in table       */
96         AUXCH   auxdata;
97         OSCBNK_OSC      *osc;           /* oscillator array             */
98 } OSCBNK;
99 
100 /* grain2 types */
101 
102 typedef struct {
103         uint32   grain_phs;      /* grain phase                  */
104         uint32   grain_frq_int;  /* grain frequency (integer)    */
105         MYFLT           grain_frq_flt;  /* grain frequency (float)      */
106         uint32   window_phs;     /* window phase                 */
107 } GRAIN2_OSC;
108 
109 typedef struct {
110         OPDS    h;
111         MYFLT   *ar, *kcps, *kfmd;      /* opcode args                  */
112         MYFLT   *kgdur, *iovrlp;
113         MYFLT   *kfn, *iwfn, *irpow;
114         MYFLT   *iseed, *imode;
115         int32_t     init_k;                 /* 1st k-cycle (0: no, 1: yes)  */
116         int32_t     mode;                   /* imode (see manual)           */
117         int32_t     nr_osc;                 /* number of oscillators        */
118         int32    seed;                   /* random seed                  */
119         int32_t     rnd_mode;               /* random distribution params   */
120         MYFLT   rnd_pow;
121         MYFLT   grain_frq, frq_scl;     /* grain frequency              */
122         MYFLT   *wft, wft_pfrac;        /* window table                 */
123         uint32   wft_lobits, wft_mask;
124         AUXCH   auxdata;
125         GRAIN2_OSC      *osc;           /* oscillator array             */
126 } GRAIN2;
127 
128 /* -------- grain3 types -------- */
129 
130 typedef struct {
131         OPDS    h;
132         MYFLT   *ar, *kcps, *kphs;      /* opcode args                  */
133         MYFLT   *kfmd, *kpmd;
134         MYFLT   *kgdur, *kdens;
135         MYFLT   *imaxovr, *kfn, *iwfn;
136         MYFLT   *kfrpow, *kprpow;
137         MYFLT   *iseed, *imode;
138         int32_t     init_k;                 /* 1st k-cycle (0: no, 1: yes)  */
139         int32_t     mode;                   /* imode (see manual)           */
140         int32_t     ovrlap;                 /* max. number of oscillators   */
141         int32    seed;                   /* random seed                  */
142         int32_t     f_rnd_mode;             /* random distribution (freq.)  */
143         MYFLT   f_rnd_pow;
144         int32_t     p_rnd_mode, pm_wrap;    /* random distribution (phase)  */
145         MYFLT   p_rnd_pow;
146         uint32   grain_frq;      /* grain frequency              */
147         MYFLT   frq_scl;
148         MYFLT   phs0;                   /* prev. kphs value for interp. */
149         uint32   x_phs;
150         MYFLT   *wft, wft_pfrac;        /* window table                 */
151         uint32   wft_lobits, wft_mask;
152         AUXCH   auxdata;
153         uint32   *phase;         /* grain phase offset           */
154         GRAIN2_OSC      *osc;           /* oscillator array             */
155         GRAIN2_OSC      *osc_start;     /* first active grain           */
156         GRAIN2_OSC      *osc_end;       /* last active grain + 1        */
157         GRAIN2_OSC      *osc_max;       /* ptr to last osc in array     */
158 } GRAIN3;
159 
160 /* -------- rnd31 types -------- */
161 
162 typedef struct {
163         OPDS    h;              /* opcode args          */
164         MYFLT   *out;                   /* output signal                */
165         MYFLT   *scl;                   /* scale                        */
166         MYFLT   *rpow;                  /* distribution                 */
167         MYFLT   *iseed;                 /* seed                         */
168                                 /* internal variables   */
169         int32    *rnd31i_seed;           /* global seed for rnd31        */
170         int32    seed;                   /* random seed                  */
171 } RND31;
172 
173 /* -------- oscilikt types -------- */
174 
175 typedef struct {
176         OPDS    h;
177         MYFLT   *sr, *xamp, *xcps, *kfn, *iphs, *istor;
178         uint32    phs, lobits, mask;
179         MYFLT   pfrac, *ft, oldfn;
180 } OSCKT;
181 
182 typedef struct {
183         OPDS    h;
184         MYFLT   *ar, *kcps, *kfn, *kphs, *istor;
185         uint32    phs, lobits, mask;
186         MYFLT   pfrac, *ft, oldfn, old_phs;
187         int32_t     init_k;
188 } OSCKTP;
189 
190 typedef struct {
191         OPDS    h;
192         MYFLT   *ar, *xamp, *xcps, *kfn, *async, *kphs, *istor;
193         uint32    phs, lobits, mask;
194         MYFLT   pfrac, *ft, oldfn;
195         int32_t     init_k;
196 } OSCKTS;
197 
198 /* ---- vco2init, vco2ft, and vco2 opcodes by Istvan Varga, Sep 2002 ---- */
199 
200 /* Select algorithm to be used for finding table numbers */
201 /* Define this macro to use simple table lookup (slower  */
202 /* at high control rate, due to float->int32_t cast), or     */
203 /* comment it out to use a search algorithm (slower with */
204 /* very fast changes in frequency)                       */
205 
206 #define VCO2FT_USE_TABLE    1
207 
208 typedef struct {
209     int32_t     npart;              /* number of harmonic partials (may be zero) */
210     int32_t     size;               /* size of the table (not incl. guard point) */
211     uint32               /* parameters needed for reading the table,  */
212             lobits, mask;       /*   and interpolation                       */
213     MYFLT   pfrac;
214     MYFLT   *ftable;            /* table data (size + 1 floats)              */
215 } VCO2_TABLE;
216 
217 struct VCO2_TABLE_ARRAY_ {
218     int32_t     ntabl;              /* number of tables                          */
219     int32_t     base_ftnum;         /* base ftable number (-1: none)             */
220 #ifdef VCO2FT_USE_TABLE
221     VCO2_TABLE  **nparts_tabl;  /* table ptrs for all numbers of partials    */
222 #else
223     MYFLT   *nparts;            /* number of partials list                   */
224 #endif
225     VCO2_TABLE  *tables;        /* array of table structures                 */
226 };
227 
228 typedef struct {
229     OPDS    h;
230     MYFLT   *ift, *iwaveforms, *iftnum, *ipmul, *iminsiz, *imaxsiz, *isrcft;
231 } VCO2INIT;
232 
233 typedef struct {
234     OPDS    h;
235     MYFLT   *ar, *kamp, *kcps, *imode, *kpw, *kphs, *inyx;
236     MYFLT   *dummy[9];
237 #ifdef VCO2FT_USE_TABLE
238     VCO2_TABLE  **nparts_tabl;  /* table ptrs for all numbers of partials    */
239 #endif
240     int32_t     init_k;             /* 1 in first k-cycle, 0 otherwise           */
241     int32_t     mode;               /* algorithm (0, 1, or 2)                    */
242     int32_t     pm_enabled;         /* phase modulation enabled (0: no, 1: yes)  */
243 #ifdef VCO2FT_USE_TABLE
244     MYFLT   f_scl, p_min, p_scl, kphs_old, kphs2_old;
245 #else
246     MYFLT   f_scl, p_min, p_scl, *npart_old, *nparts, kphs_old, kphs2_old;
247     VCO2_TABLE  *tables;        /* pointer to array of tables                */
248 #endif
249     uint32  phs, phs2;  /* oscillator phase                          */
250     VCO2_TABLE_ARRAY  ***vco2_tables;
251     int32_t             *vco2_nr_table_arrays;
252 } VCO2;
253 
254 typedef struct {
255     OPDS    h;
256     MYFLT   *kft, *kcps, *iwave, *inyx;
257     MYFLT   p_min, p_scl;
258 #ifdef VCO2FT_USE_TABLE
259     VCO2_TABLE  **nparts_tabl, *tab0;
260 #else
261     MYFLT   *npart_old, *nparts;
262 #endif
263     VCO2_TABLE_ARRAY    ***vco2_tables;
264     int32_t                 *vco2_nr_table_arrays;
265     int32_t                 base_ftnum;
266 } VCO2FT;
267 
268 typedef struct {                /* denorm a1[, a2[, a3[, ... ]]] */
269     OPDS    h;
270     MYFLT   *ar[256];
271     int32_t     *seedptr;
272 } DENORMS;
273 
274 typedef struct {                /* kr delayk ksig, idel[, imode] */
275     OPDS    h;
276     MYFLT   *ar, *ksig, *idel, *imode;
277     int32_t     npts, init_k, readp, mode;
278     AUXCH   aux;
279 } DELAYK;
280 
281 typedef struct {                /* kr vdel_k ksig, kdel, imdel[, imode] */
282     OPDS    h;
283     MYFLT   *ar, *ksig, *kdel, *imdel, *imode;
284     int32_t     npts, init_k, wrtp, mode;
285     MYFLT   frstkval;
286     AUXCH   aux;
287 } VDELAYK;
288 
289 typedef struct {                /* ar rbjeq asig, kfco, klvl, kQ, kS[, imode] */
290         OPDS    h;
291         MYFLT   *ar, *asig, *kcps, *klvl, *kQ, *kS, *imode;     /* args */
292         /* internal variables */
293         MYFLT   old_kcps, old_klvl, old_kQ, old_kS;
294         double  omega, cs, sn;
295         MYFLT   xnm1, xnm2, ynm1, ynm2;
296         MYFLT   b0, b1, b2, a1, a2;
297         int32_t
298         ftype;
299 } RBJEQ;
300 
301 #endif          /* CSOUND_OSCBNK_H */
302 
303