summaryrefslogtreecommitdiff
path: root/muse2/muse/ctrl.cpp
blob: 2793c4342ecbf3f6525dd0d465b619a7c95da0f2 (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
//=========================================================
//  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 Time E. Real (terminator356 on users dot sourceforge dot net)
//=========================================================


#include <QLocale>
#include <QColor>
//#include <stdlib.h>

#include "gconfig.h"
#include "fastlog.h"
#include "math.h"
#include "globals.h"
#include "ctrl.h"
#include "xml.h"
#include "audio.h"

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;
}



//---------------------------------------------------------
//   CtrlList
//---------------------------------------------------------

CtrlList::CtrlList(int id)
      {
      _id      = id;
      _default = 0.0;
      _curVal  = 0.0;
      _mode    = INTERPOLATE;
      _dontShow = false;
      initColor(id);
      }
//---------------------------------------------------------
//   CtrlList
//---------------------------------------------------------
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;
      initColor(id);
}
//---------------------------------------------------------
//   CtrlList
//---------------------------------------------------------

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

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

double CtrlList::value(int frame) const
{
      // Changed by Tim. p4.0.32...
      
      ///if (!automation || empty()) 
      ///      return _curVal;
      if(empty()) 
        return _curVal;

      double rv;
      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
            ///ciCtrl i = end();
            --i;
            ///const CtrlVal& val = i->second;
            ///_curVal = val.val;
            rv = i->second.val;
            }
      else if(_mode == DISCRETE)
      {
        if(i == begin())
        {
          ///_curVal = _default;
          //if(i->second.frame == frame)
            rv = i->second.val;
          //else  
          //  rv = _default;
        }  
        else
        {  
          --i;
          ///const CtrlVal& val = i->second;
          ///_curVal = val.val;
          rv = i->second.val;
        }  
      }
      else {
        ///int frame2 = i->second.frame;
        ///double val2 = i->second.val;
        ///int frame1;
        ///double val1;
        if (i == begin()) {
            ///frame1 = 0;
            ///val1   = _default;
            rv = i->second.val;
        }
        else {
            int frame2 = i->second.frame;
            double val2 = i->second.val;
            --i;
            ///frame1 = i->second.frame;
            ///val1   = i->second.val;
            int frame1 = i->second.frame;
            double val1   = i->second.val;
        ///}
            //printf("before val1=%f val2=%f\n", val1,val2);
            if (_valueType == VAL_LOG) {
              val1 = 20.0*fast_log10(val1);
              if (val1 < config.minSlider)
                val1=config.minSlider;
              val2 = 20.0*fast_log10(val2);
              if (val2 < config.minSlider)
                val2=config.minSlider;
            }
            //printf("after val1=%f val2=%f\n", val1,val2);
            frame -= frame1;
            val2  -= val1;
            frame2 -= frame1;
            val1 += (double(frame) * val2)/double(frame2);
    
            if (_valueType == VAL_LOG) {
              val1 = exp10(val1/20.0);
            }
            //printf("after val1=%f\n", val1);
            ///_curVal = val1;
            rv = val1;
          }
      }
// printf("autoVal %d %f\n", frame, _curVal);
      ///return _curVal;
      return rv;
}

//---------------------------------------------------------
//   curVal
//   returns the value at the current audio position 
//---------------------------------------------------------
double CtrlList::curVal() const
{ 
  //double v = value(Pos(audio->tickPos()).frame());      // p4.0.33
  //double v = value(audio->pos().frame());                 // Try this.
  //return v;
  return _curVal;
}

//---------------------------------------------------------
//   setCurVal
//---------------------------------------------------------
void CtrlList::setCurVal(double val)
{
  _curVal = val;
  //if (size() < 2)   // Removed p4.0.32
  //  add(0,val);
}

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

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

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

void CtrlList::del(int frame)
      {
      iCtrl e = find(frame);
      if (e == end()) {
            //printf("CtrlList::del(%d): not found\n", frame);
            return;
            }
      erase(e);
      }

//---------------------------------------------------------
//   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 = xml.s2().toInt();
                              _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 = xml.s2().toDouble();
                              _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:
                        {
                        // Changed by Tim. Users in some locales reported corrupt reading,
                        //  because of the way floating point is represented (2,3456 not 2.3456).
                        /*
                        QByteArray ba = tag.toLatin1();
                        const char* s = ba;.constData();
                        int frame;
                        double val;

                        for (;;) {
                              char* endp;
                              while (*s == ' ' || *s == '\n')
                                    ++s;
                              if (*s == 0)
                                    break;
                              frame = strtol(s, &endp, 10);
                              s     = endp;
                              while (*s == ' ' || *s == '\n')
                                    ++s;
                              val = strtod(s, &endp);
                              add(frame, val);
                              s = endp;
                              ++s;
                              }
                           */   
                          
                          //printf("CtrlList::read tag:%s\n", tag.toLatin1().constData());
                          
                          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;
                                
                                // Works OK, but only because if current locale fails it falls back on 'C' locale.
                                // So, let's skip the fallback and force use of 'C' locale.
                                //frame = fs.toInt(&ok);
                                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;
                                }
                                
                                // Works OK, but only because if current locale fails it falls back on 'C' locale.
                                // So, let's skip the fallback and force use of 'C' locale.
                                //val = vs.toDouble(&ok); 
                                val = loc.toDouble(vs, &ok);
                                if(!ok)
                                {
                                  printf("CtrlList::read failed reading value string: %s\n", vs.toLatin1().constData());
                                  break;
                                }
                                  
                                //printf("CtrlList::read i:%d len:%d fs:%s frame %d: vs:%s val %f \n", i, len, fs.toLatin1().constData(), frame, vs.toLatin1().constData(), val);
                                
                                add(frame, val);
                                
                                if(i == len)
                                      break;
                          }
                        }
                        break;
                  case Xml::TagEnd:
                        if (xml.s1() == "controller")
                        {
                              //printf("CtrlList::read _id:%d _curVal:%f\n", _id, _curVal);
                              
                              return;
                        }      
                  default:
                        break;
                  }
            }
      }

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

void CtrlListList::add(CtrlList* vl)
      {
//      printf("CtrlListList(%p)::add(id=%d) size %d\n", this, vl->id(), size());
      insert(std::pair<const int, CtrlList*>(vl->id(), vl));
      }