Nsound  0.9.4
Macros | Functions | Variables
test_mixer.cc File Reference
#include <Nsound/AudioStream.h>
#include <Nsound/Buffer.h>
#include <Nsound/Mixer.h>
#include <Nsound/Nsound.h>
#include <Nsound/Sine.h>
#include <Nsound/TicToc.h>
#include <Nsound/Wavefile.h>
#include "Test.h"
#include <stdlib.h>
#include <iostream>
Include dependency graph for test_mixer.cc:

Go to the source code of this file.

Macros

#define THIS_TEST_HEADER
 

Functions

void testRange (const Buffer &buffer, uint32 start_index, uint32 n_samples, float64 value, unsigned int line)
 
int main (int argc, char **argv)
 

Variables

static const char * THIS_FILE = "test_mixer.cc"
 

Macro Definition Documentation

#define THIS_TEST_HEADER

Definition at line 51 of file test_mixer.cc.

Function Documentation

void testRange ( const Buffer buffer,
uint32  start_index,
uint32  n_samples,
float64  value,
unsigned int  line 
)

Definition at line 54 of file test_mixer.cc.

References TEST_HEADER2.

Referenced by main().

60 {
61  for(uint32 i = start_index; i < start_index + n_samples; ++i)
62  {
63  if(buffer[i] != value)
64  {
65  cerr << endl
66  << TEST_HEADER2(line)
67  << "sample should be "
68  << value
69  << " found "
70  << buffer[i]
71  << ", FAILURE"
72  << endl
73  << flush;
74  exit(1);
75  }
76  }
77 }
unsigned int uint32
Definition: Nsound.h:153
#define TEST_HEADER2(line)
Definition: Test.h:37
int main ( int  argc,
char **  argv 
)

Definition at line 82 of file test_mixer.cc.

References Nsound::Mixer::add(), Nsound::Mixer::clear(), Nsound::Generator::drawLine(), Nsound::AudioStream::getLength(), Nsound::Mixer::getStream(), Nsound::Mixer::size(), TEST_ERROR_HEADER, TEST_HEADER, TEST_HEADER2, testRange(), Nsound::Tic(), and Nsound::Toc().

