Nsound  0.9.4
Functions
example5.cc File Reference
#include <Nsound/NsoundAll.h>
Include dependency graph for example5.cc:

Go to the source code of this file.

Functions

int my_main (void)
 
int main (int, char **)
 

Function Documentation

int my_main ( void  )

Definition at line 17 of file example5.cc.

References Nsound::Plotter::figure(), Nsound::Vocoder::filter(), Nsound::FilterBandPassIIR::filter(), Nsound::Granulator::generate(), Nsound::AudioStream::getDuration(), Nsound::Spectrogram::getFrequencyAxis(), Nsound::Spectrogram::getMagnitude(), Nsound::AudioStream::getSampleRate(), Nsound::Spectrogram::getTimeAxis(), Nsound::AudioStream::getTranspose(), Nsound::HANNING, Nsound::Plotter::imagesc(), Nsound::Buffer::normalize(), Nsound::Granulator::REVERSE_DECAY, Nsound::Wavefile::setDefaultSampleRate(), Nsound::Plotter::show(), sr, Nsound::Plotter::subplot(), Nsound::Plotter::title(), Nsound::Exception::what(), Nsound::Plotter::xlim(), and Nsound::Plotter::ylim().

Referenced by main().

18 {
19  AudioStream voice("california.wav");
20 
21  float64 sr = voice.getSampleRate();
22 
23  Wavefile::setDefaultSampleRate(sr);
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.
33  Granulator gen(sr, Granulator::REVERSE_DECAY);
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 }
static void show()
Acutally draw the plots to the screen.
Definition: Plotter.cc:252
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
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 sr
Definition: example3.cc:24
int main ( int  ,
char **   
)

Definition at line 104 of file example5.cc.

References my_main().

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 }
int my_main(void)
Definition: example5.cc:17