summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/muse/midiout.cpp
blob: c35f115f742d999e437996ac6ccd2c5388a556a5 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//=============================================================================
//  MusE
//  Linux Music Editor
//  $Id:$
//
//  Copyright (C) 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 "midiout.h"
#include "midictrl.h"
#include "miditrackbase.h"
#include "al/al.h"
#include "al/tempo.h"
#include "event.h"
#include "sync.h"
#include "audio.h"
#include "gconfig.h"

static const unsigned char mmcDeferredPlayMsg[] = { 0x7f, 0x7f, 0x06, 0x03 };
static const unsigned char mmcStopMsg[]         = { 0x7f, 0x7f, 0x06, 0x01 };

//---------------------------------------------------------
//   MidiOut
//---------------------------------------------------------

MidiOut::MidiOut()
      {
      track = 0;
      }

//---------------------------------------------------------
//   sendGmOn
//    send GM-On message to midi device and keep track
//    of device state
//---------------------------------------------------------

void MidiOut::sendGmOn()
      {
      sendSysex(gmOnMsg, gmOnMsgLen);
      track->setHwCtrlState(CTRL_PROGRAM,      0);
      track->setHwCtrlState(CTRL_PITCH,        0);
      track->setHwCtrlState(CTRL_VOLUME,     100);
      track->setHwCtrlState(CTRL_PANPOT,      64);
      track->setHwCtrlState(CTRL_REVERB_SEND, 40);
      track->setHwCtrlState(CTRL_CHORUS_SEND,  0);
      track->setMeter(0, 0.0);
      }

//---------------------------------------------------------
//   sendGsOn
//    send Roland GS-On message to midi device and keep track
//    of device state
//---------------------------------------------------------

void MidiOut::sendGsOn()
      {
      static unsigned char data2[] = { 0x41, 0x10, 0x42, 0x12, 0x40, 0x01, 0x33, 0x50, 0x3c };
      static unsigned char data3[] = { 0x41, 0x10, 0x42, 0x12, 0x40, 0x01, 0x34, 0x50, 0x3b };

      sendSysex(data2, sizeof(data2));
      sendSysex(data3, sizeof(data3));
      }

//---------------------------------------------------------
//   sendXgOn
//    send Yamaha XG-On message to midi device and keep track
//    of device state
//---------------------------------------------------------

void MidiOut::sendXgOn()
      {
      sendSysex(xgOnMsg, xgOnMsgLen);
      track->setHwCtrlState(CTRL_PROGRAM, 0);
      track->setHwCtrlState(CTRL_MODULATION, 0);
      track->setHwCtrlState(CTRL_PORTAMENTO_TIME, 0);
      track->setHwCtrlState(CTRL_VOLUME, 0x64);
      track->setHwCtrlState(CTRL_PANPOT, 0x40);
      track->setHwCtrlState(CTRL_EXPRESSION, 0x7f);
      track->setHwCtrlState(CTRL_SUSTAIN, 0x0);
      track->setHwCtrlState(CTRL_PORTAMENTO, 0x0);
      track->setHwCtrlState(CTRL_SOSTENUTO, 0x0);
      track->setHwCtrlState(CTRL_SOFT_PEDAL, 0x0);
      track->setHwCtrlState(CTRL_HARMONIC_CONTENT, 0x40);
      track->setHwCtrlState(CTRL_RELEASE_TIME, 0x40);
      track->setHwCtrlState(CTRL_ATTACK_TIME, 0x40);
      track->setHwCtrlState(CTRL_BRIGHTNESS, 0x40);
      track->setHwCtrlState(CTRL_REVERB_SEND, 0x28);
      track->setHwCtrlState(CTRL_CHORUS_SEND, 0x0);
      track->setHwCtrlState(CTRL_VARIATION_SEND, 0x0);
      track->setMeter(0, 0.0);
      }

//---------------------------------------------------------
//   sendSysex
//    send SYSEX message to midi device
//---------------------------------------------------------

void MidiOut::sendSysex(const unsigned char* p, int n)
      {
      MidiEvent event(0, ME_SYSEX, p, n);
      track->routeEvent(event);
      }

//---------------------------------------------------------
//   sendStart
//---------------------------------------------------------

void MidiOut::sendStart()
      {
      MidiEvent event(0, 0, ME_START, 0, 0);
      track->routeEvent(event);
      }

