Nsound  0.9.4
Public Member Functions | Protected Attributes | Private Member Functions | List of all members
Nsound::DelayLine Class Reference

#include <Nsound/DelayLine.h>

Public Member Functions

 DelayLine (float64 sample_rate, float64 max_delay_in_seconds)
 
void setRealtime (bool flag)
 
Buffer delay (const Buffer &x, const Buffer &delay_time)
 
float64 delay (float64 x, float64 delay_time)
 
float64 read ()
 
float64 read (float64 delay)
 
void write (float64 x)
 
void reset ()
 

Protected Attributes

float64 sample_rate_
 
float64 max_delay_time_
 
float64 delay_time_
 
std::vector< float64buffer_
 
uint32 wr_idx_
 
bool is_realtime_
 

Private Member Functions

 DelayLine (const DelayLine &copy)
 
DelayLineoperator= (const DelayLine &rhs)
 

Detailed Description

Definition at line 48 of file DelayLine.h.

Constructor & Destructor Documentation

Nsound::DelayLine::DelayLine ( float64  sample_rate,
float64  max_delay_in_seconds 
)

Definition at line 41 of file DelayLine.cc.

References M_ASSERT_VALUE.

42  :
43  sample_rate_(sample_rate),
44  max_delay_time_(max_delay_in_seconds),
45  delay_time_(max_delay_in_seconds),
46  buffer_(
47  static_cast<uint64>(sample_rate * max_delay_in_seconds + 1),
48  0.0),
49  wr_idx_(0)
50 {
51  M_ASSERT_VALUE(sample_rate, >, 0.0);
52  M_ASSERT_VALUE(max_delay_in_seconds, >, 0.0);
53 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
float64 delay_time_
Definition: DelayLine.h:72
float64 sample_rate_
Definition: DelayLine.h:70
std::vector< float64 > buffer_
Definition: DelayLine.h:74
float64 max_delay_time_
Definition: DelayLine.h:71
Nsound::DelayLine::DelayLine ( const DelayLine copy)
private

Member Function Documentation

void Nsound::DelayLine::setRealtime ( bool  flag)
inline

Definition at line 55 of file DelayLine.h.

References is_realtime_.

55 {is_realtime_ = flag;}
Buffer Nsound::DelayLine::delay ( const Buffer x,
const Buffer delay_time 
)

Definition at line 58 of file DelayLine.cc.

References Nsound::Buffer::cbegin(), is_realtime_, and reset().

Referenced by main(), and read().

59 {
60  if(!is_realtime_) reset();
61 
62  Buffer y;
63 
64  auto dt = delay_time.cbegin();
65 
66  for(auto sample : x)
67  {
68  y << delay(sample, *dt);
69  ++dt;
70  }
71 
72  return y;
73 }
Buffer delay(const Buffer &x, const Buffer &delay_time)
Definition: DelayLine.cc:58
float64 Nsound::DelayLine::delay ( float64  x,
float64  delay_time 
)

Definition at line 78 of file DelayLine.cc.

References read(), and write().

79 {
80  write(x);
81  return read(delay_time);
82 }
float64 read()
Definition: DelayLine.cc:97
void write(float64 x)
Definition: DelayLine.cc:133
float64 Nsound::DelayLine::read ( )

Definition at line 97 of file DelayLine.cc.

References buffer_, delay_time_, M_ASSERT_MSG, max_delay_time_, sample_rate_, and wr_idx_.

Referenced by delay(), DelayLine_UnitTest(), Nsound::Pluck::generate(), Nsound::GuitarBass::play(), Nsound::Clarinet::play(), Nsound::FluteSlide::play(), and read().

98 {
101  "delay time exceeds maximum ("
102  << delay_time_ << " > " << max_delay_time_ << ")");
103 
104  uint32 offset = static_cast<uint32>(sample_rate_ * delay_time_ + 0.5);
105 
106  if(offset > wr_idx_)
107  {
108  return buffer_[buffer_.size() - (offset - wr_idx_)];
109  }
110 
111  return buffer_[wr_idx_ - offset];
112 }
unsigned int uint32
Definition: Nsound.h:153
float64 delay_time_
Definition: DelayLine.h:72
#define M_ASSERT_MSG(expr, message)
Definition: Macros.h:95
float64 sample_rate_
Definition: DelayLine.h:70
std::vector< float64 > buffer_
Definition: DelayLine.h:74
float64 max_delay_time_
Definition: DelayLine.h:71
float64 Nsound::DelayLine::read ( float64  delay)

Definition at line 117 of file DelayLine.cc.

References delay(), delay_time_, M_ASSERT_MSG, max_delay_time_, and read().

118 {
119  M_ASSERT_MSG(delay > 0.0, "delay must be > 0.0, got " << delay);
120 
121  delay_time_ = delay;
122 
123  // limit
124 
126 
127  return read();
128 }
float64 delay_time_
Definition: DelayLine.h:72
#define M_ASSERT_MSG(expr, message)
Definition: Macros.h:95
float64 read()
Definition: DelayLine.cc:97
Buffer delay(const Buffer &x, const Buffer &delay_time)
Definition: DelayLine.cc:58
float64 max_delay_time_
Definition: DelayLine.h:71
void Nsound::DelayLine::write ( float64  x)

Definition at line 133 of file DelayLine.cc.

References buffer_, and wr_idx_.

Referenced by delay(), DelayLine_UnitTest(), Nsound::Pluck::generate(), Nsound::GuitarBass::play(), Nsound::Clarinet::play(), and Nsound::FluteSlide::play().

134 {
135  buffer_[wr_idx_++] = x;
136 
137  if(wr_idx_ >= buffer_.size())
138  {
139  wr_idx_ = 0;
140  }
141 }
std::vector< float64 > buffer_
Definition: DelayLine.h:74
void Nsound::DelayLine::reset ( )

Definition at line 87 of file DelayLine.cc.

References buffer_, delay_time_, max_delay_time_, and wr_idx_.

Referenced by delay().

88 {
89  wr_idx_ = 0;
91  std::fill(buffer_.begin(), buffer_.end(), 0.0);
92 }
float64 delay_time_
Definition: DelayLine.h:72
std::vector< float64 > buffer_
Definition: DelayLine.h:74
float64 max_delay_time_
Definition: DelayLine.h:71
DelayLine& Nsound::DelayLine::operator= ( const DelayLine rhs)
private

Member Data Documentation

float64 Nsound::DelayLine::sample_rate_
protected

Definition at line 70 of file DelayLine.h.

Referenced by read().

float64 Nsound::DelayLine::max_delay_time_
protected

Definition at line 71 of file DelayLine.h.

Referenced by read(), and reset().

float64 Nsound::DelayLine::delay_time_
protected

Definition at line 72 of file DelayLine.h.

Referenced by read(), and reset().

std::vector<float64> Nsound::DelayLine::buffer_
protected

Definition at line 74 of file DelayLine.h.

Referenced by read(), reset(), and write().

uint32 Nsound::DelayLine::wr_idx_
protected

Definition at line 76 of file DelayLine.h.

Referenced by read(), reset(), and write().

bool Nsound::DelayLine::is_realtime_
protected

Definition at line 78 of file DelayLine.h.

Referenced by delay(), and setRealtime().


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