Arduino STK  4.6.2
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | List of all members
stk::TapDelay Class Reference

STK non-interpolating tapped delay line class. More...

#include <TapDelay.h>

Inheritance diagram for stk::TapDelay:
stk::Filter stk::Stk

Public Member Functions

 TapDelay (std::vector< unsigned long > taps=std::vector< unsigned long >(1, 0), unsigned long maxDelay=4095)
 The default constructor creates a delay-line with maximum length of 4095 samples and a single tap at delay = 0. More...
 
 ~TapDelay ()
 Class destructor.
 
void setMaximumDelay (unsigned long delay)
 Set the maximum delay-line length. More...
 
void setTapDelays (std::vector< unsigned long > taps)
 Set the delay-line tap lengths. More...
 
std::vector< unsigned long > getTapDelays (void) const
 Return the current delay-line length.
 
StkFloat lastOut (unsigned int tap=0) const
 Return the specified tap value of the last computed frame. More...
 
StkFramestick (StkFloat input, StkFrames &outputs)
 Input one sample to the delayline and return outputs at all tap positions. More...
 
StkFramestick (StkFrames &frames, unsigned int channel=0)
 Take a channel of the StkFrames object as inputs to the filter and write outputs back to the same object. More...
 
StkFramestick (StkFrames &iFrames, StkFrames &oFrames, unsigned int iChannel=0)
 Take a channel of the iFrames object as inputs to the filter and write outputs to the oFrames object. More...
 
unsigned int channelsIn (void) const
 Return the number of input channels for the class.
 
unsigned int channelsOut (void) const
 Return the number of output channels for the class.
 
virtual void clear (void)
 Clears all internal states of the filter.
 
void setGain (StkFloat gain)
 Set the filter gain. More...
 
StkFloat getGain (void) const
 Return the current filter gain.
 
StkFloat phaseDelay (StkFloat frequency)
 Return the filter phase delay at the specified frequency. More...
 
const StkFrameslastFrame (void) const
 Return an StkFrames reference to the last output sample frame.
 
void ignoreSampleRateChange (bool ignore=true)
 A function to enable/disable the automatic updating of class data when the STK sample rate changes. More...
 

Static Public Member Functions

static StkFloat sampleRate (void)
 Static method that returns the current STK sample rate.
 
static void setSampleRate (StkFloat rate)
 Static method that sets the STK sample rate. More...
 
static void clear_alertList ()
 Static method that frees memory from alertList_.
 
static std::string rawwavePath (void)
 Static method that returns the current rawwave path.
 
static void setRawwavePath (std::string path)
 Static method that sets the STK rawwave path.
 
static void swap16 (unsigned char *ptr)
 Static method that byte-swaps a 16-bit data type.
 
static void swap32 (unsigned char *ptr)
 Static method that byte-swaps a 32-bit data type.
 
static void swap64 (unsigned char *ptr)
 Static method that byte-swaps a 64-bit data type.
 
static void sleep (unsigned long milliseconds)
 Static cross-platform method to sleep for a number of milliseconds.
 
static bool inRange (StkFloat value, StkFloat min, StkFloat max)
 Static method to check whether a value is within a specified range.
 
static void handleError (const char *message, StkError::Type type)
 Static function for error reporting and handling using c-strings.
 
static void handleError (std::string message, StkError::Type type)
 Static function for error reporting and handling using c++ strings.
 
static void showWarnings (bool status)
 Toggle display of WARNING and STATUS messages.
 
static void printErrors (bool status)
 Toggle display of error messages before throwing exceptions.
 

Static Public Attributes

static const StkFormat STK_SINT8
 
static const StkFormat STK_SINT16
 
static const StkFormat STK_SINT24
 
static const StkFormat STK_SINT32
 
static const StkFormat STK_FLOAT32
 
static const StkFormat STK_FLOAT64
 

Protected Member Functions

void handleError (StkError::Type type) const
 Internal function for error reporting that assumes message in oStream_ variable.
 
