1 |
iliev |
2012 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
|
|
* Copyright (C) 2008-2009 Anders Dahnielson <anders@dahnielson.com> * |
6 |
|
|
* Copyright (C) 2009 Grigor Iliev * |
7 |
|
|
* * |
8 |
|
|
* This program is free software; you can redistribute it and/or modify * |
9 |
|
|
* it under the terms of the GNU General Public License as published by * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This program is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this program; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#ifndef LIBSFZ_SFZ_H |
25 |
|
|
#define LIBSFZ_SFZ_H |
26 |
|
|
|
27 |
|
|
#include <fstream> |
28 |
|
|
#include <iostream> |
29 |
|
|
#include <vector> |
30 |
|
|
#include <string> |
31 |
|
|
#include <stdexcept> |
32 |
|
|
|
33 |
|
|
#include "../common/SampleFile.h" |
34 |
|
|
#include "../common/SampleManager.h" |
35 |
|
|
|
36 |
|
|
#define TRIGGER_ATTACK ((unsigned char) (1 << 0)) // 0x01 |
37 |
|
|
#define TRIGGER_RELEASE ((unsigned char) (1 << 1)) // 0x02 |
38 |
|
|
#define TRIGGER_FIRST ((unsigned char) (1 << 2)) // 0x04 |
39 |
|
|
#define TRIGGER_LEGATO ((unsigned char) (1 << 3)) // 0x08 |
40 |
|
|
|
41 |
|
|
namespace sfz |
42 |
|
|
{ |
43 |
|
|
// Forward declarations |
44 |
|
|
class Articulation; |
45 |
|
|
class Region; |
46 |
|
|
class Group; |
47 |
|
|
class Instrument; |
48 |
|
|
class File; |
49 |
|
|
|
50 |
|
|
// Enumerations |
51 |
|
|
enum sw_vel_t { VEL_CURRENT, VEL_PREVIOUS }; |
52 |
|
|
enum off_mode_t { OFF_FAST, OFF_NORMAL }; |
53 |
|
|
enum loop_mode_t { NO_LOOP, ONE_SHOT, LOOP_CONTINOUS, LOOP_SUSTAIN }; |
54 |
|
|
enum curve_t { GAIN, POWER }; |
55 |
|
|
enum filter_t { LPF_1P, HPF_1P, BPF_1P, BRF_1P, APF_1P, |
56 |
|
|
LPF_2P, HPF_2P, BPF_2P, BRF_2P, PKF_2P, |
57 |
|
|
LPF_4P, HPF_4P, |
58 |
|
|
LPF_6P, HPF_6P }; |
59 |
|
|
|
60 |
|
|
typedef unsigned char trigger_t; |
61 |
|
|
typedef unsigned char uint8_t; |
62 |
|
|
|
63 |
|
|
class SampleManager : public LinuxSampler::SampleManager<LinuxSampler::SampleFile, Region> { |
64 |
|
|
public: |
65 |
|
|
LinuxSampler::SampleFile* FindSample(std::string samplePath); |
66 |
|
|
|
67 |
|
|
protected: |
68 |
|
|
virtual void OnSampleInUse(LinuxSampler::SampleFile* pSample) { |
69 |
|
|
pSample->Open(); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
virtual void OnSampleInNotUse(LinuxSampler::SampleFile* pSample) { |
73 |
|
|
pSample->Close(); |
74 |
|
|
} |
75 |
|
|
}; |
76 |
|
|
|
77 |
|
|
///////////////////////////////////////////////////////////// |
78 |
|
|
// class Exception |
79 |
|
|
|
80 |
|
|
class Exception : |
81 |
|
|
public std::runtime_error |
82 |
|
|
{ |
83 |
|
|
public: |
84 |
|
|
Exception(const std::string& msg) : |
85 |
|
|
runtime_error(msg) |
86 |
|
|
{ |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
std::string Message() |
90 |
|
|
{ |
91 |
|
|
return what(); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
void PrintMessage() |
95 |
|
|
{ |
96 |
|
|
std::cerr << what() << std::endl << std::flush; |
97 |
|
|
} |
98 |
|
|
}; |
99 |
|
|
|
100 |
|
|
///////////////////////////////////////////////////////////// |
101 |
|
|
// class optional |
102 |
|
|
|
103 |
|
|
// Handy class nicked from LinuxSampler... |
104 |
|
|
// Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck |
105 |
|
|
// Copyright (C) 2005, 2006 Christian Schoenebeck |
106 |
|
|
|
107 |
|
|
class optional_base |
108 |
|
|
{ |
109 |
|
|
public: |
110 |
|
|
class nothing_t { public: nothing_t() {} }; |
111 |
|
|
static const nothing_t nothing; |
112 |
|
|
}; |
113 |
|
|
|
114 |
|
|
template<class T> |
115 |
|
|
class optional : |
116 |
|
|
public optional_base |
117 |
|
|
{ |
118 |
|
|
public: |
119 |
|
|
optional() |
120 |
|
|
{ |
121 |
|
|
initialized = false; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
optional(T data) |
125 |
|
|
{ |
126 |
|
|
this->data = data; |
127 |
|
|
initialized = true; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
optional(nothing_t) |
131 |
|
|
{ |
132 |
|
|
initialized = false; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
template <class T_inner> |
136 |
|
|
optional(T_inner data) |
137 |
|
|
{ |
138 |
|
|
this->data = T(data); |
139 |
|
|
initialized = true; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
const T& get() const throw (Exception) |
143 |
|
|
{ |
144 |
|
|
if (!initialized) throw Exception("optional variable not initialized"); |
145 |
|
|
return data; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
T& get() throw (Exception) |
149 |
|
|
{ |
150 |
|
|
if (!initialized) throw Exception("optional variable not initialized"); |
151 |
|
|
return data; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
void unset() |
155 |
|
|
{ |
156 |
|
|
initialized = false; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
optional& operator =(const optional& arg) throw (Exception) |
160 |
|
|
{ |
161 |
|
|
if (!arg.initialized) { |
162 |
|
|
initialized = false; |
163 |
|
|
} else { |
164 |
|
|
this->data = arg.data; |
165 |
|
|
initialized = true; |
166 |
|
|
} |
167 |
|
|
return *this; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
optional& operator =(const T& arg) |
171 |
|
|
{ |
172 |
|
|
this->data = arg; |
173 |
|
|
initialized = true; |
174 |
|
|
return *this; |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
const T& operator *() const throw (Exception) { return get(); } |
178 |
|
|
T& operator *() throw (Exception) { return get(); } |
179 |
|
|
|
180 |
|
|
const T* operator ->() const throw (Exception) |
181 |
|
|
{ |
182 |
|
|
if (!initialized) throw Exception("optional variable not initialized"); |
183 |
|
|
return &data; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
T* operator ->() throw (Exception) |
187 |
|
|
{ |
188 |
|
|
if (!initialized) throw Exception("optional variable not initialized"); |
189 |
|
|
return &data; |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
operator bool() const { return initialized; } |
193 |
|
|
bool operator !() const { return !initialized; } |
194 |
|
|
|
195 |
|
|
protected: |
196 |
|
|
T data; |
197 |
|
|
bool initialized; |
198 |
|
|
}; |
199 |
|
|
|
200 |
|
|
///////////////////////////////////////////////////////////// |
201 |
|
|
// class Articulation |
202 |
|
|
|
203 |
|
|
// Articulation containing all performance parameters for synthesis |
204 |
|
|
class Articulation |
205 |
|
|
{ |
206 |
|
|
public: |
207 |
|
|
Articulation(); |
208 |
|
|
virtual ~Articulation(); |
209 |
|
|
}; |
210 |
|
|
|
211 |
|
|
///////////////////////////////////////////////////////////// |
212 |
|
|
// class Definition |
213 |
|
|
|
214 |
|
|
// Base definition used by groups and regions |
215 |
|
|
class Definition |
216 |
|
|
{ |
217 |
|
|
public: |
218 |
|
|
Definition(); |
219 |
|
|
virtual ~Definition(); |
220 |
|
|
|
221 |
|
|
// sample definition |
222 |
|
|
std::string sample; |
223 |
|
|
|
224 |
|
|
// input controls |
225 |
|
|
int lochan; int hichan; |
226 |
|
|
int lokey; int hikey; |
227 |
|
|
int lovel; int hivel; |
228 |
|
|
std::vector<int> locc; std::vector<int> hicc; |
229 |
|
|
int lobend; int hibend; |
230 |
|
|
int lobpm; int hibpm; |
231 |
|
|
int lochanaft; int hichanaft; |
232 |
|
|
int lopolyaft; int hipolyaft; |
233 |
|
|
int loprog; int hiprog; |
234 |
|
|
float lorand; float hirand; |
235 |
|
|
float lotimer; float hitimer; |
236 |
|
|
|
237 |
|
|
int seq_length; |
238 |
|
|
int seq_position; |
239 |
|
|
|
240 |
|
|
std::vector<int> start_locc; std::vector<int> start_hicc; |
241 |
|
|
std::vector<int> stop_locc; std::vector<int> stop_hicc; |
242 |
|
|
|
243 |
|
|
int sw_lokey; int sw_hikey; |
244 |
|
|
int sw_last; |
245 |
|
|
int sw_down; |
246 |
|
|
int sw_up; |
247 |
|
|
int sw_previous; |
248 |
|
|
sw_vel_t sw_vel; |
249 |
|
|
|
250 |
|
|
trigger_t trigger; |
251 |
|
|
|
252 |
|
|
optional<int> group; |
253 |
|
|
optional<int> off_by; |
254 |
|
|
off_mode_t off_mode; |
255 |
|
|
|
256 |
|
|
std::vector<int> on_locc; std::vector<int> on_hicc; |
257 |
|
|
|
258 |
|
|
// sample player |
259 |
|
|
optional<int> count; |
260 |
|
|
optional<float> delay; optional<float> delay_random; std::vector<optional<float> > delay_oncc; |
261 |
|
|
optional<int> delay_beats; optional<int> stop_beats; |
262 |
|
|
optional<int> delay_samples; std::vector<optional<int> > delay_samples_oncc; |
263 |
|
|
optional<int> end; |
264 |
|
|
optional<float> loop_crossfade; |
265 |
|
|
optional<int> offset; optional<int> offset_random; std::vector<optional<int> > offset_oncc; |
266 |
|
|
loop_mode_t loop_mode; |
267 |
|
|
optional<int> loop_start; optional<int> loop_end; |
268 |
|
|
optional<int> sync_beats; |
269 |
|
|
optional<int> sync_offset; |
270 |
|
|
|
271 |
|
|
// amplifier |
272 |
|
|
float volume; |
273 |
|
|
float pan; |
274 |
|
|
float width; |
275 |
|
|
float position; |
276 |
|
|
float amp_keytrack; int amp_keycenter; float amp_veltrack; std::vector<float> amp_velcurve_; float amp_random; |
277 |
|
|
float rt_decay; |
278 |
|
|
std::vector<float> gain_oncc; |
279 |
|
|
int xfin_lokey; int xfin_hikey; |
280 |
|
|
int xfout_lokey; int xfout_hikey; |
281 |
|
|
curve_t xf_keycurve; |
282 |
|
|
int xfin_lovel; int xfin_hivel; |
283 |
|
|
int xfout_lovel; int xfout_hivel; |
284 |
|
|
curve_t xf_velcurve; |
285 |
|
|
std::vector<int> xfin_locc; std::vector<int> xfin_hicc; |
286 |
|
|
std::vector<int> xfout_locc; std::vector<int> xfout_hicc; |
287 |
|
|
curve_t xf_cccurve; |
288 |
|
|
|
289 |
|
|
// pitch |
290 |
|
|
int transpose; |
291 |
|
|
int tune; |
292 |
|
|
int pitch_keycenter; int pitch_keytrack; int pitch_veltrack; int pitch_random; |
293 |
|
|
int bend_up; int bend_down; int bend_step; |
294 |
|
|
|
295 |
|
|
// filter |
296 |
|
|
filter_t fil_type; filter_t fil2_type; |
297 |
|
|
optional<float> cutoff; optional<float> cutoff2; |
298 |
|
|
std::vector<int> cutoff_oncc; std::vector<int> cutoff2_oncc; |
299 |
|
|
std::vector<int> cutoff_smoothcc; std::vector<int> cutoff2_smoothcc; |
300 |
|
|
std::vector<int> cutoff_stepcc; std::vector<int> cutoff2_stepcc; |
301 |
|
|
std::vector<int> cutoff_curvecc; std::vector<int> cutoff2_curvecc; |
302 |
|
|
int cutoff_chanaft; int cutoff2_chanaft; |
303 |
|
|
int cutoff_polyaft; int cutoff2_polyaft; |
304 |
|
|
float resonance; float resonance2; |
305 |
|
|
std::vector<int> resonance_oncc; std::vector<int> resonance2_oncc; |
306 |
|
|
std::vector<int> resonance_smoothcc; std::vector<int> resonance2_smoothcc; |
307 |
|
|
std::vector<int> resonance_stepcc; std::vector<int> resonance2_stepcc; |
308 |
|
|
std::vector<int> resonance_curvecc; std::vector<int> resonance2_curvecc; |
309 |
|
|
int fil_keytrack; int fil2_keytrack; |
310 |
|
|
int fil_keycenter; int fil2_keycenter; |
311 |
|
|
int fil_veltrack; int fil2_veltrack; |
312 |
|
|
int fil_random; int fil2_random; |
313 |
|
|
|
314 |
|
|
// per voice equalizer |
315 |
|
|
float eq1_freq; float eq2_freq; float eq3_freq; |
316 |
|
|
std::vector<float> eq1_freq_oncc; std::vector<float> eq2_freq_oncc; std::vector<float> eq3_freq_oncc; |
317 |
|
|
float eq1_vel2freq; float eq2_vel2freq; float eq3_vel2freq; |
318 |
|
|
float eq1_bw; float eq2_bw; float eq3_bw; |
319 |
|
|
std::vector<float> eq1_bw_oncc; std::vector<float> eq2_bw_oncc; std::vector<float> eq3_bw_oncc; |
320 |
|
|
float eq1_gain; float eq2_gain; float eq3_gain; |
321 |
|
|
std::vector<float> eq1_gain_oncc; std::vector<float> eq2_gain_oncc; std::vector<float> eq3_gain_oncc; |
322 |
|
|
float eq1_vel2gain; float eq2_vel2gain; float eq3_vel2gain; |
323 |
|
|
}; |
324 |
|
|
|
325 |
|
|
///////////////////////////////////////////////////////////// |
326 |
|
|
// class Region |
327 |
|
|
|
328 |
|
|
/// Defines Region information of an Instrument |
329 |
|
|
class Region : |
330 |
|
|
public Definition |
331 |
|
|
{ |
332 |
|
|
public: |
333 |
|
|
Region(); |
334 |
|
|
virtual ~Region(); |
335 |
|
|
|
336 |
|
|
LinuxSampler::SampleFile* pSample; |
337 |
|
|
LinuxSampler::SampleFile* GetSample(bool create = true); |
338 |
|
|
void DestroySampleIfNotUsed(); |
339 |
|
|
|
340 |
|
|
Region* GetParent() { return this; }; // needed by EngineBase |
341 |
|
|
Instrument* GetInstrument() { return pInstrument; } |
342 |
|
|
void SetInstrument(Instrument* pInstrument) { this->pInstrument = pInstrument; } |
343 |
|
|
|
344 |
|
|
/// Return true if region is triggered by key |
345 |
|
|
bool OnKey(uint8_t chan, uint8_t key, uint8_t vel, |
346 |
|
|
int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, |
347 |
|
|
uint8_t prog, float rand, trigger_t trig, uint8_t* cc, |
348 |
|
|
float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key); |
349 |
|
|
|
350 |
|
|
/// Return true if region is triggered by control change |
351 |
|
|
bool OnControl(uint8_t chan, uint8_t cont, uint8_t val, |
352 |
|
|
int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, |
353 |
|
|
uint8_t prog, float rand, trigger_t trig, uint8_t* cc, |
354 |
|
|
float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key); |
355 |
|
|
|
356 |
|
|
/// Return an articulation for the current state |
357 |
|
|
Articulation* GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc); |
358 |
|
|
|
359 |
|
|
// unique region id |
360 |
|
|
int id; |
361 |
|
|
|
362 |
|
|
private: |
363 |
|
|
Instrument* pInstrument; |
364 |
|
|
}; |
365 |
|
|
|
366 |
|
|
///////////////////////////////////////////////////////////// |
367 |
|
|
// class Instrument |
368 |
|
|
|
369 |
|
|
/// Provides all neccessary information for the synthesis of an Instrument |
370 |
|
|
class Instrument : public SampleManager |
371 |
|
|
{ |
372 |
|
|
public: |
373 |
|
|
Instrument(std::string name = "Unknown", SampleManager* pSampleManager = NULL); |
374 |
|
|
virtual ~Instrument(); |
375 |
|
|
|
376 |
|
|
std::string GetName() { return name; } |
377 |
|
|
SampleManager* GetSampleManager() { return pSampleManager; } |
378 |
|
|
|
379 |
|
|
/** |
380 |
|
|
* @returns All regions that are triggered by key |
381 |
|
|
*/ |
382 |
|
|
std::vector<Region*> GetRegionsOnKey ( |
383 |
|
|
uint8_t chan, uint8_t key, uint8_t vel, |
384 |
|
|
int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, |
385 |
|
|
uint8_t prog, float rand, trigger_t trig, uint8_t* cc, |
386 |
|
|
float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key |
387 |
|
|
); |
388 |
|
|
|
389 |
|
|
bool DestroyRegion(Region* pRegion); |
390 |
|
|
bool HasKeyBinding(uint8_t key); |
391 |
|
|
bool HasKeySwitchBinding(uint8_t key); |
392 |
|
|
|
393 |
|
|
/// List of Regions belonging to this Instrument |
394 |
|
|
std::vector<Region*> regions; |
395 |
|
|
|
396 |
|
|
friend class sfz::File; |
397 |
|
|
|
398 |
|
|
private: |
399 |
|
|
std::string name; |
400 |
|
|
std::vector<bool> KeyBindings; |
401 |
|
|
std::vector<bool> KeySwitchBindings; |
402 |
|
|
SampleManager* pSampleManager; |
403 |
|
|
}; |
404 |
|
|
|
405 |
|
|
///////////////////////////////////////////////////////////// |
406 |
|
|
// class Group |
407 |
|
|
|
408 |
|
|
/// A Group act just as a template containing Region default values |
409 |
|
|
class Group : |
410 |
|
|
public Definition |
411 |
|
|
{ |
412 |
|
|
public: |
413 |
|
|
Group(); |
414 |
|
|
virtual ~Group(); |
415 |
|
|
|
416 |
|
|
/// Reset Group to default values |
417 |
|
|
void Reset(); |
418 |
|
|
|
419 |
|
|
/// Create a new Region |
420 |
|
|
Region* RegionFactory(); |
421 |
|
|
|
422 |
|
|
// id counter |
423 |
|
|
int id; |
424 |
|
|
|
425 |
|
|
}; |
426 |
|
|
|
427 |
|
|
///////////////////////////////////////////////////////////// |
428 |
|
|
// class File |
429 |
|
|
|
430 |
|
|
/// Parses SFZ files and provides abstract access to the data |
431 |
|
|
class File |
432 |
|
|
{ |
433 |
|
|
public: |
434 |
|
|
/// Load an existing SFZ file |
435 |
|
|
File(std::string file, SampleManager* pSampleManager = NULL); |
436 |
|
|
virtual ~File(); |
437 |
|
|
|
438 |
|
|
/// Returns a pointer to the instrument object |
439 |
|
|
Instrument* GetInstrument(); |
440 |
|
|
|
441 |
|
|
private: |
442 |
|
|
void push_header(std::string token); |
443 |
|
|
void push_opcode(std::string token); |
444 |
|
|
int parseInt(std::string value); |
445 |
|
|
float parseFloat(std::string value); |
446 |
|
|
|
447 |
|
|
std::string currentDir; |
448 |
|
|
/// Pointer to the Instrument belonging to this file |
449 |
|
|
Instrument* _instrument; |
450 |
|
|
|
451 |
|
|
// state variables |
452 |
|
|
enum section_t { UNKNOWN, GROUP, REGION, CONTROL }; |
453 |
|
|
section_t _current_section; |
454 |
|
|
Region* _current_region; |
455 |
|
|
Group* _current_group; |
456 |
|
|
Definition* pCurDef; |
457 |
|
|
|
458 |
|
|
// control header directives |
459 |
|
|
std::string default_path; |
460 |
|
|
int octave_offset; |
461 |
|
|
int note_offset; |
462 |
|
|
}; |
463 |
|
|
|
464 |
|
|
} // !namespace sfz |
465 |
|
|
|
466 |
|
|
#endif // !LIBSFZ_SFZ_H |