[4c7322]: src / xsynth_voice.c  Maximize  Restore  History

Download this file

361 lines (301 with data), 11.6 kB

  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
/* Xsynth DSSI software synthesizer plugin
*
* Copyright (C) 2004 Sean Bolton and others.
*
* Portions of this file may have come from Steve Brookes'
* Xsynth, copyright (C) 1999 S. J. Brookes.
* Portions of this file may have come from Peter Hanappe's
* Fluidsynth, copyright (C) 2003 Peter Hanappe and others.
*
* 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., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA.
*/
#define _BSD_SOURCE 1
#define _SVID_SOURCE 1
#define _ISOC99_SOURCE 1
#include <stdlib.h>
#include "xsynth_types.h"
#include "xsynth.h"
#include "xsynth_synth.h"
#include "xsynth_voice.h"
/*
* xsynth_voice_new
*/
xsynth_voice_t *
xsynth_voice_new(xsynth_synth_t *synth)
{
xsynth_voice_t *voice;
voice = (xsynth_voice_t *)calloc(sizeof(xsynth_voice_t), 1);
if (voice) {
voice->status = XSYNTH_VOICE_OFF;
}
return voice;
}
/*
* xsynth_voice_note_on
*/
void
xsynth_voice_note_on(xsynth_synth_t *synth, xsynth_voice_t *voice,
unsigned char key, unsigned char velocity)
{
int i;
voice->key = key;
voice->velocity = velocity;
if (!synth->monophonic || !(_ON(voice) || _SUSTAINED(voice))) {
/* brand-new voice, or monophonic voice in release phase; set
* everything up */
XDB_MESSAGE(XDB_NOTE, " xsynth_voice_note_on in polyphonic/new section: key %d, mono %d, old status %d\n", key, synth->monophonic, voice->status);
voice->target_pitch = xsynth_pitch[key];
switch(synth->glide) {
case XSYNTH_GLIDE_MODE_LEGATO:
if (synth->held_keys[0] >= 0) {
voice->prev_pitch = xsynth_pitch[synth->held_keys[0]];
} else {
voice->prev_pitch = voice->target_pitch;
}
break;
case XSYNTH_GLIDE_MODE_INITIAL:
if (synth->held_keys[0] >= 0) {
voice->prev_pitch = voice->target_pitch;
} else {
voice->prev_pitch = synth->last_noteon_pitch;
}
break;
case XSYNTH_GLIDE_MODE_ALWAYS:
if (synth->held_keys[0] >= 0) {
voice->prev_pitch = xsynth_pitch[synth->held_keys[0]];
} else {
voice->prev_pitch = synth->last_noteon_pitch;
}
break;
case XSYNTH_GLIDE_MODE_LEFTOVER:
/* leave voice->prev_pitch at whatever it was */
break;
default:
case XSYNTH_GLIDE_MODE_OFF:
voice->prev_pitch = voice->target_pitch;
break;
}
if (!_PLAYING(voice)) {
voice->lfo_pos = 0.0f;
voice->eg1 = 0.0f;
voice->eg2 = 0.0f;
voice->delay1 = 0.0f;
voice->delay2 = 0.0f;
voice->delay3 = 0.0f;
voice->delay4 = 0.0f;
voice->c5 = 0.0f;
voice->osc_index = 0;
voice->osc1.last_waveform = -1;
voice->osc1.pos = 0.0f;
voice->osc2.last_waveform = -1;
voice->osc2.pos = 0.0f;
}
voice->eg1_phase = 0;
voice->eg2_phase = 0;
xsynth_voice_update_pressure_mod(synth, voice);
} else {
/* synth is monophonic, and we're modifying a playing voice */
XDB_MESSAGE(XDB_NOTE, " xsynth_voice_note_on in monophonic section: old key %d => new key %d\n", synth->held_keys[0], key);
/* set new pitch */
voice->target_pitch = xsynth_pitch[key];
if (synth->glide == XSYNTH_GLIDE_MODE_INITIAL ||
synth->glide == XSYNTH_GLIDE_MODE_OFF)
voice->prev_pitch = voice->target_pitch;
/* if in 'on' or 'both' modes, and key has changed, then re-trigger EGs */
if ((synth->monophonic == XSYNTH_MONO_MODE_ON ||
synth->monophonic == XSYNTH_MONO_MODE_BOTH) &&
(synth->held_keys[0] < 0 || synth->held_keys[0] != key)) {
voice->eg1_phase = 0;
voice->eg2_phase = 0;
}
/* all other variables stay what they are */
}
synth->last_noteon_pitch = voice->target_pitch;
/* add new key to the list of held keys */
/* check if new key is already in the list; if so, move it to the
* top of the list, otherwise shift the other keys down and add it
* to the top of the list. */
for (i = 0; i < 7; i++) {
if (synth->held_keys[i] == key)
break;
}
for (; i > 0; i--) {
synth->held_keys[i] = synth->held_keys[i - 1];
}
synth->held_keys[0] = key;
if (!_PLAYING(voice)) {
xsynth_voice_start_voice(voice);
} else if (!_ON(voice)) { /* must be XSYNTH_VOICE_SUSTAINED or XSYNTH_VOICE_RELEASED */
voice->status = XSYNTH_VOICE_ON;
}
}
/*
* xsynth_voice_set_release_phase
*/
static inline void
xsynth_voice_set_release_phase(xsynth_voice_t *voice)
{
voice->eg1_phase = 2;
voice->eg2_phase = 2;
}
/*
* xsynth_voice_remove_held_key
*/
inline void
xsynth_voice_remove_held_key(xsynth_synth_t *synth, unsigned char key)
{
int i;
/* check if this key is in list of held keys; if so, remove it and
* shift the other keys up */
for (i = 7; i >= 0; i--) {
if (synth->held_keys[i] == key)
break;
}
if (i >= 0) {
for (; i < 7; i++) {
synth->held_keys[i] = synth->held_keys[i + 1];
}
synth->held_keys[7] = -1;
}
}
/*
* xsynth_voice_note_off
*/
void
xsynth_voice_note_off(xsynth_synth_t *synth, xsynth_voice_t *voice,
unsigned char key, unsigned char rvelocity)
{
unsigned char previous_top_key;
XDB_MESSAGE(XDB_NOTE, " xsynth_set_note_off: called for voice %p, key %d\n", voice, key);
/* save release velocity */
voice->rvelocity = rvelocity;
previous_top_key = synth->held_keys[0];
/* remove this key from list of held keys */
xsynth_voice_remove_held_key(synth, key);
if (synth->monophonic) { /* monophonic mode */
if (synth->held_keys[0] >= 0) {
/* still some keys held */
if (synth->held_keys[0] != previous_top_key) {
/* most-recently-played key has changed */
voice->key = synth->held_keys[0];
XDB_MESSAGE(XDB_NOTE, " note-off in monophonic section: changing pitch to %d\n", voice->key);
voice->target_pitch = xsynth_pitch[voice->key];
if (synth->glide == XSYNTH_GLIDE_MODE_INITIAL ||
synth->glide == XSYNTH_GLIDE_MODE_OFF)
voice->prev_pitch = voice->target_pitch;
/* if mono mode is 'both', re-trigger EGs */
if (synth->monophonic == XSYNTH_MONO_MODE_BOTH && !_RELEASED(voice)) {
voice->eg1_phase = 0;
voice->eg2_phase = 0;
}
}
} else { /* no keys still held */
if (XSYNTH_SYNTH_SUSTAINED(synth)) {
/* no more keys in list, but we're sustained */
XDB_MESSAGE(XDB_NOTE, " note-off in monophonic section: sustained with no held keys\n");
if (!_RELEASED(voice))
voice->status = XSYNTH_VOICE_SUSTAINED;
} else { /* not sustained */
/* no more keys in list, so turn off note */
XDB_MESSAGE(XDB_NOTE, " note-off in monophonic section: turning off voice %p\n", voice);
xsynth_voice_set_release_phase(voice);
voice->status = XSYNTH_VOICE_RELEASED;
}
}
} else { /* polyphonic mode */
if (XSYNTH_SYNTH_SUSTAINED(synth)) {
if (!_RELEASED(voice))
voice->status = XSYNTH_VOICE_SUSTAINED;
} else { /* not sustained */
xsynth_voice_set_release_phase(voice);
voice->status = XSYNTH_VOICE_RELEASED;
}
}
}
/*
* xsynth_voice_release_note
*/
void
xsynth_voice_release_note(xsynth_synth_t *synth, xsynth_voice_t *voice)
{
XDB_MESSAGE(XDB_NOTE, " xsynth_voice_release_note: turning off voice %p\n", voice);
if (_ON(voice)) {
/* dummy up a release velocity */
voice->rvelocity = 64;
}
xsynth_voice_set_release_phase(voice);
voice->status = XSYNTH_VOICE_RELEASED;
}
/*
* xsynth_voice_set_ports
*/
void
xsynth_voice_set_ports(xsynth_synth_t *synth, xsynth_patch_t *patch)
{
*(synth->osc1_pitch) = patch->osc1_pitch;
*(synth->osc1_waveform) = (float)patch->osc1_waveform;
*(synth->osc1_pulsewidth) = patch->osc1_pulsewidth;
*(synth->osc2_pitch) = patch->osc2_pitch;
*(synth->osc2_waveform) = (float)patch->osc2_waveform;
*(synth->osc2_pulsewidth) = patch->osc2_pulsewidth;
*(synth->osc_sync) = (float)patch->osc_sync;
*(synth->osc_balance) = patch->osc_balance;
*(synth->lfo_frequency) = patch->lfo_frequency;
*(synth->lfo_waveform) = (float)patch->lfo_waveform;
*(synth->lfo_amount_o) = patch->lfo_amount_o;
*(synth->lfo_amount_f) = patch->lfo_amount_f;
*(synth->eg1_attack_time) = patch->eg1_attack_time;
*(synth->eg1_decay_time) = patch->eg1_decay_time;
*(synth->eg1_sustain_level) = patch->eg1_sustain_level;
*(synth->eg1_release_time) = patch->eg1_release_time;
*(synth->eg1_vel_sens) = patch->eg1_vel_sens;
*(synth->eg1_amount_o) = patch->eg1_amount_o;
*(synth->eg1_amount_f) = patch->eg1_amount_f;
*(synth->eg2_attack_time) = patch->eg2_attack_time;
*(synth->eg2_decay_time) = patch->eg2_decay_time;
*(synth->eg2_sustain_level) = patch->eg2_sustain_level;
*(synth->eg2_release_time) = patch->eg2_release_time;
*(synth->eg2_vel_sens) = patch->eg2_vel_sens;
*(synth->eg2_amount_o) = patch->eg2_amount_o;
*(synth->eg2_amount_f) = patch->eg2_amount_f;
*(synth->vcf_cutoff) = patch->vcf_cutoff;
*(synth->vcf_qres) = patch->vcf_qres;
*(synth->vcf_mode) = (float)patch->vcf_mode;
*(synth->glide_time) = patch->glide_time;
*(synth->volume) = patch->volume;
}
/*
* xsynth_voice_update_pressure_mod
*/
void
xsynth_voice_update_pressure_mod(xsynth_synth_t *synth, xsynth_voice_t *voice)
{
unsigned char kp = synth->key_pressure[voice->key];
unsigned char cp = synth->channel_pressure;
float p;
/* add the channel and key pressures together in a way that 'feels' good */
if (kp > cp) {
p = (float)kp / 127.0f;
p += (1.0f - p) * ((float)cp / 127.0f);
} else {
p = (float)cp / 127.0f;
p += (1.0f - p) * ((float)kp / 127.0f);
}
/* set the pressure modifier so it ranges between 1.0 (no pressure, no
* resonance boost) and 0.25 (full pressure, resonance boost 75% of the way
* to filter oscillation) */
voice->pressure = 1.0f - (p * 0.75f);
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks