Nsound  0.9.4
example5.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: example5.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 //-----------------------------------------------------------------------------
6 
7 // Nsound headers
8 #include <Nsound/NsoundAll.h>
9 
10 using namespace Nsound;
11 
12 using std::cerr;
13 using std::endl;
14 
15 //-----------------------------------------------------------------------------
16 int
17 my_main(void)
18 {
19  AudioStream voice("california.wav");
20 
21  float64 sr = voice.getSampleRate();
22 
24 
25  // Get rid of some annoying frequencies from the recording.
26  FilterBandPassIIR bpf(sr, 6, 45.0, 16000.0, 0.01);
27 
28  voice = bpf.filter(voice);
29 
30  float64 voice_dur = voice.getDuration();
31 
32  // The spectral rich granulator is used carrier frequency.
34 
35  Buffer carrier = gen.generate(voice_dur, 16000.0, 3, 40.0);
36 
37  // The vocoder processes the input with a filter bank. The end result
38  // should have a similar spectral distrubution of the energy, but with a
39  // timbre.
40  //
41  Vocoder vocod(sr, 0.0050, 32, 4000);
42 
43  Buffer output = vocod.filter(voice[0], carrier);
44 
45  output.normalize();
46 
47  output *= 0.666;
48 
49  output >> "example5.wav";
50 
51  try
52  {
53  AudioPlayback pb(sr, 2, 16);
54  output >> pb;
55  }
56  catch(Exception e)
57  {
58  cerr << "Could not play audio: " << e.what() << endl;
59  }
60 
61  // Create some spectrogram plots.
62 
63  Plotter pylab;
64 
65  pylab.figure();
66  Axes ax1 = pylab.subplot(2, 1, 1);
67 
68  float64 window = 0.080; // seconds
69  float64 step = 0.020; // seconds
70 
71  Spectrogram spec1(voice[0], sr, window, step, HANNING);
72 
73  pylab.imagesc(
74  spec1.getTimeAxis(),
75  spec1.getFrequencyAxis(),
76  spec1.getMagnitude().getTranspose());
77 
78  float64 fmax = 2000.0;
79 
80  pylab.ylim(0.0, fmax);
81  pylab.xlim(0.0, voice_dur);
82 
83  pylab.title("Voice input");
84 
85  pylab.subplot(2, 1, 2, /*kwargs=*/ "", /*sharex=*/ &ax1, /*sharey=*/ &ax1);
86 
87  Spectrogram spec2(output, sr, window, step, HANNING);
88 
89  pylab.imagesc(
90  spec2.getTimeAxis(),
91  spec2.getFrequencyAxis(),
92  spec2.getMagnitude().getTranspose());
93 
94  pylab.ylim(0.0, fmax);
95  pylab.xlim(0.0, voice_dur);
96 
97  pylab.title("Voice output");
98 
99  pylab.show();
100 
101  return 0;
102 }
103 
104 int main(int /*argc*/, char ** /*argv*/)
105 {
106  try
107  {
108  my_main();
109  return 0;
110  }
111  catch(std::exception & e)
112  {
113  cerr << "Exception: " << e.what() << endl;
114  return 1;
115  }
116 
117  return 0;
118 }
119 
static void show()
Acutally draw the plots to the screen.
Definition: Plotter.cc:252
float64 getDuration() const
Returns the number of seconds of audio data in the stream.
Definition: AudioStream.cc:183
Buffer getTimeAxis() const
Definition: Spectrogram.cc:272
Nsound::Buffer generate(const float64 &duration, const float64 &grain_frequency, const float64 &waves_per_grain, const float64 &grains_per_second)
Definition: Granulator.cc:193
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
AudioStream getTranspose() const
Retuns a copy of the AudioStream transposed.
Definition: AudioStream.h:554
void figure(const std::string &kwargs="") const
Creates a new figure window to plot in.
Definition: Plotter.cc:455
A wrapper around a Matplotlib Axes object.
Definition: Plotter.h:54
void title(const std::string &title, const std::string &kwargs="")
Add a title to the plot at the top and centered.
Definition: Plotter.cc:1127
void xlim(const float64 &xmin, const float64 &xmax)
Sets the limit for the x & y axis.
Definition: Plotter.cc:389
double float64
Definition: Nsound.h:146
The result from an STFT.
Definition: Spectrogram.h:47
const char * what() const
Definition: Nsound.h:169
Axes subplot(const uint32 n_rows, const uint32 n_cols, const uint32 n, const std::string &kwargs="", Axes *sharex=NULL, Axes *sharey=NULL)
Creates a figure in a subplot, subplot(A, B, C, **kwargs)
Definition: Plotter.cc:1031
void ylim(const float64 &ymin, const float64 &ymax)
Definition: Plotter.cc:422
void imagesc(const AudioStream &Z, const std::string &kwargs="")
Plots the AudioStream like a 2D matrix.
Definition: Plotter.cc:593
void normalize()
Multiplies the Buffer by a constant gain so the peak sample has magnitude 1.0.
Definition: Buffer.cc:1064
Buffer getFrequencyAxis() const
Definition: Spectrogram.cc:258
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream filter(const AudioStream &x)
static void setDefaultSampleRate(const int32 rate)
Definition: Wavefile.cc:70
int main(int, char **)
Definition: example5.cc:104
AudioStream getMagnitude() const
Definition: Spectrogram.cc:265
int my_main(void)
Definition: example5.cc:17
Buffer filter(const Buffer &voice, const Buffer &carrier)
Definition: Vocoder.cc:234
float64 sr
Definition: example3.cc:24