Nsound  0.9.4
Square.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Square.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 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/Generator.h>
31 #include <Nsound/Square.h>
32 
33 #include <cmath>
34 #include <cstring>
35 #include <cstdlib>
36 
37 using namespace Nsound;
38 
39 //-----------------------------------------------------------------------------
40 Buffer
42  const float64 & sample_rate,
43  const float64 & percent_lambda_1,
44  const float64 & amplitude_1,
45  const float64 & percent_lambda_2,
46  const float64 & percent_lambda_3,
47  const float64 & amplitude_3,
48  const float64 & percent_lambda_4)
49 {
50  Buffer waveform;
51 
52  // 1----2
53  // | |
54  // | |
55  // | |
56  // --1 2--3 4--5
57  // | |
58  // | |
59  // | |
60  // 3----4
61  //
62  // |-----lambda------|
63 
64  // Draw the 5 lines
65  Generator line(sample_rate);
66 
67  // Line 1
68  waveform << line.drawLine(percent_lambda_1, // pt1_time,
69  0.0,
70  0.0);
71  // Line 2
72  waveform << line.drawLine(percent_lambda_2 - percent_lambda_1, //pt2_time - pt1_time,
73  amplitude_1,
74  amplitude_1);
75  // Line 3
76  waveform << line.drawLine(percent_lambda_3 - percent_lambda_2, //pt3_time - pt2_time,
77  0.0,
78  0.0);
79  // Line 4
80  waveform << line.drawLine(percent_lambda_4 - percent_lambda_3, //pt4_time - pt3_time,
81  amplitude_3,
82  amplitude_3);
83  // Line 5
84  waveform << line.drawLine(1.0 - percent_lambda_4, //pt5_time - pt4_time,
85  0.0,
86  0.0);
87 
88  return waveform;
89 }
90 
91 //-----------------------------------------------------------------------------
93 Square(const float64 & sample_rate) : Generator(sample_rate)
94 {
95  Buffer waveform;
96 
97  waveform << drawLine(0.5, 1.0, 1.0) << drawLine(0.5, -1.0, -1.0);
98 
99  ctor(sample_rate, waveform);
100 }
101 
102 //-----------------------------------------------------------------------------
103 Square::
105  const float64 & sample_rate,
106  const int32 n_harmonics)
107  :
108  Generator(sample_rate)
109 {
110  // From wikipedia's definition of a square wave.
111 
112  float64 Nf = static_cast<float64>(std::abs(n_harmonics));
113 
114  if(Nf < 1.0) Nf = 1.0;
115 
116  Buffer waveform = Buffer::zeros(static_cast<uint32>(sample_rate_));
117 
118  for(float64 k = 1.0; k <= Nf; k += 1.0)
119  {
120  float64 kk = 2.0 * k - 1.0;
121 
122  waveform += drawSine(1.0, kk) / kk;
123  }
124 
125  waveform *= 4.0 / M_PI;
126 
127  ctor(sample_rate, waveform);
128 }
129 
130 
131 //-----------------------------------------------------------------------------
132 Square::
134  const float64 & sample_rate,
135  const float64 & percent_lambda_1,
136  const float64 & amplitude_1,
137  const float64 & percent_lambda_2,
138  const float64 & percent_lambda_3,
139  const float64 & amplitude_3,
140  const float64 & percent_lambda_4) : Generator(sample_rate)
141 {
142  ctor(sample_rate,
143  drawSquare(
144  sample_rate,
145  percent_lambda_1,
146  amplitude_1,
147  percent_lambda_2,
148  percent_lambda_3,
149  amplitude_3,
150  percent_lambda_4));
151 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
Square(const float64 &sample_rate)
Definition: Square.cc:93
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
Buffer drawSquare(const float64 &sample_rate, const float64 &percent_lambda_1, const float64 &amplitude_1, const float64 &percent_lambda_2, const float64 &percent_lambda_3, const float64 &amplitude_3, const float64 &percent_lambda_4)
Definition: Square.cc:41
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
A Buffer for storing audio samples.
Definition: Buffer.h:60
signed int int32
Definition: Nsound.h:142
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50