File: channel.cpp

package info (click to toggle)
musescore2 2.3.2%2Bdfsg4-15~bpo10%2B1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 168,212 kB
  • sloc: cpp: 262,317; xml: 176,707; sh: 3,377; ansic: 1,384; python: 356; makefile: 228; perl: 82; pascal: 78
file content (125 lines) | stat: -rw-r--r-- 3,792 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
//=============================================================================
//  Zerberus
//  Zample player
//
//  Copyright (C) 2013 Werner Schweer
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2
//  as published by the Free Software Foundation and appearing in
//  the file LICENCE.GPL
//=============================================================================

#include "synthesizer/event.h"
#include "zerberus.h"
#include "channel.h"
#include "voice.h"
#include "instrument.h"
#include "zone.h"

// static const float PI_2 =  1.57079632679489661923;    /* pi/2 */

//---------------------------------------------------------
//   Channel
//---------------------------------------------------------

Channel::Channel(Zerberus* ms, int i)
      {
      _msynth     = ms;
      _idx        = i;
      _instrument = 0;
      _gain       = 1.0;
      _midiVolume = 1.0;
      _panLeftGain  = cosf(M_PI_2 * 64.0/126.0);
      _panRightGain = sinf(M_PI_2 * 64.0/126.0);
      memset(ctrl, 0, 128 * sizeof(char));
      ctrl[Ms::CTRL_EXPRESSION] = 127;
      }

//---------------------------------------------------------
//   pitchBend
//---------------------------------------------------------

void Channel::pitchBend(int)
      {
      }

//---------------------------------------------------------
//   controller
//    realtime
//---------------------------------------------------------

void Channel::controller(int c, int val)
      {
      if (!_msynth)
            return;
      ctrl[c] = val;
      if (c == Ms::CTRL_SUSTAIN) {
            if (val < 0x40) {
                  for (Voice* v = _msynth->getActiveVoices(); v; v = v->next()) {
                        if (v->isSustained()) {
                              // printf("sustain off %p\n", v);
                              v->stop();
                              }
                        }
                  }
            }
      else if (c == Ms::CTRL_PANPOT) {
            val -= 1;
            if (val < 0)
                  val = 0;
            _panLeftGain  = cosf(M_PI_2 * float(val)/126.0);
            _panRightGain = sinf(M_PI_2 * float(val)/126.0);
            }
      else if (c == Ms::CTRL_VOLUME) {
            _midiVolume = (float(val) * float(ctrl[Ms::CTRL_EXPRESSION])) / (127.0 * 127.0);
            }
      else if (c == Ms::CTRL_ALL_NOTES_OFF) {
            for (Voice* v = _msynth->getActiveVoices(); v; v = v->next()) {
                  if (!v->isOff())
                        v->stop();
                  }
            }
      else if (c == Ms::CTRL_PROGRAM) {
            printf("Zerberus: program %d\n", val);
            ZInstrument* zi = _msynth->instrument(val);
            if (zi == 0)
                  printf("   not found\n");
            if (zi && zi != _instrument) {
                  for (Voice* v = _msynth->getActiveVoices(); v; v = v->next())
                        v->off();
                  _instrument = zi;
                  resetCC();
                  }
            }

      for (Zone *z : instrument()->zones())
            z->updateCCGain(this);
//      else
//            qDebug("Zerberus: ctrl 0x%02x 0x%02x", ctrl, val);
      }

//---------------------------------------------------------
//   sustain
//---------------------------------------------------------

int Channel::sustain() const
      {
      return ctrl[Ms::CTRL_SUSTAIN];
      }

int Channel::getCtrl(int CTRL) const
      {
      return ctrl[CTRL];
      }


void Channel::resetCC()
      {
      if (!_instrument)
            return;
      for (int i = 0; i < 128; i++) {
            if (_instrument->getSetCC(i) != -1)
                  ctrl[i] = _instrument->getSetCC(i);
            }
      }