virtual void sampleRateChanged (StkFloat newRate, StkFloat oldRate)
 This function should be implemented in subclasses that depend on the sample rate.
 
void addSampleRateAlert (Stk *ptr)
 Add class pointer to list for sample rate change notification.
 
void removeSampleRateAlert (Stk *ptr)
 Remove class pointer from list for sample rate change notification.
 

Detailed Description

STK non-interpolating tapped delay line class.

This class implements a non-interpolating digital delay-line with an arbitrary number of output "taps". If the maximum length and tap delays are not specified during instantiation, a fixed maximum length of 4095 and a single tap delay of zero is set.

A non-interpolating delay line is typically used in fixed delay-length applications, such as for reverberation.

by Perry R. Cook and Gary P. Scavone, 1995–2019.

Constructor & Destructor Documentation

◆ TapDelay()

stk::TapDelay::TapDelay ( std::vector< unsigned long >  taps = std::vector< unsigned long >(1, 0),
unsigned long  maxDelay = 4095 
)

The default constructor creates a delay-line with maximum length of 4095 samples and a single tap at delay = 0.

An StkError will be thrown if any tap delay parameter is less than zero, the maximum delay parameter is less than one, or any tap delay parameter is greater than the maxDelay value.

Member Function Documentation

◆ setMaximumDelay()

void stk::TapDelay::setMaximumDelay ( unsigned long  delay)

Set the maximum delay-line length.

This method should generally only be used during initial setup of the delay line. If it is used between calls to the tick() function, without a call to clear(), a signal discontinuity will likely occur. If the current maximum length is greater than the new length, no change will be made.

◆ setTapDelays()

void stk::TapDelay::setTapDelays ( std::vector< unsigned long >  taps)

Set the delay-line tap lengths.

The valid range for each tap length is from 0 to the maximum delay-line length.

◆ lastOut()

StkFloat stk::TapDelay::lastOut ( unsigned int  tap = 0) const
inline

Return the specified tap value of the last computed frame.

Use the lastFrame() function to get all tap values from the last computed frame. The tap argument must be less than the number of delayline taps (the first tap is specified by 0). However, range checking is only performed if STK_DEBUG is defined during compilation, in which case an out-of-range value will trigger an StkError exception.

116 {
117 #if defined(_STK_DEBUG_)
118  if ( tap >= lastFrame_.size() ) {
119  oStream_ << "TapDelay::lastOut(): tap argument and number of taps are incompatible!";
120  handleError( StkError::FUNCTION_ARGUMENT );
121  }
122 #endif
123 
124  return lastFrame_[tap];
125 }
size_t size() const
Returns the total number of audio samples represented by the object.
Definition: Stk.h:373
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.

◆ tick() [1/3]

StkFrames & stk::TapDelay::tick ( StkFloat  input,
StkFrames outputs 
)
inline

Input one sample to the delayline and return outputs at all tap positions.

The StkFrames argument reference is returned. The output values are ordered according to the tap positions set using the setTapDelays() function (no sorting is performed). The StkFrames argument must contain at least as many channels as the number of taps. However, range checking is only performed if STK_DEBUG is defined during compilation, in which case an out-of-range value will trigger an StkError exception.

128 {
129 #if defined(_STK_DEBUG_)
130  if ( outputs.channels() < outPoint_.size() ) {
131  oStream_ << "TapDelay::tick(): number of taps > channels in StkFrames argument!";
132  handleError( StkError::FUNCTION_ARGUMENT );
133  }
134 #endif
135 
136  inputs_[inPoint_++] = input * gain_;
137 
138  // Check for end condition
139  if ( inPoint_ == inputs_.size() )
140  inPoint_ = 0;
141 
142  // Read out next values
143  StkFloat *outs = &outputs[0];
144  for ( unsigned int i=0; i<outPoint_.size(); i++ ) {
145  *outs++ = inputs_[outPoint_[i]];
146  lastFrame_[i] = *outs;
147  if ( ++outPoint_[i] == inputs_.size() )
148  outPoint_[i] = 0;
149  }
150 
151  return outputs;
152 }

