MuseScore  3.4
Music composition and notation
voice.h
Go to the documentation of this file.
1 //=============================================================================
2 // Zerberus
3 // Zample player
4 //
5 // Copyright (C) 2013 Werner Schweer
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2
9 // as published by the Free Software Foundation and appearing in
10 // the file LICENCE.GPL
11 //=============================================================================
12 
13 #ifndef __MVOICE_H__
14 #define __MVOICE_H__
15 
16 #include <cstdint>
17 #include <math.h>
18 #include "filter.h"
19 
20 // Disable warning C4201: nonstandard extension used: nameless struct/union in VS2017
21 #if (defined (_MSCVER) || defined (_MSC_VER))
22  #pragma warning ( disable: 4201)
23 #endif
24 
25 class Channel;
26 struct Zone;
27 class Sample;
28 class Zerberus;
29 
30 enum class LoopMode : char;
31 enum class OffMode : char;
32 enum class Trigger : char;
33 
34 static const int EG_SIZE = 256;
35 
36 //---------------------------------------------------------
37 // Envelope
38 //---------------------------------------------------------
39 
40 struct Envelope {
41  static float egPow[EG_SIZE];
42  static float egLin[EG_SIZE];
43 
44  int steps, count;
45  bool constant = false;
46  float offset = 0.0;
47  float max = 1.0;
48  float val;
49  float* table;
50 
51  void setTable(float* f) { table = f; }
52  bool step() {
53  if (count) {
54  --count;
55  if (!constant)
56  val = table[EG_SIZE * count/steps]*(max-offset)+offset;
57  return false;
58  }
59  else
60  return true;
61  }
62  void setTime(float ms, int sampleRate);
63  void setConstant(float v) { constant = true; val = v; }
64  void setVariable() { constant = false; }
65  };
66 
67 //-----------------------------------------------------------------------------
68 // Phase
69 // Playing pointer for voice playback
70 //
71 // When a sample is played back at a different pitch, the playing pointer
72 // in the source sample will not advance exactly one sample per output sample.
73 //
74 // This playing pointer is implemented using Phase.
75 //-----------------------------------------------------------------------------
76 
77 struct Phase {
78  union {
79  int64_t data;
80  struct {
81  uint8_t _fract;
82  };
83  };
84 
85  void operator+=(const Phase& p) { data += p.data; }
86  void set(int b) { data = b * 256; }
87  void set(double b) { data = b * 256.0; }
88  void setIndex(int b) { data = b * 256 + _fract; }
89  int index() const { return data >> 8; }
90  unsigned fract() const { return _fract; }
91 
92  Phase() {}
93  Phase(int64_t v) : data(v) {}
94  };
95 
96 enum class VoiceState : char {
97  OFF,
98  ATTACK,
99  PLAYING,
100  SUSTAINED,
101  STOP
102  };
103 
104 enum V1Envelopes : int {
112  };
113 
114 //---------------------------------------------------------
115 // Voice
116 //---------------------------------------------------------
117 
118 class Voice {
121 
124  int _key;
127 
128  short* data;
129  long long eidx;
132  int _offBy;
133  long long _loopStart;
134  long long _loopEnd;
135  bool _looping;
137 
138  float gain;
139 
140  Phase phase, phaseIncr;
141 
143 
146 
148 
149  const Zone* z;
150 
151  public:
152  Voice(Zerberus*);
153  Voice* next() const { return _next; }
154  void setNext(Voice* v) { _next = v; }
155 
156  void start(Channel* channel, int key, int velo, const Zone*, double durSinceNoteOn);
157  void updateEnvelopes();
158  void process(int frames, float*);
159  void updateLoop();
160  short getData(long long pos);
161 
162  Channel* channel() const { return _channel; }
163  int key() const { return _key; }
164  int velocity() const { return _velocity; }
165 
166  bool isPlaying() const { return _state == VoiceState::PLAYING || _state == VoiceState::ATTACK; }
167  bool isSustained() const { return _state == VoiceState::SUSTAINED; }
168  bool isOff() const { return _state == VoiceState::OFF; }
169  bool isStopped() const { return _state == VoiceState::STOP; }
170  void stop() { envelopes[currentEnvelope].step(); envelopes[V1Envelopes::RELEASE].max = envelopes[currentEnvelope].val; currentEnvelope = V1Envelopes::RELEASE; _state = VoiceState::STOP; }
171  void stop(float time);
172  void sustained() { _state = VoiceState::SUSTAINED; }
173  void off() { _state = VoiceState::OFF; }
174  const char* state() const;
175  LoopMode loopMode() const { return _loopMode; }
176  int getSamplesSinceStart() { return _samplesSinceStart; }
177  float getGain() { return gain; }
178 
179  OffMode offMode() const { return _offMode; }
180  int offBy() const { return _offBy; }
181  static void init();
182  };
183 
184 #endif
185 
bool step()
Definition: voice.h:52
float val
Definition: voice.h:48
int key() const
Definition: voice.h:163
long long _loopEnd
Definition: voice.h:134
int velocity() const
Definition: voice.h:164
int currentEnvelope
Definition: voice.h:144
V1Envelopes
Definition: voice.h:104
Definition: voice.h:105
int _offBy
Definition: voice.h:132
OffMode offMode() const
Definition: voice.h:179
Definition: voice.h:118
float max
Definition: voice.h:47
int _velocity
Definition: voice.h:125
int _key
Definition: voice.h:124
bool isPlaying() const
Definition: voice.h:166
void setConstant(float v)
Definition: voice.h:63
Definition: zerberus.h:75
void stop()
Definition: voice.h:170
LoopMode _loopMode
Definition: voice.h:130
int64_t data
Definition: voice.h:79
long long eidx
Definition: voice.h:129
Definition: voice.h:106
uint8_t _fract
Definition: voice.h:81
Voice * _next
Definition: voice.h:119
void setNext(Voice *v)
Definition: voice.h:154
int offBy() const
Definition: voice.h:180
Zerberus * _zerberus
Definition: voice.h:120
void setTime(float ms, int sampleRate)
Definition: voice.cpp:34
Definition: voice.h:110
Definition: voice.h:77
Phase()
Definition: voice.h:92
void setIndex(int b)
Definition: voice.h:88
ZFilter filter
Definition: voice.h:142
Phase phaseIncr
Definition: voice.h:140
Definition: zone.h:56
bool _looping
Definition: voice.h:135
Channel * _channel
Definition: voice.h:123
Trigger
Definition: zone.h:23
Definition: filter.h:24
float gain
Definition: voice.h:138
Phase(int64_t v)
Definition: voice.h:93
void operator+=(const Phase &p)
Definition: voice.h:85
Definition: voice.h:111
int index() const
Definition: voice.h:89
VoiceState
Definition: voice.h:96
bool isOff() const
Definition: voice.h:168
Channel * channel() const
Definition: voice.h:162
float getGain()
Definition: voice.h:177
void off()
Definition: voice.h:173
Trigger trigger
Definition: voice.h:147
LoopMode loopMode() const
Definition: voice.h:175
int steps
Definition: voice.h:44
OffMode
Definition: zone.h:39
int getSamplesSinceStart()
Definition: voice.h:176
LoopMode
Definition: zone.h:31
const Zone * z
Definition: voice.h:149
OffMode _offMode
Definition: voice.h:131
void setVariable()
Definition: voice.h:64
bool isSustained() const
Definition: voice.h:167
Voice * next() const
Definition: voice.h:153
float * table
Definition: voice.h:49
Definition: sample.h:20
unsigned fract() const
Definition: voice.h:90
Definition: voice.h:108
bool isStopped() const
Definition: voice.h:169
Definition: voice.h:40
Definition: voice.h:107
long long _loopStart
Definition: voice.h:133
short * data
Definition: voice.h:128
int count
Definition: voice.h:44
int _samplesSinceStart
Definition: voice.h:136
float offset
Definition: voice.h:46
void sustained()
Definition: voice.h:172
int audioChan
Definition: voice.h:126
void setTable(float *f)
Definition: voice.h:51
Definition: channel.h:23