//---------------------------------------------------------
//   sendStop
//---------------------------------------------------------

void MidiOut::sendStop()
      {
      MidiEvent event(0, 0, ME_STOP, 0, 0);
      track->routeEvent(event);
      }

//---------------------------------------------------------
//   sendClock
//---------------------------------------------------------

void MidiOut::sendClock()
      {
      MidiEvent event(0, 0, ME_CLOCK, 0, 0);
      track->routeEvent(event);
      }

//---------------------------------------------------------
//   sendContinue
//---------------------------------------------------------

void MidiOut::sendContinue()
      {
      MidiEvent event(0, 0, ME_CONTINUE, 0, 0);
      track->routeEvent(event);
      }

//---------------------------------------------------------
//   sendSongpos
//---------------------------------------------------------

void MidiOut::sendSongpos(int pos)
      {
      MidiEvent event(0, 0, ME_SONGPOS, pos, 0);
      track->routeEvent(event);
      }

//---------------------------------------------------------
//   playMidiEvent
//    called from GUI
//---------------------------------------------------------

void MidiOut::playMidiEvent(MidiEvent* ev)
      {
	if (eventFifo.put(*ev))
            printf("MidiPort::playMidiEvent(): port overflow, drop event\n");
      }

//---------------------------------------------------------
//    seek
//---------------------------------------------------------

void MidiOut::seek(unsigned tickPos, unsigned framePos)
      {
      if (genMCSync && track->sendSync()) {
            int beat = (tickPos * 4) / config.division;
            sendStop();
            sendSongpos(beat);
            sendContinue();
            }
      if (track->mute())
            return;

//    if (pos == 0 && !song->record())
//          audio->initDevices();

      //---------------------------------------------------
      //    stop all notes
      //---------------------------------------------------

      for (iMidiEvent i = _schedEvents.begin(); i != _schedEvents.end(); ++i) {
            MidiEvent ev = *i;
            if (ev.isNoteOff()) {
                  ev.setTime(framePos);
                  track->routeEvent(ev);
                  }
            }
      _schedEvents.clear();

      //---------------------------------------------------
      //    set all controller
      //---------------------------------------------------

      if (track->autoRead()) {
            CtrlList* cl = track->controller();
            for (iCtrl ic = cl->begin(); ic != cl->end(); ++ic) {
                  Ctrl* c = ic->second;
                  int val = c->value(tickPos).i;
                  if ((val != CTRL_VAL_UNKNOWN) && (c->curVal().i != val)) {
                        MidiEvent ev(0, -1, ME_CONTROLLER, c->id(), val);
                        track->routeEvent(ev);
                        c->setCurVal(val);
                        }
                  }
            }
      foreach (const Route& r, *track->inRoutes()) {
            MidiTrackBase* t = (MidiTrackBase*)r.src.track;
            int dstChannel = r.dst.channel;
            if (t->isMute() || !t->autoRead())
                  continue;
            CtrlList* cl = t->controller();
            for (iCtrl ic = cl->begin(); ic != cl->end(); ++ic) {
                  Ctrl* c = ic->second;
                  int val = c->value(tickPos).i;
                  if ((val != CTRL_VAL_UNKNOWN) && (c->curVal().i != val)) {
                        MidiEvent ev(0, dstChannel, ME_CONTROLLER, c->id(), val);
                        track->routeEvent(ev);
                        c->setCurVal(val);
                        }
                  }
            }
      }

//---------------------------------------------------------
//   stop
//---------------------------------------------------------

