Nsound  0.9.4
Sawtooth.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Sawtooth.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2004-2006 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/Buffer.h>
30 #include <Nsound/Sawtooth.h>
31 #include <Nsound/Sine.h>
32 
33 #include <cmath>
34 #include <cstring>
35 #include <cstdlib>
36 
37 using namespace Nsound;
38 
39 //-----------------------------------------------------------------------------
41 Sawtooth(const float64 & sample_rate, const int32 n_harmonics)
42  : Generator(sample_rate)
43 {
44 
45  // From wikipedia's definition of a sawtooth wave.
46 
47  float64 Nf = static_cast<float64>(std::abs(n_harmonics));
48 
49  if(Nf < 1.0) Nf = 1.0;
50 
51  Buffer waveform = Buffer::zeros(static_cast<uint32>(sample_rate_));
52 
53  for(float64 k = 1.0; k <= Nf; k += 1.0)
54  {
55  waveform += drawSine(1.0, k) / k;
56  }
57 
58  waveform *= 2.0 / M_PI;
59 
60  ctor(sample_rate, waveform);
61 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
virtual void ctor(const float64 &sample_rate)
DOXME.
Definition: Generator.cc:201
#define M_PI
Definition: Nsound.h:121
static Buffer zeros(const uint32 n_samples)
Returns a Buffer full of zeros of length n_samples.
Definition: Buffer.cc:2265
double float64
Definition: Nsound.h:146
Buffer drawSine(const float64 &duration, const float64 &frequency)
This method draws a static sine wave.
Definition: Generator.cc:544
Sawtooth(const float64 &sample_rate, const int32 n_harmonics=100)
DOXME.
Definition: Sawtooth.cc:41
A Buffer for storing audio samples.
Definition: Buffer.h:60
signed int int32
Definition: Nsound.h:142
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50