File: tuning.cpp

package info (click to toggle)
musescore 2.3.2%2Bdfsg2-7~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 187,932 kB
  • sloc: cpp: 264,610; xml: 176,707; sh: 3,311; ansic: 1,384; python: 356; makefile: 188; perl: 82; pascal: 78
file content (388 lines) | stat: -rw-r--r-- 12,299 bytes parent folder | download | duplicates (7)
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
/*
  ZynAddSubFX - a software synthesizer

  Microtonal.C - Tuning settings and microtonal capabilities
  Copyright (C) 2002-2005 Nasca Octavian Paul
  Author: Nasca Octavian Paul

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License
  as published by the Free Software Foundation.

  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 (version 2 or later) for more details.

  You should have received a copy of the GNU General Public License (version 2)
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

*/

#include <math.h>
#include "tuning.h"

#define MAX_LINE_SIZE 512

//---------------------------------------------------------
//   Tuning
//---------------------------------------------------------

Tuning::Tuning()
      {
      invertupdown       = false;
      invertupdowncenter = 60;
      octavesize         = 12;
      enabled            = false;
      aNote              = 69;
      aFreq              = 440.0;
      scaleshift         = 64;
      firstkey           = 0;
      lastkey            = 127;
      middlenote         = 60;
      mapsize            = 12;
      mappingenabled     = false;

      for (int i = 0; i < 128; i++)
            mapping[i] = i;

      for (int i = 0; i < MAX_OCTAVE_SIZE; i++) {
            octave[i].tuning = tmpoctave[i].tuning = pow(2,(i % octavesize + 1) / 12.0);
            octave[i].type   = tmpoctave[i].type   = 1;
            octave[i].x1     = tmpoctave[i].x1     = (i % octavesize + 1) * 100;
            octave[i].x2     = tmpoctave[i].x2     = 0;
            };
      octave[11].type  = 2;
      octave[11].x1    = 2;
      octave[11].x2    = 1;
      name             = "12tET";
      comment          = "Equal Temperament 12 notes per octave";
      globalfinedetune = 64;
      }

//---------------------------------------------------------
//   getoctavesize
//---------------------------------------------------------

int Tuning::getoctavesize()
      {
      return enabled ? octavesize : 12;
      }

//---------------------------------------------------------
//   getnotefreq
//    Get the frequency according the note number
//---------------------------------------------------------

