summaryrefslogtreecommitdiff
path: root/attic/muse/synti/organ/organ.h
blob: 308646e186b3b4e76c2baa8522aff64c32678669 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: organ.h,v 1.5.2.3 2009/11/19 04:20:33 terminator356 Exp $
//
//  Parts of this file taken from:
//      Organ - Additive Organ Synthesizer Voice
//      Copyright (c) 1999, 2000 David A. Bartold
//
//  (C) Copyright 2001-2004 Werner Schweer (ws@seh.de)
//=========================================================

#ifndef __ORGAN_H__
#define __ORGAN_H__

#include "muse/midictrl.h"
#include "libsynti/mess.h"

#define RESOLUTION   (16384*2)
#define VOICES          128    // max polyphony

class OrganGui;

static const int MAX_ATTENUATION = 960;

enum EnvelopeState {
      ATTACK,
      DECAY,
      SUSTAIN,
      RELEASE,
      OFF
      };

//---------------------------------------------------------
//   Envelope
//---------------------------------------------------------

struct Envelope {
      int ticks;        // len of segment
      int error, delta, schritt;
      int y, yinc;

      void set(int t, int y1, int y2) {
            ticks   = t;
            y       = y1;
            int dy  = y2 - y1;
            int dx  = t;
            error   = -dx;
            schritt = 2*dx;
            if (dy < 0) {
                  yinc = -1;
                  delta = -2 * dy;
                  }
            else {
                  yinc = 1;
                  delta   = 2 * dy;
                  }
            }

      // return false on envelope end
      bool step(int* a) {
            *a = y;
            if (ticks == 0)
                  return false;
            error += delta;
            while (error > 0) {
                  y += yinc;
                  error -= schritt;
                  }
            --ticks;
            return true;
            }
      };

static const int HARM0      =  0 + CTRL_RPN14_OFFSET;
static const int HARM1      =  1 + CTRL_RPN14_OFFSET;
static const int HARM2      =  2 + CTRL_RPN14_OFFSET;
static const int HARM3      =  3 + CTRL_RPN14_OFFSET;
static const int HARM4      =  4 + CTRL_RPN14_OFFSET;
static const int HARM5      =  5 + CTRL_RPN14_OFFSET;
static const int ATTACK_LO  =  6 + CTRL_RPN14_OFFSET;
static const int DECAY_LO   =  7 + CTRL_RPN14_OFFSET;
static const int SUSTAIN_LO =  8 + CTRL_RPN14_OFFSET;
static const int RELEASE_LO =  9 + CTRL_RPN14_OFFSET;
static const int ATTACK_HI  = 10 + CTRL_RPN14_OFFSET;
static const int DECAY_HI   = 11 + CTRL_RPN14_OFFSET;
static const int SUSTAIN_HI = 12 + CTRL_RPN14_OFFSET;
static const int RELEASE_HI = 13 + CTRL_RPN14_OFFSET;
static const int BRASS      = 14 + CTRL_RPN14_OFFSET;
static const int FLUTE      = 15 + CTRL_RPN14_OFFSET;
static const int REED       = 16 + CTRL_RPN14_OFFSET;
static const int VELO       = 17 + CTRL_RPN14_OFFSET;

//---------------------------------------------------------
//   SynthCtrl
//---------------------------------------------------------

struct SynthCtrl {
      const char* name;
      int num;
      int val;
      };

//---------------------------------------------------------
//   Voice
//---------------------------------------------------------

struct Voice {
      bool isOn;
      int pitch;
      int channel;

      double velocity;

      int state1, state2;
      Envelope envL1, envL2, envL3;
      Envelope envH1, envH2, envH3;

      unsigned harm0_accum;
      unsigned harm1_accum;
      unsigned harm2_accum;
      unsigned harm3_accum;
      unsigned harm4_accum;
      unsigned harm5_accum;
      };

//---------------------------------------------------------
//   Preset
//---------------------------------------------------------

struct Preset {
      char* name;
      bool brass, flute, reed;
      int attack0, attack1;
      int release0, release1;
      int decay0, decay1;
      double harm0, harm1, harm2, harm3, harm4, harm5;
      bool velo;
      };

//---------------------------------------------------------
//   Organ
//---------------------------------------------------------

class Organ : public Mess {
      static int useCount;

      static double cb2amp_tab[MAX_ATTENUATION];
      static unsigned freq256[128];
      static double cb2amp(int cb);

      int* idata;  // buffer for init data

      bool brass, flute, reed;
      int attack0, attack1;
      int release0, release1;
      int decay0, decay1;        // ticks
      int sustain0, sustain1;    // centibel
      bool velo;
      double volume;

      double harm0, harm1, harm2, harm3, harm4, harm5;

      Voice voices[VOICES];

      static float* sine_table;
      static float* g_triangle_table;
      static float* g_pulse_table;

      void noteoff(int channel, int pitch);
      void setController(int ctrl, int val);


      OrganGui* gui;

   public:
      virtual void processMessages();
      virtual void process(float**, int, int);
      virtual bool playNote(int channel, int pitch, int velo);
      virtual bool setController(int channel, int ctrl, int val);

      virtual int getControllerInfo(int, const char**, int*, int*, int*, int*) const;
      virtual void getInitData(int*, const unsigned char**) const;

      virtual bool guiVisible() const;
      virtual void showGui(bool);
      virtual bool hasGui() const { return true; }
      virtual void getGeometry(int* x, int* y, int* w, int* h) const;
      virtual void setGeometry(int x, int y, int w, int h);
      virtual bool sysex(int, const unsigned char*);
      static SynthCtrl synthCtrl[];
      Organ(int sampleRate);
      ~Organ();
      bool init(const char* name);
      };

#endif