Nsound  0.9.4
demo-stretcher.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: bebot.cc 887 2015-04-26 02:49:19Z weegreenblobbie $
4 //
5 //-----------------------------------------------------------------------------
6 
7 #include <Nsound/NsoundAll.h>
8 
9 #include <iostream>
10 
11 using std::cout;
12 using std::cerr;
13 using std::endl;
14 
15 using namespace Nsound;
16 
17 
19 stretch_to(const AudioStream & x, float64 new_duration)
20 {
21  float64 x_dur = x.getDuration();
22 
23  float64 ratio = new_duration / x_dur;
24 
25  Stretcher st(x.getSampleRate());
26 
27  st.showProgress(true);
28 
29  return st.timeShift(x, ratio);
30 }
31 
32 
33 int
34 main(void)
35 {
36  AudioStream x0("california.wav");
37 
38  float64 dur = x0.getDuration();
39 
40  AudioStream x1 = stretch_to(x0, 1.15 * dur);
41  AudioStream x2 = stretch_to(x0, 1.30 * dur);
42 
43  // The longest one is x2, so add all of them to x2.
44 
45  cout
46  << "x2.getLength() = " << x2.getLength() << "\n"
47  << "x1.getLength() = " << x1.getLength() << "\n"
48  << "x0.getLength() = " << x0.getLength() << "\n";
49 
50  x2.add(x0, 1.30 * dur - dur);
51  x2.add(x1, 1.15 * dur - dur);
52 
53  for(uint32 i = 0; i < x0.getSampleRate() / 2.0; ++i)
54  {
55  x2 << 0.0;
56  }
57 
58  x2.normalize();
59  x2 *= 0.666;
60 
61  cout << "x2.getLength() = " << x2.getLength() << "\n";
62 
63  x2 >> "demo-st.wav";
64 
65  return 0;
66 }
unsigned int uint32
Definition: Nsound.h:153
float64 getDuration() const
Returns the number of seconds of audio data in the stream.
Definition: AudioStream.cc:183
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
int main(void)
uint32 getLength() const
Returns the number of samples of audio data in the stream.
Definition: AudioStream.cc:197
double float64
Definition: Nsound.h:146
AudioStream stretch_to(const AudioStream &x, float64 new_duration)
void normalize()
Multiplies the AudioStream by a constant gain so the peak sample has magnitude 1.0.
Definition: AudioStream.cc:285
void showProgress(boolean flag)
Definition: Stretcher.h:86
void add(const AudioStream &as, uint32 offset, uint32 n_samples=0)
This method adds the passed AudioStream to this AudioStream.
Definition: AudioStream.cc:121