Nsound  0.9.4
Pulse.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Pulse.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/Pulse.h>
31 
32 #include <cmath>
33 #include <iostream>
34 
35 using namespace Nsound;
36 
37 using std::cout;
38 using std::cerr;
39 using std::endl;
40 
41 Pulse::
43  const float64 & sample_rate,
44  const float64 & pulse_width,
45  const PulseUnits & units)
46  :
47  Generator(sample_rate),
48  rise_t_(0.0),
49  fall_t_(-1.0),
50  pulse_width_(pulse_width),
51  units_(units)
52 {
53 }
54 
55 Pulse::
57 {}
58 
59 float64
60 Pulse::
61 generate(const float64 & frequency)
62 {
63  return generate2(frequency, pulse_width_);
64 }
65 
66 float64
67 Pulse::
68 generate2(const float64 & frequency, const float64 & pulse_width)
69 {
70  float64 sample = 0.0;
71 
72  if(units_ == PULSE_SECONDS)
73  {
74  fall_t_ = rise_t_ + pulse_width;
75  }
76  else
77  {
78  fall_t_ = rise_t_ + (pulse_width / frequency);
79  }
80 
81  if(t_ >= rise_t_)
82  {
83  if(t_ < fall_t_)
84  {
85  sample = 1.0;
86  }
87  else
88  {
89  rise_t_ += 1.0 / frequency;
90  }
91  }
92 
93  t_ += sample_time_;
94 
95  return sample;
96 }
97 
98 Buffer
99 Pulse::
101  const float64 & duration,
102  const float64 & frequency,
103  const float64 & pulse_width)
104 {
105  return Generator::generate2(duration, frequency, pulse_width);
106 }
107 
108 void
109 Pulse::
111 {
112  t_ = 0.0;
113  rise_t_ = 0.0;
114  fall_t_ = -1.0;
115 }
116 
PulseUnits units_
The default pulse width to use.
Definition: Pulse.h:164
float64 generate(const float64 &frequency)
This is a real-time method for the Pulse generator.
Definition: Pulse.cc:61
float64 pulse_width_
The falling edge time for the pulse.
Definition: Pulse.h:163
double float64
Definition: Nsound.h:146
void reset()
Resets the position pointer back to the begging of the waveform.
Definition: Pulse.cc:110
float64 fall_t_
The rising edge time for the pulse.
Definition: Pulse.h:162
float64 t_
The time step between samples in seconds.
Definition: Generator.h:618
float64 generate2(const float64 &frequency, const float64 &pulse_width)
This is a real-time method for the Pulse generator.
Definition: Pulse.cc:68
Pulse(const float64 &sample_rate, const float64 &pulse_width, const PulseUnits &units=PULSE_PERCENT)
Creates a new Pulse Generator.
Definition: Pulse.cc:42
float64 sample_time_
The number of samples per second to generate.
Definition: Generator.h:617
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 rise_t_
Definition: Pulse.h:157
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
virtual float64 generate2(const float64 &frequency, const float64 &phase)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:979