aboutsummaryrefslogtreecommitdiff
path: root/src/midiio/src/Voice.cpp
blob: ed05bcecafbc49275c654e5db5d3a6a2ec426797 (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
//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Sun Oct 11 18:39:14 PDT 1998
// Last Modified: Sun Oct 11 18:39:18 PDT 1998
// Filename:      ...sig/maint/code/control/Voice/Voice.cpp
// Web Address:   http://www-ccrma.stanford.edu/~craig/improv/src/Voice.cpp
// Syntax:        C++
//
// Description:   The Voice class is a MIDI output class which keeps
//                track of the last note played in to it.  If the last
//                note was not turned off when a new note is played,
//                then the old note will be turned off before the 
//                new note is played.
//

#include "Voice.h"


// declare static variables:
SigTimer Voice::timer;

//////////////////////////////
//
// Voice::Voice
//

Voice::Voice(int aChannel) {
   chan = oldChan = aChannel;
   vel = oldVel = key = oldKey = 0;
}


Voice::Voice(const Voice& aVoice) {
   chan = aVoice.chan;
   vel = aVoice.vel;
   key = aVoice.key;
   oldChan = 0;
   oldVel = 0;
   oldKey = 0;
}


Voice::Voice(void) {
   chan = oldChan = key = oldKey = vel = oldVel = 0;
}


//////////////////////////////
//
// Voice::~Voice
//

Voice::~Voice() {
   off();
}



//////////////////////////////
//
// Voice::cont -- use default channel if none specified for
//   the continuous controller message.
//

void Voice::cont(int controller, int data) {
   MidiOutput::cont(getChannel(), controller, data);
}



//////////////////////////////
//
// Voice::getChan -- returns the channel of the voice.  
//     Synonym for getChannel.
//

int Voice::getChan(void) const {
   return chan;
}



//////////////////////////////
//
// Voice::getChannel -- returs the channel of the voice.
//

int Voice::getChannel(void) const {
   return chan;
}



//////////////////////////////
//
// Voice::getKey -- returns the current MIDI key number of the voice.
//     Synonym for getKeynum.
//

int Voice::getKey(void) const {
   return key;
}



//////////////////////////////
//
// Voice::getKeynum -- returns the current MIDI key number of the voice.
//

int Voice::getKeynum(void) const {
   return key;
}


//////////////////////////////
//
// Voice::getOffTime -- returns the last note off message sent
//     out of the voice object
//

int Voice::getOffTime(void) const {
   return offTime;
}



//////////////////////////////
//
// Voice::getOnTime -- returns the last note on message sent
//     out of the voice object.
//

int Voice::getOnTime(void) const {
   return onTime;
}



//////////////////////////////
//
// Voice::getVel -- returns the current velocity of the MIDI key.
//     Synonym for getVelocity.
//

int Voice::getVel(void) const {
   return vel;
}



//////////////////////////////
//
// Voice::getVelocity -- returns the current velocity of the MIDI key.
//

int Voice::getVelocity(void) const {
   return vel;
}



//////////////////////////////
//
// Voice::off
//

void Voice::off(void) {
   if (status() != 0) {
      offTime = timer.getTime();
      MidiOutput::play(oldChan, oldKey, 0);
      oldVel = 0;
   }
}



//////////////////////////////
//
// Voice::pc -- use default channel if none is specified
//

void Voice::pc(int aTimbre) {
   MidiOutput::pc(getChannel(), aTimbre);
}



//////////////////////////////
//
// Voice::play
//

void Voice::play(int aChannel, int aKeyno, int aVelocity) {
   off();
   MidiOutput::play(aChannel, aKeyno, aVelocity);
   oldChan = aChannel;
   oldKey = aKeyno;
   oldVel = aVelocity;
   setChannel(aChannel);
   setKeynum(aKeyno);
   setVelocity(aVelocity);

   if (aVelocity != 0) {
      onTime = timer.getTime();
   } else {
      offTime = timer.getTime();
   }
}


void Voice::play(int aKeyno, int aVelocity) {
   off();
   MidiOutput::play(getChannel(), aKeyno, aVelocity);
   oldChan = getChannel();
   oldKey = aKeyno;
   oldVel = aVelocity;
   setKeynum(aKeyno);
   setVelocity(aVelocity);

   if (aVelocity != 0) {
      onTime = timer.getTime();
   } else {
      offTime = timer.getTime();
   }
}


void Voice::play(void) {
   off();
   MidiOutput::play(getChannel(), getKey(), getVel());
   oldChan = getChannel();
   oldKey = getKey();
   oldVel = getVel();

   if (getVel() != 0) {
      onTime = timer.getTime();
   } else {
      offTime = timer.getTime();
   }
}



//////////////////////////////
//
// Voice::setChan -- set the MIDI channel.  Synonym for setChannel.
//

void Voice::setChan(int aChannel) {
   chan = aChannel;
}



//////////////////////////////
//
// Voice::setChannel -- set the MIDI channel of the voice.
//

void Voice::setChannel(int aChannel) {
   chan = aChannel;
}



//////////////////////////////
//
// Voice::setKey -- set the keynumber of the voice
//     Synonym for setKeyno.
//

void Voice::setKey(int aKeynum) {
   key = aKeynum;
}



//////////////////////////////
//
// Voice::setKeynum -- set the keynumber of the voice
//

void Voice::setKeynum(int aKeynum) {
   key = aKeynum;
}



//////////////////////////////
//
// Voice::setVel -- set the MIDI velocity of the voice.
//     Synonym for setVelocity.
//

void Voice::setVel(int aVelocity) {
   vel = aVelocity;
}



//////////////////////////////
//
// Voice::setVelocity
//

void Voice::setVelocity(int aVelocity) {
   vel = aVelocity;
}



//////////////////////////////
//
// Voice::status -- returns zero if off or positive if on.
//

int Voice::status(void) const {
   return oldVel;
}



//////////////////////////////
//
//  Voice::sustain -- uses default channel if none specified.
//

void Voice::sustain(int aStatus) {
   MidiOutput::sustain(getChannel(), aStatus);
}


// md5sum:	d02ca41ae3b4e07efe7fedc720e52573  - Voice.cpp =css= 20030102