File: division.cpp

package info (click to toggle)
musescore-snapshot 3.2.s20190704%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 218,116 kB
  • sloc: cpp: 290,563; xml: 200,238; sh: 3,706; ansic: 1,447; python: 393; makefile: 222; perl: 82; pascal: 79
file content (207 lines) | stat: -rw-r--r-- 5,270 bytes parent folder | download | duplicates (12)
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
/*
    Copyright (C) 2003-2008 Fons Adriaensen <fons@kokkinizita.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; either 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


#include "division.h"

//---------------------------------------------------------
//   Division
//---------------------------------------------------------

Division::Division (Asection *asect, float fsam)
  : _asect (asect),
    _nrank (0),
    _dmask (0),
    _trem (0),
    _fsam (fsam),
    _swel (1.0f),
    _gain (0.1f),
    _w (0.0f),
    _c (1.0f),
    _s (0.0f),
    _m (0.0f)
      {
      for (int i = 0; i < NRANKS; i++)
            _ranks [i] = 0;
      }

Division::~Division()
      {
      }

//---------------------------------------------------------
//   process
//---------------------------------------------------------

void Division::process()
      {
      memset (_buff, 0, NCHANN * PERIOD * sizeof (float));
      for (int i = 0; i < _nrank; i++)
            _ranks [i]->play (1);

      float g = _swel;
      if (_trem) {
            _s += _w * _c;
            _c -= _w * _s;
            float t = sqrtf (_c * _c + _s * _s);
            _c /= t;
            _s /= t;
            if ((_trem == 2) && (fabsf (_s) < 0.05f)) {
                  _trem = 0;
                  _c = 1;
                  _s = 0;
                  }
            g *= 1.0f + _m * _s;
            }

      float t = 1.05f * _gain;
      if (g > t)
            g = t;
      t = 0.95f * _gain;
      if (g < t)
            g = t;

      float d  = (g - _gain) / PERIOD;
      g        = _gain;
      float* p = _buff;
      float* q = _asect->get_wptr ();

      for (int i = 0; i < PERIOD; i++) {
            g += d;
            q [0 * PERIOD * MIXLEN] += p [0 * PERIOD] * g;
            q [1 * PERIOD * MIXLEN] += p [1 * PERIOD] * g;
            q [2 * PERIOD * MIXLEN] += p [2 * PERIOD] * g;
            q [3 * PERIOD * MIXLEN] += p [3 * PERIOD] * g;
            p++;
            q++;
            }
      _gain = g;
      }

void Division::set_rank (int ind, Rankwave *W, int pan, int del)
{
    Rankwave *C;

    C = _ranks [ind];
    if (C) { W->_nmask = C->_cmask; delete C; }
    else W->_nmask = 0;
    W->_cmask = 0;
    _ranks [ind] = W;
    del = (int)(1e-3f * del * _fsam / PERIOD);
    if (del > 31) del = 31;
    W->set_param (_buff, del, pan);
    if (_nrank < ++ind) _nrank = ind;
}


//---------------------------------------------------------
//   update
//---------------------------------------------------------

void Division::update (int note, int mask)
      {
      for (int r = 0; r < _nrank; r++) {
            Rankwave* W = _ranks [r];
            if (W->_cmask & 0x7f) {
                  if (mask & W->_cmask)
                        W->note_on (note + 36);
                  else
                        W->note_off (note + 36);
                  }
            }
      }

void Division::update (unsigned char *keys)
      {
      for (int r = 0; r < _nrank; r++) {
            Rankwave* W = _ranks [r];

            if ((W->_cmask ^ W->_nmask) & 0x7f) {
                  int m = W->_nmask & 127;
                  if (m) {
                        int n0 = W->n0 ();
                        int n1 = W->n1 ();
                        uchar* k = keys;
                        int d = n0 - 36;
                        if (d > 0)
                              k += d;
                        for (int n = n0; n <= n1; n++) {
                              if (*k++ & m)
                                    W->note_on (n);
                              else
                                    W->note_off (n);
                              }
                        }
                  else {
                        W->all_off ();
                        }
                  }
            W->_cmask = W->_nmask;
            }
      }


void Division::set_div_mask (int bits)
{
    int       r;
    Rankwave *W;

    bits &= 127;
    _dmask |= bits;
    for (r = 0; r < _nrank; r++)
    {
        W = _ranks [r];
        if (W->_nmask & 128) W->_nmask |= bits;
    }
}


void Division::clr_div_mask (int bits)
{
    int       r;
    Rankwave *W;

    bits &= 127;
    _dmask &= ~bits;
    for (r = 0; r < _nrank; r++)
    {
        W = _ranks [r];
        if (W->_nmask & 128) W->_nmask &= ~bits;
    }
}


void Division::set_rank_mask (int ind, int bits)
      {
      Rankwave *W = _ranks [ind];

      if (bits == 128)
            bits |= _dmask;
      W->_nmask |= bits;
      }


void Division::clr_rank_mask (int ind, int bits)
      {
      Rankwave *W = _ranks [ind];

      if (bits == 128)
            bits |= _dmask;
      W->_nmask &= ~bits;
      }