1 /* WhySynth DSSI software synthesizer plugin
2  *
3  * Copyright (C) 2005 Sean Bolton and others.
4  *
5  * Portions of this file come from MSS, copyright (C) 2002 Mats
6  * Olsson.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be
14  * useful, but WITHOUT ANY WARRANTY; without even the implied
15  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  * PURPOSE.  See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301 USA.
22  */
23 
24 #ifndef _AGRAN_OSCILLATOR_H
25 #define _AGRAN_OSCILLATOR_H
26 
27 #include "whysynth.h"
28 #include "whysynth_voice.h"
29 
30 #define AG_DEFAULT_GRAIN_COUNT  (Y_MAX_POLYPHONY * 10)
31 
32 #define AG_GRAIN_ENVELOPE_COUNT 31
33 
34 enum grain_envelope_type {
35     AG_ENVELOPE_RECTANGULAR,
36     AG_ENVELOPE_LINEAR,
37     AG_ENVELOPE_TRIANGULAR,
38     AG_ENVELOPE_GAUSSIAN,
39     AG_ENVELOPE_ROADSIAN   /* Curtis, not Randy */
40 };
41 
42 /* Paraphrased from MSS:
43  *
44  * "For the AG_ENVELOPE_LINEAR envelope the grain_envelope_decriptor_t.rate is
45  * the portion of the grain length needed to linearly increase the envelope
46  * amplitude from 0 to 1 when the grain starts (and correspondingly decrease
47  * envelope amplitude from 1 to 0 when the grain ends).
48  *
49  * For the AG_ENVELOPE_TRIANGULAR envelope the grain_envelope_decriptor_t.rate
50  * is the portion of the grain length needed to linearly increase the
51  * envelope amplitude from 0 to 1 when the grain starts. The rest of the grain
52  * length the envelope amplitude is then linearly decreased from 1 to 0 (this
53  * means that for a 0.5 grain_envelope_decriptor_t.rate the linear and
54  * triangular envelopes are identical).
55  *
56  * For the AG_ENVELOPE_GAUSSIAN envelope the grain_envelope_decriptor_t.rate
57  * is the amplitude at the start and the end of the grain (relative to the 1.0
58  * in the middle)."
59  *
60  * AG_ENVELOPE_ROADSIAN is like the linear envelope, but with Gaussian attack
61  * and release instead.
62  */
63 
64 struct _grain_envelope_descriptor_t
65 {
66     const char *             name;
67     enum grain_envelope_type type;
68     float                    rate;
69     float                    length; /* in milliseconds */
70 };
71 
72 typedef struct _grain_envelope_descriptor_t grain_envelope_descriptor_t;
73 
74 struct _grain_envelope_data_t
75 {
76     unsigned long length; /* in samples */
77     float *       data;
78 };
79 
80 struct _grain_t
81 {
82     struct _grain_t *next;
83     int              env_pos;  /* index into envelope */
84     float            wave_pos; /* wavetable wave phase */
85     float            w;        /* per-sample phase increment for this grain */
86 };
87 
88 extern grain_envelope_descriptor_t grain_envelope_descriptors[AG_GRAIN_ENVELOPE_COUNT];
89 
90 void agran_oscillator(unsigned long sample_count,
91                       y_synth_t *synth, y_sosc_t *sosc,
92                       y_voice_t *voice, struct vosc *vosc, int index, float w);
93 int  new_grain_array(y_synth_t *synth, int grain_count);
94 grain_envelope_data_t *
95      create_grain_envelopes(unsigned long sample_rate);
96 void free_grain_envelopes(grain_envelope_data_t *envelopes);
97 void free_active_grains(y_synth_t *synth, y_voice_t *voice);
98 
99 #endif /* _AGRAN_OSCILLATOR_H */
100