1 //=========================================================
2 // MusE
3 // Linux Music Editor
4 // $Id: midictrl.cpp,v 1.17.2.10 2009/06/10 00:34:59 terminator356 Exp $
5 //
6 // (C) Copyright 2001-2004 Werner Schweer (ws@seh.de)
7 // (C) Copyright 2012 Tim E. Real (terminator356 on users dot sourceforge dot net)
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; version 2 of
12 // the License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 //=========================================================
24
25 #include <cstdio>
26 #include "muse_math.h"
27
28 #include "globaldefs.h"
29 #include "midictrl.h"
30 #include "globals.h"
31 #include "audio.h"
32 #include "midi_consts.h"
33 #include "midiport.h"
34 #include "minstrument.h"
35 #include "track.h"
36 #include "mpevent.h"
37
38 namespace MusECore {
39
40 enum {
41 COL_NAME = 0, COL_TYPE,
42 COL_HNUM, COL_LNUM, COL_MIN, COL_MAX
43 };
44
45 MidiControllerList defaultMidiController;
46 //
47 // some global controller which are always available:
48 //
49 // Zero note on vel is not allowed now.
50 MidiController veloCtrl("Velocity", CTRL_VELOCITY, 1, 127, 0, 0);
51 MidiController pitchCtrl("PitchBend", CTRL_PITCH, -8192, +8191, 0, 0);
52 MidiController programCtrl("Program", CTRL_PROGRAM, 0, 0xffffff, 0, 0);
53 MidiController mastervolCtrl("MasterVolume", CTRL_MASTER_VOLUME, 0, 0x3fff, 0x3000, 0x3000);
54 MidiController volumeCtrl("MainVolume", CTRL_VOLUME, 0, 127, 100, 100);
55 MidiController panCtrl("Pan", CTRL_PANPOT, -64, 63, 0, 0);
56 MidiController reverbSendCtrl("ReverbSend", CTRL_REVERB_SEND, 0, 127, 0, 0);
57 MidiController chorusSendCtrl("ChorusSend", CTRL_CHORUS_SEND, 0, 127, 0, 0);
58 MidiController variationSendCtrl("VariationSend", CTRL_VARIATION_SEND, 0, 127, 0, 0);
59
60 //---------------------------------------------------------
61 // initMidiController
62 //---------------------------------------------------------
63
initMidiController()64 void initMidiController()
65 {
66 defaultMidiController.add(&veloCtrl);
67 defaultMidiController.add(&pitchCtrl);
68 defaultMidiController.add(&programCtrl);
69 defaultMidiController.add(&mastervolCtrl);
70 defaultMidiController.add(&volumeCtrl);
71 defaultMidiController.add(&panCtrl);
72 defaultMidiController.add(&reverbSendCtrl);
73 defaultMidiController.add(&chorusSendCtrl);
74 defaultMidiController.add(&variationSendCtrl);
75 }
76
77 //---------------------------------------------------------
78 // MidiCtrlValList
79 //---------------------------------------------------------
80
MidiCtrlValList(int c)81 MidiCtrlValList::MidiCtrlValList(int c)
82 {
83 ctrlNum = c;
84 _hwVal = _lastValidHWVal = _lastValidByte2 = _lastValidByte1 = _lastValidByte0 = CTRL_VAL_UNKNOWN;
85 }
86
87 //---------------------------------------------------------
88 // MidiCtrlValListList
89 //---------------------------------------------------------
90
MidiCtrlValListList()91 MidiCtrlValListList::MidiCtrlValListList()
92 {
93 _RPN_Ctrls_Reserved = false;
94 }
95
96 // TODO: Finish copy constructor, but first MidiCtrlValList might need one too ?
97 // MidiCtrlValListList::MidiCtrlValListList(const MidiCtrlValListList& mcvl) : std::map<int, MidiCtrlValList*>()
98 // {
99 // for(ciMidiCtrlValList i = mcvl.begin(); i != mcvl.end(); ++i)
100 // {
101 // MidiCtrlValList* mcl = i->second;
102 // add(new MidiCtrlValList(*mcl));
103 // }
104 // update_RPN_Ctrls_Reserved();
105 // }
106
add(int channel,MidiCtrlValList * vl,bool update)107 void MidiCtrlValListList::add(int channel, MidiCtrlValList* vl, bool update)
108 {
109 // TODO: If per-channel instruments are ever added to MusE, this routine would need changing.
110
111 const int num = vl->num();
112 if(!_RPN_Ctrls_Reserved && update)
113 {
114 const bool isCtl7 = ((num & CTRL_OFFSET_MASK) == CTRL_7_OFFSET);
115 const bool isCtl14 = ((num & CTRL_OFFSET_MASK) == CTRL_14_OFFSET);
116 if(isCtl14 || isCtl7)
117 {
118 const int l = num & 0xff;
119 if(l == CTRL_HDATA ||
120 l == CTRL_LDATA ||
121 l == CTRL_DATA_INC ||
122 l == CTRL_DATA_DEC ||
123 l == CTRL_HNRPN ||
124 l == CTRL_LNRPN ||
125 l == CTRL_HRPN ||
126 l == CTRL_LRPN)
127 _RPN_Ctrls_Reserved = true;
128 }
129 if(!_RPN_Ctrls_Reserved && isCtl14)
130 {
131 const int h = (num >> 8) & 0xff;
132 if(h == CTRL_HDATA ||
133 h == CTRL_LDATA ||
134 h == CTRL_DATA_INC ||
135 h == CTRL_DATA_DEC ||
136 h == CTRL_HNRPN ||
137 h == CTRL_LNRPN ||
138 h == CTRL_HRPN ||
139 h == CTRL_LRPN)
140 _RPN_Ctrls_Reserved = true;
141 }
142 }
143 insert(std::pair<const int, MidiCtrlValList*>((channel << 24) + num, vl));
144 }
145
del(iMidiCtrlValList ictl,bool update)146 void MidiCtrlValListList::del(iMidiCtrlValList ictl, bool update)
147 {
148 erase(ictl);
149 if(update)
150 update_RPN_Ctrls_Reserved();
151 }
152
del(int num,bool update)153 MidiCtrlValListList::size_type MidiCtrlValListList::del(int num, bool update)
154 {
155 MidiCtrlValListList::size_type res = erase(num);
156 if(update)
157 update_RPN_Ctrls_Reserved();
158 return res;
159 }
160
del(iMidiCtrlValList first,iMidiCtrlValList last,bool update)161 void MidiCtrlValListList::del(iMidiCtrlValList first, iMidiCtrlValList last, bool update)
162 {
163 erase(first, last);
164 if(update)
165 update_RPN_Ctrls_Reserved();
166 }
167
clr()168 void MidiCtrlValListList::clr()
169 {
170 clear();
171 update_RPN_Ctrls_Reserved();
172 }
173
174 //---------------------------------------------------------
175 // clearDelete
176 //---------------------------------------------------------
177
clearDelete(bool deleteLists)178 void MidiCtrlValListList::clearDelete(bool deleteLists)
179 {
180 for(iMidiCtrlValList imcvl = begin(); imcvl != end(); ++imcvl)
181 {
182 if(imcvl->second)
183 {
184 imcvl->second->clear();
185 if(deleteLists)
186 delete imcvl->second;
187 }
188 }
189 if(deleteLists)
190 clr();
191 }
192
193 //---------------------------------------------------------
194 // resetAllHwVals
195 //---------------------------------------------------------
196
resetAllHwVals(bool doLastHwValue)197 bool MidiCtrlValListList::resetAllHwVals(bool doLastHwValue)
198 {
199 bool changed = false;
200 for(iMidiCtrlValList imcvl = begin(); imcvl != end(); ++imcvl)
201 {
202 if(imcvl->second)
203 {
204 if(imcvl->second->resetHwVal(doLastHwValue))
205 changed = true;
206 }
207 }
208 return changed;
209 }
210
211 //---------------------------------------------------------
212 // searchControllers
213 //---------------------------------------------------------
214
searchControllers(int channel,int ctl)215 iMidiCtrlValList MidiCtrlValListList::searchControllers(int channel, int ctl)
216 {
217 const int type = ctl & CTRL_OFFSET_MASK;
218 const unsigned ch_bits = (channel << 24);
219 int n;
220
221 // Looking for Controller7? See if any Controller14 contains the number and should be used instead.
222 if(type == CTRL_7_OFFSET)
223 {
224 const int num = ctl & 0xff;
225 for(iMidiCtrlValList imc = lower_bound(ch_bits | CTRL_14_OFFSET); imc != end(); ++imc)
226 {
227 // There is ->second->num(), but this is only way to get channel.
228 n = imc->first;
229 // Stop if we went beyond this channel number or this ctrl14 block.
230 if((n & 0xff000000) != ch_bits || (n & CTRL_OFFSET_MASK) != CTRL_14_OFFSET)
231 break;
232 if(((n >> 8) & 0xff) == num || (n & 0xff) == num)
233 return imc;
234 }
235 }
236 // Looking for RPN? See if any RPN14 also uses the number and should be used instead.
237 else if (type == CTRL_RPN_OFFSET)
238 {
239 const int num = ctl & 0xffff;
240 for(iMidiCtrlValList imc = lower_bound(ch_bits | CTRL_RPN14_OFFSET); imc != end(); ++imc)
241 {
242 // There is ->second->num(), but this is only way to get channel.
243 n = imc->first;
244 // Stop if we went beyond this channel number or this RPN14 block.
245 if((n & 0xff000000) != ch_bits || (n & CTRL_OFFSET_MASK) != CTRL_RPN14_OFFSET)
246 break;
247 if((n & 0xffff) == num)
248 return imc;
249 }
250 }
251 // Looking for NRPN? See if any NRPN14 also uses the number and should be used instead.
252 else if (type == CTRL_NRPN_OFFSET)
253 {
254 const int num = ctl & 0xffff;
255 for(iMidiCtrlValList imc = lower_bound(ch_bits | CTRL_NRPN14_OFFSET); imc != end(); ++imc)
256 {
257 // There is ->second->num(), but this is only way to get channel.
258 n = imc->first;
259 // Stop if we went beyond this channel number or this NRPN14 block.
260 if((n & 0xff000000) != ch_bits || (n & CTRL_OFFSET_MASK) != CTRL_NRPN14_OFFSET)
261 break;
262 if((n & 0xffff) == num)
263 return imc;
264 }
265 }
266
267 // Looking for any other type? Do a regular find.
268 return std::map<int, MidiCtrlValList*, std::less<int> >::find(ch_bits | ctl);
269 }
270
271 //---------------------------------------------------------
272 // update_RPN_Ctrls_Reserved
273 // Manual check and update of the flag. For convenience, returns the flag.
274 // Cost depends on types and number of list controllers, so it is good for deferring
275 // an update until AFTER some lengthy list operation. JUST BE SURE to call this!
276 //---------------------------------------------------------
277
update_RPN_Ctrls_Reserved()278 bool MidiCtrlValListList::update_RPN_Ctrls_Reserved()
279 {
280 // TODO: If per-channel instruments are ever added to MusE, this routine would need changing.
281
282 int num, h, l;
283 for(int ch = 0; ch < MusECore::MUSE_MIDI_CHANNELS; ++ch)
284 {
285 const unsigned ch_bits = (ch << 24);
286
287 if(find(ch, CTRL_HDATA) != end() ||
288 find(ch, CTRL_LDATA) != end() ||
289 find(ch, CTRL_DATA_INC) != end() ||
290 find(ch, CTRL_DATA_DEC) != end() ||
291 find(ch, CTRL_HNRPN) != end() ||
292 find(ch, CTRL_LNRPN) != end() ||
293 find(ch, CTRL_HRPN) != end() ||
294 find(ch, CTRL_LRPN) != end())
295 {
296 _RPN_Ctrls_Reserved = true;
297 return true;
298 }
299
300 // Search: Get a head-start by taking lower bound.
301 for(iMidiCtrlValList imc = lower_bound(ch_bits | CTRL_14_OFFSET); imc != end(); ++imc)
302 {
303 // There is ->second->num(), but this is only way to get channel.
304 num = imc->first;
305 // Stop if we went beyond this channel number or its ctrl14 block.
306 if((num & 0xff000000) != ch_bits || (num & CTRL_OFFSET_MASK) != CTRL_14_OFFSET)
307 {
308 _RPN_Ctrls_Reserved = false;
309 return false;
310 }
311 h = (num >> 8) & 0xff;
312 l = num & 0xff;
313 if(h == CTRL_HDATA || l == CTRL_HDATA ||
314 h == CTRL_LDATA || l == CTRL_LDATA ||
315 h == CTRL_DATA_INC || l == CTRL_DATA_INC ||
316 h == CTRL_DATA_DEC || l == CTRL_DATA_DEC ||
317 h == CTRL_HNRPN || l == CTRL_HNRPN ||
318 h == CTRL_LNRPN || l == CTRL_LNRPN ||
319 h == CTRL_HRPN || l == CTRL_HRPN ||
320 h == CTRL_LRPN || l == CTRL_LRPN)
321 {
322 _RPN_Ctrls_Reserved = true;
323 return true;
324 }
325 }
326 }
327
328 _RPN_Ctrls_Reserved = false;
329 return false;
330 }
331
332 //---------------------------------------------------------
333 // Catch all insert, erase, clear etc.
334 //---------------------------------------------------------
335
operator =(const MidiCtrlValListList & cl)336 MidiCtrlValListList& MidiCtrlValListList::operator=(const MidiCtrlValListList& cl)
337 {
338 #ifdef _MIDI_CTRL_DEBUG_
339 printf("MidiCtrlValListList::operator=\n");
340 #endif
341 _RPN_Ctrls_Reserved = cl._RPN_Ctrls_Reserved;
342
343 // Let map copy the items.
344 std::map<int, MidiCtrlValList*, std::less<int> >::operator=(cl);
345 return *this;
346 }
347
348 //=========================================================
349 #ifdef _MIDI_CTRL_METHODS_DEBUG_
350
swap(MidiCtrlValListList & cl)351 void MidiCtrlValListList::swap(MidiCtrlValListList& cl)
352 {
353 #ifdef _MIDI_CTRL_DEBUG_
354 printf("MidiCtrlValListList::swap\n");
355 #endif
356 std::map<int, MidiCtrlValList*, std::less<int> >::swap(cl);
357 }
358
insert(const std::pair<int,MidiCtrlValList * > & p)359 std::pair<iMidiCtrlValList, bool> MidiCtrlValListList::insert(const std::pair<int, MidiCtrlValList*>& p)
360 {
361 #ifdef _MIDI_CTRL_DEBUG_
362 printf("MidiCtrlValListList::insert num:%d\n", p.second->num());
363 #endif
364 std::pair<iMidiCtrlValList, bool> res = std::map<int, MidiCtrlValList*, std::less<int> >::insert(p);
365 return res;
366 }
367
insert(iMidiCtrlValList ic,const std::pair<int,MidiCtrlValList * > & p)368 iMidiCtrlValList MidiCtrlValListList::insert(iMidiCtrlValList ic, const std::pair<int, MidiCtrlValList*>& p)
369 {
370 #ifdef _MIDI_CTRL_DEBUG_
371 printf("MidiCtrlValListList::insertAt num:%d\n", p.second->num());
372 #endif
373 iMidiCtrlValList res = std::map<int, MidiCtrlValList*, std::less<int> >::insert(ic, p);
374 return res;
375 }
376
erase(iMidiCtrlValList ictl)377 void MidiCtrlValListList::erase(iMidiCtrlValList ictl)
378 {
379 #ifdef _MIDI_CTRL_DEBUG_
380 printf("MidiCtrlValListList::erase iMidiCtrlValList num:%d\n", ictl->second->num());
381 #endif
382 std::map<int, MidiCtrlValList*, std::less<int> >::erase(ictl);
383 }
384
erase(int num)385 MidiCtrlValListList::size_type MidiCtrlValListList::erase(int num)
386 {
387 #ifdef _MIDI_CTRL_DEBUG_
388 printf("MidiCtrlValListList::erase num:%d\n", num);
389 #endif
390 size_type res = std::map<int, MidiCtrlValList*, std::less<int> >::erase(num);
391 return res;
392 }
393
erase(iMidiCtrlValList first,iMidiCtrlValList last)394 void MidiCtrlValListList::erase(iMidiCtrlValList first, iMidiCtrlValList last)
395 {
396 #ifdef _MIDI_CTRL_DEBUG_
397 printf("MidiCtrlValListList::erase range first num:%d second num:%d\n",
398 first->second->num(), last->second->num());
399 #endif
400 std::map<int, MidiCtrlValList*, std::less<int> >::erase(first, last);
401 }
402
clear()403 void MidiCtrlValListList::clear()
404 {
405 #ifdef _MIDI_CTRL_DEBUG_
406 printf("MidiCtrlValListList::clear\n");
407 #endif
408 std::map<int, MidiCtrlValList*, std::less<int> >::clear();
409 }
410
411 #endif
412 // =========================================================
413
414
resetHwVal(bool doLastHwValue)415 bool MidiCtrlValList::resetHwVal(bool doLastHwValue)
416 {
417 bool changed = false;
418 if(!hwValIsUnknown())
419 {
420 _hwVal = CTRL_VAL_UNKNOWN;
421 changed = true;
422 }
423
424 if(doLastHwValue)
425 {
426 if(!lastHwValIsUnknown())
427 changed = true;
428 _lastValidHWVal = _lastValidByte2 = _lastValidByte1 = _lastValidByte0 = CTRL_VAL_UNKNOWN;
429 }
430
431 return changed;
432 }
433
434 //---------------------------------------------------------
435 // setHwVal
436 // Returns false if value is already equal, true if value is changed.
437 //---------------------------------------------------------
438
setHwVal(const double v)439 bool MidiCtrlValList::setHwVal(const double v)
440 {
441 const double r_v = muse_round2micro(v);
442 if(_hwVal == r_v)
443 return false;
444
445 _hwVal = r_v;
446
447 const int i_val = MidiController::dValToInt(_hwVal);
448 if(!MidiController::iValIsUnknown(i_val))
449 {
450 _lastValidHWVal = _hwVal;
451 const int hb = (i_val >> 16) & 0xff;
452 const int lb = (i_val >> 8) & 0xff;
453 const int pr = i_val & 0xff;
454 if(hb >= 0 && hb <= 127)
455 _lastValidByte2 = hb;
456 if(lb >= 0 && lb <= 127)
457 _lastValidByte1 = lb;
458 if(pr >= 0 && pr <= 127)
459 _lastValidByte0 = pr;
460 }
461
462 return true;
463 }
464
465 //---------------------------------------------------------
466 // setHwVals
467 // Sets current and last HW values.
468 // Handy for forcing labels to show 'off' and knobs to show specific values
469 // without having to send two messages.
470 // Returns false if both values are already set, true if either value is changed.
471 //---------------------------------------------------------
472
setHwVals(const double v,const double lastv)473 bool MidiCtrlValList::setHwVals(const double v, const double lastv)
474 {
475 const double r_v = muse_round2micro(v);
476 const double r_lastv = muse_round2micro(lastv);
477
478 if(_hwVal == r_v && _lastValidHWVal == r_lastv)
479 return false;
480
481 _hwVal = r_v;
482 // Don't want to break our own rules - _lastValidHWVal can't be unknown while _hwVal is valid...
483 // But _hwVal can be unknown while _lastValidHWVal is valid...
484 if(MidiController::dValIsUnknown(r_lastv))
485 _lastValidHWVal = _hwVal;
486 else
487 _lastValidHWVal = r_lastv;
488
489 const int i_lasthwval = MidiController::dValToInt(_lastValidHWVal);
490 if(!MidiController::iValIsUnknown(i_lasthwval))
491 {
492 const int hb = (i_lasthwval >> 16) & 0xff;
493 const int lb = (i_lasthwval >> 8) & 0xff;
494 const int pr = i_lasthwval & 0xff;
495 if(hb >= 0 && hb <= 127)
496 _lastValidByte2 = hb;
497 if(lb >= 0 && lb <= 127)
498 _lastValidByte1 = lb;
499 if(pr >= 0 && pr <= 127)
500 _lastValidByte0 = pr;
501 }
502
503 return true;
504 }
505
506 //---------------------------------------------------------
507 // partAtTick
508 //---------------------------------------------------------
509
partAtTick(unsigned int tick) const510 Part* MidiCtrlValList::partAtTick(unsigned int tick) const
511 {
512 // Determine (first) part at tick. Return 0 if none found.
513
514 ciMidiCtrlVal i = lower_bound(tick);
515 if (i == end() || i->first != tick) {
516 if (i == begin())
517 return 0;
518 --i;
519 }
520 return i->second.part;
521 }
522
523 //---------------------------------------------------------
524 // iValue
525 //---------------------------------------------------------
526
iValue(unsigned int tick)527 iMidiCtrlVal MidiCtrlValList::iValue(unsigned int tick)
528 {
529 // Determine value at tick, using values stored by ANY part...
530
531 iMidiCtrlVal i = lower_bound(tick);
532 if (i == end() || i->first != tick) {
533 if (i == begin())
534 return end();
535 --i;
536 }
537 return i;
538 }
539
540 //---------------------------------------------------------
541 // value
542 //---------------------------------------------------------
543
value(unsigned int tick) const544 int MidiCtrlValList::value(unsigned int tick) const
545 {
546 // Determine value at tick, using values stored by ANY part...
547
548 ciMidiCtrlVal i = lower_bound(tick);
549 if (i == end() || i->first != tick) {
550 if (i == begin())
551 return CTRL_VAL_UNKNOWN;
552 --i;
553 }
554 return i->second.val;
555 }
556
value(unsigned int tick,Part * part) const557 int MidiCtrlValList::value(unsigned int tick, Part* part) const
558 {
559 // Determine value at tick, using values stored by the SPECIFIC part...
560
561 // Get the first value found with with a tick equal or greater than specified tick.
562 ciMidiCtrlVal i = lower_bound(tick);
563 // Since values from different parts can have the same tick, scan for part in all values at that tick.
564 for(ciMidiCtrlVal j = i; j != end() && j->first == tick; ++j)
565 {
566 if(j->second.part == part)
567 return j->second.val;
568 }
569 // Scan for part in all previous values, regardless of tick.
570 while(i != begin())
571 {
572 --i;
573 if(i->second.part == part)
574 return i->second.val;
575 }
576 // No previous values were found belonging to the specified part.
577 return CTRL_VAL_UNKNOWN;
578 }
579
visibleValue(unsigned int tick,bool inclMutedParts,bool inclMutedTracks,bool inclOffTracks) const580 int MidiCtrlValList::visibleValue(unsigned int tick, bool inclMutedParts, bool inclMutedTracks, bool inclOffTracks) const
581 {
582 // Determine value at tick, using values stored by ANY part,
583 // ignoring values that are OUTSIDE of their parts, or muted or off parts or tracks...
584
585 // Get the first value found with with a tick equal or greater than specified tick.
586 ciMidiCtrlVal i = lower_bound(tick);
587 // Since values from different parts can have the same tick, scan for part in all values at that tick.
588 for(ciMidiCtrlVal j = i; j != end() && j->first == tick; ++j)
589 {
590 const Part* part = j->second.part;
591 // Ignore values that are outside of the part.
592 if(tick < part->tick() || tick >= (part->tick() + part->lenTick()))
593 continue;
594 // Ignore if part or track is muted or off.
595 if(!inclMutedParts && part->mute())
596 continue;
597 const Track* track = part->track();
598 if(track && ((!inclMutedTracks && track->isMute()) || (!inclOffTracks && track->off())))
599 continue;
600 return j->second.val;
601 }
602 // Scan for part in all previous values, regardless of tick.
603 while(i != begin())
604 {
605 --i;
606 const Part* part = i->second.part;
607 // Ignore values that are outside of the part.
608 if(tick < part->tick() || tick >= (part->tick() + part->lenTick()))
609 continue;
610 // Ignore if part or track is muted or off.
611 if(!inclMutedParts && part->mute())
612 continue;
613 const Track* track = part->track();
614 if(track && ((!inclMutedTracks && track->isMute()) || (!inclOffTracks && track->off())))
615 continue;
616 return i->second.val;
617 }
618 // No previous values were found belonging to the specified part.
619 return CTRL_VAL_UNKNOWN;
620 }
621
visibleValue(unsigned int tick,Part * part,bool inclMutedParts,bool inclMutedTracks,bool inclOffTracks) const622 int MidiCtrlValList::visibleValue(unsigned int tick, Part* part, bool inclMutedParts, bool inclMutedTracks, bool inclOffTracks) const
623 {
624 // Determine value at tick, using values stored by the SPECIFIC part,
625 // ignoring values that are OUTSIDE of the part, or muted or off part or track...
626
627 if((!inclMutedParts && part->mute()) || (part->track() && ((!inclMutedTracks && part->track()->isMute()) || (!inclOffTracks && part->track()->off()))))
628 return CTRL_VAL_UNKNOWN;
629
630 // Get the first value found with with a tick equal or greater than specified tick.
631 ciMidiCtrlVal i = lower_bound(tick);
632 // Ignore if part or track is muted or off.
633 // Since values from different parts can have the same tick, scan for part in all values at that tick.
634 for(ciMidiCtrlVal j = i; j != end() && j->first == tick; ++j)
635 {
636 if(j->second.part == part)
637 {
638 // Ignore values that are outside of the part.
639 if(tick < part->tick() || tick >= (part->tick() + part->lenTick()))
640 continue;
641 return j->second.val;
642 }
643 }
644 // Scan for part in all previous values, regardless of tick.
645 while(i != begin())
646 {
647 --i;
648 if(i->second.part == part)
649 return i->second.val;
650 }
651 // No previous values were found belonging to the specified part.
652 return CTRL_VAL_UNKNOWN;
653 }
654
655 //---------------------------------------------------------
656 // add
657 // return true if new controller value added
658 // Accepts duplicate controller items at the same position, to accurately reflect
659 // what is really in the event lists.
660 // REMOVE Tim. Ctrl. Added comment.
661 // NOTE: It is FORBIDDEN to have multiple controller events at the same time with the same controller number.
662 // To preserve speed, we RELY on catching that at higher levels like the add event dialog or during song loading.
663 // See detailed comments in EventList::add().
664 //---------------------------------------------------------
665
addMCtlVal(unsigned int tick,int val,Part * part)666 bool MidiCtrlValList::addMCtlVal(unsigned int tick, int val, Part* part)
667 {
668 insert(MidiCtrlValListInsertPair_t(tick, MidiCtrlVal(part, val)));
669 return true;
670 }
671
672 //---------------------------------------------------------
673 // del
674 //---------------------------------------------------------
675
delMCtlVal(unsigned int tick,Part * part,int val)676 void MidiCtrlValList::delMCtlVal(unsigned int tick, Part* part, int val)
677 {
678 iMidiCtrlVal e = findMCtlVal(tick, part, val);
679 if (e == end()) {
680 if(MusEGlobal::debugMsg)
681 printf("MidiCtrlValList::delMCtlVal(%u): not found (size %zd)\n", tick, size());
682 return;
683 }
684 erase(e);
685 }
686
687 //---------------------------------------------------------
688 // find
689 //---------------------------------------------------------
690
findMCtlVal(unsigned int tick,Part * part,int val)691 iMidiCtrlVal MidiCtrlValList::findMCtlVal(unsigned int tick, Part* part, int val)
692 {
693 MidiCtrlValRange range = equal_range(tick);
694 for(iMidiCtrlVal i = range.first; i != range.second; ++i)
695 {
696 if(i->second.part == part && (val == -1 || i->second.val == val))
697 return i;
698 }
699 return end();
700 }
701
702 //---------------------------------------------------------
703 // MidiEncoder
704 //---------------------------------------------------------
705
MidiEncoder()706 MidiEncoder::MidiEncoder()
707 {
708 _curMode = EncIdle;
709 // Try starting with ParamModeUnknown. Requires either RPN or NRPN params at least once.
710 // Possibly may want to start with ParamModeRPN or ParamModeNRPN,
711 // possibly make it depend on planned all-encompassing 'Optimizations' Setting,
712 // and provide reset with 'Panic' button, just as it now resets output optimizations.
713 _curParamMode = ParamModeUnknown;
714 _curData = 0;
715 _curTime = 0;
716 _timer = 0;
717 _curCtrl = 0;
718 _nextCtrl = 0;
719 _curRPNH = 0;
720 _curRPNL = 0;
721 _curNRPNL = 0;
722 _curNRPNH = 0;
723 }
724
725 //---------------------------------------------------------
726 // encodeEvent
727 //---------------------------------------------------------
728
encodeEvent(const MidiRecordEvent & ev,int port,int channel)729 void MidiEncoder::encodeEvent(const MidiRecordEvent& ev, int port, int channel)
730 {
731 const int type = ev.type();
732 if(type != ME_PITCHBEND && type != ME_AFTERTOUCH && type != ME_POLYAFTER && type != ME_PROGRAM && type != ME_CONTROLLER)
733 return;
734
735 MidiPort* mp = &MusEGlobal::midiPorts[port];
736
737 MidiCtrlValListList* mcvll = mp->controller();
738 MidiInstrument* instr = mp->instrument();
739 MidiControllerList* mcl = instr->controller();
740
741 int num;
742 int data;
743 //int ctrlH;
744 //int ctrlL;
745 //const int ch_bits = channel << 24;
746
747 if(_curMode != EncIdle)
748 {
749 if(_curMode == EncCtrl14)
750 num = CTRL_14_OFFSET + ((_curCtrl << 8) | _nextCtrl);
751 else if(_curMode == EncRPN14)
752 num = CTRL_RPN14_OFFSET + ((_curRPNH << 8) | _curRPNL);
753 else if(_curMode == EncNRPN14)
754 num = CTRL_NRPN14_OFFSET + ((_curNRPNH << 8) | _curNRPNL);
755 else
756 {
757 // Error
758 _curMode = EncIdle;
759 return;
760 }
761
762 iMidiCtrlValList imcvl = mcvll->find(channel, num);
763 if(imcvl == mcvll->end())
764 {
765 // Error, controller should exist
766 _curMode = EncIdle;
767 return;
768 }
769 MidiCtrlValList* mcvl = imcvl->second;
770
771 if(type == ME_CONTROLLER && ev.dataA() == _nextCtrl)
772 {
773 data = (_curData << 7) | (ev.dataB() & 0x7f);
774 mcvl->setHwVal(data);
775 _curMode = EncIdle;
776 return;
777 }
778 else
779 {
780 data = (_curData << 7) | (mcvl->hwVal() & 0x7f);
781 mcvl->setHwVal(data);
782 //_curMode = EncIdle;
783 //return;
784 }
785
786 //return;
787 }
788
789 if(type == ME_CONTROLLER)
790 {
791 num = ev.dataA();
792 const int val = ev.dataB();
793 // Is it one of the 8 reserved GM controllers for RPN/NRPN?
794 if(num == CTRL_HDATA || num == CTRL_LDATA || num == CTRL_DATA_INC || num == CTRL_DATA_DEC ||
795 num == CTRL_HNRPN || num == CTRL_LNRPN || num == CTRL_HRPN || num == CTRL_LRPN)
796 {
797 // Does the working controller list, and instrument, allow RPN/NRPN?
798 // (If EITHER the working controller list or instrument defines ANY of these
799 // 8 controllers as plain 7-bit, then we cannot accept this data as RPN/NRPN.)
800 const bool rpn_reserved = mcvll->RPN_Ctrls_Reserved() | mcl->RPN_Ctrls_Reserved();
801 if(!rpn_reserved)
802 {
803 switch(num)
804 {
805 case CTRL_HDATA:
806 {
807 _curData = val;
808 switch(_curParamMode)
809 {
810 case ParamModeUnknown:
811 return; // Sorry, we shouldn't accept it without valid (N)RPN numbers.
812 case ParamModeRPN:
813 {
814 const int param_num = (_curRPNH << 8) | _curRPNL;
815 iMidiCtrlValList imcvl = mcvll->searchControllers(channel, CTRL_RPN_OFFSET | param_num);
816 if(imcvl == mcvll->end())
817 {
818 // Set mode, _curTime, and _timer to wait for next event.
819 _curMode = EncDiscoverRPN;
820 _curTime = MusEGlobal::audio->curFrame();
821 _timer = 0;
822 return;
823 }
824 else if((imcvl->first & CTRL_OFFSET_MASK) == CTRL_RPN_OFFSET)
825 {
826 // Done. Take _curData and param_num and compose something to return,
827 // and set the HWval... TODO
828 _curMode = EncIdle;
829 return;
830 }
831 else if((imcvl->first & CTRL_OFFSET_MASK) == CTRL_RPN14_OFFSET)
832 {
833 // Set mode, _curTime, and _timer to wait for next event.
834 _curMode = EncRPN14;
835 _curTime = MusEGlobal::audio->curFrame();
836 _timer = 0;
837 return;
838 }
839 else
840 {
841 fprintf(stderr, "MidiEncoder::encodeEvent unknown type %d\n", imcvl->first & CTRL_OFFSET_MASK);
842 return;
843 }
844
845 break;
846 }
847 case ParamModeNRPN:
848 break;
849 default:
850 fprintf(stderr, "MidiEncoder::encodeEvent unknown ParamMode %d\n", _curParamMode);
851 return;
852 }
853
854 break;
855 }
856
857 case CTRL_LDATA:
858 break;
859 case CTRL_DATA_INC:
860 break;
861 case CTRL_DATA_DEC:
862 break;
863 case CTRL_HRPN:
864 _curRPNH = val;
865 _curParamMode = ParamModeRPN;
866 return;
867 case CTRL_LRPN:
868 _curRPNL = val;
869 _curParamMode = ParamModeRPN;
870 return;
871 case CTRL_HNRPN:
872 _curNRPNH = val;
873 _curParamMode = ParamModeNRPN;
874 return;
875 case CTRL_LNRPN:
876 _curNRPNL = val;
877 _curParamMode = ParamModeNRPN;
878 return;
879 default:
880 break;
881 }
882 }
883 }
884
885 // for(iMidiCtrlValList imcvl = mcvll->begin(); imcvl != mcvll->end(); ++imcvl)
886 // {
887 // if(((imcvl->first >> 24) & 0xf) != channel)
888 // continue;
889 // MidiCtrlValList* mcvl = imcvl->second;
890 // }
891 }
892
893 _curMode = EncIdle;
894 return;
895
896
897 // if(_curMode != EncIdle)
898 // {
899 // if(_curMode == EncCtrl14 && type == ME_CONTROLLER && ev.dataA() == _nextCtrl)
900 // {
901 // num = CTRL_14_OFFSET + ((_curCtrl << 8) | (_nextCtrl & 0x7f));
902 // iMidiCtrlValList imcvl = mcvll->find(channel, num);
903 // if(imcvl == mcvll->end())
904 // return; // Error, controller should exist
905 // MidiCtrlValList* mcvl = imcvl->second;
906 // data = (_curData << 7) | (ev.dataB() & 0x7f);
907 // mcvl->setHwVal(data);
908 // //_timer = 0;
909 // _curMode = EncIdle;
910 // return;
911 // }
912 // else if(_curMode == EncRPN14 && type == ME_CONTROLLER && ev.dataA() == CTRL_LDATA)
913 // {
914 //
915 // }
916 // }
917
918 //mcvl->find();
919
920 // for(ciMidiCtrlValList imcvl = mcvl->begin(); imcvl != mcvl->end(); ++imcvl)
921 // {
922 // const int ch = imcvl->first >> 24;
923 // if(ch != channel)
924 // continue;
925 // MidiCtrlValList* mcvl = imcvl->second;
926 // num = mcvl->num();
927 // ctrlH = (num >> 8) & 0x7f;
928 // ctrlL = num & 0xff;
929 // if(type == ME_CONTROLLER)
930 // {
931 // const int ev_num = ev.dataA();
932 // if(num < CTRL_14_OFFSET) // 7-bit controller 0 - 0x10000
933 // {
934 // if(ev_num == num)
935 // {
936 //
937 // }
938 // }
939 // else if(num < CTRL_RPN_OFFSET) // 14-bit controller 0x10000 - 0x20000
940 // {
941 //
942 // }
943 // }
944 // }
945
946
947 // int num;
948 //
949 // switch(type)
950 // {
951 // // TODO
952 // case ME_PITCHBEND:
953 // num = CTRL_PITCH;
954 // break;
955 // case ME_AFTERTOUCH:
956 // num = CTRL_AFTERTOUCH;
957 // break;
958 // case ME_POLYAFTER:
959 // num = CTRL_POLYAFTER | (ev.dataA() & 0x7f);
960 // break;
961 // case ME_PROGRAM:
962 // num = CTRL_PROGRAM;
963 // break;
964 //
965 // case ME_CONTROLLER:
966 // {
967 // //num = CTRL_;
968 // }
969 // break;
970 //
971 // default:
972 // return;
973 // }
974
975
976
977 // if(instr)
978 // {
979 // int num;
980 // int ctrlH;
981 // int ctrlL;
982 // MidiControllerList* mcl = instr->controller();
983 // MidiController* mc;
984 //
985 // if (_outputInstrument) {
986 // MidiControllerList* mcl = _outputInstrument->controller();
987 // for (iMidiController i = mcl->begin(); i != mcl->end(); ++i) {
988 // int cn = i->second->num();
989 // if (cn == num)
990 // return i->second;
991 // // wildcard?
992 // if (i->second->isPerNoteController() && ((cn & ~0xff) == (num & ~0xff)))
993 // return i->second;
994 // }
995 // }
996 //
997 // for (iMidiController i = defaultMidiController.begin(); i != defaultMidiController.end(); ++i) {
998 // int cn = i->second->num();
999 // if (cn == num)
1000 // return i->second;
1001 // // wildcard?
1002 // if (i->second->isPerNoteController() && ((cn & ~0xff) == (num & ~0xff)))
1003 // return i->second;
1004 // }
1005 //
1006 //
1007 // QString name = midiCtrlName(num);
1008 // int min = 0;
1009 // int max = 127;
1010 //
1011 // MidiController::ControllerType t = midiControllerType(num);
1012 // switch (t) {
1013 // case MidiController::RPN:
1014 // case MidiController::NRPN:
1015 // case MidiController::Controller7:
1016 // case MidiController::PolyAftertouch:
1017 // case MidiController::Aftertouch:
1018 // max = 127;
1019 // break;
1020 // case MidiController::Controller14:
1021 // case MidiController::RPN14:
1022 // case MidiController::NRPN14:
1023 // max = 16383;
1024 // break;
1025 // case MidiController::Program:
1026 // max = 0xffffff;
1027 // break;
1028 // case MidiController::Pitch:
1029 // max = 8191;
1030 // min = -8192;
1031 // break;
1032 // case MidiController::Velo: // cannot happen
1033 // break;
1034 // }
1035 // MidiController* c = new MidiController(name, num, min, max, 0);
1036 // defaultMidiController.add(c);
1037 // return c;
1038 //
1039 // for(ciMidiController imc = mcl->begin(); imc != mcl->end(); ++imc)
1040 // {
1041 // mc = imc->second;
1042 // num = mc->num();
1043 // ctrlH = (num >> 8) & 0x7f;
1044 // ctrlL = num & 0xff;
1045 // if(num < CTRL_14_OFFSET) // 7-bit controller 0 - 0x10000
1046 // {
1047 // if(ctrlL == 0xff || ctrlL == a)
1048 // is_7bit = true;
1049 //
1050 // if(ctrlL == 0xff)
1051 // RPNH_reserved = RPNL_reserved = NRPNH_reserved = NRPNL_reserved = DATAH_reserved = DATAL_reserved = true;
1052 // else if(ctrlL == CTRL_HRPN)
1053 // RPNH_reserved = true;
1054 // else if(ctrlL == CTRL_LRPN)
1055 // RPNL_reserved = true;
1056 // else if(ctrlL == CTRL_HNRPN)
1057 // NRPNH_reserved = true;
1058 // else if(ctrlL == CTRL_LNRPN)
1059 // NRPNL_reserved = true;
1060 // else if(ctrlL == CTRL_HDATA)
1061 // DATAH_reserved = true;
1062 // else if(ctrlL == CTRL_LDATA)
1063 // DATAL_reserved = true;
1064 // }
1065 // else if(num < CTRL_RPN_OFFSET) // 14-bit controller 0x10000 - 0x20000
1066 // {
1067 // if(ctrlH == a)
1068 // {
1069 // //is_14bitH = true;
1070 // is_14bit = true;
1071 // if(!instr->waitForLSB())
1072 // {
1073 // MidiRecordEvent single_ev;
1074 // single_ev.setChannel(chn);
1075 // single_ev.setType(ME_CONTROLLER);
1076 // single_ev.setA(CTRL_14_OFFSET + (a << 8) + ctrlL);
1077 // single_ev.setB((b << 7) + mcs->ctrls[ctrlL]);
1078 // mdev->recordEvent(single_ev);
1079 // }
1080 // }
1081 // if(ctrlL == 0xff || ctrlL == a)
1082 // {
1083 // //is_14bitL = true;
1084 // is_14bit = true;
1085 // MidiRecordEvent single_ev;
1086 // single_ev.setChannel(chn);
1087 // single_ev.setType(ME_CONTROLLER);
1088 // single_ev.setA(CTRL_14_OFFSET + (ctrlH << 8) + a);
1089 // single_ev.setB((mcs->ctrls[ctrlH] << 7) + b);
1090 // mdev->recordEvent(single_ev);
1091 // }
1092 //
1093 // if(ctrlL == 0xff)
1094 // RPNH_reserved = RPNL_reserved = NRPNH_reserved = NRPNL_reserved = DATAH_reserved = DATAL_reserved = true;
1095 // else if(ctrlL == CTRL_HRPN || ctrlH == CTRL_HRPN)
1096 // RPNH_reserved = true;
1097 // else if(ctrlL == CTRL_LRPN || ctrlH == CTRL_LRPN)
1098 // RPNL_reserved = true;
1099 // else if(ctrlL == CTRL_HNRPN || ctrlH == CTRL_HNRPN)
1100 // NRPNH_reserved = true;
1101 // else if(ctrlL == CTRL_LNRPN || ctrlH == CTRL_LNRPN)
1102 // NRPNL_reserved = true;
1103 // else if(ctrlL == CTRL_HDATA || ctrlH == CTRL_HDATA)
1104 // DATAH_reserved = true;
1105 // else if(ctrlL == CTRL_LDATA || ctrlH == CTRL_LDATA)
1106 // DATAL_reserved = true;
1107 // }
1108 // else if(num < CTRL_NRPN_OFFSET) // RPN 7-Bit Controller 0x20000 - 0x30000
1109 // {
1110 // //if(a == CTRL_HDATA && mcs->ctrls[CTRL_HRPN] < 128 && mcs->ctrls[CTRL_LRPN] < 128)
1111 // if(a == CTRL_HDATA && !mcs->modeIsNRP && ctrlH == mcs->ctrls[CTRL_HRPN] && (ctrlL == 0xff || ctrlL == mcs->ctrls[CTRL_LRPN]))
1112 // is_RPN = true;
1113 // }
1114 // else if(num < CTRL_INTERNAL_OFFSET) // NRPN 7-Bit Controller 0x30000 - 0x40000
1115 // {
1116 // //if(a == CTRL_HDATA && mcs->ctrls[CTRL_HNRPN] < 128 && mcs->ctrls[CTRL_LNRPN] < 128)
1117 // if(a == CTRL_HDATA && mcs->modeIsNRP && ctrlH == mcs->ctrls[CTRL_HNRPN] && (ctrlL == 0xff || ctrlL == mcs->ctrls[CTRL_LNRPN]))
1118 // is_NRPN = true;
1119 // }
1120 // else if(num < CTRL_RPN14_OFFSET) // Unaccounted for internal controller 0x40000 - 0x50000
1121 // continue;
1122 // else if(num < CTRL_NRPN14_OFFSET) // RPN14 Controller 0x50000 - 0x60000
1123 // {
1124 // //if(a == CTRL_LDATA && mcs->ctrls[CTRL_HRPN] < 128 && mcs->ctrls[CTRL_LRPN] < 128)
1125 // if(a == CTRL_LDATA && !mcs->modeIsNRP && ctrlH == mcs->ctrls[CTRL_HRPN] && (ctrlL == 0xff || ctrlL == mcs->ctrls[CTRL_LRPN]))
1126 // is_RPN14 = true;
1127 // }
1128 // else if(num < CTRL_NONE_OFFSET) // NRPN14 Controller 0x60000 - 0x70000
1129 // {
1130 // //if(a == CTRL_LDATA && mcs->ctrls[CTRL_HNRPN] < 128 && mcs->ctrls[CTRL_LNRPN] < 128)
1131 // if(a == CTRL_LDATA && mcs->modeIsNRP && ctrlH == mcs->ctrls[CTRL_HNRPN] && (ctrlL == 0xff || ctrlL == mcs->ctrls[CTRL_LNRPN]))
1132 // is_NRPN14 = true;
1133 // }
1134 // }
1135 // }
1136
1137 }
1138
1139 //---------------------------------------------------------
1140 // endCycle
1141 //---------------------------------------------------------
1142
endCycle(unsigned int)1143 void MidiEncoder::endCycle(unsigned int /*blockSize*/)
1144 {
1145 // TODO
1146 }
1147
1148 } // namespace MusECore
1149