Nsound  0.9.4
example3.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: example3.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Simulates a drum. Based on the Csound drum by Hans Mikelson.
6 //
7 // source: http://www.csounds.com/ezine/winter2001/synthesis/
8 //
9 //-----------------------------------------------------------------------------
10 
11 // Nsound headers
12 #include <Nsound/NsoundAll.h>
13 
14 // C++ header
15 #include <iostream>
16 
17 using namespace Nsound;
18 
19 using std::cerr;
20 using std::endl;
21 using std::flush;
22 
23 // Eck, globals
24 float64 sr = 44100.0;
25 uint64 SAMPLE_RATE = static_cast<uint64>(sr);
27 
28 //-----------------------------------------------------------------------------
29 Buffer
31  float32 duration,
32  float32 attack_time,
33  float32 high_frequency,
34  float32 low_frequency,
35  float32 tension,
36  float32 resident_frequency)
37 {
38  Sine sin(sr);
39 
40  Buffer frequency_sweep;
41  frequency_sweep
42  << sin.drawLine(attack_time, high_frequency, low_frequency)
43  << sin.drawLine((duration - attack_time), low_frequency, low_frequency);
44 
45  Buffer hz_20 = sin.generate(duration, resident_frequency);
46 
47  Buffer rezzy = hz_20 * frequency_sweep;
48 
49  Buffer parabola = sin.drawParabola(duration, 1.0, duration / 2, 0.25, 0.0);
50 
51  rezzy *= parabola;
52 
53  Buffer temp1 = rezzy * tension;
54 
55  frequency_sweep -= temp1;
56 
57  Buffer audio = sin.generate(duration, frequency_sweep);
58 
59  audio *= sin.drawParabola(duration, 1.0, 0.5 * duration, 0.3, 0.0);
60 
61  return audio;
62 }
63 
64 int
65 my_main(void)
66 {
67  Sine sine(sr);
68 
69  DrumBD01 db01(sr);
70  DrumKickBass dkb(sr, 266, 0.0);
71 
72  AudioStream out(sr, 1);
73 
74  out << db01.play()
75  << sine.silence(0.25)
76  << dkb.play()
77  << sine.silence(0.25)
78 
79  // duration, attack, high f, low f, tension, ressonance
80  << drum(0.5f, 0.012f, 160, 51, 0.9f, 54)
81  << drum(0.5f, 0.012f, 160, 51, 0.9f, 54)
82  << drum(0.5f, 0.012f, 160, 51, 0.9f, 54)
83  << drum(0.5f, 0.012f, 160, 51, 0.9f, 54)
84  << sine.silence(0.25);
85 
86  out *= 0.5;
87 
88  Hat hat(sr);
89 
90  out << 0.666 * hat.play() << sine.silence(0.25);
91 
92  out >> "example3.wav";
93 
94  // ReverberationRoom(sample_rate, room_feedback, wet_percent, dry_percent, low_pass_freq)
95  ReverberationRoom room(sr, 0.60, 0.5, 1.0, 100.0);
96 
97  AudioStream out2 = 0.5 * room.filter(out);
98 
99  out2 >> "example3_reverb.wav";
100 
101  // Try to play the audio
102 
103  try
104  {
105  AudioPlayback pb(sr, 2, 16);
106  out2 >> pb;
107  }
108  catch(std::exception & e)
109  {
110  cerr << "Warning: Could not play audio: " << e.what() << endl;
111  }
112 
113  return 0;
114 }
115 
116 int
117 main(void)
118 {
119  try
120  {
121  my_main();
122  }
123  catch(std::exception & e)
124  {
125  cerr << "Exception: " << e.what() << endl;
126  return 1;
127  }
128 
129  return 0;
130 }
AudioStream play()
Plays a demo for this instrument.
Definition: DrumKickBass.cc:57
AudioStream play()
Plays a demo for this instrument.
Definition: Hat.cc:93
AudioStream play()
Plays a demo for this instrument.
Definition: DrumBD01.cc:50
double float64
Definition: Nsound.h:146
Buffer drawParabola(const float64 &duration, const float64 &y1, const float64 &x2, const float64 &y2, const float64 &y3) const
This method draws a parabola between three points, intersecting the middle point. ...
Definition: Generator.cc:490
AudioStream filter(const AudioStream &x)
uint64 SAMPLE_RATE
Definition: example3.cc:25
Buffer silence(const float64 &duration) const
This method generates silence.
Definition: Generator.cc:1310
int main(void)
Definition: example3.cc:117
Buffer drum(float32 duration, float32 attack_time, float32 high_frequency, float32 low_frequency, float32 tension, float32 resident_frequency)
Definition: example3.cc:30
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
Class DrumBD01.
Definition: DrumBD01.h:50
unsigned long long uint64
Definition: Nsound.h:154
uint64 BITS_PER_SAMPLE
Definition: example3.cc:26
A Buffer for storing audio samples.
Definition: Buffer.h:60
float float32
Definition: Nsound.h:145
Class Hat.
Definition: Hat.h:61
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
DOXME.
Definition: Sine.h:43
int my_main(void)
Definition: example3.cc:65
float64 sr
Definition: example3.cc:24