1 /*
2  * Author: Harry van Haaren 2014
3  *         harryhaaren@gmail.com
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18  * MA 02110-1301, USA.
19  */
20 
21 #ifndef OPENAV_FABLA2_PAD_HXX
22 #define OPENAV_FABLA2_PAD_HXX
23 
24 #include <vector>
25 #include <string>
26 #include <stdio.h>
27 
28 namespace Fabla2
29 {
30 
31 class Sample;
32 class Fabla2DSP;
33 
34 /** Pad
35  * The Pad class is a collection of resources that apply to this pad. As multiple
36  * samples can be hosted on one pad, it is essential to have a clean structure to
37  * maintain these resources. The Pad class takes care of this.
38  *
39  * Multiple samples are contained here, as well as the necessary options to play
40  * them back: round-robin or velocity mapped? Both?
41  *
42  * The Library class loads / saves data from the Pads, providing a clean method
43  * to interact with the Pad contents in an RT-safe way.
44  */
45 class Pad
46 {
47 public:
48 	Pad( Fabla2DSP* dsp, int rate, int ID );
49 	~Pad();
50 
51 	/// position of pad (0 - 15) drum pads layout where 0 is bottom left
ID()52 	int ID()
53 	{
54 		return ID_;
55 	}
56 
57 	// arbitrary limit of 20 chars max for names
58 	void setName( const char* n );
getName()59 	const char* getName()
60 	{
61 		return name;
62 	}
63 
64 	/// the bank this Pad is on
bank(int b)65 	void bank(int b)
66 	{
67 		bank_ = b;
68 	}
bank()69 	int bank()
70 	{
71 		return bank_;
72 	}
73 
74 	/// library functions
75 	void add( Sample* );
76 	void remove( Sample* s );
77 
78 	void clearAllSamples();
loaded()79 	bool loaded()
80 	{
81 		return loaded_;
82 	}
83 
muteGroup(int mg)84 	void muteGroup( int mg )
85 	{
86 		muteGroup_ = mg;
87 	}
muteGroup()88 	int  muteGroup()
89 	{
90 		return muteGroup_;
91 	}
92 
offGroup(int og)93 	void offGroup( int og )
94 	{
95 		offGroup_ = og;
96 	}
offGroup()97 	int  offGroup()
98 	{
99 		return offGroup_;
100 	}
101 
102 	/// Sets play method for samples: gated or one-shot
103 	enum TRIGGER_MODE {
104 		TM_GATED = 0,       /// Note on starts, note off does ADSR->release()
105 		TM_ONE_SHOT,        /// Always plays samples until end
106 	};
107 
triggerMode(TRIGGER_MODE tm)108 	void triggerMode( TRIGGER_MODE tm )
109 	{
110 		triggerMode_ = tm;
111 	}
triggerMode()112 	int triggerMode()
113 	{
114 		return triggerMode_;
115 	}
116 
117 	/// get a layer: wether its velocity or Round-robin doesn't matter: this
118 	/// is for UI interaction
nLayers()119 	int nLayers()
120 	{
121 		return samples.size();
122 	}
123 	Sample* layer( int id );
124 
125 	/// playback functions
126 	int lastPlayedLayer();
127 	Sample* getPlaySample( float velocity );
128 	float getPlayVolume( float velocity );
129 
130 	/// Sets the switch system between samples
131 	enum SAMPLE_SWITCH_SYSTEM {
132 		SS_NONE = 0,        /// always plays selected sample
133 		SS_VELOCITY_VOLUME, /// changes volume according to velocity
134 		SS_ROUND_ROBIN,     /// iterates over all samples incrementally
135 		SS_VELOCITY_LAYERS, /// takes velocity into account, and plays a sample
136 
137 		// min & max values for validation & input
138 		SS_MIN = SS_NONE,
139 		SS_MAX = SS_VELOCITY_LAYERS,
140 	};
141 	void switchSystem( SAMPLE_SWITCH_SYSTEM sss );
switchSystem()142 	int switchSystem() { return sampleSwitchSystem; }
143 	void layersDistribute();
144 
145 	// MIDI sampling
146 	void midiNoteAdd(int note, int velo);
147 	void midiNotesClear();
148 
149 	typedef struct {
150 		int note;
151 		int velocity;
152 	} MidiNote ;
getMidiNotes()153 	std::vector<MidiNote>* getMidiNotes(){return &midiNotes;}
154 
155 
156 	/// testing func
157 	void checkAll();
158 
159 	/// volume: is used by Sampler to multiply into each sample. Default 0.75.
160 	float volume;
161 	float sends[4];
162 
163 private:
164 	Fabla2DSP* dsp;
165 	int sr;
166 	int bank_;// pad bank
167 	int ID_; // pad place within Bank
168 	int muteGroup_;
169 	int offGroup_;
170 	int triggerMode_;
171 	bool loaded_;
172 
173 	std::vector<MidiNote> midiNotes;
174 
175 	char name[21];
176 
177 	SAMPLE_SWITCH_SYSTEM sampleSwitchSystem;
178 	int sampleLayerCounter;
179 
180 	/// shared pointer to each of the samples available on this pad
181 	std::vector<Sample*> samples;
182 };
183 
184 };
185 
186 #endif // OPENAV_FABLA2_PAD_HXX
187