summaryrefslogtreecommitdiff
path: root/muse/synti/zynaddsubfx/Synth/LFO.C
blob: 4ae548c1c2731f70f0634c163da7d4358497fc3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
  ZynAddSubFX - a software synthesizer
 
  LFO.C - LFO implementation
  Copyright (C) 2002-2005 Nasca Octavian Paul
  Author: Nasca Octavian Paul

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License 
  as published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License (version 2) for more details.

  You should have received a copy of the GNU General Public License (version 2)
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

*/

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include "LFO.h"


LFO::LFO(LFOParams *lfopars,REALTYPE basefreq){
    if (lfopars->Pstretch==0) lfopars->Pstretch=1;
    REALTYPE lfostretch=pow(basefreq/440.0,(lfopars->Pstretch-64.0)/63.0);//max 2x/octave

    REALTYPE lfofreq=(pow(2,lfopars->Pfreq*10.0)-1.0)/12.0*lfostretch;
    incx=fabs(lfofreq)*(REALTYPE)SOUND_BUFFER_SIZE/(REALTYPE)SAMPLE_RATE;

    if (lfopars->Pcontinous==0){
	if (lfopars->Pstartphase==0) x=RND;
	else x=fmod((lfopars->Pstartphase-64.0)/127.0+1.0,1.0);
    } else {
	REALTYPE tmp=fmod(lfopars->time*incx,1.0);
	x=fmod((lfopars->Pstartphase-64.0)/127.0+1.0+tmp,1.0);
    };

    //Limit the Frequency(or else...)
    if (incx>0.49999999) incx=0.499999999;
    

    lfornd=lfopars->Prandomness/127.0;
    if (lfornd<0.0) lfornd=0.0; else if (lfornd>1.0) lfornd=1.0;

//    lfofreqrnd=pow(lfopars->Pfreqrand/127.0,2.0)*2.0*4.0;
    lfofreqrnd=pow(lfopars->Pfreqrand/127.0,2.0)*4.0;

    switch (lfopars->fel){
	case 1:lfointensity=lfopars->Pintensity/127.0;break;
	case 2:lfointensity=lfopars->Pintensity/127.0*4.0;break;//in octave
	default:lfointensity=pow(2,lfopars->Pintensity/127.0*11.0)-1.0;//in centi
		x-=0.25;//chance the starting phase
		break;
    };

    amp1=(1-lfornd)+lfornd*RND;
    amp2=(1-lfornd)+lfornd*RND;
    lfotype=lfopars->PLFOtype;
    lfodelay=lfopars->Pdelay/127.0*4.0;//0..4 sec
    incrnd=nextincrnd=1.0;
    freqrndenabled=(lfopars->Pfreqrand!=0);
    computenextincrnd();
    computenextincrnd();//twice because I want incrnd & nextincrnd to be random
};

LFO::~LFO(){
};

/*
 * LFO out
 */
REALTYPE LFO::lfoout(){
    REALTYPE out;    
    switch (lfotype){
	case 1: //LFO_TRIANGLE
		if ((x>=0.0)&&(x<0.25)) out=4.0*x;
		else if ((x>0.25)&&(x<0.75)) out=2-4*x;
		     else out=4.0*x-4.0;
		break;
	case 2: //LFO_SQUARE
		if (x<0.5) out=-1; 
		    else   out=1;
		break;
	case 3: //LFO_RAMPUP
		out=(x-0.5)*2.0;
		break;
	case 4: //LFO_RAMPDOWN
		out=(0.5-x)*2.0;
		break;
	case 5: //LFO_EXP_DOWN 1
		out=pow(0.05,x)*2.0-1.0;
		break;
	case 6: //LFO_EXP_DOWN 2
		out=pow(0.001,x)*2.0-1.0;
		break;
	default:out=cos(x*2.0*PI);//LFO_SINE
    };


    if ((lfotype==0)||(lfotype==1)) out*=lfointensity*(amp1+x*(amp2-amp1));
        else out*=lfointensity*amp2;
    if (lfodelay<0.00001) {
	    if (freqrndenabled==0) x+=incx;
		else {
		    float tmp=(incrnd*(1.0-x)+nextincrnd*x);
		    if (tmp>1.0) tmp=1.0;
			else if (tmp<0.0) tmp=0.0;
		    x+=incx*tmp;
		};
	    if (x>=1) {
		x=fmod(x,1.0);
		amp1=amp2;
		amp2=(1-lfornd)+lfornd*RND;

		computenextincrnd();
	    };
    } else lfodelay-=(REALTYPE)SOUND_BUFFER_SIZE/(REALTYPE)SAMPLE_RATE;
    return(out);
};

/*
 * LFO out (for amplitude)
 */
REALTYPE LFO::amplfoout(){
    REALTYPE out;
    out=1.0-lfointensity+lfoout();
    if (out<-1.0) out=-1.0;
	else if (out>1.0) out=1.0;
    return(out);
};


void LFO::computenextincrnd(){
	if (freqrndenabled==0) return;
	incrnd=nextincrnd;
	nextincrnd=pow(0.5,lfofreqrnd)+RND*(pow(2.0,lfofreqrnd)-1.0);
};