~ubuntu-branches/ubuntu/raring/muse/raring-proposed

« back to all changes in this revision

Viewing changes to synti/iiwu/synth.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2004-02-07 15:18:22 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040207151822-es27xxkzbcxkebjm
Tags: 0.6.3-1
* New upstream version.
* Added patches:
  + [10_alsa_init_fix] New, from upstream CVS.
    Initialize direction variable when setting Alsa parameters.
  + [10_canvas_translation_fix] New, from upstream CVS.
    Do not translate tooltips twice in canvas popup.
  + [10_checkbox_fix] New, from upstream CVS.
    Use proper set/test methods on metronome checkboxes.
  + [10_html_doc_cleanup] New.
    Fix links and HTML errors in documentation.
  + [20_allow_system_timer] New.
    The new upstream version fails by default if the real-time clock
    could not be accessed (usually the case when not running suid-root).
    This patch reverts the old behaviour of falling back to the more
    inaccurate system timer.
* Updated patches:
  + [11_PIC_fixes_fixup] Rediffed.
* Removed patches:
  + [20_no_atomic_asm] Merged upstream.
* debian/compat: Splice out debhelper compatibility level from rules file.
* debian/control: Build-depend on latest jack release by default.
  Closes: #228788
* debian/control: Bump standards version.
* debian/control: Use auto-generated debconf dependency via misc:Depends.
* debian/control: Minor tweaks to the long description.
* debian/control: Tighten fluidsynth build dependency to sane version.
* debian/muse.doc-base: New. Register HTML documentation with doc-base.
* debian/templates: Tiny rewording, and typo fix.
* debian/templates, debian/po/*: Switch to po-debconf for translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//=========================================================
2
 
//  MusE
3
 
//  Linux Music Editor
4
 
//  $Id: synth.h,v 1.6 2002/02/11 10:10:57 muse Exp $
5
 
//
6
 
//  This file is derived from IIWU Synth and modified
7
 
//    for MusE.
8
 
//  Parts of IIWU are derived from Smurf Sound Font Editor.
9
 
//  Parts of Smurf Sound Font Editor are derived from
10
 
//    awesfx utilities
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
 
//=========================================================
16
 
 
17
 
#ifndef _IIWU_SYNTH_H
18
 
#define _IIWU_SYNTH_H
19
 
 
20
 
class SFont;
21
 
 
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 */
25
 
 
26
 
// int sfont_preset_compare_func (const void* a, const void* b);
27
 
 
28
 
/* sfont file chunk sizes */
29
 
#define SFPHDRSIZE      38
30
 
#define SFBAGSIZE       4
31
 
#define SFMODSIZE       10
32
 
#define SFGENSIZE       4
33
 
#define SFIHDRSIZE      22
34
 
#define SFSHDRSIZE      46
35
 
 
36
 
// SFData *sfload_file(FILE*);
37
 
 
38
 
#define FAIL    0
39
 
#define OK            1
40
 
 
41
 
enum {
42
 
      ErrWarn, ErrFatal, ErrStatus, ErrCorr, ErrEof, ErrMem, Errno,
43
 
      ErrRead, ErrWrite
44
 
      };
45
 
 
46
 
#include <list>
47
 
 
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
55
 
 
56
 
#ifndef PI
57
 
#define PI                     3.141592654
58
 
#endif
59
 
 
60
 
/* Choose the type of interpolation */
61
 
// #define USE_TRUNCATION
62
 
// #define USE_LINEAR_INTERPOLATION
63
 
#define USE_CUBIC_INTERPOLATION
64
 
 
65
 
 
66
 
enum iiwu_loop {
67
 
      IIWU_UNLOOPED,
68
 
      IIWU_LOOP_DURING_RELEASE,
69
 
      IIWU_NOTUSED,
70
 
      IIWU_LOOP
71
 
      };
72
 
 
73
 
enum iiwu_mod_flags {
74
 
      IIWU_MOD_POSITIVE = 0,
75
 
      IIWU_MOD_NEGATIVE = 1,
76
 
      IIWU_MOD_UNIPOLAR = 0,
77
 
      IIWU_MOD_BIPOLAR = 2,
78
 
      IIWU_MOD_LINEAR = 0,
79
 
      IIWU_MOD_CONCAVE = 4,
80
 
      IIWU_MOD_CONVEX = 8,
81
 
      IIWU_MOD_SWITCH = 12,
82
 
      IIWU_MOD_GC = 0,
83
 
      IIWU_MOD_CC = 16
84
 
      };
85
 
 
86
 
enum iiwu_mod_src {
87
 
      IIWU_MOD_NONE = 0,
88
 
      IIWU_MOD_VELOCITY = 2,
89
 
      IIWU_MOD_KEY = 3,
90
 
      IIWU_MOD_KEYPRESSURE = 10,
91
 
      IIWU_MOD_CHANNELPRESSURE = 13,
92
 
      IIWU_MOD_PITCHWHEEL = 14,
93
 
