summaryrefslogtreecommitdiff
path: root/synth/lfos.cpp
blob: 59169374e718572f5538a7b61655821dc9234f34 (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
#include <math.h>
#include <stdlib.h>

#include "lfos.h"
#include "globals.h"
#include "fixed.h"
#include "defines.h"

void uninit_lfo(int i)
{
	if (lfo[i])
	{
		for (int j=0;j<lfo_res[i];j++)
			delete [] lfo[i][j];
		
		delete [] lfo[i];
		lfo[i]=NULL;
	}
}

void init_lfo(int i)
{
	//two possible divisions by zero are avoided, because
	//values <= 0 will make the program use the default
	//(nonzero) values.
	lfo_res[i]=samp_rate/lfo_freq_hz[i]/lfo_update_frames;

	lfo[i]=new fixed_t* [lfo_res[i]];
	for (int j=0;j<lfo_res[i];j++)
	{
		lfo[i][j]=new fixed_t [N_LFO_LEVELS];
		float temp=sin(j*2.0*3.141592654/lfo_res[i]);
		for (int k=0;k<N_LFO_LEVELS;k++)
			lfo[i][j][k]= (1.0 + temp*(float(LFO_MAX)*k/N_LFO_LEVELS)) * ONE;
	}

}

void init_snh()
{
	sample_and_hold_frames=samp_rate/snh_freq_hz;
}

void maybe_calc_lfos()
{
	static int lfocnt=0;
	static int snhcnt=0;
	
	if (lfocnt==0)
	{
		lfocnt=lfo_update_frames;
		
		for (int i=0;i<N_LFOS;i++)
		{
			lfo_phase[i]=(lfo_phase[i]+1)%lfo_res[i];
			curr_lfo[i]=lfo[i][lfo_phase[i]];
		}
	}
	
	if (snhcnt==0)
	{
		snhcnt=sample_and_hold_frames;

		//temp ranges between -ONE and ONE
		fixed_t temp = (float(rand())/(RAND_MAX/2)  - 1.0) * ONE;
		
		for (int i=0;i<N_LFO_LEVELS;i++)
			sample_and_hold[i]= temp*i/(N_LFO_LEVELS-1) + ONE;
		
		curr_lfo[SNH_LFO]=sample_and_hold;
		// could be moved to some init function, but looks clearer and
		// does not eat up the cpu too much ;)
	}
	
	lfocnt--;
	snhcnt--;
}