1 /*
2     ADnote.h
3 
4     Original ZynAddSubFX author Nasca Octavian Paul
5     Copyright (C) 2002-2005 Nasca Octavian Paul
6     Copyright 2009-2011, Alan Calvert
7     Copyright 2014-2019, Will Godfrey & others
8     Copyright 2020-2021 Kristian Amlie & Will Godfrey
9 
10     This file is part of yoshimi, which is free software: you can redistribute
11     it and/or modify it under the terms of the GNU Library General Public
12     License as published by the Free Software Foundation; either version 2 of
13     the License, or (at your option) any later version.
14 
15     yoshimi is distributed in the hope that it will be useful, but WITHOUT ANY
16     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17     FOR A PARTICULAR PURPOSE.   See the GNU General Public License (version 2 or
18     later) for more details.
19 
20     You should have received a copy of the GNU General Public License along with
21     yoshimi; if not, write to the Free Software Foundation, Inc., 51 Franklin
22     Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 
24     This file is a derivative of a ZynAddSubFX original
25 
26 */
27 
28 #ifndef AD_NOTE_H
29 #define AD_NOTE_H
30 
31 #include "Params/ADnoteParameters.h"
32 #include "Misc/RandomGen.h"
33 
34 class ADnoteParameters;
35 class Controller;
36 class Envelope;
37 class LFO;
38 class Filter;
39 
40 // Globals
41 
42 #define FM_AMP_MULTIPLIER 14.71280603f
43 
44 #define OSCIL_SMP_EXTRA_SAMPLES 5
45 
46 class SynthEngine;
47 
48 class ADnote
49 {
50     public:
51         ADnote(ADnoteParameters *adpars_, Controller *ctl_, float freq_, float velocity_,
52                int portamento_, int midinote_, SynthEngine *_synth);
53         ADnote(ADnote *topVoice_, float freq_, int phase_offset_, int subVoiceNumber_,
54                float *parentFMmod_, bool forFM_);
55         ADnote(const ADnote &orig, ADnote *topVoice_ = NULL, float *parentFMmod_ = NULL);
56         ~ADnote();
57 
58         void construct();
59 
60         int noteout(float *outl, float *outr);
61         void releasekey();
finished()62         bool finished() const
63         {
64             return NoteStatus == NOTE_DISABLED ||
65                 (NoteStatus != NOTE_KEEPALIVE && legatoFade == 0.0f);
66         }
67         void legatoFadeIn(float freq_, float velocity_, int portamento_, int midinote_);
68         void legatoFadeOut(const ADnote &syncwith);
69 
70         // Whether the note has samples to output.
71         // Currently only used for dormant legato notes.
ready()72         bool ready() { return legatoFade != 0.0f || legatoFadeStep != 0.0f; };
73 
74     private:
75 
76         void setfreq(int nvoice, float in_freq, float pitchdetune);
77         void setfreqFM(int nvoice, float in_freq, float pitchdetune);
setPitchDetuneFromParent(float pitch)78         void setPitchDetuneFromParent(float pitch)
79         {
80             detuneFromParent = pitch;
81         }
setUnisonDetuneFromParent(float factor)82         void setUnisonDetuneFromParent(float factor)
83         {
84             unisonDetuneFactorFromParent = factor;
85         }
86         void computeUnisonFreqRap(int nvoice);
87         void computeNoteParameters(void);
88         void computeWorkingParameters(void);
89         void initParameters(void);
90         void initSubVoices(void);
91         void killVoice(int nvoice);
92         void killNote(void);
93         float getVoiceBaseFreq(int nvoice);
94         float getFMVoiceBaseFreq(int nvoice);
95         void computeVoiceOscillatorLinearInterpolation(int nvoice);
96         void applyVoiceOscillatorMorph(int nvoice);
97         void applyVoiceOscillatorRingModulation(int nvoice);
98         void computeVoiceModulator(int nvoice, int FMmode);
99         void computeVoiceModulatorLinearInterpolation(int nvoice);
100         void applyAmplitudeOnVoiceModulator(int nvoice);
101         void normalizeVoiceModulatorFrequencyModulation(int nvoice, int FMmode);
102         void computeVoiceModulatorFrequencyModulation(int nvoice, int FMmode);
103         void computeVoiceModulatorForFMFrequencyModulation(int nvoice);
104         void computeVoiceOscillatorFrequencyModulation(int nvoice);
105         void computeVoiceOscillatorForFMFrequencyModulation(int nvoice);
106             // FMmode = 0 for phase modulation, 1 for Frequency modulation
107         //  void ComputeVoiceOscillatorFrequencyModulation(int nvoice);
108         void computeVoiceOscillatorPitchModulation(int nvoice);
109 
110         void computeVoiceNoise(int nvoice);
111         void ComputeVoicePinkNoise(int nvoice);
112         void ComputeVoiceSpotNoise(int nvoice);
113 
114         void computeVoiceOscillator(int nvoice);
115 
116         void fadein(float *smps);
117 
118 
119         // Globals
120         ADnoteParameters *adpars;
121         bool  stereo;
122         int   midinote;
123         float velocity;
124         float basefreq;
125 
126         enum {
127             NOTE_DISABLED,
128             NOTE_ENABLED,
129             NOTE_KEEPALIVE
130         } NoteStatus;
131         Controller *ctl;
132 
133         // Global parameters
134         struct ADnoteGlobal {
135             // Frequency global parameters
136             float  Detune; // cents
137             Envelope *FreqEnvelope;
138             LFO      *FreqLfo;
139 
140             // Amplitude global parameters
141             float Volume;  //  0 .. 1
142             float randpanL;
143             float randpanR;
144 
145             Envelope *AmpEnvelope;
146             LFO      *AmpLfo;
147 
148             float Fadein_adjustment;
149             struct {
150                 int      Enabled;
151                 float initialvalue, dt, t;
152             } Punch;
153 
154             // Filter global parameters
155             Filter *GlobalFilterL;
156             Filter *GlobalFilterR;
157             Envelope *FilterEnvelope;
158             LFO      *FilterLfo;
159         } NoteGlobalPar;
160 
161         // Voice parameters
162         struct ADnoteVoice {
163             bool Enabled;
164             int Voice;        // Voice I use as source.
165             int noisetype;    // (sound/noise)
166             int filterbypass;
167             int DelayTicks;
168             float *OscilSmp;  // Waveform of the Voice. Shared with sub voices.
169             int phase_offset; // PWM emulation
170 
171             // Frequency parameters
172             int fixedfreq;   // if the frequency is fixed to 440 Hz
173             int fixedfreqET; // if the "fixed" frequency varies according to the note (ET)
174 
175             float Detune;     // cents = basefreq * VoiceDetune
176             float FineDetune;
177             float BendAdjust;
178             float OffsetHz;
179 
180             Envelope *FreqEnvelope;
181             LFO      *FreqLfo;
182 
183             // Amplitude parameters
184             float Volume;  // -1.0 .. 1.0
185             float Panning; // 0.0 = left, 0.5 = center, 1.0 = right
186             float randpanL;
187             float randpanR;
188 
189             Envelope *AmpEnvelope;
190             LFO      *AmpLfo;
191 
192             struct {
193                 int   Enabled;
194                 float initialvalue, dt, t;
195             } Punch;
196 
197             // Filter parameters
198             Filter   *VoiceFilterL;
199             Filter   *VoiceFilterR;
200 
201             Envelope *FilterEnvelope;
202             LFO      *FilterLfo;
203 
204             // Modulator parameters
205             FMTYPE FMEnabled;
206             bool FMringToSide;
207             unsigned char FMFreqFixed;
208             int    FMVoice;
209             float *VoiceOut; // Voice Output used by other voices if use this as modullator
210             float *FMSmp;    // Wave of the Voice. Shared by sub voices.
211             int    FMphase_offset;
212             float  FMVolume;
213             bool FMDetuneFromBaseOsc;  // Whether we inherit the base oscillator's detuning
214             float  FMDetune; // in cents
215             Envelope *FMFreqEnvelope;
216             Envelope *FMAmpEnvelope;
217         };
218         ADnoteVoice NoteVoicePar[NUM_VOICES];
219 
220         // Internal values of the note and of the voices
221         float time; // time from the start of the note
222         int Tspot; // spot noise noise interrupt time
223 
224         RandomGen paramRNG; // A preseeded random number generator, reseeded
225                             // with a known seed every time parameters are
226                             // updated. This allows parameters to be changed
227                             // smoothly. New notes will get a new seed.
228         uint32_t paramSeed; // The seed for paramRNG.
229 
230         //pinking filter (Paul Kellet)
231         float pinking[NUM_VOICES][14];
232 
233         int unison_size[NUM_VOICES]; // the size of unison for a single voice
234 
235         float unison_stereo_spread[NUM_VOICES]; // stereo spread of unison subvoices (0.0=mono,1.0=max)
236 
237         float *oscposlo[NUM_VOICES], *oscfreqlo[NUM_VOICES]; // fractional part (skip)
238 
239         int *oscposhi[NUM_VOICES], *oscfreqhi[NUM_VOICES]; // integer part (skip)
240 
241         float *oscposloFM[NUM_VOICES], *oscfreqloFM[NUM_VOICES]; // fractional part (skip) of the Modullator
242 
243         float *unison_base_freq_rap[NUM_VOICES]; // the unison base_value
244 
245         float *unison_freq_rap[NUM_VOICES]; // how the unison subvoice's frequency is changed (1.0 for no change)
246 
247         // These are set by parent voices.
248         float detuneFromParent;             // How much the voice should be detuned.
249         float unisonDetuneFactorFromParent; // How much the voice should be detuned from unison.
250 
251         bool *unison_invert_phase[NUM_VOICES]; // which unison subvoice has phase inverted
252 
253         struct { // unison vibratto
254             float  amplitude; // amplitude which be added to unison_freq_rap
255             float *step;      // value which increments the position
256             float *position;  // between -1.0 and 1.0
257         } unison_vibratto[NUM_VOICES];
258 
259         // integer part (skip) of the Modullator
260         int *oscposhiFM[NUM_VOICES];
261         int *oscfreqhiFM[NUM_VOICES];
262 
263         float oldamplitude[NUM_VOICES];  // used to compute and interpolate the
264         float newamplitude[NUM_VOICES];  // amplitudes of voices and modullators
265         float FMoldamplitude[NUM_VOICES];
266         float FMnewamplitude[NUM_VOICES];
267 
268         float *FMoldsmp[NUM_VOICES]; // used by Frequency Modulation (for integration)
269 
270         float *FMFMoldPhase[NUM_VOICES]; // use when rendering FM modulator with parent FM
271         float *FMFMoldInterpPhase[NUM_VOICES];
272         float *FMFMoldPMod[NUM_VOICES];
273         float *oscFMoldPhase[NUM_VOICES]; // use when rendering oscil with parent FM that will
274                                          // be used for FM
275         float *oscFMoldInterpPhase[NUM_VOICES];
276         float *oscFMoldPMod[NUM_VOICES];
277         bool forFM; // Whether this voice will be used for FM modulation.
278 
279         float **tmpwave_unison;
280         int max_unison;
281 
282         float **tmpmod_unison;
283         bool freqbasedmod[NUM_VOICES];
284 
285         float globaloldamplitude; // interpolate the amplitudes
286         float globalnewamplitude;
287 
288         char firsttick[NUM_VOICES]; // 1 - if it is the fitst tick.
289                                     // used to fade in the sound
290 
291         int portamento; // 1 if the note has portamento
292 
293         float bandwidthDetuneMultiplier; // how the fine detunes are made bigger or smaller
294 
295         // Legato vars
296         float legatoFade;
297         float legatoFadeStep;
298 
299         float pangainL;
300         float pangainR;
301 
302         ADnote **subVoice[NUM_VOICES];
303         ADnote **subFMVoice[NUM_VOICES];
304 
305         int subVoiceNumber;
306         // For sub voices: The original, "topmost" voice that triggered this
307         // one.
308         ADnote *topVoice;
309         // For sub voices: Pointer to the closest parent that has
310         // phase/frequency modulation.
311         float *parentFMmod;
312 
313         Presets::PresetsUpdate paramsUpdate;
314 
315         SynthEngine *synth;
316 };
317 
318 #endif
319