float Tuning::getnotefreq(int note, int keyshift)
      {
      // in this function will appears many times things like this:
      // var = (a + b * 100) % b
      // I had written this way because if I use var=a%b gives unwanted results when a < 0
      // This is the same with divisions.

      if (invertupdown &&((!mappingenabled) || (!enabled)))
            note = invertupdowncenter * 2 - note;

      //compute global fine detune
      float globalfinedetunerap = pow(2.0,(globalfinedetune-64.0)/1200.0);//-64.0 .. 63.0 cents

      if (!enabled) {
            //12tET
            return pow(2.0,(note - aNote + keyshift) / 12.0) * aFreq * globalfinedetunerap;
            }

      int lscaleshift = (scaleshift - 64 + octavesize * 100) % octavesize;

      //compute the keyshift
      float rap_keyshift = 1.0;

      if (keyshift) {
            int kskey    =  (keyshift + octavesize * 100) % octavesize;
            int ksoct    =  (keyshift + octavesize * 100) /octavesize - 100;
            rap_keyshift =  (kskey==0) ? 1.0 : octave[kskey-1].tuning;
            rap_keyshift *= pow(octave[octavesize-1].tuning, ksoct);
            }

      //if the mapping is enabled
      float freq;
      if (mappingenabled) {
            if ((note < firstkey) || (note > lastkey))
                  return -1.0;

            //Compute how many mapped keys are from middle note to reference note
            //and find out the proportion between the freq. of middle note and "A" note

            int tmp    = aNote - middlenote;
            bool minus = false;
            if (tmp < 0) {
                  tmp = -tmp;
                  minus = true;
                  }
            int deltanote = 0;
            for (int i=0;i<tmp;i++)  {
                  if (mapping[i % mapsize] >= 0)
                        deltanote++;
                  }

            float rap_anote_middlenote = (deltanote == 0) ? 1.0 : octave[(deltanote-1) % octavesize].tuning;
            if (deltanote!=0)
                  rap_anote_middlenote*=pow(octave[octavesize-1].tuning,(deltanote-1)/octavesize);
            if (minus)
                  rap_anote_middlenote=1.0/rap_anote_middlenote;

            //Convert from note (midi) to degree (note from the tuning)
            int degoct = (note - middlenote + mapsize*200)/mapsize - 200;
            int degkey = (note - middlenote+(int)mapsize*100) % mapsize;
            degkey     = mapping[degkey];
            if (degkey < 0)
                  return -1.0;      //this key is not mapped

            //invert the keyboard upside-down if it is asked for
            //TODO: do the right way by using invertupdowncenter
            if (invertupdown) {
                  degkey=octavesize-degkey-1;
                  degoct=-degoct;
                  }
            //compute the frequency of the note
            degkey =  degkey+lscaleshift;
            degoct += degkey/octavesize;
            degkey %= octavesize;

            freq = (degkey==0) ? (1.0):octave[degkey-1].tuning;
            freq *= pow(octave[octavesize-1].tuning, degoct);
            freq *= aFreq/rap_anote_middlenote;
            }
      else {      //if the mapping is disabled
            int nt     = note - aNote + lscaleshift;
            int ntkey  = (nt + octavesize * 100) % octavesize;
            int ntoct  = (nt-ntkey) / octavesize;
            float oct  = octave[octavesize - 1].tuning;
            freq = octave[(ntkey + octavesize-1) % octavesize].tuning * pow(oct, ntoct) * aFreq;
            if (ntkey == 0)
                  freq /= oct;
            }
      if (lscaleshift)
            freq /= octave[scaleshift-1].tuning;
      freq *= globalfinedetunerap;
      return freq * rap_keyshift;
      }

//---------------------------------------------------------
//   linetotunings
//    Convert a line to tunings; returns true if it ok
//---------------------------------------------------------

bool Tuning::linetotunings(int nline, const char* line)
      {
      int  x1=-1, x2=-1, type=-1;
      float x=-1.0,tmp,tuning=1.0;

      if (strstr(line,"/") == 0) {
            if (strstr(line,".") == 0) {  // M case (M=M/1)
                  sscanf(line,"%d", &x1);
                  x2   = 1;
                  type = 2;   //division
                  }
            else {// float number case
                  sscanf(line,"%f",&x);
                  if (x < 0.000001)
                        return false;
                  type = 1;   //float type(cents)
                  }
            }
      else {      // M/N case
            sscanf(line,"%d/%d",&x1,&x2);
            if ((x1 < 0) || (x2 < 0))
                  return false;
            if (x2 == 0)
                  x2 = 1;
            type = 2;//division
            }

      if (x1 <= 0)
            x1 = 1;     //not allow zero frequency sounds (consider 0 as 1)

      //convert to float if the number are too big
      if ((type == 2) && ((x1 > (128*128*128-1)) || (x2 > (128*128*128-1)))) {
            type = 1;
            x = ((float) x1) / x2;
            }
      switch (type) {
            case 1:
                  x1     = (int) floor(x);
                  tmp    = fmod(x, 1.0);
                  x2     = (int) (floor (tmp*1e6));
                  tuning = pow(2.0,x/1200.0);
                  break;
            case 2:
                  tuning = ((float)x1)/x2;
                  break;
            }
      tmpoctave[nline].tuning = tuning;
      tmpoctave[nline].type   = type;
      tmpoctave[nline].x1     = x1;
      tmpoctave[nline].x2     = x2;
      return true;
      }

//---------------------------------------------------------
//   loadLine
//---------------------------------------------------------

