Nsound  0.9.4
FilterDelay.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterDelay.cc 874 2014-09-08 02:21:29Z 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 <Nsound/AudioStream.h>
30 #include <Nsound/Buffer.h>
31 #include <Nsound/FilterDelay.h>
32 #include <Nsound/Plotter.h>
33 
34 #include <cmath>
35 #include <iostream>
36 #include <string.h>
37 
38 using namespace Nsound;
39 
40 using std::cerr;
41 using std::endl;
42 
43 //-----------------------------------------------------------------------------
46  const float64 & sample_rate,
47  const float64 & max_delay_time_seconds)
48  :
49  Filter(sample_rate),
50  buffer_(NULL),
51  buffer_end_ptr_(NULL),
52  read_ptr_(NULL),
53  write_ptr_(NULL),
54  delay_(max_delay_time_seconds),
55  n_samples_(0)
56 {
57  M_ASSERT_VALUE(delay_, >, 0.0);
58 
59  n_samples_ = static_cast<uint32>(std::ceil(sample_rate_ * delay_));
60 
61  ++n_samples_;
62 
63  buffer_ = new float64 [n_samples_];
64 
66 
68 }
69 
70 //-----------------------------------------------------------------------------
72 FilterDelay(const FilterDelay & copy)
73  :
74  Filter(copy.sample_rate_),
75  buffer_(NULL),
76  buffer_end_ptr_(NULL),
77  read_ptr_(NULL),
78  write_ptr_(NULL),
79  delay_(copy.delay_),
80  n_samples_(copy.n_samples_)
81 {
82  buffer_ = new float64 [n_samples_];
83 
85 
86  *this = copy;
87 }
88 
89 //-----------------------------------------------------------------------------
92 {
93  delete [] buffer_;
94 }
95 
98 filter(const AudioStream & x)
99 {
100  return Filter::filter(x);
101 }
102 
105 filter(const AudioStream & x, const float64 & delay)
106 {
107  return Filter::filter(x, delay);
108 }
109 
112 filter(const AudioStream & x, const Buffer & delay)
113 {
114  return Filter::filter(x, delay);
115 }
116 
117 Buffer
119 filter(const Buffer & x)
120 {
121  return Filter::filter(x);
122 }
123 
124 Buffer
126 filter(const Buffer & x, const float64 & delay)
127 {
128  return Filter::filter(x, delay);
129 }
130 
131 Buffer
133 filter(const Buffer & x, const Buffer & delay)
134 {
135  return Filter::filter(x, delay);
136 }
137 
138 float64
140 filter(const float64 & x)
141 {
142  return FilterDelay::filter(x, delay_);
143 
144 }
145 
146 float64
148 filter(const float64 & x, const float64 & delay)
149 {
150  *write_ptr_ = x;
151  ++write_ptr_;
152 
153  // Bounds check
155  {
157  }
158 
159  float64 del = delay;
160 
161  // limit delay
162  if(del > delay_) del = delay_;
163  else if(del < 0.0) del = 0.0;
164 
165  uint32 d = static_cast<uint32>(sample_rate_ * del);
166 
167  read_ptr_ = write_ptr_ - d - 1;
168 
169  // Bounds check.
170  if(read_ptr_ < buffer_)
171  {
173  }
174 
175  float64 y = *read_ptr_;
176 
177  return y;
178 }
179 
180 
182 FilterDelay &
184 operator=(const FilterDelay & rhs)
185 {
186  if(this == &rhs)
187  {
188  return *this;
189  }
190 
191  if(n_samples_ != rhs.n_samples_)
192  {
193  n_samples_ = rhs.n_samples_;
194  delete [] buffer_;
195  buffer_ = new float64 [n_samples_];
197  reset();
198  }
199 
200  delay_ = rhs.delay_;
201  n_samples_ = rhs.n_samples_;
202 
203  // Copy the buffer.
204  memcpy(buffer_, rhs.buffer_, sizeof(float64) * n_samples_);
205 
206  // Set the pointers to the same offsets into the buffer.
207  read_ptr_ = buffer_ + (rhs.read_ptr_ - rhs.buffer_);
208  write_ptr_ = buffer_ + (rhs.write_ptr_ - rhs.buffer_);
209 
210  return *this;
211 }
212 
213 void
216 {
217  // Clear all the memory in the buffer.
218  memset(buffer_, 0, sizeof(float64) * n_samples_);
219 
220  read_ptr_ = buffer_;
222 }
223 
224 
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
FilterDelay & operator=(const FilterDelay &rhs)
Definition: FilterDelay.cc:184
float64 * write_ptr_
Definition: FilterDelay.h:94
double float64
Definition: Nsound.h:146
AudioStream filter(const AudioStream &x)
Definition: FilterDelay.cc:98
float64 * buffer_end_ptr_
Definition: FilterDelay.h:92
Base class for IIR Filters, defines the interface.
Definition: Filter.h:49
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
FilterDelay(const float64 &sample_rate, const float64 &max_delay_time_seconds)
Definition: FilterDelay.cc:45
A Buffer for storing audio samples.
Definition: Buffer.h:60
A class for filtering audio in the frequecy domain.
Definition: FilterDelay.h:47
float64 * read_ptr_
Definition: FilterDelay.h:93
float64 sample_rate_
Definition: Filter.h:113