◆ tick() [2/3]

StkFrames & stk::TapDelay::tick ( StkFrames frames,
unsigned int  channel = 0 
)
inlinevirtual

Take a channel of the StkFrames object as inputs to the filter and write outputs back to the same object.

The StkFrames argument reference is returned. The output values are ordered according to the tap positions set using the setTapDelays() function (no sorting is performed). The StkFrames argument must contain at least as many channels as the number of taps. However, range checking is only performed if STK_DEBUG is defined during compilation, in which case an out-of-range value will trigger an StkError exception.

Implements stk::Filter.

155 {
156 #if defined(_STK_DEBUG_)
157  if ( channel >= frames.channels() ) {
158  oStream_ << "TapDelay::tick(): channel and StkFrames arguments are incompatible!";
159  handleError( StkError::FUNCTION_ARGUMENT );
160  }
161  if ( frames.channels() < outPoint_.size() ) {
162  oStream_ << "TapDelay::tick(): number of taps > channels in StkFrames argument!";
163  handleError( StkError::FUNCTION_ARGUMENT );
164  }
165 #endif
166 
167  StkFloat *iSamples = &frames[channel];
168  StkFloat *oSamples = &frames[0];
169  size_t j;
170  unsigned int iHop = frames.channels();
171  size_t oHop = frames.channels() - outPoint_.size();
172  for ( unsigned long i=0; i<frames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
173  inputs_[inPoint_++] = *iSamples * gain_;
174  if ( inPoint_ == inputs_.size() ) inPoint_ = 0;
175  for ( j=0; j<outPoint_.size(); j++ ) {
176  *oSamples++ = inputs_[outPoint_[j]];
177  if ( ++outPoint_[j] == inputs_.size() ) outPoint_[j] = 0;
178  }
179  }
180 
181  oSamples -= frames.channels();
182  for ( j=0; j<outPoint_.size(); j++ ) lastFrame_[j] = *oSamples++;
183  return frames;
184 }
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:415

◆ tick() [3/3]

StkFrames & stk::TapDelay::tick ( StkFrames iFrames,
StkFrames oFrames,
unsigned int  iChannel = 0 
)
inline

Take a channel of the iFrames object as inputs to the filter and write outputs to the oFrames object.

The iFrames object reference is returned. The output values are ordered according to the tap positions set using the setTapDelays() function (no sorting is performed). The iChannel argument must be less than the number of channels in the iFrames argument (the first channel is specified by 0). The oFrames argument must contain at least as many channels as the number of taps. However, range checking is only performed if STK_DEBUG is defined during compilation, in which case an out-of-range value will trigger an StkError exception.

187 {
188 #if defined(_STK_DEBUG_)
189  if ( iChannel >= iFrames.channels() ) {
190  oStream_ << "TapDelay::tick(): channel and StkFrames arguments are incompatible!";
191  handleError( StkError::FUNCTION_ARGUMENT );
192  }
193  if ( oFrames.channels() < outPoint_.size() ) {
194  oStream_ << "TapDelay::tick(): number of taps > channels in output StkFrames argument!";
195  handleError( StkError::FUNCTION_ARGUMENT );
196  }
197 #endif
198 
199  StkFloat *iSamples = &iFrames[iChannel];
200  StkFloat *oSamples = &oFrames[0];
201  size_t j;
202  unsigned int iHop = iFrames.channels();
203  size_t oHop = oFrames.channels() - outPoint_.size();
204  for ( unsigned long i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
205  inputs_[inPoint_++] = *iSamples * gain_;
206  if ( inPoint_ == inputs_.size() ) inPoint_ = 0;
207  for ( j=0; j<outPoint_.size(); j++ ) {
208  *oSamples++ = inputs_[outPoint_[j]];
209  if ( ++outPoint_[j] == inputs_.size() ) outPoint_[j] = 0;
210  }
211  }
212 
213  oSamples -= oFrames.channels();
214  for ( j=0; j<outPoint_.size(); j++ ) lastFrame_[j] = *oSamples++;
215  return iFrames;
216 }

