1
//=========================================================
4
// $Id: synth.h,v 1.6 2002/02/11 10:10:57 muse Exp $
6
// This file is derived from IIWU Synth and modified
8
// Parts of IIWU are derived from Smurf Sound Font Editor.
9
// Parts of Smurf Sound Font Editor are derived from
11
// Smurf: Copyright (C) 1999-2000 Josh Green
12
// IIWU: Copyright (C) 2001 Peter Hanappe
13
// MusE: Copyright (C) 2001 Werner Schweer
14
// awesfx: Copyright (C) 1996-1999 Takashi Iwai
15
//=========================================================
22
#define Gen_MaxValid Gen_Dummy - 1 /* maximum valid generator */
23
#define Gen_Count Gen_Dummy /* count of generators */
24
#define GenArrSize sizeof(SFGenAmount)*Gen_Count /* gen array size */
26
// int sfont_preset_compare_func (const void* a, const void* b);
28
/* sfont file chunk sizes */
36
// SFData *sfload_file(FILE*);
42
ErrWarn, ErrFatal, ErrStatus, ErrCorr, ErrEof, ErrMem, Errno,
48
#define IIWU_NUM_CHANNELS 16
49
#define IIWU_NUM_PROGRAMS 129
50
#define IIWU_NUM_MOD 64
51
#define IIWU_CENTS_HZ_SIZE 1200
52
#define IIWU_VEL_CB_SIZE 128
53
#define IIWU_CB_AMP_SIZE 961
54
#define IIWU_PAN_SIZE 1002
57
#define PI 3.141592654
60
/* Choose the type of interpolation */
61
// #define USE_TRUNCATION
62
// #define USE_LINEAR_INTERPOLATION
63
#define USE_CUBIC_INTERPOLATION
68
IIWU_LOOP_DURING_RELEASE,
74
IIWU_MOD_POSITIVE = 0,
75
IIWU_MOD_NEGATIVE = 1,
76
IIWU_MOD_UNIPOLAR = 0,
88
IIWU_MOD_VELOCITY = 2,
90
IIWU_MOD_KEYPRESSURE = 10,
91
IIWU_MOD_CHANNELPRESSURE = 13,
92
IIWU_MOD_PITCHWHEEL = 14,
93
IIWU_MOD_PITCHWHEELSENS = 16
96
//---------------------------------------------------------
98
//---------------------------------------------------------
114
/***************************************************************
116
* TYPE DEFINITIONS & FUNCTION DECLARATIONS
119
#if defined(USE_TRUNCATION)
121
typedef char iiwu_interp_coeff_t;
123
#elif defined(USE_LINEAR_INTERPOLATION)
126
} iiwu_interp_coeff_t;
128
#elif defined(USE_CUBIC_INTERPOLATION)
130
double a0, a1, a2, a3;
131
} iiwu_interp_coeff_t;
134
#error No interpolation defined
142
#define IIWU_INTERP_BITS 8
143
#define IIWU_INTERP_BITS_MASK 0xff000000
144
#define IIWU_INTERP_BITS_SHIFT 24
145
#define IIWU_INTERP_MAX 256
147
#define IIWU_FRACT_MAX ((double)4294967296.0)
149
//---------------------------------------------------------
151
//---------------------------------------------------------
159
Phase(int a) { _index = a; _fract = 0; }
160
Phase(double a) { set(a); }
162
void setInt(int a) { _index = a; _fract = 0; }
163
int index() const { return _index; }
164
void dump() const { printf("Phase %d-%ud\n", _index, _fract); }
167
_fract = (u_int32_t) (((double)(a) - (double)(_index)) * IIWU_FRACT_MAX);
169
friend bool operator< (const Phase& a, const Phase& b) {
170
return ((a._index < b._index) || ((a._index == b._index) && (a._fract < b._fract)));
172
friend bool operator== (const Phase& a, const Phase& b) {
173
return ((a._index == b._index) && (a._fract == b._fract));
175
friend bool operator> (const Phase& a, const Phase& b) {
176
return ((a._index > b._index) || ((a._index == b._index) && (a._fract > b._fract)));
178
void operator+=(const Phase& b) {
180
_index += b._index + (_fract < b._fract);
182
void operator-=(const Phase& b) {
183
_index -= b._index - (_fract < b._fract);
186
int interp_index() const {
187
return (int)((_fract & IIWU_INTERP_BITS_MASK) >> IIWU_INTERP_BITS_SHIFT);
191
//---------------------------------------------------------
193
//---------------------------------------------------------
199
unsigned char flags1;
201
unsigned char flags2;
205
void clone(Mod* src);
206
void set_source1(int src, int flags);
207
void set_source2(int src, int flags);
208
void set_dest(int dst) { dest = dst; }
209
void set_amount(double a) { amount = a; }
210
double get_value(Channel* chan, SynthProc* sp);
213
#define iiwu_mod_has_source(mod,cc,ctrl) \
214
( ((((mod)->src1 == ctrl) && (((mod)->flags1 & IIWU_MOD_CC) != 0) && (cc != 0)) \
215
|| ((((mod)->src1 == ctrl) && (((mod)->flags1 & IIWU_MOD_CC) == 0) && (cc == 0)))) \
216
|| ((((mod)->src2 == ctrl) && (((mod)->flags2 & IIWU_MOD_CC) != 0) && (cc != 0)) \
217
|| ((((mod)->src2 == ctrl) && (((mod)->flags2 & IIWU_MOD_CC) == 0) && (cc == 0)))))
225
//---------------------------------------------------------
227
//---------------------------------------------------------
240
int channel_pressure;
241
int pitch_wheel_sensitivity;
246
void setChannum(int num);
248
const Preset* preset() const { return _preset; }
249
void setPreset(Preset* val) { _preset = val; }
251
char chan() const { return _chan; }
252
void setChan(char c) { _chan = c; }
254
char hbank() const { return _hbank; }
255
char lbank() const { return _lbank; }
256
char prog() const { return _prog; }
257
void setHbank(char val) { _hbank = val; }
258
void setLbank(char val) { _lbank = val; }
259
void setProg(char val) { _prog = val; }
261
void noteon(ISynth* synth, int key, int vel);
262
int pitchBend() const { return _pitch_bend; }
263
void setPitchBend(int val) { _pitch_bend = val; }
264
int cc(int num) const { return _cc[num]; }
265
void setCc(int num, int value) { _cc[num] = value; }
268
//---------------------------------------------------------
271
//---------------------------------------------------------
274
double _pitch; // the pitch in midicents
281
unsigned char key; // the key, quick acces for noteoff
282
unsigned char vel; // the velocity
286
Mod mod[IIWU_NUM_MOD];
290
Phase phase; // the phase of the sample wave
292
/* basic parameters */
293
double attenuation; // the attenuation in centibels
296
/* sample and loop start and end points */
299
int loop_start_offset;
303
EnvStatus volenv_state; // the state of the volume envelope
304
double volenv_val; // the value of the volume envelope
305
unsigned int volenv_delay; // duration delay phase in ticks
306
double volenv_attack;
307
double volenv_hold; // ticks
309
double volenv_sustain;
310
double volenv_release;
313
EnvStatus modenv_state; // the state of the modulation envelope */
314
double modenv_val; /* the value of the modulation envelope */
315
unsigned int modenv_delay;
316
double modenv_attack;
319
double modenv_sustain;
320
double modenv_release;
322
double modenv_to_pitch;
324
double modlfo_val; /* the value of the modulation LFO */
325
unsigned int modlfo_delay; /* the delay of the lfo in samples */
326
double modlfo_incr; /* the lfo frequency is converted to a per-buffer increment */
328
double modlfo_to_pitch;
329
double modlfo_to_vol;
331
double viblfo_val; /* the value of the vibrato LFO */
332
unsigned int viblfo_delay; /* the delay of the lfo in samples */
333
double viblfo_incr; /* the lfo frequency is converted to a per-buffer increment */
334
double viblfo_to_pitch;
335
/* for the resonant filter */
336
double fres; /* the resonance frequency, in cents (not absolute cents) */
337
double q; /* the q-factor */
338
double w1, w2; /* delayed samples */
341
/* for debugging purposeses */
342
void update_param(int gen);
347
void init(Channel* channel, Sample* sample);
348
void write(int len, float* mono, float* left, float* right);
349
void modulate(int cc, int ctrl, int val);
353
void add_mod(Mod* mod);
354
double pitch() const { return _pitch; }
355
int chan() const { return channel->chan(); }
356
bool sustained() const { return _sustained; }
357
void setSustained(bool val) { _sustained = val; }
358
bool released() const { return volenv_state == ENV_RELEASE; }
361
#define _SAMPLEMODE() ((int)(gen[GEN_SAMPLEMODE].val))
362
#define _GEN(_n) (gen[_n].val + gen[_n].mod)
364
//---------------------------------------------------------
366
//---------------------------------------------------------
368
class ISynth : public Mess {
369
static bool initialized;
371
enum SfOp { SF_REPLACE, SF_ADD, SF_REMOVE };
374
int _masterVol; // 0 - 0x3fff
375
double startFrame; // synthi start time
379
mutable SFont* nextSFont; // for getNextParam
381
void noteon(int chan, int key, int vel);
382
void noteoff(int chan, int key);
383
void sysex(const unsigned char* data, int len);
385
void sysexSoundFont(SfOp op, const char* data);
386
void setMasterVol(int val) { _masterVol = val; }
388
void program_change(int chan, char program);
389
void hbank_select(int chan, char bank);
390
void lbank_select(int chan, char bank);
391
void setCc(int chan, int ctrl, int val);
392
void pitch_bend(int chan, int val);
395
void resetAllController(int);
397
const MidiPatch* getFirstPatch(int ch) const;
398
static void initSynth();
399
virtual void write(int n, float** ports, int offset);
402
virtual void processEvent(MEvent*);
405
SynthProc* alloc_sp(Channel* chan, int key, int vel);
406
SFont* sfont; // the loaded soundfont
407
Channel channel[IIWU_NUM_CHANNELS]; // the channels
409
SynthProc* spFree; // the free synthesis processes
410
SynthProc* spBusy; // the busy synthesis processes
415
pthread_t midiThread;
419
ISynth(const char* classname);
422
void stop(SynthProc*);
423
void start(SynthProc*);
425
int masterVol() const { return _masterVol; }
428
/** Load a SoundFont. The newly loaded SoundFont will be put on top
429
* of the SoundFont stack. Presets are searched starting from the
430
* SoundFont on the top of the stack, working the way down the stack
431
* until a preset is found. */
432
int sfload(const char* filename);
434
/** Get the name of the next preset of a SoundFont */
435
char* next_preset(int font);
436
void program_reset();
437
SFont* get_font(int num);
439
void dump(); // debugging
442
// experimental MESSS interface:
444
virtual const char* getPatchName(int, int, int, int, MType);
445
// virtual const MidiPatch* getFirstPatch(int ch) const;
446
virtual const MidiPatch* getNextPatch(int ch, const MidiPatch*) const;
447
virtual bool getFirstParameter(const char*& name, const char*& value) const;
448
virtual bool getNextParameter(const char*& name, const char*& value) const;
449
virtual void setParameter(const char* name, const char* value);
452
#endif /* _IIWU_SYNTH_H */