~ubuntu-branches/ubuntu/breezy/muse/breezy

« back to all changes in this revision

Viewing changes to synti/stklib/ADSR.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2004-02-07 15:18:22 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040207151822-es27xxkzbcxkebjm
Tags: 0.6.3-1
* New upstream version.
* Added patches:
  + [10_alsa_init_fix] New, from upstream CVS.
    Initialize direction variable when setting Alsa parameters.
  + [10_canvas_translation_fix] New, from upstream CVS.
    Do not translate tooltips twice in canvas popup.
  + [10_checkbox_fix] New, from upstream CVS.
    Use proper set/test methods on metronome checkboxes.
  + [10_html_doc_cleanup] New.
    Fix links and HTML errors in documentation.
  + [20_allow_system_timer] New.
    The new upstream version fails by default if the real-time clock
    could not be accessed (usually the case when not running suid-root).
    This patch reverts the old behaviour of falling back to the more
    inaccurate system timer.
* Updated patches:
  + [11_PIC_fixes_fixup] Rediffed.
* Removed patches:
  + [20_no_atomic_asm] Merged upstream.
* debian/compat: Splice out debhelper compatibility level from rules file.
* debian/control: Build-depend on latest jack release by default.
  Closes: #228788
* debian/control: Bump standards version.
* debian/control: Use auto-generated debconf dependency via misc:Depends.
* debian/control: Minor tweaks to the long description.
* debian/control: Tighten fluidsynth build dependency to sane version.
* debian/muse.doc-base: New. Register HTML documentation with doc-base.
* debian/templates: Tiny rewording, and typo fix.
* debian/templates, debian/po/*: Switch to po-debconf for translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************/
2
 
/*  ADSR Subclass of the Envelope Class,   */
3
 
/*  by Perry R. Cook, 1995-96              */ 
4
 
/*  This is the traditional ADSR (Attack   */
5
 
/*  Decay, Sustain, Release) ADSR.         */
6
 
/*  It responds to simple KeyOn and KeyOff */
7
 
/*  messages, keeping track of it's state. */         
8
 
/*  There are two tick (update value)      */
9
 
/*  methods, one returns the value, and    */
10
 
/*  other returns the state (0 = A, 1 = D, */
11
 
/*  2 = S, 3 = R)                          */
12
 
/*******************************************/
13
 
 
14
 
#include "ADSR.h"    
15
 
 
16
 
ADSR :: ADSR() : Envelope()
17
 
{    
18
 
  target = (MY_FLOAT) 0.0;
19
 
  value = (MY_FLOAT) 0.0;
20
 
  attackRate = (MY_FLOAT) 0.001;
21
 
  decayRate = (MY_FLOAT) 0.001;
22
 
  sustainLevel = (MY_FLOAT) 0.5;
23
 
  releaseRate = (MY_FLOAT) 0.01;
24
 
  state = 0;
25
 
}
26
 
 
27
 
ADSR :: ~ADSR()
28
 
{    
29
 
  /* Nothing to do here */
30
 
}
31
 
 
32
 
void ADSR :: keyOn()
33
 
{
34
 
  target = (MY_FLOAT) 1.0;
35
 
  rate = attackRate;
36
 
  state = 0;
37
 
}
38
 
 
39
 
void ADSR :: keyOff()
40
 
{
41
 
  target = (MY_FLOAT) 0.0;
42
 
  rate = releaseRate;
43
 
  state = 3;
44
 
}
45
 
 
46
 
void ADSR :: setAttackRate(MY_FLOAT aRate)
47
 
{
48
 
  if (aRate < 0.0) {
49
 
    printf("negative rates not allowed!!, correcting\n");
50
 
    attackRate = -aRate;
51
 
  }
52
 
  else attackRate = aRate;
53
 
}
54
 
 
55
 
void ADSR :: setDecayRate(MY_FLOAT aRate)
56
 
{
57
 
  if (aRate < 0.0) {
58
 
    printf("negative rates not allowed!!, correcting\n");
59
 
    decayRate = -aRate;
60
 
  }
61
 
  else decayRate = aRate;
62
 
}
63
 
 
64
 
void ADSR :: setSustainLevel(MY_FLOAT aLevel)
65
 
{
66
 
  if (aLevel < 0.0 ) {
67
 
    printf("Sustain level out of range!!, correcting\n");
68
 
    sustainLevel = (MY_FLOAT)  0.0;
69
 
  }
70
 
  else sustainLevel = aLevel;
71
 
}
72
 
 
73
 
void ADSR :: setReleaseRate(MY_FLOAT aRate)
74
 
