/[svn]/linuxsampler/trunk/src/eg_vca.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/eg_vca.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show annotations) (download)
Tue Feb 3 13:21:19 2004 UTC (20 years, 2 months ago) by schoenebeck
File size: 5251 byte(s)
* introduced time stamped events
* implemented jitter correction
* added pitchbend wheel support
* src/audiothread.cpp: using voice pool instead of a voice array, makes
  voice allocation more efficient and code more readable
* src/rtelmemorypool: redesigned, added some new methods and pool is now
  derived from list

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck *
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 as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20 * MA 02111-1307 USA *
21 ***************************************************************************/
22
23 #include "eg_vca.h"
24
25 EG_VCA::EG_VCA() {
26 Stage = stage_end;
27 Level = 0.0;
28 }
29
30 /**
31 * Will be called by the voice for every audio fragment to let the EG
32 * queue it's modulation changes for the current audio fragment.
33 *
34 * @param Samples - total number of sample points to be rendered in this
35 * audio fragment cycle by the audio engine
36 */
37 void EG_VCA::Process(uint Samples) {
38 if (Stage == stage_sustain && !ReleaseSignalReceived) return; // nothing to do
39
40 int iSample = TriggerDelay;
41 while (iSample < Samples) {
42 switch (Stage) {
43 case stage_attack: {
44 int to_process = Min(Samples - iSample, AttackStepsLeft);
45 int process_end = iSample + to_process;
46 AttackStepsLeft -= to_process;
47 while (iSample < process_end) {
48 Level += AttackCoeff;
49 ModulationSystem::pDestinationParameter[ModulationSystem::destination_vca][iSample++] *= Level;
50 }
51 TriggerDelay = 0;
52 if (!AttackStepsLeft) Stage = (ReleaseSignalReceived) ? stage_release : stage_sustain;
53 break;
54 }
55 case stage_sustain: {
56 if (!ReleaseSignalReceived) return; // nothing to do
57 Stage = stage_release;
58 break;
59 }
60 case stage_release: {
61 iSample = ReleaseDelay;
62 while (iSample < Samples) {
63 Level -= Level * ReleaseCoeff;
64 ModulationSystem::pDestinationParameter[ModulationSystem::destination_vca][iSample++] *= Level;
65 }
66 if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_end;
67 ReleaseDelay = 0;
68 return;
69 }
70 case stage_end: {
71 while (iSample < Samples) {
72 ModulationSystem::pDestinationParameter[ModulationSystem::destination_vca][iSample++] *= Level;
73 }
74 return;
75 }
76 }
77 }
78 }
79
80 /**
81 * Will be called by the voice when the key / voice was triggered.
82 *
83 * @param PreAttack - Preattack value for the envelope (0 - 1000 permille)
84 * @param AttackTime - Attack time for the envelope (0.000 - 60.000s)
85 * @param ReleaseTIme - Release time for the envelope (0.000 - 60.000s)
86 * @param Delay - number of sample points triggering should be delayed
87 */
88 void EG_VCA::Trigger(uint PreAttack, double AttackTime, double ReleaseTime, uint Delay) {
89 ReleaseSignalReceived = false;
90 TriggerDelay = Delay;
91 Stage = stage_attack;
92
93 // calculate attack stage parameters
94 AttackStepsLeft = (long) (AttackTime * ModulationSystem::SampleRate());
95 if (AttackStepsLeft) {
96 Level = PreAttack;
97 AttackCoeff = (1.0 - PreAttack) / AttackStepsLeft;
98 }
99 else {
100 Level = 1.0;
101 AttackCoeff = 0.0;
102 }
103
104 // calcuate release stage parameters
105 if (ReleaseTime < EG_MIN_RELEASE_TIME) ReleaseTime = EG_MIN_RELEASE_TIME; // to avoid click sounds at the end of the sample playback
106 ReleaseStepsLeft = (long) (ReleaseTime * ModulationSystem::SampleRate());
107 ReleaseCoeff = 1.0 - exp(log(EG_ENVELOPE_LIMIT) / (double) ReleaseStepsLeft);
108
109 dmsg(4,("AttackLength=%d, ReleaseLength=%d",AttackStepsLeft,ReleaseStepsLeft));
110 }
111
112 /**
113 * Will be called by the voice when the key / voice was released.
114 *
115 * @param Delay - number of sample points the release stage should be delayed
116 */
117 void EG_VCA::Release(uint Delay) {
118 ReleaseSignalReceived = true;
119 ReleaseDelay = Delay;
120 }

  ViewVC Help
Powered by ViewVC