Nsound  0.9.4
DelayLine.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: DelayLine.cc 908 2015-07-08 02:04:41Z weegreenblobbie $
4 //
5 // Copyright (c) 2007 Nick Hilton
6 //
7 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
8 //
9 //-----------------------------------------------------------------------------
10 
11 //-----------------------------------------------------------------------------
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //
27 //-----------------------------------------------------------------------------
28 
29 #include <algorithm>
30 
31 #include <Nsound/AudioStream.h>
32 #include <Nsound/Buffer.h>
33 #include <Nsound/DelayLine.h>
34 
35 
36 namespace Nsound
37 {
38 
39 
41 DelayLine(float64 sample_rate, float64 max_delay_in_seconds)
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 }
54 
55 
56 Buffer
58 delay(const Buffer & x, const Buffer & delay_time)
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 }
74 
75 
76 float64
78 delay(float64 x, float64 delay_time)
79 {
80  write(x);
81  return read(delay_time);
82 }
83 
84 
85 void
88 {
89  wr_idx_ = 0;
91  std::fill(buffer_.begin(), buffer_.end(), 0.0);
92 }
93 
94 
95 float64
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 }
113 
114 
115 float64
117 read(float64 delay)
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 }
129 
130 
131 void
134 {
135  buffer_[wr_idx_++] = x;
136 
137  if(wr_idx_ >= buffer_.size())
138  {
139  wr_idx_ = 0;
140  }
141 }
142 
143 
144 } // namespace
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
float64 delay_time_
Definition: DelayLine.h:72
#define M_ASSERT_MSG(expr, message)
Definition: Macros.h:95
double float64
Definition: Nsound.h:146
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
float64 sample_rate_
Definition: DelayLine.h:70
std::vector< float64 > buffer_
Definition: DelayLine.h:74
DelayLine(float64 sample_rate, float64 max_delay_in_seconds)
Definition: DelayLine.cc:41
unsigned long long uint64
Definition: Nsound.h:154
float64 read()
Definition: DelayLine.cc:97
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer delay(const Buffer &x, const Buffer &delay_time)
Definition: DelayLine.cc:58
float64 max_delay_time_
Definition: DelayLine.h:71
void write(float64 x)
Definition: DelayLine.cc:133