{
75
 
  if (aRate < 0.0) {
76
 
    printf("negative rates not allowed!!, correcting\n");
77
 
    releaseRate = -aRate;
78
 
  }
79
 
  else releaseRate = aRate;
80
 
}
81
 
 
82
 
void ADSR :: setAttackTime(MY_FLOAT aTime)
83
 
{
84
 
  if (aTime < 0.0) {
85
 
    printf("negative times not allowed!!, correcting\n");
86
 
    attackRate = ONE_OVER_SRATE / -aTime;
87
 
  }
88
 
  else attackRate = ONE_OVER_SRATE / aTime;
89
 
}
90
 
 
91
 
void ADSR :: setDecayTime(MY_FLOAT aTime)
92
 
{
93
 
  if (aTime < 0.0) {
94
 
    printf("negative times not allowed!!, correcting\n");
95
 
    decayRate = ONE_OVER_SRATE / -aTime;
96
 
  }
97
 
  else decayRate = ONE_OVER_SRATE / aTime;
98
 
}
99
 
 
100
 
void ADSR :: setReleaseTime(MY_FLOAT aTime)
101
 
{
102
 
  if (aTime < 0.0) {
103
 
    printf("negative times not allowed!!, correcting\n");
104
 
    releaseRate = ONE_OVER_SRATE / -aTime;
105
 
  }
106
 
  else releaseRate = ONE_OVER_SRATE / aTime;
107
 
}
108
 
 
109
 
void ADSR :: setAllTimes(MY_FLOAT attTime, MY_FLOAT decTime, MY_FLOAT susLevel, MY_FLOAT relTime)
110
 
{
111
 
  this->setAttackTime(attTime);
112
 
  this->setDecayTime(decTime);
113
 
  this->setSustainLevel(susLevel);
114
 
  this->setReleaseTime(relTime);
115
 
}
116
 
 
117
 
void ADSR :: setTarget(MY_FLOAT aTarget)
118
 
{
119
 
  target = aTarget;
120
 
  if (value < target) {
121
 
    state = ATTACK;
122
 
    this->setSustainLevel(target);
123
 
    rate = attackRate;
124
 
  }
125
 
  if (value > target) {
126
 
    this->setSustainLevel(target);
127
 
    state = DECAY;
128
 
    rate = decayRate;
129
 
  }
130
 
}
131
 
 
132
 
void ADSR :: setValue(MY_FLOAT aValue)
133
 
{
134
 
  state = SUSTAIN;
135
 
  target = aValue;
136
 
  value = aValue;
137
 
  this->setSustainLevel(aValue);
138
 
  rate = (MY_FLOAT)  0.0;
139
 
}
140
 
 
141
 
MY_FLOAT ADSR :: tick()
142
 
{
143
 
  if (state==ATTACK)  {
144
 
    value += rate;
145
 
    if (value >= target)    {
146
 
      value = target;
147
 
      rate = decayRate;
148
 
      target = sustainLevel;
149
 
            state = DECAY;
150
 
    }
151
 
  }    
152
 
  else if (state==DECAY)  {
153
 
    value -= decayRate;
154
 
    if (value <= sustainLevel)    {
155
 
      value = sustainLevel;
156
 
      rate = (MY_FLOAT) 0.0;
157
 
      state = SUSTAIN;
158
 
    }
159
 
  }
160
 
  else if (state==RELEASE)  {
161
 
    value -= releaseRate;
162
 
    if (value <= 0.0)       {
163
 
      value = (MY_FLOAT) 0.0;
164
 
      state = 4;
165
 
    }
166
 
  }
167
 
  return value;
168
 
}
169
 
 
170
 
int ADSR :: informTick()
171
 
{
172
 
  this->tick();
173
 
  return state;
174
 
}
175
 
 
176
 
MY_FLOAT ADSR :: lastOut()
177
 
{
178
 
  return value;
179
 
}
180
 
 
181
 
/************  Test Main  ************************/
182
 
/*
183
 
void main()
184
 
{
185
 
    long i;
186
 
    ADSR test;
187
 
    
188
 
    test.setAttackRate(0.15);
189
 
    test.keyOn();
190
 
    while(test.informTick()==ATTACK) printf("%lf\n",test.tick());
191
 
    test.setDecayRate(0.1);
192
 
    while (test.informTick()==DECAY) printf("%lf\n",test.lastOut());
193
 
    test.setReleaseRate(0.05);
194
 
    test.keyOff();
195
 
    while(test.informTick()==RELEASE) printf("%lf\n",test.lastOut());
196
 
}
197
 
*/