~ubuntu-branches/ubuntu/oneiric/muse/oneiric

« back to all changes in this revision

Viewing changes to synti/stklib/FormSwep.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
/*  Sweepable Formant (2-pole)             */
 
3
/*  Filter Class, by Perry R. Cook, 1995-96*/ 
 
4
/*  See books on filters to understand     */
 
5
/*  more about how this works.  This drives*/
 
6
/*  to a target at speed set by rate.      */
 
7
/*******************************************/
 
8
 
 
9
#include "FormSwep.h"
 
10
 
 
11
FormSwep :: FormSwep() : Filter()
 
12
{
 
13
  outputs = (MY_FLOAT *) malloc(2 * sizeof(MY_FLOAT));
 
14
  poleCoeffs[0] = (MY_FLOAT) 0.0;
 
15
  poleCoeffs[1] = (MY_FLOAT) 0.0;
 
16
  gain = (MY_FLOAT) 1.0;
 
17
  freq = (MY_FLOAT) 0.0;
 
18
  reson = (MY_FLOAT) 0.0;
 
19
  currentGain = (MY_FLOAT) 1.0;
 
20
  currentFreq = (MY_FLOAT) 0.0;
 
21
  currentReson = (MY_FLOAT) 0.0;
 
22
  targetGain = (MY_FLOAT) 1.0;
 
23
  targetFreq = (MY_FLOAT) 0.0;
 
24
  targetReson = (MY_FLOAT) 0.0;
 
25
  deltaGain = (MY_FLOAT) 0.0;
 
26
  deltaFreq = (MY_FLOAT) 0.0;
 
27
  deltaReson = (MY_FLOAT) 0.0;
 
28
  sweepState = (MY_FLOAT)  0.0;
 
29
  sweepRate = (MY_FLOAT) 0.002;
 
30
  dirty = 0;
 
31
  this->clear();
 
32
}
 
33
 
 
34
FormSwep :: ~FormSwep()
 
35
{
 
36
  free(outputs);
 
37
}
 
38
 
 
39
void FormSwep :: clear()
 
40
{
 
41
  outputs[0] = (MY_FLOAT) 0.0;
 
42
  outputs[1] = (MY_FLOAT) 0.0;
 
43
}
 
44
 
 
45
void FormSwep :: setPoleCoeffs(MY_FLOAT *coeffs)
 
46
{
 
47
  dirty = 0;
 
48
  poleCoeffs[0] = coeffs[0];
 
49
  poleCoeffs[1] = coeffs[1];
 
50
}
 
51
 
 
52
void FormSwep :: setFreqAndReson(MY_FLOAT aFreq, MY_FLOAT aReson)
 
53
{
 
54
  dirty = 0;
 
55
  reson = aReson;
 
56
  freq = aFreq;
 
57
  currentReson = aReson;
 
58
  currentFreq = aFreq;
 
59
  poleCoeffs[1] = - (reson * reson);
 
60
  poleCoeffs[0] = (MY_FLOAT) 2.0 * reson * (MY_FLOAT)  cos(TWO_PI * freq / SRATE);
 
61
}
 
62
 
 
63
void FormSwep :: setStates(MY_FLOAT aFreq, MY_FLOAT aReson, MY_FLOAT aGain)
 
64
{
 
65
  dirty = 0;
 
66
  freq = aFreq;
 
67
  reson = aReson;
 
68
  gain = aGain;
 
69
  targetFreq = aFreq;
 
70
  targetReson = aReson;
 
71
  targetGain = aGain;
 
72
  currentFreq = aFreq;
 
73
  currentReson = aReson;
 
74
  currentGain = aGain;
 
75
}
 
76
 
 
77
void FormSwep :: setTargets(MY_FLOAT aFreq, MY_FLOAT aReson, MY_FLOAT aGain)
 
78
{
 
79
  dirty = 1;
 
80
  targetFreq = aFreq;
 
81
  targetReson = aReson;
 
82
  targetGain = aGain;
 
83
  deltaFreq = aFreq - currentFreq;
 
84
  deltaReson = aReson - currentReson;
 
85
  deltaGain = aGain - currentGain;
 
86
  sweepState = (MY_FLOAT) 0.0;
 
87
}
 
88
 
 
89
void FormSwep :: setSweepRate(MY_FLOAT aRate)
 
90
{
 
91
  sweepRate = aRate;
 
92
}
 
93
 
 
94
void FormSwep :: setSweepTime(MY_FLOAT aTime)
 
95
{
 
96
  sweepRate = ONE_OVER_SRATE / aTime;
 
97
}
 
98
 
 
99
void FormSwep :: setGain(MY_FLOAT aValue)
 
100
{
 
101
  gain = aValue;
 
102
}
 
103
 
 
104
MY_FLOAT FormSwep :: tick(MY_FLOAT sample)  // Perform Filter Operation
 
105
{                                     
 
106
  MY_FLOAT temp;                    
 
107
 
 
108
  if (dirty)  {
 
109
    sweepState += sweepRate;
 
110
    if (sweepState>= 1.0)   {
 
111
      sweepState = (MY_FLOAT) 1.0;
 
112
      dirty = 0;
 
113
      currentReson = targetReson;
 
114
      reson        = targetReson;
 
115
      currentFreq = targetFreq;
 
116
      freq        = targetFreq;
 
117
      currentGain = targetGain;
 
118
      gain        = targetGain;
 
119
    }
 
120
    else  {
 
121
      currentReson = reson + (deltaReson * sweepState);
 
122
      currentFreq = freq + (deltaFreq * sweepState);
 
123
      currentGain = gain + (deltaGain * sweepState);
 
124
    }
 
125
    poleCoeffs[1] = - (currentReson * currentReson);
 
126
    poleCoeffs[0] = (MY_FLOAT) 2.0 * currentReson * 
 
127
      (MY_FLOAT)  cos(TWO_PI * currentFreq / SRATE);
 
128
  }
 
129
 
 
130
  temp = currentGain * sample;
 
131
  temp += poleCoeffs[0] * outputs[0];
 
132
  temp += poleCoeffs[1] * outputs[1];
 
133
  outputs[1] = outputs[0];
 
134
  outputs[0] = temp;
 
135
  lastOutput = outputs[0];
 
136
  return lastOutput;
 
137
}
 
138
 
 
139