Nsound  0.9.4
CircularBuffer.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: CircularBuffer.cc 878 2014-11-23 04:51:23Z weegreenblobbie $
4 //
5 // Copyright (c) 2008 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/CircularBuffer.h>
32 
33 using namespace Nsound;
34 
35 using std::cerr;
36 using std::endl;
37 
40  :
41  buffer_(new Buffer(Buffer::zeros(n_samples))),
42  itor_(new Buffer::circular_iterator(buffer_->cbegin()))
43 {
44 }
45 
48 {
49  delete buffer_;
50  delete itor_;
51 };
52 
53 Buffer
55 read() const
56 {
57  const uint32 N = buffer_->getLength();
58 
60 
61  Buffer b(N);
62 
63  for(uint32 i = 0; i < N; ++i)
64  {
65  b << *ci;
66  ++ci;
67  }
68 
69  return b;
70 }
71 
72 void
75 {
76  *(*itor_) = d;
77  ++(*itor_);
78 }
79 
80 void
82 write(const AudioStream & as)
83 {
84  write(as.getMono()[0]);
85 }
86 
87 void
89 write(const Buffer & src)
90 {
91  const uint32 M = src.getLength();
92 
93  for(uint32 i = 0; i < M; ++i)
94  {
95  *(*itor_) = src[i];
96  ++(*itor_);
97  }
98 }
unsigned int uint32
Definition: Nsound.h:153
A circulator iterator for class Buffer.
Buffer read() const
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
void write(float64 d)
CircularBuffer(uint32 n_samples)
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream getMono() const
Collapses all channels into one Buffer making it mono.
Definition: AudioStream.cc:265
Buffer::circular_iterator * itor_