summaryrefslogtreecommitdiff
path: root/attic/muse_qt4_evolution/muse/midictrl.cpp
blob: 89e40ec147c71abd7467a555cd72ff31ca9fe67e (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//=============================================================================
//  MusE
//  Linux Music Editor
//  $Id:$
//
//  Copyright (C) 2002-2006 by Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#include "midictrl.h"
#include "al/xml.h"

static const char* ctrlName[] = {
       "BankSelMSB",  "Modulation",  "BreathCtrl",  "Control 3",  "Foot Ctrl",
       "Porta Time",  "DataEntMSB",  "MainVolume",  "Balance",    "Control 9",
/*10*/ "Pan",         "Expression",  "Control 12",  "Control 13", "Control 14",
       "Control 15",  "Gen.Purp.1",  "Gen.Purp.2",  "Gen.Purp.3", "Gen.Purp.4",
/*20*/ "Control 20",  "Control 21",  "Control 22",  "Control 23", "Control 24",
       "Control 25",  "Control 26",  "Control 27",  "Control 28", "Control 29",
/*30*/ "Control 30",  "Control 31",  "BankSelLSB",  "Modul. LSB", "BrthCt.LSB",
       "Control 35", "FootCt.LSB",   "Port.T LSB",  "DataEntLSB", "MainVolLSB",
       "BalanceLSB",  "Control 41",  "Pan LSB",     "Expr. LSB",  "Control 44",
       "Control 45",  "Control 46",  "Control 47",  "Gen.P.1LSB", "Gen.P.2LSB",
/*50*/ "Gen.P.3LSB",  "Gen.P.4LSB", "Control 52",   "Control 53",  "Control 54",
       "Control 55", "Control 56",  "Control 57",   "Control 58",  "Control 59",
       "Control 60",  "Control 61",  "Control 62",  "Control 63", "Sustain",
       "Porta Ped",   "Sostenuto",  "Soft Pedal", "Control 68",  "Hold 2",
       "Control 70",  "HarmonicCo", "ReleaseTime", "Attack Time", "Brightness",
       "Control 75", "Control 76",  "Control 77",  "Control 78",  "Control 79",
       "Gen.Purp.5",  "Gen.Purp.6",  "Gen.Purp.7", "Gen.Purp.8", "Porta Ctrl",
       "Control 85",  "Control 86",  "Control 87", "Control 88",  "Control 89",
       "Control 90",  "Effect1Dep", "Effect2Dep",  "Effect3Dep",  "Effect4Dep",
       "Phaser Dep", "Data Incr",   "Data Decr",   "NRPN LSB",    "NRPN MSB",
/*100*/ "RPN LSB",     "RPN MSB",     "Control102", "Control103", "Control104",
       "Control105",  "Control106",  "Control107", "Control108",  "Control109",
       "Control110",  "Control111", "Control112",  "Control113",  "Control114",
       "Control115", "Control116",  "Control117",  "Control118",  "Control119",
       "AllSndOff",   "Reset Ctrl",  "Local Ctrl", "AllNoteOff", "OmniModOff",
       "OmniModeOn",  "MonoModeOn",  "PolyModeOn"
      };

#if 0
static const char* ctrl14Name[] = {
     "BankSel",    "Modulation",  "BreathCtrl",
     "Control 3",  "Foot Ctrl",   "Porta Time",  "DataEntry",
     "MainVolume", "Balance",     "Control 9",   "Pan",
     "Expression", "Control 12",  "Control 13",  "Control 14",
     "Control 15", "Gen.Purp.1",  "Gen.Purp.2",  "Gen.Purp.3",
     "Gen.Purp.4", "Control 20",  "Control 21",  "Control 22",
     "Control 23", "Control 24",  "Control 25",  "Control 26",
     "Control 27", "Control 28",  "Control 29",  "Control 30",
     "Control 31",
     };
#endif

enum {
      COL_NAME = 0, COL_TYPE,
      COL_HNUM, COL_LNUM, COL_MIN, COL_MAX
      };

MidiControllerList defaultMidiController;
//
// some global controller which are always available:
//
MidiController veloCtrl("Velocity",                 CTRL_VELOCITY,      0, 127,   0);
static MidiController pitchCtrl("PitchBend",        CTRL_PITCH,     -8192, +8191, 0);
static MidiController programCtrl("Program",        CTRL_PROGRAM,       0, 0xffffff, 0);
static MidiController mastervolCtrl("MasterVolume", CTRL_MASTER_VOLUME, 0, 0x3fff, 0x3000);
static MidiController volumeCtrl("MainVolume",      CTRL_VOLUME,        0, 127, 100);
static MidiController panCtrl("Pan",                CTRL_PANPOT,        0, 127,  64);
static MidiController reverbCtrl("ReverbSend",      CTRL_REVERB_SEND,   0, 127,  0);
static MidiController chorusCtrl("ChorusSend",      CTRL_CHORUS_SEND,   0, 127,  0);
static MidiController variationCtrl("VariationSend",CTRL_VARIATION_SEND,0, 127,  0);

//---------------------------------------------------------
//   ctrlType2Int
//   int2ctrlType
//---------------------------------------------------------

static struct {
      MidiController::ControllerType type;
      QString name;
      }  ctrlTypes[] = {
      { MidiController::Controller7, QString("Control7") },
      { MidiController::Controller14, QString("Control14") },
      { MidiController::RPN, QString("RPN") },
      { MidiController::NRPN, QString("NRPN") },
      { MidiController::Pitch, QString("Pitch") },
      { MidiController::Program, QString("Program") },
      { MidiController::RPN14, QString("RPN14") },
      { MidiController::NRPN14, QString("NRPN14") },
      { MidiController::Controller7, QString("Control") },    // alias
      };

//---------------------------------------------------------
//   ctrlType2Int
//---------------------------------------------------------

MidiController::ControllerType ctrlType2Int(const QString& s)
      {
      int n = sizeof(ctrlTypes)/sizeof(*ctrlTypes);
      for (int i = 0; i < n; ++i) {
            if (ctrlTypes[i].name == s)
                  return ctrlTypes[i].type;
            }
      return MidiController::ControllerType(0);
      }

//---------------------------------------------------------
//   int2ctrlType
//---------------------------------------------------------

const QString& int2ctrlType(int n)
      {
      static QString dontKnow("?T?");
      int size = sizeof(ctrlTypes)/sizeof(*ctrlTypes);
      for (int i = 0; i < size; ++i) {
            if (ctrlTypes[i].type == n)
                  return ctrlTypes[i].name;
            }
      return dontKnow;
      }

//---------------------------------------------------------
//   initMidiController
//---------------------------------------------------------

void initMidiController()
      {
      static bool initialized = false;
      if (initialized)
            return;
      defaultMidiController.push_back(&veloCtrl);
      defaultMidiController.push_back(&pitchCtrl);
      defaultMidiController.push_back(&programCtrl);
      defaultMidiController.push_back(&mastervolCtrl);
      defaultMidiController.push_back(&volumeCtrl);
      defaultMidiController.push_back(&panCtrl);
      defaultMidiController.push_back(&reverbCtrl);
      defaultMidiController.push_back(&chorusCtrl);
      defaultMidiController.push_back(&variationCtrl);
      initialized = true;
      }

//---------------------------------------------------------
//   midiCtrlName
//---------------------------------------------------------

QString midiCtrlName(int ctrl)
      {
      if (ctrl < 0x10000)
            return QString(ctrlName[ctrl]);
      QString s("?N?");
      switch(ctrl) {
            case CTRL_PITCH:
                  s = "Pitch";
                  break;
            case CTRL_PROGRAM:
                  s = "ProgramChange";
                  break;
            case CTRL_VELOCITY:
                  s = "Velocity";
                  break;
            case CTRL_MASTER_VOLUME:
                  s = "MasterVolume";
                  break;
            default:
                  printf("midiCtrlName unknown %x\n", ctrl);
                  break;
            }
      return s;
      }

//---------------------------------------------------------
//   MidiController
//---------------------------------------------------------

MidiController::MidiController()
   : _name(QString(QT_TR_NOOP("Velocity")))
      {
      _num     = CTRL_VELOCITY;
      _minVal  = 0;
      _maxVal  = 127;
      _initVal = 0;
      _moveWithPart = true;
      }

MidiController::MidiController(const QString& s, int n, int min, int max, int init,
   bool mwp)
   : _name(s), _num(n), _minVal(min), _maxVal(max), _initVal(init),
     _moveWithPart(mwp)
      {
      }

//---------------------------------------------------------
//   type
//---------------------------------------------------------

MidiController::ControllerType midiControllerType(int num)
      {
      if (num == CTRL_PITCH)
            return MidiController::Pitch;
      if (num == CTRL_PROGRAM)
            return MidiController::Program;
      if (num == CTRL_VELOCITY || num == CTRL_SVELOCITY)
            return MidiController::Velo;
      if (num < CTRL_14_OFFSET)
            return MidiController::Controller7;
      if (num < CTRL_RPN_OFFSET)
            return MidiController::Controller14;
      if (num < CTRL_NRPN_OFFSET)
            return MidiController::RPN;
      if (num < CTRL_RPN14_OFFSET)
            return MidiController::NRPN;
      if (num < CTRL_NRPN14_OFFSET)
            return MidiController::RPN14;
      if (num < CTRL_NONE_OFFSET)
            return MidiController::NRPN14;
      return MidiController::Controller7;
      }

//---------------------------------------------------------
//   MidiController::write
//---------------------------------------------------------

void MidiController::write(Xml& xml) const
      {
      ControllerType t = midiControllerType(_num);
      QString type(int2ctrlType(t));

      int h = (_num >> 8) & 0x7f;
      int l = _num & 0x7f;

      QString sl;
      if ((_num & 0xff) == 0xff)
            sl = "Pitch";
      else
            sl.setNum(l);

      QString s = QString("Controller name=\"%1\" type=\"%2\" h=\"%3\" l=\"%4\" min=\"%5\" max=\"%6\" init=\"%7\"")
         .arg(Xml::xmlString(_name)).arg(type).arg(h).arg(sl).arg(_minVal).arg(_maxVal).arg(_initVal);
      if (_comment.isEmpty())
            xml.tagE(s);
      else {
            xml.putLevel();
            xml << '<' << s << '>' << Xml::xmlString(_comment) << "</Controller>\n";
            }
      }

//---------------------------------------------------------
//   MidiController::read
//---------------------------------------------------------

void MidiController::read(QDomNode node)
      {
      ControllerType t = Controller7;
      static const int NOT_SET = 0x100000;
      int l    = 0;

      QDomElement e = node.toElement();
      _name    = e.attribute("name");
      _comment = e.text();
      t = ctrlType2Int(e.attribute("type"));
      int h = e.attribute("h","0").toInt(0,0);
      QString s = e.attribute("l");
      if (s == "pitch")
            l = 0xff;
      else
            l  = s.toInt(0,0);
      _minVal  = e.attribute("min",  "0").toInt(0,0);
      _maxVal  = e.attribute("max",  "0x100000").toInt(0,0);
      _initVal = e.attribute("init", "-1").toInt(0,0);

      _num = (h << 8) + l;
      switch (t) {
            case RPN:
                  if (_maxVal == NOT_SET)
                        _maxVal = 127;
                  _num |= 0x20000;
                  break;
            case NRPN:
                  if (_maxVal == NOT_SET)
                        _maxVal = 127;
                  _num |= 0x30000;
                  break;
            case Controller7:
                  if (_maxVal == NOT_SET)
                        _maxVal = 127;
                  break;
            case Controller14:
                  _num |= 0x10000;
                  if (_maxVal == NOT_SET)
                        _maxVal = 127 * 127 * 127;
                  break;
            case RPN14:
                  if (_maxVal == NOT_SET)
                        _maxVal = 127 * 127 * 127;
                  _num |= 0x50000;
                  break;
            case NRPN14:
                  if (_maxVal == NOT_SET)
                        _maxVal = 127 * 127 * 127;
                  _num |= 0x60000;
                  break;
            case Pitch:
                  if (_maxVal == NOT_SET)
                        _maxVal = 8191;
                  if (_minVal == NOT_SET)
                        _minVal = -8192;
                  _num = CTRL_PITCH;
                  break;
            case Program:
                  if (_maxVal == NOT_SET)
                        _maxVal = 127 * 127 * 127;
                  _num = CTRL_PROGRAM;
                  break;
            case Velo:        // cannot happen
                  break;
            }
      return;
      }

//---------------------------------------------------------
//   type
//---------------------------------------------------------

MidiController::ControllerType MidiController::type() const         
      { 
      return midiControllerType(num()); 
      }

//---------------------------------------------------------
//   genNum
//---------------------------------------------------------


int MidiController::genNum(MidiController::ControllerType t, int h, int l)
      {
      int val = (h << 8) + l;
      switch(t) {
            case Controller7:
                  return l;
            case Controller14:
                  return val + CTRL_14_OFFSET;
            case RPN:
                  return l + CTRL_RPN_OFFSET;
            case NRPN:
                  return l + CTRL_NRPN_OFFSET;
            case RPN14:
                  return val + CTRL_RPN14_OFFSET;
            case NRPN14:
                  return val + CTRL_NRPN14_OFFSET;
            case Pitch:
                  return CTRL_PITCH;
            case Program:
                  return CTRL_PROGRAM;
            default:
                  return -1;
            }      
      }