void MidiOut::stop()
      {
      //---------------------------------------------------
      //    stop all notes
      //---------------------------------------------------

      int time = audio->seqTime()->lastFrameTime;
      for (iMidiEvent i = _schedEvents.begin(); i != _schedEvents.end(); ++i) {
            MidiEvent ev = *i;
            if (ev.isNoteOff()) {
                  ev.setTime(time);
                  track->routeEvent(ev);
                  }
            }
      _schedEvents.clear();

      //---------------------------------------------------
      //    reset sustain
      //---------------------------------------------------

      foreach (const Route& r, *track->inRoutes()) {
            MidiTrackBase* t = (MidiTrackBase*)r.src.track;
            int dstChannel = r.dst.channel;
            if (t->isMute() || !t->autoRead())
                  continue;
            CtrlList* cl = t->controller();
            for (iCtrl ic = cl->begin(); ic != cl->end(); ++ic) {
                  Ctrl* c = ic->second;
                  if (c->id() == CTRL_SUSTAIN) {
                        if (c->curVal().i > 0) {
                              MidiEvent ev(time, dstChannel, ME_CONTROLLER, c->id(), 0);
                              track->routeEvent(ev);
                              c->setCurVal(0);
                              }
                        }
                  }
            }

      if (track->sendSync()) {
            if (genMMC) {
                  unsigned char mmcPos[] = {
                        0x7f, 0x7f, 0x06, 0x44, 0x06, 0x01,
                        0, 0, 0, 0, 0
                        };
                  double frame = double(audio->seqTime()->pos.frame());
                  MTC mtc(frame / double(AL::sampleRate));
                  mmcPos[6] = mtc.h() | (AL::mtcType << 5);
                  mmcPos[7] = mtc.m();
                  mmcPos[8] = mtc.s();
                  mmcPos[9] = mtc.f();
                  mmcPos[10] = mtc.sf();
                  sendSysex(mmcStopMsg, sizeof(mmcStopMsg));
                  sendSysex(mmcPos, sizeof(mmcPos));
                  }
            if (genMCSync) {         // Midi Clock
                  // send STOP and
                  // "set song position pointer"
                  sendStop();
                  sendSongpos(audio->seqTime()->curTickPos * 4 / config.division);
                  }
            }
      }

//---------------------------------------------------------
//   start
//---------------------------------------------------------

void MidiOut::start()
      {
      // TODO: set sustain to old value?

      if (!(genMMC || genMCSync || track->sendSync()))
            return;
      if (genMMC)
            sendSysex(mmcDeferredPlayMsg, sizeof(mmcDeferredPlayMsg));
      if (genMCSync) {
            if (audio->seqTime()->curTickPos)
                  sendContinue();
            else
                  sendStart();
            }
      }

//---------------------------------------------------------
//   reset
//---------------------------------------------------------

void MidiOut::reset()
      {
/* TODO
      MidiEvent ev;
      ev.setType(0x90);
      for (int chan = 0; chan < MIDI_CHANNELS; ++chan) {
            ev.setChannel(chan);
            for (int pitch = 0; pitch < 128; ++pitch) {
                  ev.setA(pitch);
                  ev.setB(0);
                  mp->putEvent(ev);
                  }
            }
*/
      }

//-------------------------------------------------------------------
//   process
//    Collect all midi events for the current process cycle and put
//    into _schedEvents queue. For note on events create the proper
//    note off events. The note off events maybe played later after the
//    current process cycle.
//    From _schedEvents queue copy all events for the current cycle
//    to all output routes. Events routed to ALSA go into the
//    _playEvents queue which is processed by the MidiSeq thread.
//-------------------------------------------------------------------

void MidiOut::processMidi(MidiEventList& el, const SeqTime* time)
      {
      while (!eventFifo.isEmpty())
            el.insert(eventFifo.get());

      // collect port controller
      if (track->autoRead() && (time->curTickPos != time->nextTickPos)) {  // if rolling
            CtrlList* cl = track->controller();
            for (iCtrl ic = cl->begin(); ic != cl->end(); ++ic) {
                  Ctrl* c = ic->second;
                  iCtrlVal is = c->lowerBound(time->curTickPos);
                  iCtrlVal ie = c->lowerBound(time->nextTickPos);
                  for (iCtrlVal ic = is; ic != ie; ++ic) {
                        unsigned frame = AL::tempomap.tick2frame(ic.key());
                        MidiEvent ev(frame, -1, ME_CONTROLLER, c->id(), ic.value().i);
                        el.insert(ev);
                        c->setCurVal(ic.value().i);
                        }
                  }
            }
      foreach (const Route& r, *track->inRoutes()) {
            MidiTrackBase* t = (MidiTrackBase*)r.src.track;
            int dstChannel = r.dst.channel;
            if (t->isMute())
                  continue;
            MidiEventList ell;
            t->getEvents(time->curTickPos, time->nextTickPos, r.src.channel, &ell);
            int velo = 0;
            for (iMidiEvent i = ell.begin(); i != ell.end(); ++i) {
                  MidiEvent ev(*i);
                  ev.setChannel(dstChannel);
                  el.insert(ev);
                  if (ev.type() == ME_NOTEON)
                        velo += ev.dataB();
                  }
            t->addMidiMeter(velo);
            }
      }