~ubuntu-branches/ubuntu/feisty/muse/feisty

« back to all changes in this revision

Viewing changes to synti/stklib/Envelope.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2002-04-23 17:28:23 UTC
  • Revision ID: james.westby@ubuntu.com-20020423172823-w8yplzr81a759xa3
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************/
 
2
/*  Envelope Class, Perry R. Cook, 1995-96 */
 
3
/*                                         */
 
4
/*  This is the base class for envelopes.  */
 
5
/*  This one is capable of ramping state   */
 
6
/*  from where it is to a target value by  */
 
7
/*  a rate.  It also responds to simple    */
 
8
/*  KeyOn and KeyOff messages, ramping to  */         
 
9
/*  1.0 on keyon and to 0.0 on keyoff.     */
 
10
/*  There are two tick (update value)      */
 
11
/*  methods, one returns the value, and    */
 
12
/*  other returns 0 if the envelope is at  */
 
13
/*  the target value (the state bit).      */
 
14
/*******************************************/
 
15
 
 
16
#include "Envelope.h"    
 
17
 
 
18
Envelope :: Envelope() : Object()
 
19
{    
 
20
  target = (MY_FLOAT) 0.0;
 
21
  value = (MY_FLOAT) 0.0;
 
22
  rate = (MY_FLOAT) 0.001;
 
23
  state = 0;
 
24
}
 
25
 
 
26
Envelope :: ~Envelope()
 
27
{    
 
28
}
 
29
 
 
30
void Envelope :: keyOn()
 
31
{
 
32
  target = (MY_FLOAT) 1.0;
 
33
  if (value != target) state = 1;
 
34
}
 
35
 
 
36
void Envelope :: keyOff()
 
37
{
 
38
  target = (MY_FLOAT) 0.0;
 
39
  if (value != target) state = 1;
 
40
}
 
41
 
 
42
void Envelope :: setRate(MY_FLOAT aRate)
 
43
{
 
44
  if (aRate < 0.0) {
 
45
    printf("negative rates not allowed!!, correcting\n");
 
46
    rate = -aRate;
 
47
  }
 
48
  else rate = aRate;
 
49
}
 
50
 
 
51
void Envelope :: setTime(MY_FLOAT aTime)
 
52
{
 
53
  if (aTime < 0.0) {
 
54
    printf("negative times not allowed!!, correcting\n");
 
55
    rate = ONE_OVER_SRATE / -aTime ;
 
56
  }
 
57
  else rate = ONE_OVER_SRATE / aTime ;
 
58
}
 
59
 
 
60
void Envelope :: setTarget(MY_FLOAT aTarget)
 
61
{
 
62
  target = aTarget;
 
63
  if (value != target) state = 1;
 
64
}
 
65
 
 
66
void Envelope :: setValue(MY_FLOAT aValue)
 
67
{
 
68
  state = 0;
 
69
  target = aValue;
 
70
  value = aValue;
 
71
}
 
72
 
 
73
MY_FLOAT Envelope :: tick()
 
74
{
 
75
  if (state)  {
 
76
    if (target > value)    {
 
77
      value += rate;
 
78
      if (value >= target)    {
 
79
        value = target;
 
80
        state = 0;
 
81
      }
 
82
    }
 
83
    else    {
 
84
      value -= rate;
 
85
      if (value <= target)    {
 
86
        value = target;
 
87
        state = 0;
 
88
      }
 
89
    }
 
90
  }
 
91
  return value;
 
92
}
 
93
 
 
94
int Envelope :: informTick()
 
95
{
 
96
  this->tick();
 
97
  return state;
 
98
}
 
99
 
 
100
MY_FLOAT Envelope :: lastOut()
 
101
{
 
102
  return value;
 
103
}
 
104
 
 
105
/************  Test Main  ************************/
 
106
/*
 
107
void main()
 
108
{
 
109
    long i;
 
110
    Envelope test;
 
111
    
 
112
    test.setRate(0.15);
 
113
    test.keyOn();
 
114
    for (i=0;i<10;i++) printf("%lf\n",test.tick());
 
115
    test.setRate(0.1);
 
116
    test.setTarget(0.5);
 
117
    while (test.informTick()) printf("%lf\n",test.lastOut());
 
118
    test.setRate(0.05);
 
119
    test.keyOff();
 
120
    while(test.informTick()) printf("%lf\n",test.lastOut());
 
121
}
 
122
*/