QByteArray Tuning::loadLine(QFile* file)
      {
      QByteArray tmp;
      for (;;) {
            tmp = file->readLine();
            if (tmp.isEmpty() || tmp[0] != '!')
                  break;
            }
      return tmp;
      }

//---------------------------------------------------------
//   loadscl
//    return false on error
//---------------------------------------------------------

bool Tuning::loadscl(const QString& filename)
      {
      QFile file(filename);
      if (!file.open(QIODevice::ReadOnly))
            return false;

      //loads the short description
      QByteArray tmp = loadLine(&file).trimmed();
      if (tmp.isEmpty())
            return false;

      name    = tmp;
      comment = tmp;

      //loads the number of the notes
      tmp = loadLine(&file);
      if (tmp.isEmpty())
            return false;

      bool ok;
      int nnotes = tmp.toInt(&ok);
      if (!ok)
            nnotes = MAX_OCTAVE_SIZE;
      else if (nnotes > MAX_OCTAVE_SIZE)
            return false;

      //load the tunnings
      for (int nline = 0; nline < nnotes; nline++) {
            tmp = loadLine(&file);
            if (tmp.isEmpty())
                  return false;
            if (!linetotunings(nline, tmp.data()))
                  return false;
            }

      octavesize = nnotes;
      for (int i = 0; i < octavesize; i++) {
            octave[i].tuning = tmpoctave[i].tuning;
            octave[i].type   = tmpoctave[i].type;
            octave[i].x1     = tmpoctave[i].x1;
            octave[i].x2     = tmpoctave[i].x2;
            }
      return true;
      }

//---------------------------------------------------------
//   loadkbm
//    return false on error
//---------------------------------------------------------

bool Tuning::loadkbm(const QString& filename)
      {
      QFile file(filename);
      if (!file.open(QIODevice::ReadOnly))
            return false;

      int x;
      QByteArray tmp;
      bool ok;

      //loads the mapsize
      tmp = loadLine(&file);
      if (tmp.isEmpty())
            return false;
      x = tmp.toInt(&ok);
      if (!ok)
            return false;
      mapsize = qBound(1, x, 127);

      //loads first MIDI note to retune
      tmp = loadLine(&file);
      if (tmp.isEmpty())
            return false;
      x = tmp.toInt(&ok);
      if (!ok)
            return false;
      firstkey = qBound(1, x, 127);

      //loads last MIDI note to retune
      tmp = loadLine(&file);
      if (tmp.isEmpty())
            return false;
      x = tmp.toInt(&ok);
      if (!ok)
            return false;
      lastkey = qBound(1, x, 127);

      //loads last the middle note where scale fro scale degree=0
      tmp = loadLine(&file);
      if (tmp.isEmpty())
            return false;
      x = tmp.toInt(&ok);
      if (!ok)
            return false;
      middlenote = qBound(1, x, 127);

      //loads the reference note
      tmp = loadLine(&file);
      if (tmp.isEmpty())
            return false;
      x = tmp.toInt(&ok);
      if (!ok)
            return false;
      aNote = qBound(1, x, 127);

      //loads the reference freq.
      tmp = loadLine(&file);
      if (tmp.isEmpty())
            return false;
      float tmpPAfreq = tmp.toFloat(&ok);
      if (!ok)
            return false;
      aFreq = tmpPAfreq;

      //the scale degree(which is the octave) is not loaded, it is obtained
      // by the tunnings with getoctavesize() method
      tmp = loadLine(&file);
      if (tmp.isEmpty())
            return false;

      //load the mappings
      if (mapsize) {
            for (int nline = 0; nline < mapsize; nline++) {
                  tmp = loadLine(&file);
                  if (tmp.isEmpty())
                        return false;
                  x = tmp.toInt(&ok);
                  mapping[nline] = ok ? x : -1;
                  }
            mappingenabled = true;
            }
      else {
            mappingenabled = false;
            mapping[0]     = 0;
            mapsize        = 1;
            }
      return true;
      }