CSL  6.0
DistanceSimulator.cpp
Go to the documentation of this file.
1 //
2 // DistanceSimulator.cpp -- Specification of the DistanceSimulators
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 // Created by Jorge Castellanos on 6/21/06. Hacked 8/09 by STP.
5 //
6 
7 #include "DistanceSimulator.h"
8 
9 using namespace csl;
10 
11 // DistanceSimulator
12 
14  this->addInput(CSL_INPUT, source);
15  mPosition = new CPoint(1.0, 0.0, 0.0); // If my parent is a simple UGen then I own my Position.
18 }
19 
21  this->addInput(CSL_INPUT, source);
22  mPosition = source.position(); // If my parent is a SpatialSource, then just point to it.
25 }
26 
28  delete mIntensityCue;
29  delete mAirAbsorptionCue;
30 }
31 
32 /// Returns whether the sound source position changed since last block call.
33 
35  return ((SpatialSource *) mInputs[CSL_INPUT]->mUGen)->positionChanged();
36 }
37 
38 // work-horse method
39 
40 void DistanceSimulator::nextBuffer(Buffer & outputBuffer, unsigned outBufNum) throw (CException) {
41  float distance = this->distance(); // I call distance of myself, which in turn points to the source distance.
42  // coded this way, in order to allow UnitGenerators passed to a distance simulator.
43  Port *inPort = mInputs[CSL_INPUT];
44  UnitGenerator *inputSound = inPort->mUGen;
45 
46  inputSound->nextBuffer(outputBuffer); // get its UGen
47 
48 // UnitGenerator *soundSource = ((UGenPort *)mInputs[CSL_INPUT])->mUGen;
49 // soundSource->nextBuffer(outputBuffer);
50 
51  mIntensityCue->compute(distance);
52  mIntensityCue->process(outputBuffer);
53  // Air damping at short distances (< than 10, 15 meters) can be ignored
54  if (distance > 10) {
55  mAirAbsorptionCue->compute(distance);
56  mAirAbsorptionCue->process(outputBuffer);
57  }
58  mPositionChanged = ((SpatialSource *) inputSound)->positionChanged();
59 }
60 
61 // IntensityAttenuationCue
62 
63 void IntensityAttenuationCue::compute(float distance) {
64  mGain = 1 / distance; //(distance * distance);
65 }
66 
68  SampleBuffer outputPtr = aBuffer.buffer(0);
69  unsigned numFrames = aBuffer.mNumFrames;
70 
71  for (unsigned k = 0; k < numFrames; k++) // k loops through sample buffers
72  *outputPtr++ *= mGain;
73 }
74 
75 // AirAbsorptionCue
76 
78  mPrevOutput = 0;
79  mPrevInput = 0;
80 }
81 
83 
84 }
85 
86 void AirAbsorptionCue::compute(float distance) {
87  unsigned frameRate = CGestalt::frameRate();
88  float cutoffFreq = 22000 * 9/distance;
89  float w = 2.0 * frameRate;
90  float Norm;
91 
92  cutoffFreq *= 2.0F * CSL_PI;
93  Norm = 1.0 / (cutoffFreq + w);
94  mBCoeff = (w - cutoffFreq) * Norm;
95  mACoeff[0] = mACoeff[1] = cutoffFreq * Norm;
96 }
97 
98 
100  SampleBuffer outputPtr = aBuffer.buffer(0);
101  unsigned numFrames = aBuffer.mNumFrames;
102 
103  for (unsigned i = 0; i < numFrames; i++) {
104  mPrevOutput = (*outputPtr) * mACoeff[0] + mPrevInput * mACoeff[1] + mPrevOutput * mBCoeff;
105  mPrevInput = (*outputPtr);
106  *outputPtr++ = mPrevOutput;
107  }
108 }
109 
110 // mBCoeff[0] = 1.0 - exp((-CSL_PI) * 2 * cutoffFreq/frameRate);
111 // mACoeff[0] = 1.0 - mBCoeff[0];
112 //
113 
114 // prevOuts[0] = mBCoeff[0] * (*outputPtr) - mACoeff[0] * prevOuts[0]; // put current output sample in the output buffer and increment
115 // *outputPtr++ = prevOuts[0];
116 
117 
118 //AirAbsorptionCue::AirAbsorptionCue() {
119 //
120 //}
121 //
122 //AirAbsorptionCue::~AirAbsorptionCue() {
123 //
124 //}
125 //
126 //void AirAbsorptionCue::compute(float distance) {
127 // unsigned cutoffFreq = 10000;
128 // unsigned frameRate = CGestalt::frameRate();
129 // float C = 1 / (tan (CSL_PI * (cutoffFreq/frameRate)) );
130 //
131 // mBCoeff[0] = 1 / (1 + (CSL_SQRT_TWO * C) + (C * C) );
132 // mBCoeff[1] = 2 * mBCoeff[0];
133 // mBCoeff[2] = mBCoeff[0];
134 // mACoeff[0] = 0.f;
135 // mACoeff[1] = 2 * mBCoeff[0] * (1 - (C * C));
136 // mACoeff[2] = mBCoeff[0] * (1 - (CSL_SQRT_TWO * C) + (C * C) );
137 //}
138 //
139 //
140 //void AirAbsorptionCue::process(Buffer &aBuffer) {
141 // SampleBuffer outputPtr = aBuffer.buffer(0];
142 // unsigned numFrames = aBuffer.mNumFrames;
143 // float *prevOuts = mPrevOutputs;
144 // float *prevIns = mPrevInputs;
145 //
146 // for (unsigned i = 0; i < numFrames; i++) {
147 //
148 // prevOuts[0] = 0.f;
149 // prevIns[0] = *outputPtr; // get next input sample
150 // for (unsigned j = 2; j > 0; j--) {
151 // prevOuts[0] += mBCoeff[j] * prevIns[j];
152 // prevIns[j] = prevIns[j-1];
153 // }
154 // prevOuts[0] += mBCoeff[0] * prevIns[0];
155 // for (unsigned k = 2; k > 0; k--) {
156 // prevOuts[0] += -mACoeff[k] * prevOuts[k];
157 // prevOuts[k] = prevOuts[k-1];
158 // }
159 // *outputPtr++ = prevOuts[0]; // put current output sample in the output buffer and increment
160 // }
161 //
162 //}
sample * SampleBuffer
1-channel buffer data type, vector of (sample)
Definition: CSL_Types.h:194
AirAbsorptionCue * mAirAbsorptionCue
virtual void compute(float distance)
unsigned mNumFrames
num frames used in each buffer
Definition: CSL_Core.h:113
virtual bool positionChanged()
Returns wether the sound source position changed since last block call.
virtual void process(Buffer &aBuffer)
Process each sample in the buffer, applying the filter.
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
virtual void compute(float distance)
Calculate the filter coefficients.
CPoint * mPosition
source position
Definition: SpatialSource.h:71
virtual SampleBuffer buffer(unsigned bufNum)
convenience accessors for sample buffers
Definition: CSL_Core.cpp:66
IntensityAttenuationCue * mIntensityCue
Sound attenuation due to increasing distance. This "Base" Class uses the inverse square law...
virtual void nextBuffer(Buffer &outputBuffer)
get a buffer of Frames – this is the core CSL "pull" function; the given buffer can be written into...
Definition: CSL_Core.cpp:726
Simulates the frequency dependent air absorption using a one pole/zero lowpass filter.
#define CSL_PI
Definition: CSL_Types.h:334
CPoint * position(unsigned channelNum=0)
Returns the distance from the center.
virtual void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
really compute the next buffer given an offset base channel; this is called by nextBuffer, possibly multiple times
Temp Spatial Sound Source.
Definition: SpatialSource.h:29
#define CSL_INPUT
Definition: CSL_Types.h:275
static unsigned frameRate()
default frame rate
Definition: CGestalt.cpp:51
virtual void process(Buffer &aBuffer)
UnitGenerator * mUGen
my unit generator (pointer or NULL)
Definition: CSL_Core.h:320
DistanceSimulator(UnitGenerator &source)
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
Port – used to represent constant, control-rate or signal inputs and outputs in named maps; holds a ...
Definition: CSL_Core.h:312
void addInput(CSL_MAP_KEY name, UnitGenerator &ugen)
Plug in a unit generator to the named input slot.
Definition: CSL_Core.cpp:894
PortMap mInputs
the map of my inputs or controls (used by the mix-in classes)
Definition: CSL_Core.h:378
forward declaration
Definition: CSL_Core.h:241
Base class of CSL exceptions (written upper-case). Has a string message.