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

Searches the target Buffer for zero crossings at or after the window_size position. More...

#include <Nsound/BufferWindowSearch.h>

Public Member Functions

 BufferWindowSearch (const Buffer &buffer, uint32 window_size=2048)
 
 BufferWindowSearch (const BufferWindowSearch &copy)
 
 ~BufferWindowSearch ()
 
BufferWindowSearchoperator= (const BufferWindowSearch &rhs)
 
Buffer getNextWindow ()
 Searches the target Buffer for a zero crossing at or after the window_size position. More...
 
uint32 getSamplesLeft () const
 Returns how many samples are left in the target Buffer. More...
 
void reset ()
 Resets the search. More...
 
void setBuffer (const Buffer &buffer)
 Search a different Buffer. More...
 

Protected Attributes

const Buffertarget_buffer_
 
uint32 window_size_
 
uint32 position_
 

Detailed Description

Searches the target Buffer for zero crossings at or after the window_size position.

Definition at line 46 of file BufferWindowSearch.h.

Constructor & Destructor Documentation

BufferWindowSearch::BufferWindowSearch ( const Buffer buffer,
uint32  window_size = 2048 
)

Definition at line 49 of file BufferWindowSearch.cc.

References M_ASSERT_VALUE, and window_size_.

50  :
51  target_buffer_(&target),
52  window_size_(window_size),
53  position_(0)
54 {
56 };
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
BufferWindowSearch::BufferWindowSearch ( const BufferWindowSearch copy)

Definition at line 60 of file BufferWindowSearch.cc.

Nsound::BufferWindowSearch::~BufferWindowSearch ( )
inline

Definition at line 54 of file BufferWindowSearch.h.

54 {};

Member Function Documentation

BufferWindowSearch & BufferWindowSearch::operator= ( const BufferWindowSearch rhs)

Definition at line 71 of file BufferWindowSearch.cc.

References position_, target_buffer_, and window_size_.

72 {
73  if(this == &rhs)
74  {
75  return *this;
76  }
77 
80  position_ = rhs.position_;
81 
82  return *this;
83 };
Buffer BufferWindowSearch::getNextWindow ( )

Searches the target Buffer for a zero crossing at or after the window_size position.

Definition at line 87 of file BufferWindowSearch.cc.

References Nsound::Buffer::getLength(), position_, Nsound::Buffer::subbuffer(), target_buffer_, and window_size_.

Referenced by Nsound::Buffer::getResample().

88 {
89  enum State
90  {
91  INIT,
92  POS,
93  NEG,
94  END,
95  DONE
96  };
97 
98  //~ std::string STRINGS[5] = { "init", "pos", "neg", "end", "done" };
99 
100  State s = INIT;
101 
102  uint32 next_pos = position_ + window_size_;
103 
104  //~ Plotter pylab;
105  //~
106  //~ boolean debug_plot = false;
107  //~
108  //~ if(next_pos == 0)
109  //~ {
110  //~ Buffer x_axis;
111  //~ for(uint32 i = next_pos; i < next_pos + 100; ++i)
112  //~ {
113  //~ x_axis << i;
114  //~ }
115  //~
116  //~ pylab.plot(x_axis, target_buffer_->subbuffer(next_pos, 100));
117  //~
118  //~ pylab.title("window 26303");
119  //~
120  //~ pylab.hold(true);
121  //~
122  //~ debug_plot = true;
123  //~ }
124 
125  //~ cerr << "position_ = " << position_
126  //~ << " start = " << next_pos;
127 
129 
130  // Are we off the end?
131  if(next_pos >= end)
132  {
134  position_ = end;
135  return w;
136  }
137 
138  // DEBUG
139  Buffer x(1);
140  Buffer y(1);
141  x << next_pos;
142  y << (*target_buffer_)[next_pos];
143 
144 
145  // Initalize the state.
146  if((*target_buffer_)[next_pos] > 0.0)
147  {
148  s = POS;
149  //~ if(debug_plot) pylab.plot(x,y, "g+");
150  }
151  else if((*target_buffer_)[next_pos] < 0.0)
152  {
153  s = NEG;
154  //~ if(debug_plot) pylab.plot(x,y, "r+");
155  }
156  else
157  {
158  s = DONE;
159  }
160 
161  //~ cerr << " state = " << STRINGS[s];
162 
163  State last_s = s;
164 
165  while(last_s == s)
166  {
167  last_s = s;
168 
169  ++next_pos;
170 
171  //~ if(next_pos > position_ + window_size_ + 200 ) debug_plot = false;
172  //~
173  //~ // DEBUG
174  //~ x = Buffer(1);
175  //~ y = Buffer(1);
176  //~ x << next_pos;
177  //~ y << (*target_buffer_)[next_pos];
178 
179  // Are we off the end?
180  if(next_pos >= end)
181  {
182  Buffer w = target_buffer_->subbuffer(position_);
183  position_ = end;
184  return w;
185  }
186 
187  // Update state.
188  if((*target_buffer_)[next_pos] > 0.0)
189  {
190  s = POS;
191  //~ if(debug_plot) pylab.plot(x,y, "g+");
192  }
193  else if((*target_buffer_)[next_pos] < 0.0)
194  {
195  s = NEG;
196  //~ if(debug_plot) pylab.plot(x,y, "r+");
197  }
198  else
199  {
200  break;
201  }
202  }
203 
204  // Pick the sample that is closest to zero.
205  if( std::fabs((*target_buffer_)[next_pos-1]) <
206  std::fabs((*target_buffer_)[next_pos]))
207  {
208  --next_pos;
209  }
210 
211  //~ cerr << " state = " << STRINGS[s];
212  //~ cerr << " n_samples_ = " << next_pos - position_ << endl;
213 
214  Buffer w = target_buffer_->subbuffer(position_, next_pos - position_);
215 
216  position_ = next_pos;
217 
218  return w;
219 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
A Buffer for storing audio samples.
Definition: Buffer.h:60
uint32 BufferWindowSearch::getSamplesLeft ( ) const

Returns how many samples are left in the target Buffer.

Definition at line 223 of file BufferWindowSearch.cc.

References Nsound::Buffer::getLength(), position_, and target_buffer_.

Referenced by Nsound::Buffer::getResample().

224 {
225  return target_buffer_->getLength() - position_;
226 }
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
void Nsound::BufferWindowSearch::reset ( )
inline

Resets the search.

Definition at line 69 of file BufferWindowSearch.h.

References position_.

Referenced by setBuffer().

void Nsound::BufferWindowSearch::setBuffer ( const Buffer buffer)
inline

Search a different Buffer.

Definition at line 74 of file BufferWindowSearch.h.

References reset(), and target_buffer_.

75  {
76  target_buffer_ = &buffer;
77  reset();
78  }
void reset()
Resets the search.

Member Data Documentation

const Buffer* Nsound::BufferWindowSearch::target_buffer_
protected

Definition at line 82 of file BufferWindowSearch.h.

Referenced by getNextWindow(), getSamplesLeft(), operator=(), and setBuffer().

uint32 Nsound::BufferWindowSearch::window_size_
protected

Definition at line 83 of file BufferWindowSearch.h.

Referenced by BufferWindowSearch(), getNextWindow(), and operator=().

uint32 Nsound::BufferWindowSearch::position_
protected

Definition at line 84 of file BufferWindowSearch.h.

Referenced by getNextWindow(), getSamplesLeft(), operator=(), and reset().


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