Nsound  0.9.4
Hat.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Hat.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Nsound is a C++ library and Python module for audio synthesis featuring
6 // dynamic digital filters. Nsound lets you easily shape waveforms and write
7 // to disk or plot them. Nsound aims to be as powerful as Csound but easy to
8 // use.
9 //
10 // Copyright (c) 2009-Present Nick Hilton
11 //
12 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
13 //
14 //-----------------------------------------------------------------------------
15 
16 //-----------------------------------------------------------------------------
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 // This program is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 // GNU Library General Public License for more details.
27 //
28 // You should have received a copy of the GNU General Public License
29 // along with this program; if not, write to the Free Software
30 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 //
32 //-----------------------------------------------------------------------------
33 
34 #include <Nsound/AudioStream.h>
35 #include <Nsound/Buffer.h>
36 #include <Nsound/Hat.h>
38 #include <Nsound/Square.h>
39 
40 using namespace Nsound;
41 using std::cout;
42 using std::endl;
43 
44 //-----------------------------------------------------------------------------
45 Hat::
46 Hat(const float64 & sample_rate)
47  :
48  Instrument(sample_rate),
49  square_(NULL),
50  hpf_(NULL)
51 {
53  hpf_ = new FilterHighPassIIR(sample_rate_, 2, 4872.0, 0.05);
54 }
55 
56 //-----------------------------------------------------------------------------
57 Hat::
58 Hat(const Hat & rhs)
59  :
60  Instrument(rhs.sample_rate_),
61  square_(NULL),
62  hpf_(NULL)
63 {
64  square_ = new Square(*rhs.square_);
65  hpf_ = new FilterHighPassIIR(*rhs.hpf_);
66 }
67 
68 //-----------------------------------------------------------------------------
69 Hat::
71 {
72  delete square_;
73  delete hpf_;
74 }
75 
76 //-----------------------------------------------------------------------------
77 Hat &
78 Hat::
79 operator=(const Hat & rhs)
80 {
81  delete square_;
82  delete hpf_;
83 
85  square_ = new Square(*rhs.square_);
86  hpf_ = new FilterHighPassIIR(*rhs.hpf_);
87 
88  return *this;
89 }
90 
92 Hat::
94 {
96 
97  float64 offset = 0.124;
98 
99  y << play(1.0, 1.0, true);
100 
101  // duration, tune, is_open
102 //~ y.add(play(1.0, 1.0, true), 0 * offset);
103  y.add(play(1.0, 1.0, true), 1 * offset);
104  y.add(play(1.0, 1.0, false), 2 * offset);
105  y.add(play(1.0, 1.0, true) , 3 * offset);
106 
107  y.add(play(1.0, 1.0, true) , 4 * offset);
108  y.add(play(1.0, 1.0, true) , 5 * offset);
109  y.add(play(1.0, 1.0, true) , 6 * offset);
110  y.add(play(1.0, 1.0, false), 7 * offset);
111 
112  y.add(play(1.0, 1.0, true) , 8 * offset);
113  y.add(play(1.0, 1.0, true) , 9 * offset);
114  y.add(play(1.0, 1.0, false), 10 * offset);
115  y.add(play(1.0, 1.0, true) , 11 * offset);
116 
117  y.add(play(1.0, 1.0, true) , 12 * offset);
118  y.add(play(1.0, 1.0, true) , 13 * offset);
119  y.add(play(1.0, 1.0, true) , 14 * offset);
120  y.add(play(1.0, 1.0, false), 15 * offset);
121 
122  y.add(play(1.0, 1.0, true) , 16 * offset);
123  y.add(play(1.0, 1.0, true) , 17 * offset);
124  y.add(play(1.0, 1.0, false), 18 * offset);
125  y.add(play(1.0, 1.0, true) , 19 * offset);
126 
127  y.add(play(1.0, 1.0, true) , 20 * offset);
128  y.add(play(1.0, 1.0, true) , 21 * offset);
129  y.add(play(1.0, 1.0, true) , 22 * offset);
130  y.add(play(1.0, 1.0, false), 23 * offset);
131 
132  y.add(play(1.0, 0.9, true) , 24 * offset);
133  y.add(play(1.0, 0.8, true) , 25 * offset);
134  y.add(play(1.0, 0.7, true) , 26 * offset);
135  y.add(play(1.0, 0.6, true) , 27 * offset);
136 
137  y.add(play(1.0, 0.5, true) , 28 * offset);
138  y.add(play(1.0, 0.4, true) , 29 * offset);
139  y.add(play(1.0, 0.3, true) , 30 * offset);
140  y.add(play(1.0, 0.2, true) , 31 * offset);
141 
142  return y;
143 }
144 
145 #include <Nsound/Plotter.h>
146 
148 Hat::
150  const float64 & duration,
151  const float64 & tune,
152  boolean is_closed)
153 {
154  float64 play_duration = duration;
155  float64 silence_duration = 0.0;
156 
157  float64 env_offset = 0.55 * play_duration;
158 
159  if(is_closed)
160  {
161  play_duration *= 0.06;
162  silence_duration = duration - play_duration;
163  env_offset = 0.0;
164  }
165 
166  float64 frequency = tune * 530.0;
167 
168  Buffer env = square_->drawLine(0.0005, 0.1, 1.0)
169  << square_->drawDecay(play_duration-0.0005 - env_offset, 7.0)
170  << square_->silence(silence_duration + env_offset);
171 
172  AudioStream out(sample_rate_, 1);
173 
174  out << square_->generate(play_duration, frequency);
175 
176  out += square_->generate(play_duration, frequency * 1.5038);
177  out += square_->generate(play_duration, frequency * 1.6132);
178  out += square_->generate(play_duration, frequency * 1.9642);
179  out += square_->generate(play_duration, frequency * 2.5321);
180  out += square_->generate(play_duration, frequency * 2.7547);
181 
182  out /= 2.0;
183 
184  out = hpf_->filter(out);
185  out = hpf_->filter(out);
186 
187  return out * env;
188 }
189 
190 // :mode=c++: jEdit modeline
AudioStream play()
Plays a demo for this instrument.
Definition: Hat.cc:93
Hat(const float64 &sample_rate)
Creates a Hat.
Definition: Hat.cc:46
FilterHighPassIIR * hpf_
Definition: Hat.h:108
Square generator.
Definition: Square.h:50
double float64
Definition: Nsound.h:146
Hat & operator=(const Hat &rhs)
Assignment.
Definition: Hat.cc:79
~Hat()
Destructor.
Definition: Hat.cc:70
A class for filtering audio in the frequecy domain.
Buffer silence(const float64 &duration) const
This method generates silence.
Definition: Generator.cc:1310
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
Buffer drawDecay(const float64 &duration, const float64 &alpha=2.0 *M_PI) const
This method draws an exponential curve that decays from 1.0 to 0.0 over the duration.
Definition: Generator.cc:388
Square * square_
Definition: Hat.h:103
The Nsound Instrument baseclass. All Nsound instruments extend this class.
Definition: Instrument.h:50
float64 sample_rate_
Definition: Instrument.h:76
A Buffer for storing audio samples.
Definition: Buffer.h:60
void add(const AudioStream &as, uint32 offset, uint32 n_samples=0)
This method adds the passed AudioStream to this AudioStream.
Definition: AudioStream.cc:121
Class Hat.
Definition: Hat.h:61
AudioStream filter(const AudioStream &x)
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