Nsound  0.9.4
Triangle.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Triangle.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/Triangle.h>
32 
33 #include <iostream>
34 
35 using namespace Nsound;
36 
37 using std::cout;
38 using std::endl;
39 
41 Triangle(const float64 & sample_rate) : Generator(sample_rate)
42 {
43  float64 third = 1.0/3.0;
44 
45  Buffer waveform;
46 
47  waveform << drawLine(third, 0.0, 1.0);
48  waveform << drawLine(third, 1.0, -1.0);
49  waveform << drawLine(third, -1.0, 0.0);
50 
51  uint32 n = static_cast<uint32>(sample_rate);
52 
53  // Pad if necessary.
54  while(waveform.getLength() < n) waveform << 0.0;
55 
56  // Throw away if necessary.
57  if(waveform.getLength() > n)
58  {
59  waveform = waveform.subbuffer(0, n);
60  }
61 
62  ctor(sample_rate, waveform);
63 }
64 
67  const float64 & sample_rate,
68  const float64 & attack_time,
69  const float64 & release_time) : Generator(sample_rate)
70 {
71  M_ASSERT_VALUE(attack_time, >=, 0);
72  M_ASSERT_VALUE(release_time, >=, 0);
73  M_ASSERT_VALUE(1.0 - attack_time - release_time, >, 0);
74 
75  Buffer waveform;
76 
77  if(attack_time > 0)
78  {
79  waveform << drawLine(attack_time, 0, 1.0);
80  }
81 
82  waveform << drawLine(1.0 - attack_time - release_time, 1.0, -1.0);
83 
84  if(release_time > 0)
85  {
86  waveform << drawLine(release_time, -1.0, 0.0);
87  }
88 
89  ctor(sample_rate, waveform);
90 }
91 
92 
93 //-----------------------------------------------------------------------------
94 // drawTriangle
95 //
96 // 2 //
97 // /\ //
98 // / \ //
99 // / \ //
100 // / \ //
101 // --1 3--4 6-- //
102 // \ / //
103 // \ / //
104 // \ / //
105 // \ / //
106 // \/ //
107 // 5 //
108 // //
109 // |----------lambda----------| //
110 //
111 //-----------------------------------------------------------------------------
112 Buffer
114  const float64 & sample_rate,
115  const float64 & percent_lambda_1,
116  const float64 & percent_lambda_2,
117  const float64 & amplitude_2,
118  const float64 & percent_lambda_3,
119  const float64 & percent_lambda_4,
120  const float64 & percent_lambda_5,
121  const float64 & amplitude_5,
122  const float64 & percent_lambda_6)
123 {
124  Buffer waveform;
125 
126  // Draw the 7 lines
127  Generator line(sample_rate);
128 
129  // Line 1
130  waveform << line.drawLine(percent_lambda_1,
131  0.0,
132  0.0);
133  // Line 2
134  waveform << line.drawLine(percent_lambda_2 - percent_lambda_1,
135  0.0,
136  amplitude_2);
137  // Line 3
138  waveform << line.drawLine(percent_lambda_3 - percent_lambda_2,
139  amplitude_2,
140  0.0);
141  // Line 4
142  waveform << line.drawLine(percent_lambda_4 - percent_lambda_3,
143  0.0,
144  0.0);
145  // Line 5
146  waveform << line.drawLine(percent_lambda_5 - percent_lambda_4,
147  0.0,
148  amplitude_5);
149  // Line 6
150  waveform << line.drawLine(percent_lambda_6 - percent_lambda_5,
151  amplitude_5,
152  0.0);
153  // Line 7
154  waveform << line.drawLine(1.0 - percent_lambda_6,
155  0.0,
156  0.0);
157 
158  return waveform;
159 }
160 
163  const float64 & sample_rate,
164  const float64 & percent_lambda_1,
165  const float64 & percent_lambda_2,
166  const float64 & amplitude_2,
167  const float64 & percent_lambda_3,
168  const float64 & percent_lambda_4,
169  const float64 & percent_lambda_5,
170  const float64 & amplitude_5,
171  const float64 & percent_lambda_6) : Generator(sample_rate)
172 {
173  // 2 //
174  // /\ //
175  // / \ //
176  // / \ //
177  // / \ //
178  // --1 3--4 6-- //
179  // \ / //
180  // \ / //
181  // \ / //
182  // \ / //
183  // \/ //
184  // 5 //
185  // //
186  // |----------lambda----------| //
187 
188  Buffer waveform = drawTriangle(
189  sample_rate,
190  percent_lambda_1,
191  percent_lambda_2,
192  amplitude_2,
193  percent_lambda_3,
194  percent_lambda_4,
195  percent_lambda_5,
196  amplitude_5,
197  percent_lambda_6);
198 
199  ctor(sample_rate, waveform);
200 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
Triangle(const Nsound::float64 &sample_rate)
Definition: Triangle.cc:41
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
virtual void ctor(const float64 &sample_rate)
DOXME.
Definition: Generator.cc:201
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Buffer drawTriangle(const float64 &sample_rate, const float64 &percent_lambda_1, const float64 &percent_lambda_2, const float64 &amplitude_2, const float64 &percent_lambda_3, const float64 &percent_lambda_4, const float64 &percent_lambda_5, const float64 &amplitude_5, const float64 &percent_lambda_6)
Definition: Triangle.cc:113
A Buffer for storing audio samples.
Definition: Buffer.h:60
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