      IIWU_MOD_PITCHWHEELSENS = 16
94
 
      };
95
 
 
96
 
//---------------------------------------------------------
97
 
//   EnvStatus
98
 
//---------------------------------------------------------
99
 
 
100
 
enum EnvStatus {
101
 
      ENV_SKIP,
102
 
      ENV_DELAY,
103
 
      ENV_ATTACK,
104
 
      ENV_HOLD,
105
 
      ENV_DECAY,
106
 
      ENV_SUSTAIN,
107
 
      ENV_RELEASE,
108
 
      ENV_OFF
109
 
      };
110
 
 
111
 
class SynthProc;
112
 
class Channel;
113
 
 
114
 
/***************************************************************
115
 
 *
116
 
 *         TYPE DEFINITIONS & FUNCTION DECLARATIONS
117
 
 */
118
 
 
119
 
#if defined(USE_TRUNCATION)
120
 
 
121
 
typedef char iiwu_interp_coeff_t;
122
 
 
123
 
#elif defined(USE_LINEAR_INTERPOLATION)
124
 
typedef struct {
125
 
      double a0, a1;
126
 
      } iiwu_interp_coeff_t;
127
 
 
128
 
#elif defined(USE_CUBIC_INTERPOLATION)
129
 
typedef struct {
130
 
      double a0, a1, a2, a3;
131
 
      } iiwu_interp_coeff_t;
132
 
 
133
 
#else
134
 
#error No interpolation defined
135
 
#endif
136
 
 
137
 
 
138
 
/*
139
 
 *  phase
140
 
 */
141
 
 
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
146
 
 
147
 
#define IIWU_FRACT_MAX ((double)4294967296.0)
148
 
 
149
 
//---------------------------------------------------------
150
 
//   Phase
151
 
//---------------------------------------------------------
152
 
 
153
 
class Phase {
154
 
      int      _index;
155
 
      unsigned _fract;
156
 
 
157
 
   public:
158
 
      Phase() {}
159
 
      Phase(int a)       { _index = a; _fract = 0; }
160
 
      Phase(double a)    { set(a); }
161
 
 
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); }
165
 
      void set(double a) {
166
 
            _index = int(a);
167
 
            _fract = (u_int32_t) (((double)(a) - (double)(_index)) * IIWU_FRACT_MAX);
168
 
            }
169
 
      friend bool operator< (const Phase& a, const Phase& b) {
170
 
            return ((a._index < b._index) || ((a._index == b._index) && (a._fract < b._fract)));
171
 
            }
172
 
      friend bool operator== (const Phase& a, const Phase& b) {
173
 
            return ((a._index == b._index) && (a._fract == b._fract));
174
 
            }
175
 
      friend bool operator> (const Phase& a, const Phase& b) {
176
 
            return ((a._index > b._index) || ((a._index == b._index) && (a._fract > b._fract)));
177
 
            }
178
 
      void operator+=(const Phase& b) {
179
 
            _fract += b._fract;
180
 
            _index += b._index + (_fract < b._fract);
181
 
            }
182
 
      void operator-=(const Phase& b) {
183
 
            _index -= b._index - (_fract < b._fract);
184
 
            _fract -= b._fract;
185
 
            }
186
 
      int interp_index() const {
187
 
            return (int)((_fract & IIWU_INTERP_BITS_MASK) >> IIWU_INTERP_BITS_SHIFT);
188
 
            }
189
 
      };
190
 
 
191
 
//---------------------------------------------------------
192
 
//   Mod
193
 
//---------------------------------------------------------
194
 
 
195
 
class Mod {
196
 
   public:
197
 
      unsigned char dest;
198
 
      unsigned char src1;
199
 
      unsigned char flags1;
200
 
      unsigned char src2;
201
 
      unsigned char flags2;
202
 
      double amount;
203
 
 
204
 
   public:
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);
211
 
      };
212
 
 
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)))))
218
 
 
219
 
 
220
 
class SFont;
221
 
class ISynth;
222
 
class Preset;
223
 
class Sample;
224
 
 
225
 
//---------------------------------------------------------
226
 
//   Channel
227
 
//---------------------------------------------------------
228
 
 
229
 
class Channel {
230
 
      char _chan;
231
 
      char _hbank;
232
 
      char _lbank;
233
 
      char _prog;
234
 
      Preset* _preset;
235
 
      int _pitch_bend;
236
 
      char _cc[128];
237
 
 
238
 
   public:
239
 
      int key_pressure;
240
 
      int channel_pressure;
241
 
      int pitch_wheel_sensitivity;
242
 
 
243
 
   public:
244
 
      Channel();
245
 
      ~Channel() {}
246
 
      void setChannum(int num);
247
 
 
248
 
      const Preset* preset() const        { return _preset; }
249
 
      void setPreset(Preset* val)         { _preset = val;   }
250
 
 
251
 
      char chan() const                   { return _chan; }
252
 