83 {
84  cout << "////////////////////////////////////////////////////////////"
85  << endl
86  << "// Testing class Nsound::Mixer"
87  << endl
88  << "////////////////////////////////////////////////////////////"
89  << endl
90  << flush;
91 
92  Tic();
93  cout << TEST_HEADER
94  << "Mixer::add(), Mixer::getStream() ... " << flush;
95 
96  uint32 SAMPLES_PER_SECOND = 10;
97 
98  Sine sine(SAMPLES_PER_SECOND);
99 
100  AudioStream as1(SAMPLES_PER_SECOND, 1);
101 
102  // Draws 1 second (1000 samples) of samples at 1.0.
103  as1 << sine.drawLine(1.0,1.0,1.0);
104 
105  Mixer mixer;
106 
107  // Insert stream at time 0, 30 beats per minute.
108  mixer.add(0.0, 30.0, as1);
109 
110  // Grab 4.0 seconds of the resulting stream.
111 
112  AudioStream result;
113 
114  result = mixer.getStream(4.0);
115 
116  if(result.getLength() != 4.0 * (1.0 * SAMPLES_PER_SECOND))
117  {
118  cerr << endl
119  << TEST_HEADER2(__LINE__)
120  << "result has incorrect length! FAILURE"
121  << endl
122  << flush;
123  exit(1);
124  }
125 
126  // Should produce
127  //
128  // 10 samples at 1.0
129  // 10 samples at 0.0
130  // 10 samples at 1.0
131  // 10 samples at 0.0
132 
133  uint32 samples = 10;
134 
135  testRange(result[0], 0, 10, 1.0, __LINE__);
136  testRange(result[0], 10, 10, 0.0, __LINE__);
137  testRange(result[0], 20, 10, 1.0, __LINE__);
138  testRange(result[0], 30, 10, 0.0, __LINE__);
139 
140  cout << Toc() << " seconds: SUCCESS"
141  << endl
142  << TEST_HEADER
143  << "Mixer::add(), Mixer::getStream() ... " << flush;
144 
145  // Add as1 again.
146  mixer.add(0.0, 30.0, as1);
147 
148  result = mixer.getStream(4.0);
149 
150  samples = 10;
151  // This should produce:
152  //
153  // 10 samples at 2.0
154  // 10 samples at 0.0
155  // 10 samples at 2.0
156  // 10 samples at 0.0
157 
158  testRange(result[0], 0, 10, 2.0, __LINE__);
159  testRange(result[0], 10, 10, 0.0, __LINE__);
160  testRange(result[0], 20, 10, 2.0, __LINE__);
161  testRange(result[0], 30, 10, 0.0, __LINE__);
162 
163  cout << Toc() << " seconds: SUCCESS"
164  << endl
165  << TEST_HEADER
166  << "Mixer::add(), Mixer::getStream() with offset ... " << flush;
167 
168  result = mixer.getStream(0.5, 4.5);
169 
170  if(result.getLength() != 4.0 * (1.0 * SAMPLES_PER_SECOND))
171  {
172  cerr << endl
173  << TEST_HEADER2(__LINE__)
174  << "result has incorrect length! FAILURE"
175  << endl
176  << flush;
177  exit(1);
178  }
179 
180  samples = 5;
181 
182  // This should produce:
183  //
184  // 5 samples at 2.0
185  // 10 samples at 0.0
186  // 10 samples at 2.0
187  // 10 samples at 0.0
188  // 5 samples at 2.0
189 
190  testRange(result[0], 0, 5, 2.0, __LINE__);
191  testRange(result[0], 5, 10, 0.0, __LINE__);
192  testRange(result[0], 15, 10, 2.0, __LINE__);
193  testRange(result[0], 25, 10, 0.0, __LINE__);
194  testRange(result[0], 35, 5, 2.0, __LINE__);
195 
196  cout << Toc() << " seconds: SUCCESS"
197  << endl
198  << TEST_HEADER
199  << "Mixer::add(), Mixer::clear() ... " << flush;
200 
201  mixer.clear();
202 
203  if(mixer.size() != 0)
204  {
205  cerr << TEST_ERROR_HEADER
206  << "mixer.size() should be 0.0, found "
207  << mixer.size()
208  << endl;
209  exit(1);
210  }
211 
212  cout << Toc() << " seconds: SUCCESS"
213  << endl
214  << TEST_HEADER
215  << "Mixer::add(), Mixer::add() with different BPM ... " << flush;
216 
217  // Insert the AudioStream at different bpm.
218 
219  mixer.add(0.0, 30.0, as1);
220  mixer.add(0.5, 30.0, as1);
221 
222  result = mixer.getStream(4.0);
223 
224  // result[0].plot("result:234");
225 
226  samples = 5;
227 
228  // This should produce:
229  //
230  // 5 samples at 1.0
231  // 5 samples at 2.0
232  // 5 samples at 1.0
233  // 5 samples at 0.0
234  // 5 samples at 1.0
235  // 5 samples at 2.0
236  // 5 samples at 1.0
237  // 5 samples at 0.0
238 
239  testRange(result[0], 0, 5, 1.0, __LINE__);
240  testRange(result[0], 5, 5, 2.0, __LINE__);
241  testRange(result[0], 10, 5, 1.0, __LINE__);
242  testRange(result[0], 15, 5, 0.0, __LINE__);
243  testRange(result[0], 20, 5, 1.0, __LINE__);
244  testRange(result[0], 25, 5, 2.0, __LINE__);
245  testRange(result[0], 30, 5, 1.0, __LINE__);
246  testRange(result[0], 35, 5, 0.0, __LINE__);
247 
248  cout << Toc() << " seconds: SUCCESS"
249  << endl
250  << TEST_HEADER
251  << "Mixer::add(), Mixer::getStream() with offset ... " << flush;
252 
253  result = mixer.getStream(0.5, 4.5);
254 
255  // result[0].plot("result:266");
256 
257  if(result.getLength() != 4.0 * (1.0 * SAMPLES_PER_SECOND))
258  {
259  cerr << endl
260  << TEST_HEADER2(__LINE__)
261  << "result has incorrect length! FAILURE"
262  << endl
263  << flush;
264  exit(1);
265  }
266 
267  samples = 5;
268 
269  // This should produce:
270  //
271  // 5 samples at 2.0
272  // 5 samples at 1.0
273  // 5 samples at 0.0
274  // 5 samples at 1.0
275  // 5 samples at 2.0
276  // 5 samples at 1.0
277  // 5 samples at 0.0
278  // 5 samples at 1.0
279 
280  testRange(result[0], 0, 5, 2.0, __LINE__);
281  testRange(result[0], 5, 5, 1.0, __LINE__);
282  testRange(result[0], 10, 5, 0.0, __LINE__);
283  testRange(result[0], 15, 5, 1.0, __LINE__);
284  testRange(result[0], 20, 5, 2.0, __LINE__);
285  testRange(result[0], 25, 5, 1.0, __LINE__);
286  testRange(result[0], 30, 5, 0.0, __LINE__);
287  testRange(result[0], 35, 5, 1.0, __LINE__);
288 
289  cout << Toc() << " seconds: SUCCESS"
290  << endl
291  << TEST_HEADER
292  << "Mixer::add(), Mixer::getStream() with zero bpm ... " << flush;
293 
294  AudioStream as2(SAMPLES_PER_SECOND, 1);
295 
296  as2 << sine.drawLine(0.5,3.0,3.0);
297 
298  mixer.add(1.5, 0.0, as2);
299 
300  result = mixer.getStream(0.5, 4.5);
301 
302  // result[0].plot("result: 314");
303 
304  // Plotter::show();
305 
306  samples = 5;
307 
308  // This should produce:
309  //
310  // 5 samples at 2.0
311  // 5 samples at 1.0
312  // 5 samples at 3.0
313  // 5 samples at 1.0
314  // 5 samples at 2.0
315  // 5 samples at 1.0
316  // 5 samples at 0.0
317  // 5 samples at 1.0
318 
319  testRange(result[0], 0, 5, 2.0, __LINE__);
320  testRange(result[0], 5, 5, 1.0, __LINE__);
321  testRange(result[0], 10, 5, 3.0, __LINE__);
322  testRange(result[0], 15, 5, 1.0, __LINE__);
323  testRange(result[0], 20, 5, 2.0, __LINE__);
324  testRange(result[0], 25, 5, 1.0, __LINE__);
325  testRange(result[0], 30, 5, 0.0, __LINE__);
326  testRange(result[0], 35, 5, 1.0, __LINE__);
327 
328  cout << Toc() << " seconds: SUCCESS"
329  << endl;
330 
331  return 0;
332 }
unsigned int uint32
Definition: Nsound.h:153
void add(float64 first_beat_time, float64 beats_per_minute, const AudioStream &audio_stream)
This method inserts the AudioStream to the Mixer's LinkList.
Definition: Mixer.cc:59
uint32 getLength() const
Returns the number of samples of audio data in the stream.
Definition: AudioStream.cc:197
#define TEST_HEADER
Definition: Test.h:45
This class enables easy scheduling and mixing of multiple AudioStreams.
Definition: Mixer.h:63
#define TEST_HEADER2(line)
Definition: Test.h:37
#define TEST_ERROR_HEADER
Definition: Test.h:49
AudioStream getStream(float64 end_time)
This method returns one AudioStream composed of all AudioStreams stored in the Mixer's LinkList...
Definition: Mixer.cc:91
void testRange(const Buffer &buffer, uint32 start_index, uint32 n_samples, float64 value, unsigned int line)
Definition: test_mixer.cc:54
void clear()
This method removes all streams from the mixer.
Definition: Mixer.cc:84
Nsound::float64 Toc()
Definition: TicToc.cc:42
void Tic()
Definition: TicToc.cc:37
uint32 size() const
This method returns the number of stream stored in it.
Definition: Mixer.h:124
DOXME.
Definition: Sine.h:43

Variable Documentation

const char* THIS_FILE = "test_mixer.cc"
static

Definition at line 49 of file test_mixer.cc.