Nsound  0.9.4
GeneratorDecay.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: GeneratorDecay.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2005-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/GeneratorDecay.h>
31 
32 using namespace Nsound;
33 
34 // Constructor
37 
38 // Constructor
40 GeneratorDecay(const float64 & sample_rate) : Generator(sample_rate) {};
41 
42 float64
44 generate(const float64 & frequency)
45 {
46  return generate2(frequency, 1.0);
47 }
48 
49 float64
51 generate2(const float64 & frequency, const float64 & alpha)
52 {
53  ++sync_count_;
54 
55  if(sync_is_slave_ && !sync_vector_.empty())
56  {
57  uint32 sync_count = sync_vector_.front();
58 
59  if(sync_count_ == sync_count)
60  {
61  sync_vector_.erase(sync_vector_.begin());
62  position_ = 0;
63  }
64  }
65 
67 
68  float64 sample = ::exp(t * -alpha);
69 
70  position_ += frequency;
71  sync_pos_ += frequency;
72 
73  // limit
75  {
77  sync_pos_ = 0;
78 
79  if(sync_is_master_)
80  {
81  std::set<Generator *>::iterator itor = this->sync_slaves_.begin();
82 
83  while(itor != sync_slaves_.end())
84  {
85  GeneratorDecay * ptr = static_cast<GeneratorDecay *>(*itor);
86 
87  ptr->sync_vector_.push_back(sync_count_);
88  ++itor;
89  }
90  }
91  }
92 
93  return sample;
94 }
95 
96 Buffer
99  const float64 & duration,
100  const float64 & frequency,
101  const float64 & alpha)
102 {
103  return Generator::generate2(duration, frequency, alpha);
104 }
105 
106 Buffer
109  const float64 & duration,
110  const float64 & frequency,
111  const Buffer & alpha)
112 {
113  return Generator::generate2(duration, frequency, alpha);
114 }
115 
116 Buffer
119  const float64 & duration,
120  const Buffer & frequencies,
121  const float64 & alpha)
122 {
123  return Generator::generate2(duration, frequencies, alpha);
124 }
125 
126 Buffer
129  const float64 & duration,
130  const Buffer & frequencies,
131  const Buffer & alpha)
132 {
133  return Generator::generate2(duration, frequencies, alpha);
134 }
135 
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
unsigned int uint32
Definition: Nsound.h:153
A class tha generates decay curves.
std::set< Generator * > sync_slaves_
Stores sample counts when synced.
Definition: Generator.h:638
float64 position_
Used for phase offset adjustment.
Definition: Generator.h:614
double float64
Definition: Nsound.h:146
float64 sync_pos_
The number of samples into the wavefrom.
Definition: Generator.h:615
boolean sync_is_slave_
Indicates if this generator is the master.
Definition: Generator.h:635
boolean sync_is_master_
Definition: Generator.h:634
A Buffer for storing audio samples.
Definition: Buffer.h:60
std::vector< uint32 > sync_vector_
Indicates the number of samples since reset.
Definition: Generator.h:637
uint32 sync_count_
Indicates if this generator is a slave.
Definition: Generator.h:636
virtual float64 generate2(const float64 &frequency, const float64 &alpha)
This is a real-time method for the wavetable oscillator.
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
virtual float64 generate(const float64 &frequency)
This is a real-time method for the decay curve ossilator.