summaryrefslogtreecommitdiff
path: root/muse2/muse/ctrl.cpp
blob: f32d19789fad0ded041ec0b84938a692563a7a1f (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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: ctrl.cpp,v 1.1.2.4 2009/06/10 00:34:59 terminator356 Exp $
//
//    controller handling for mixer automation
//
//  (C) Copyright 2003 Werner Schweer (ws@seh.de)
//  (C) Copyright 2011-2013 Tim E. Real (terminator356 on users dot sourceforge dot net)
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; version 2 of
//  the License, or (at your option) any later version.
//
//  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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//=========================================================

// Turn on debugging messages
//#define _CTRL_DEBUG_

#include <QLocale>
#include <QColor>
#include <map>

#include <math.h>

#include "gconfig.h"
#include "fastlog.h"
#include "globals.h"
#include "ctrl.h"
#include "midictrl.h"
#include "xml.h"

namespace MusECore {

void CtrlList::initColor(int i)
{
  QColor collist[] = { Qt::red, Qt::yellow, Qt::blue , Qt::black, Qt::white, Qt::green };

  if (i < 6)
    _displayColor = collist[i%6];
  else
    _displayColor = Qt::green;
  _visible = false;
}

//---------------------------------------------------------
//   midi2AudioCtrlValue
//   Apply mapper if it is non-null
//---------------------------------------------------------

double midi2AudioCtrlValue(const CtrlList* audio_ctrl_list, const MidiAudioCtrlStruct* /*mapper*/, int midi_ctlnum, int midi_val)
{
  double fmin, fmax;
  audio_ctrl_list->range(&fmin, &fmax);
  double frng = fmax - fmin;             // The audio control range.
  
  MidiController::ControllerType t = midiControllerType(midi_ctlnum);
  CtrlValueType aud_t = audio_ctrl_list->valueType();
  
  #ifdef _CTRL_DEBUG_
  printf("midi2AudioCtrlValue: midi_ctlnum:%d val:%d fmin:%f fmax:%f\n", midi_ctlnum, midi_val, fmin, fmax);  
  #endif  
  
  int ctlmn = 0;
  int ctlmx = 127;
  
  int bval = midi_val;
  switch(t) 
  {
    case MidiController::RPN:
    case MidiController::NRPN:
    case MidiController::Controller7:
      ctlmn = 0;
      ctlmx = 127;
    break;
    case MidiController::Controller14:
    case MidiController::RPN14:
    case MidiController::NRPN14:
      ctlmn = 0;
      ctlmx = 16383;
    break;
    case MidiController::Program:
      ctlmn = 0;
      ctlmx = 0xffffff;
    break;
    case MidiController::Pitch:
      ctlmn = -8192;
      ctlmx = 8191;
      bval += 8192;
    break;
    case MidiController::Velo:        // cannot happen
    default:
      break;
  }

  double fictlrng = double(ctlmx - ctlmn);   // Float version of the integer midi range.
  double normval = double(bval) / fictlrng;  // Float version of the normalized midi value.

  // ----------  TODO: Do stuff with the mapper, if supplied.
  
  if(aud_t == VAL_LOG)
  {
    // FIXME: Although this should be correct, some sliders show "---" at top end, some don't. 
    // Possibly because of use of fast_log10 in value(), and in sliders and automation IIRC.
    fmin = 20.0*log10(fmin);
    fmax = 20.0*log10(fmax);
    frng = fmax - fmin;
    double ret = exp10((normval * frng + fmin) / 20.0);
    #ifdef _CTRL_DEBUG_
    printf("midi2AudioCtrlValue: is VAL_LOG normval:%f frng:%f returning:%f\n", normval, frng, ret);          
    #endif
    return ret;
  }

  if(aud_t == VAL_LINEAR)
  {
    double ret = normval * frng + fmin;
    #ifdef _CTRL_DEBUG_
    printf("midi2AudioCtrlValue: is VAL_LINEAR normval:%f frng:%f returning:%f\n", normval, frng, ret);       
    #endif
    return ret;
  }

  if(aud_t == VAL_INT)
  {
    double ret = int(normval * frng + fmin);
    #ifdef _CTRL_DEBUG_
    printf("midi2AudioCtrlValue: is VAL_INT returning:%f\n", ret);   
    #endif
    return ret;  
  }
  
  if(aud_t == VAL_BOOL) 
  {
    #ifdef _CTRL_DEBUG_
    printf("midi2AudioCtrlValue: is VAL_BOOL\n");  
    #endif
    //if(midi_val > ((ctlmx - ctlmn)/2 + ctlmn))
    if((normval * frng + fmin) > (frng/2.0 + fmin))
      return fmax;
    else
      return fmin;
  }
  
  printf("midi2AudioCtrlValue: unknown audio controller type:%d\n", aud_t);
  return 0.0;
}      

//---------------------------------------------------------
// Midi to audio controller stuff
//---------------------------------------------------------

MidiAudioCtrlStruct::MidiAudioCtrlStruct()  
{ 
  _audio_ctrl_id = 0;
};

MidiAudioCtrlStruct::MidiAudioCtrlStruct(int audio_ctrl_id) : _audio_ctrl_id(audio_ctrl_id) 
{ 
};

MidiAudioCtrlMap_idx_t MidiAudioCtrlMap::index_hash(int midi_port, int midi_chan, int midi_ctrl_num) const
{ 
  return ((MidiAudioCtrlMap_idx_t(midi_port) & 0xff) << 24) | 
          ((MidiAudioCtrlMap_idx_t(midi_chan) & 0xf) << 20) | 
          (MidiAudioCtrlMap_idx_t(midi_ctrl_num) & 0xfffff);  
}

void MidiAudioCtrlMap::hash_values(MidiAudioCtrlMap_idx_t hash, int* midi_port, int* midi_chan, int* midi_ctrl_num) const
{
  if(midi_ctrl_num)
    *midi_ctrl_num = hash & 0xfffff;
  if(midi_chan)
    *midi_chan     = (hash >> 20) & 0xf;
  if(midi_port)
    *midi_port     = (hash >> 24) & 0xff;
}

iMidiAudioCtrlMap MidiAudioCtrlMap::add_ctrl_struct(int midi_port, int midi_chan, int midi_ctrl_num,  
                                                    const MidiAudioCtrlStruct& macs)
{
  MidiAudioCtrlMap_idx_t h = index_hash(midi_port, midi_chan, midi_ctrl_num);
  std::pair<iMidiAudioCtrlMap, iMidiAudioCtrlMap> range = equal_range(h);
  for(iMidiAudioCtrlMap imacp = range.first; imacp != range.second; ++imacp)
    if(imacp->second.audioCtrlId() == macs.audioCtrlId())
       return imacp;
  return insert(std::pair<MidiAudioCtrlMap_idx_t, MidiAudioCtrlStruct >(h, macs));
}

void MidiAudioCtrlMap::erase_ctrl_struct(int midi_port, int midi_chan, int midi_ctrl_num, int audio_ctrl_id)
{
  MidiAudioCtrlMap_idx_t h = index_hash(midi_port, midi_chan, midi_ctrl_num);
  std::pair<iMidiAudioCtrlMap, iMidiAudioCtrlMap> range = equal_range(h);
  MidiAudioCtrlMap macm;
  macm.insert(range.first, range.second);
  for(iMidiAudioCtrlMap imacm = macm.begin(); imacm != macm.end(); ++imacm)
    if(imacm->second.audioCtrlId() == audio_ctrl_id)
       erase(imacm);
}

void MidiAudioCtrlMap::find_audio_ctrl_structs(int audio_ctrl_id, AudioMidiCtrlStructMap* amcs) //const
{
  for(iMidiAudioCtrlMap imacm = begin(); imacm != end(); ++imacm)
    if(imacm->second.audioCtrlId() == audio_ctrl_id)
      amcs->push_back(imacm);
}

void MidiAudioCtrlMap::write(int level, Xml& xml) const
{
  for(ciMidiAudioCtrlMap imacm = begin(); imacm != end();  ++imacm)
  {
      int port, chan, mctrl;
      hash_values(imacm->first, &port, &chan, &mctrl);
      int actrl = imacm->second.audioCtrlId();
      QString s= QString("midiMapper port=\"%1\" ch=\"%2\" mctrl=\"%3\" actrl=\"%4\"")
                          .arg(port)
                          .arg(chan)
                          .arg(mctrl)
                          .arg(actrl);
      xml.tag(level++, s.toAscii().constData());
      
      // TODO
      //const MidiAudioCtrlStruct& macs = imacs->second;
      //xml.intTag(level, "macs ???", macs.);
      
      xml.etag(level--, "midiMapper");
  }
}

//---------------------------------------------------------
//   read
//---------------------------------------------------------

void MidiAudioCtrlMap::read(Xml& xml)
      {
      int port = -1, chan = -1, midi_ctrl = -1;
      MidiAudioCtrlStruct macs(-1);
      
      QLocale loc = QLocale::c();
      bool ok;
      int errcount = 0;
      for (;;) {
            Xml::Token token = xml.parse();
            const QString& tag = xml.s1();
            switch (token) {
                  case Xml::Error:
                  case Xml::End:
                        return;
                  case Xml::Attribut:
                        if (tag == "port")
                        {
                              port = loc.toInt(xml.s2(), &ok);
                              if(!ok)
                              { 
                                ++errcount;
                                printf("MidiAudioCtrlPortMap::read failed reading port string: %s\n", xml.s2().toLatin1().constData());
                              }
                        }
                        else if (tag == "ch")
                        {
                              chan = loc.toInt(xml.s2(), &ok);
                              if(!ok)
                              {
                                ++errcount;
                                printf("MidiAudioCtrlPortMap::read failed reading ch string: %s\n", xml.s2().toLatin1().constData());
                              }
                        }        
                        else if (tag == "mctrl")
                        {
                              midi_ctrl = loc.toInt(xml.s2(), &ok);
                              if(!ok)
                              {
                                ++errcount;
                                printf("MidiAudioCtrlPortMap::read failed reading mctrl string: %s\n", xml.s2().toLatin1().constData());
                              } 
                        }
                        else if (tag == "actrl")
                        {
                              macs.setAudioCtrlId(loc.toInt(xml.s2(), &ok));
                              if(!ok)
                              {
                                ++errcount;
                                printf("MidiAudioCtrlPortMap::read failed reading actrl string: %s\n", xml.s2().toLatin1().constData());
                              }
                        }
                        else
                              printf("unknown tag %s\n", tag.toLatin1().constData());
                        break;
                  case Xml::TagStart:
                        // TODO
                        //if (tag == "???") {  
                        //      }
                        //else
                              xml.unknown("midiMapper");
                        break;
                  case Xml::TagEnd:
                        if (xml.s1() == "midiMapper")
                        {
                              if(errcount == 0 && port != -1 && chan != -1 && midi_ctrl != -1 && macs.audioCtrlId() != -1)
                                  add_ctrl_struct(port, chan, midi_ctrl, macs);
                              return;
                        }
                  default:
                        break;
                  }
            }
      }
//---------------------------------------------------------
//   CtrlList
//---------------------------------------------------------

CtrlList::CtrlList(bool dontShow)
      {
      _id      = 0;
      _default = 0.0;
      _curVal  = 0.0;
      _mode    = INTERPOLATE;
      _dontShow = dontShow;
      _visible = false;
      _guiUpdatePending = false;
      initColor(0);
      }

CtrlList::CtrlList(int id, bool dontShow)
      {
      _id      = id;
      _default = 0.0;
      _curVal  = 0.0;
      _mode    = INTERPOLATE;
      _dontShow = dontShow;
      _visible = false;
      _guiUpdatePending = false;
      initColor(id);
      }

CtrlList::CtrlList(int id, QString name, double min, double max, CtrlValueType v, bool dontShow)
{
      _id      = id;
      _default = 0.0;
      _curVal  = 0.0;
      _mode    = INTERPOLATE;
      _name    = name;
      _min     = min;
      _max     = max;
      _valueType = v;
      _dontShow = dontShow;
      _visible = false;
      _guiUpdatePending = false;
      initColor(id);
}

CtrlList::CtrlList(const CtrlList& l, int flags)
{
  _id        = l._id;
  _valueType = l._valueType;
  assign(l, flags | ASSIGN_PROPERTIES);
}

//---------------------------------------------------------
//   assign
//---------------------------------------------------------

void CtrlList::assign(const CtrlList& l, int flags)
{
  if(flags & ASSIGN_PROPERTIES)
  {
    _default       = l._default;
    _curVal        = l._curVal;
    _mode          = l._mode;
    _name          = l._name;
    _min           = l._min;
    _max           = l._max;
    _dontShow      = l._dontShow;
    _displayColor  = l._displayColor;
    _visible       = l._visible;
  }
  
  if(flags & ASSIGN_VALUES)
  {
    std::map<int, CtrlVal, std::less<int> >::operator=(l); // Let map copy the items.
    _guiUpdatePending = true;
  }
}

//---------------------------------------------------------
//   getInterpolation
//   Fills CtrlInterpolate struct for given frame.
//   cur_val_only means read the current 'manual' value, not from the list even if it is not empty.
//   CtrlInterpolate member eFrame can be -1 meaning no next value (wide-open, endless).
//---------------------------------------------------------

void CtrlList::getInterpolation(int frame, bool cur_val_only, CtrlInterpolate* interp)
{
  interp->eStop = false; // During processing, control FIFO ring buffers will set this true.

  if(cur_val_only || empty())
  {
    interp->sFrame = 0;
    interp->eFrame = -1;
    interp->sVal = _curVal;
    interp->eVal = _curVal;
    interp->doInterp = false;
    return;
  }
  ciCtrl i = upper_bound(frame); // get the index after current frame
  if (i == end())   // if we are past all items just return the last value
  { 
        --i;
        interp->sFrame = 0;
        interp->eFrame = -1;
        interp->sVal = i->second.val;
        interp->eVal = i->second.val;
        interp->doInterp = false;
        return;
  }
  else if(_mode == DISCRETE)
  {
    if(i == begin())
    {
      interp->sFrame = 0;
      interp->eFrame = i->second.frame;
      interp->sVal = i->second.val;
      interp->eVal = i->second.val;
      interp->doInterp = false;
    }
    else
    {
      interp->eFrame = i->second.frame;
      interp->eVal = i->second.val;
      --i;
      interp->sFrame = i->second.frame;
      interp->sVal = i->second.val;
      interp->doInterp = false;
    }
  }
  else   // INTERPOLATE
  {                  
    if(i == begin())
    {
      interp->sFrame = 0;
      interp->eFrame = i->second.frame;
      interp->sVal = i->second.val;
      interp->eVal = i->second.val;
      interp->doInterp = false;
    }
    else
    {
      interp->eFrame = i->second.frame;
      interp->eVal = i->second.val;
      --i;
      interp->sFrame = i->second.frame;
      interp->sVal = i->second.val;
      interp->doInterp = (interp->eVal != interp->sVal && interp->eFrame > interp->sFrame);
    }
  }
}

//---------------------------------------------------------
//   interpolate
//   Returns interpolated value at given frame, from a CtrlInterpolate struct.
//   For speed, no checking is done for frame = frame2, val1 = val2 or even CtrlInterpolate::doInterp.
//   Those are to be taken care of before calling this routine. See getInterpolation().
//---------------------------------------------------------

double CtrlList::interpolate(int frame, const CtrlInterpolate& interp)
{
  int frame1 = interp.sFrame;
  int frame2 = interp.eFrame;
  double val1 = interp.sVal;
  double val2 = interp.eVal;
  if(frame >= frame2)        // frame2 can also be -1
  {
    if(_valueType == VAL_LOG)
    {
      const double min = exp10(MusEGlobal::config.minSlider / 20.0);  // TODO Try fastexp10
      if(val2 < min)
        val2 = min;
    }
    return val2;
  }
  if(frame <= frame1)
  {
    if(_valueType == VAL_LOG)
    {
      const double min = exp10(MusEGlobal::config.minSlider / 20.0);  // TODO Try fastexp10
      if(val1 < min)
        val1 = min;
    }
    return val1;
  }

  if(_valueType == VAL_LOG)
  {
    val1 = 20.0*fast_log10(val1);
    if (val1 < MusEGlobal::config.minSlider)
      val1=MusEGlobal::config.minSlider;
    val2 = 20.0*fast_log10(val2);
    if (val2 < MusEGlobal::config.minSlider)
      val2=MusEGlobal::config.minSlider;
  }
  val2 -= val1;
  val1 += (double(frame - frame1) * val2) / double(frame2 - frame1);
  if (_valueType == VAL_LOG)
    val1 = exp10(val1/20.0);
  return val1;
}

//---------------------------------------------------------
//   value
//   Returns value at frame.
//   cur_val_only means read the current 'manual' value, not from the list even if it is not empty.
//   If passed a nextFrame, sets nextFrame to the next event frame, or -1 if no next frame (wide-open), or, 
//    since CtrlList is a map, ZERO if should be replaced with some other frame by the caller (interpolation). 
//---------------------------------------------------------

double CtrlList::value(int frame, bool cur_val_only, int* nextFrame) const
{
      if(cur_val_only || empty()) 
      {
        if(nextFrame)
          *nextFrame = -1;
        return _curVal;
      }

      double rv;
      int nframe;

      ciCtrl i = upper_bound(frame); // get the index after current frame
      if (i == end()) { // if we are past all items just return the last value
            --i;
            if(nextFrame)
              *nextFrame = -1;
            return i->second.val;
            }
      else if(_mode == DISCRETE)
      {
        if(i == begin())
        {
            nframe = i->second.frame;
            rv = i->second.val;
        }  
        else
        {  
          nframe = i->second.frame;
          --i;
          rv = i->second.val;
        }  
      }
      else {                  // INTERPOLATE
        if (i == begin()) {
            nframe = i->second.frame;
            rv = i->second.val;
        }
        else {
            int frame2 = i->second.frame;
            double val2 = i->second.val;
            --i;
            int frame1 = i->second.frame;
            double val1   = i->second.val;

            
            if(val2 != val1)
              nframe = 0; // Zero signifies the next frame should be determined by caller.
            else
              nframe = frame2;
            
            if (_valueType == VAL_LOG) {
              val1 = 20.0*fast_log10(val1);
              if (val1 < MusEGlobal::config.minSlider)
                val1=MusEGlobal::config.minSlider;
              val2 = 20.0*fast_log10(val2);
              if (val2 < MusEGlobal::config.minSlider)
                val2=MusEGlobal::config.minSlider;
            }

            val2  -= val1;
            val1 += (double(frame - frame1) * val2)/double(frame2 - frame1);
    
            if (_valueType == VAL_LOG) {
              val1 = exp10(val1/20.0);
            }

            rv = val1;
          }
      }

      if(nextFrame)
          *nextFrame = nframe;
      
      return rv;
}

//---------------------------------------------------------
//   curVal
//   returns the static 'manual' value
//---------------------------------------------------------
double CtrlList::curVal() const
{ 
  return _curVal;
}

//---------------------------------------------------------
//   setCurVal
//   Sets the static 'manual' value
//---------------------------------------------------------
void CtrlList::setCurVal(double val)
{
#ifdef _CTRL_DEBUG_
  printf("CtrlList::setCurVal val:%f\n", val);  
#endif
  
  bool upd = (val != _curVal);
  _curVal = val;
  // If empty, any controller graphs etc. will be displaying this value.
  // Otherwise they'll be displaying the list, so update is not required.
  if(empty() && upd)     
    _guiUpdatePending = true;
}

//---------------------------------------------------------
//
//     Catch all insert, erase, clear etc.
//
//---------------------------------------------------------

CtrlList& CtrlList::operator=(const CtrlList& cl)
{
#ifdef _CTRL_DEBUG_
  printf("CtrlList::operator= id:%d\n", cl.id());  
#endif
  _id            = cl._id;
  _default       = cl._default;
  _curVal        = cl._curVal;
  _mode          = cl._mode;
  _name          = cl._name;
  _min           = cl._min;
  _max           = cl._max;
  _valueType     = cl._valueType;
  _dontShow      = cl._dontShow;
  _displayColor  = cl._displayColor;
  _visible       = cl._visible;
  
  // Let map copy the items.
  std::map<int, CtrlVal, std::less<int> >::operator=(cl);
  _guiUpdatePending = true;
  return *this;
}

void CtrlList::swap(CtrlList& cl)
{
#ifdef _CTRL_DEBUG_
  printf("CtrlList::swap id:%d\n", cl.id());  
#endif
  std::map<int, CtrlVal, std::less<int> >::swap(cl);
  cl.setGuiUpdatePending(true);
  _guiUpdatePending = true;
}

std::pair<iCtrl, bool> CtrlList::insert(const std::pair<int, CtrlVal>& p)
{
#ifdef _CTRL_DEBUG_
  printf("CtrlList::insert frame:%d val:%f\n", p.first, p.second.val);  
#endif
  std::pair<iCtrl, bool> res = std::map<int, CtrlVal, std::less<int> >::insert(p);
  _guiUpdatePending = true;
  return res;
}

iCtrl CtrlList::insert(iCtrl ic, const std::pair<int, CtrlVal>& p)
{
#ifdef _CTRL_DEBUG_
  printf("CtrlList::insert2 frame:%d val:%f\n", p.first, p.second.val); 
#endif
  iCtrl res = std::map<int, CtrlVal, std::less<int> >::insert(ic, p);
  _guiUpdatePending = true;
  return res;
}

void CtrlList::erase(iCtrl ictl)
{
#ifdef _CTRL_DEBUG_
  printf("CtrlList::erase iCtrl frame:%d val:%f\n", ictl->second.frame, ictl->second.val);  
#endif
  std::map<int, CtrlVal, std::less<int> >::erase(ictl);
  _guiUpdatePending = true;
}

std::map<int, CtrlVal, std::less<int> >::size_type CtrlList::erase(int frame)
{
#ifdef _CTRL_DEBUG_
  printf("CtrlList::erase frame:%d\n", frame);  
#endif
  size_type res = std::map<int, CtrlVal, std::less<int> >::erase(frame);
  _guiUpdatePending = true;
  return res;
}

void CtrlList::erase(iCtrl first, iCtrl last)
{
#ifdef _CTRL_DEBUG_
  printf("CtrlList::erase range first frame:%d val:%f second frame:%d val:%f\n", 
         first->second.frame, first->second.val,
         last->second.frame, last->second.val);  
#endif
  std::map<int, CtrlVal, std::less<int> >::erase(first, last);
  _guiUpdatePending = true;
}

void CtrlList::clear()
{
#ifdef _CTRL_DEBUG_
  printf("CtrlList::clear\n");  
#endif
  std::map<int, CtrlVal, std::less<int> >::clear();
  _guiUpdatePending = true;
}

//---------------------------------------------------------
//   add
//   Add, or replace, an event at time frame having value val. 
//---------------------------------------------------------

void CtrlList::add(int frame, double val)
      {
      iCtrl e = find(frame);
      if (e != end())
      {
            bool upd = (val != e->second.val);
            e->second.val = val;
#ifdef _CTRL_DEBUG_
            printf("CtrlList::add frame:%d val:%f\n", frame, val);  
#endif
            if(upd)
              _guiUpdatePending = true;
      }
      else
            insert(std::pair<const int, CtrlVal> (frame, CtrlVal(frame, val)));
      }

//---------------------------------------------------------
//   del
//---------------------------------------------------------

void CtrlList::del(int frame)
      {
      iCtrl e = find(frame);
      if (e == end())
            return;
      
      erase(e);
      }

//---------------------------------------------------------
//   updateCurValues
//   Set the current static 'manual' value (non-automation value) 
//    from the automation value at the given time.
//---------------------------------------------------------

void CtrlList::updateCurValue(int frame)
{
  double v = value(frame);
  bool upd = (v != _curVal);
  _curVal = v;
  // If empty, any controller graphs etc. will be displaying this value.
  // Otherwise they'll be displaying the list, so update is not required.
  if(empty() && upd)     
    _guiUpdatePending = true;
}
      
//---------------------------------------------------------
//   read
//---------------------------------------------------------

void CtrlList::read(Xml& xml)
      {
      QLocale loc = QLocale::c();
      bool ok;
      for (;;) {
            Xml::Token token = xml.parse();
            const QString& tag = xml.s1();
            switch (token) {
                  case Xml::Error:
                  case Xml::End:
                        return;
                  case Xml::Attribut:
                        if (tag == "id")
                        {
                              _id = loc.toInt(xml.s2(), &ok);
                              if(!ok)
                                printf("CtrlList::read failed reading _id string: %s\n", xml.s2().toLatin1().constData());
                        }
                        else if (tag == "cur")
                        {
                              _curVal = loc.toDouble(xml.s2(), &ok);
                              if(!ok)
                                printf("CtrlList::read failed reading _curVal string: %s\n", xml.s2().toLatin1().constData());
                        }        
                        else if (tag == "visible")
                        {
                              _visible = loc.toInt(xml.s2(), &ok);
                              if(!ok)
                                printf("CtrlList::read failed reading _visible string: %s\n", xml.s2().toLatin1().constData());
                        }
                        else if (tag == "color")
                        {
#if QT_VERSION >= 0x040700
                              ok = _displayColor.isValidColor(xml.s2());
                              if (!ok) {
                                printf("CtrlList::read failed reading color string: %s\n", xml.s2().toLatin1().constData());
                                break;
                              }
#endif
                              _displayColor.setNamedColor(xml.s2());
                        }
                        else
                              printf("unknown tag %s\n", tag.toLatin1().constData());
                        break;
                  case Xml::Text:
                        {
                          int len = tag.length();
                          int frame;
                          double val;
  
                          int i = 0;
                          for(;;) 
                          {
                                while(i < len && (tag[i] == ',' || tag[i] == ' ' || tag[i] == '\n'))
                                  ++i;
                                if(i == len)
                                      break;
                                
                                QString fs;
                                while(i < len && tag[i] != ' ')
                                {
                                  fs.append(tag[i]); 
                                  ++i;
                                }
                                if(i == len)
                                      break;
                                
                                frame = loc.toInt(fs, &ok);
                                if(!ok)
                                {
                                  printf("CtrlList::read failed reading frame string: %s\n", fs.toLatin1().constData());
                                  break;
                                }
                                  
                                while(i < len && (tag[i] == ' ' || tag[i] == '\n'))
                                  ++i;
                                if(i == len)
                                      break;
                                
                                QString vs;
                                while(i < len && tag[i] != ' ' && tag[i] != ',')
                                {
                                  vs.append(tag[i]); 
                                  ++i;
                                }
                                
                                val = loc.toDouble(vs, &ok);
                                if(!ok)
                                {
                                  printf("CtrlList::read failed reading value string: %s\n", vs.toLatin1().constData());
                                  break;
                                }
                                  
                                add(frame, val);
                                
                                if(i == len)
                                      break;
                          }
                        }
                        break;
                  case Xml::TagEnd:
                        if (xml.s1() == "controller")
                              return;
                  default:
                        break;
                  }
            }
      }

//---------------------------------------------------------
//   add
//---------------------------------------------------------

void CtrlListList::add(CtrlList* vl)
      {
      insert(std::pair<const int, CtrlList*>(vl->id(), vl));
      }

//---------------------------------------------------------
//   value
//   Returns value at frame for controller with id ctrlId.
//   cur_val_only means read the current 'manual' value, not from the list even if it is not empty.
//   If passed a nextFrame, sets nextFrame to the next event frame, or -1 if no next frame (wide-open), or, 
//    since CtrlList is a map, ZERO if should be replaced with some other frame by the caller (interpolation). 
//---------------------------------------------------------

double CtrlListList::value(int ctrlId, int frame, bool cur_val_only, int* nextFrame) const     
      {
      ciCtrlList cl = find(ctrlId);
      if (cl == end())
      {
        if(nextFrame)
          *nextFrame = -1;
        return 0.0;
      }
      
      return cl->second->value(frame, cur_val_only, nextFrame);  
      }

//---------------------------------------------------------
//   updateCurValues
//   Set the current 'manual' values (non-automation values) 
//    from the automation values at the given time.
//   This is typically called right after a track's automation type changes
//    to OFF, so that the manual value becomes the last automation value.
//   There are some interesting advantages to having completely independent 
//    'manual' and automation values, but the jumping around when switching to OFF
//    becomes disconcerting.
//---------------------------------------------------------

void CtrlListList::updateCurValues(int frame)
{
  for(ciCtrlList cl = begin(); cl != end(); ++cl)
    cl->second->updateCurValue(frame);
}

//---------------------------------------------------------
//   value
//---------------------------------------------------------

void CtrlListList::write(int level, Xml& xml) const
{
  for (ciCtrlList icl = begin(); icl != end(); ++icl) {
        const CtrlList* cl = icl->second;

        QString s= QString("controller id=\"%1\" cur=\"%2\"").arg(cl->id()).arg(cl->curVal()).toAscii().constData();
        s += QString(" color=\"%1\" visible=\"%2\"").arg(cl->color().name()).arg(cl->isVisible());
        xml.tag(level++, s.toAscii().constData());
        int i = 0;
        for (ciCtrl ic = cl->begin(); ic != cl->end(); ++ic) {
              QString s("%1 %2, ");
              xml.nput(level, s.arg(ic->second.frame).arg(ic->second.val).toAscii().constData());
              ++i;
              if (i >= 4) {
                    xml.put(level, "");
                    i = 0;
                    }
              }
        if (i)
              xml.put(level, "");
        xml.etag(level--, "controller");
        }
  
  _midi_controls.write(level, xml);
}


} // namespace MusECore