◆ setGain()

void stk::Filter::setGain ( StkFloat  gain)
inlineinherited

Set the filter gain.

The gain is applied at the filter input and does not affect the coefficient values. The default gain value is 1.0.

42 { gain_ = gain; };

◆ phaseDelay()

StkFloat stk::Filter::phaseDelay ( StkFloat  frequency)
inlineinherited

Return the filter phase delay at the specified frequency.

Note that the phase delay calculation accounts for the filter gain. The frequency value should be greater than 0.0 and less than or equal to one-half the sample rate.

94 {
95  if ( frequency <= 0.0 || frequency > 0.5 * Stk::sampleRate() ) {
96  oStream_ << "Filter::phaseDelay: argument (" << frequency << ") is out of range!";
97  handleError( StkError::WARNING ); return 0.0;
98  }
99 
100  StkFloat omegaT = 2 * STK_PI * frequency / Stk::sampleRate();
101  StkFloat real = 0.0, imag = 0.0;
102  for ( unsigned int i=0; i<b_.size(); i++ ) {
103  real += b_[i] * std::cos( i * omegaT );
104  imag -= b_[i] * std::sin( i * omegaT );
105  }
106  real *= gain_;
107  imag *= gain_;
108 
109  StkFloat phase = atan2( imag, real );
110 
111  real = 0.0, imag = 0.0;
112  for ( unsigned int i=0; i<a_.size(); i++ ) {
113  real += a_[i] * std::cos( i * omegaT );
114  imag -= a_[i] * std::sin( i * omegaT );
115  }
116 
117  phase -= std::atan2( imag, real );
118  phase = std::fmod( -phase, 2 * STK_PI );
119  return phase / omegaT;
120 }
static StkFloat sampleRate(void)
Static method that returns the current STK sample rate.
Definition: Stk.h:156

◆ setSampleRate()

static void stk::Stk::setSampleRate ( StkFloat  rate)
staticinherited

Static method that sets the STK sample rate.

The sample rate set using this method is queried by all STK classes that depend on its value. It is initialized to the default SRATE set in Stk.h. Many STK classes use the sample rate during instantiation. Therefore, if you wish to use a rate that is different from the default rate, it is imperative that it be set BEFORE STK objects are instantiated. A few classes that make use of the global STK sample rate are automatically notified when the rate changes so that internal class data can be appropriately updated. However, this has not been fully implemented. Specifically, classes that appropriately update their own data when either a setFrequency() or noteOn() function is called do not currently receive the automatic notification of rate change. If the user wants a specific class instance to ignore such notifications, perhaps in a multi-rate context, the function Stk::ignoreSampleRateChange() should be called.

◆ ignoreSampleRateChange()

void stk::Stk::ignoreSampleRateChange ( bool  ignore = true)
inlineinherited

A function to enable/disable the automatic updating of class data when the STK sample rate changes.

This function allows the user to enable or disable class data updates in response to global sample rate changes on a class by class basis.

184 { ignoreSampleRateChange_ = ignore; };

Member Data Documentation

◆ STK_SINT8

const StkFormat stk::Stk::STK_SINT8
staticinherited

-128 to +127

◆ STK_SINT16

const StkFormat stk::Stk::STK_SINT16
staticinherited

-32768 to +32767

◆ STK_SINT24

const StkFormat stk::Stk::STK_SINT24
staticinherited

Lower 3 bytes of 32-bit signed integer.

◆ STK_SINT32

const StkFormat stk::Stk::STK_SINT32
staticinherited

-2147483648 to +2147483647.

◆ STK_FLOAT32

const StkFormat stk::Stk::STK_FLOAT32
staticinherited

Normalized between plus/minus 1.0.

◆ STK_FLOAT64

const StkFormat stk::Stk::STK_FLOAT64
staticinherited

Normalized between plus/minus 1.0.


The documentation for this class was generated from the following file: