summaryrefslogtreecommitdiff
path: root/synth/envelope.h
blob: 0024f56a541fb431332387b7670b90a295a5aefa (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
/*
    Copyright (C) 2010-2012 Florian Jung
     
    This file is part of flo's FM synth.

    flo's FM synth is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    flo's FM synth 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 for more details.

    You should have received a copy of the GNU General Public License
    along with flo's FM synth.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef __ENVELOPE_H__
#define __ENVELOPE_H__

#include <jack/jack.h>

#include "programs.h"
#include "fixed.h"

// when frames is given, this tells the envelope that get_level() is
// only called every (frames) frames. 1 means "every time", 2 means
// "every second time" and so on.
// the caller must manage to call get_level() to the appropriate times

class Envelope
{
	public:
		Envelope(env_settings_t s, int frames=1);
		void release_key();
		void reattack();
		void reset();
		fixed_t get_level();
		bool still_active();
		void set_hold(bool h);		
		void set_attack(jack_nframes_t a);
		void set_decay(jack_nframes_t d);
		void set_sustain(fixed_t s);
		void set_release(jack_nframes_t r);
		void set_max(fixed_t m)
		{
			max=m;
			if (max>ONE) max=ONE;
			else if (max<0) max=0;
		}
		void set_ratefactor(double factor);
		
		bool get_hold() { return hold; }
		jack_nframes_t get_attack() { return attack_orig; }
		jack_nframes_t get_decay() { return decay_orig; }
		fixed_t get_sustain() { return sustain_orig; }
		jack_nframes_t get_release() { return release_orig; }
		
		
	private:
		fixed_t max;
		jack_nframes_t attack;
		jack_nframes_t decay;
		jack_nframes_t release;
		jack_nframes_t attack_orig;
		jack_nframes_t decay_orig;
		jack_nframes_t release_orig;
		jack_nframes_t rel_t;
		fixed_t sustain;
		fixed_t sustain_orig;
		fixed_t level;
		bool hold;
		bool has_release_phase;
		jack_nframes_t t;
		fixed_t ratefactor;
		
		int nth_frame;
		
		enum
		{
			ATTACK,
			DECAY,
			HOLD,
			RELEASE,
			DONE
		} state;
};

#endif