      void setChan(char c)                { _chan = c; }
253
 
 
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;  }
260
 
 
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;   }
266
 
      };
267
 
 
268
 
//---------------------------------------------------------
269
 
//   SynthProc
270
 
//    synthesis process
271
 
//---------------------------------------------------------
272
 
 
273
 
class SynthProc {
274
 
      double _pitch;                // the pitch in midicents
275
 
      ISynth* synth;
276
 
      Sample* _sample;
277
 
      bool _sustained;
278
 
 
279
 
   public:
280
 
      SynthProc* next;
281
 
      unsigned char key;            // the key, quick acces for noteoff
282
 
      unsigned char vel;            // the velocity
283
 
      Channel* channel;
284
 
 
285
 
      Gen gen[GEN_LAST];
286
 
      Mod mod[IIWU_NUM_MOD];
287
 
 
288
 
      int mod_count;
289
 
      unsigned int ticks;
290
 
      Phase phase;                  // the phase of the sample wave
291
 
 
292
 
      /* basic parameters */
293
 
      double attenuation;           // the attenuation in centibels
294
 
      double root_pitch;
295
 
 
296
 
      /* sample and loop start and end points */
297
 
      int start_offset;
298
 
      int end_offset;
299
 
      int loop_start_offset;
300
 
      int loop_end_offset;
301
 
 
302
 
      /* vol env */
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
308
 
      double volenv_decay;
309
 
      double volenv_sustain;
310
 
      double volenv_release;
311
 
 
312
 
      /* mod env */
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;
317
 
      double modenv_hold;
318
 
      double modenv_decay;
319
 
      double modenv_sustain;
320
 
      double modenv_release;
321
 
      double modenv_to_fc;
322
 
      double modenv_to_pitch;
323
 
      /* mod lfo */
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 */
327
 
      double modlfo_to_fc;
328
 
      double modlfo_to_pitch;
329
 
      double modlfo_to_vol;
330
 
      /* vib lfo */
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 */
339
 
      /* pan */
340
 
      double pan;
341
 
      /* for debugging purposeses */
342
 
      void update_param(int gen);
343
 
 
344
 
   public:
345
 
      SynthProc(ISynth*);
346
 
      ~SynthProc() {}
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);
350
 
      void noteoff();
351
 
      void off();
352
 
      void optimize();
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; }
359
 
      };
360
 
 
361
 
#define _SAMPLEMODE()   ((int)(gen[GEN_SAMPLEMODE].val))
362
 
#define _GEN(_n)        (gen[_n].val + gen[_n].mod)
363
 
 
364
 
//---------------------------------------------------------
365
 
//   ISynth
366
 
//---------------------------------------------------------
367
 
 
368
 
class ISynth : public Mess {
369
 
      static bool initialized;
370
 
 
371
 
      enum SfOp { SF_REPLACE, SF_ADD, SF_REMOVE };
372
 
 
373
 
      bool _gmMode;
374
 
      int _masterVol;         // 0 - 0x3fff
375
 
      double startFrame;      // synthi start time
376
 
      unsigned curSample;
377
 
 
378
 
      int maxBufferSize;
379
 
      mutable SFont* nextSFont;       // for getNextParam
380
 
 
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);
384
 
      void gmOn(bool);
385
 
      void sysexSoundFont(SfOp op, const char* data);
386
 
      void setMasterVol(int val)   { _masterVol = val;  }
387
 
 
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);
393
 
      void deleteSFonts();
394
 
      void allNotesOff();
395
 
      void resetAllController(int);
396
 
 
397
 
      const MidiPatch* getFirstPatch(int ch) const;
398
 
      static void initSynth();
399
 
      virtual void write(int n, float** ports, int offset);
400
 
 
401
 
   protected:
402
 
      virtual void processEvent(MEvent*);
403
 
 
404
 
   public:
405
 
      SynthProc* alloc_sp(Channel* chan, int key, int vel);
406
 
      SFont* sfont;               // the loaded soundfont
407
 
      Channel channel[IIWU_NUM_CHANNELS]; // the channels
408
 
 
409
 
      SynthProc* spFree;   // the free synthesis processes
410
 
      SynthProc* spBusy;   // the busy synthesis processes
411
 
 
412
 
      float* mono_buf;
413
 
 
414
 
      //----threads:
415
 
      pthread_t midiThread;
416
 
      void midiRun();
417
 
 
418
 
   public:
419
 
      ISynth(const char* classname);
420
 
      ~ISynth();
421
 
 
422
 
      void stop(SynthProc*);
423
 
      void start(SynthProc*);
424
 
 
425
 
      int masterVol() const        { return _masterVol; }
426
 
      bool init();
427
 
 
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);
433
 
 
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);
438
 
 
439
 
      void dump();      // debugging
440
 
 
441
 
      //
442
 
      //   experimental MESSS interface:
443
 
      //
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);
450
 
      };
451
 
 
452
 
#endif  /* _IIWU_SYNTH_H */