OpenShot Library | libopenshot  0.1.9
AudioResampler.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for AudioResampler class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #include "../include/AudioResampler.h"
29 
30 using namespace std;
31 using namespace openshot;
32 
33 // Default constructor, max frames to cache is 20 // resample_source(NULL), buffer_source(NULL), num_of_samples(0), new_num_of_samples(0), dest_ratio(0), source_ratio(0), resampled_buffer(NULL), isPrepared(false)
34 AudioResampler::AudioResampler()
35 {
36  resample_source = NULL;
37  buffer_source = NULL;
38  num_of_samples = 0;
39  new_num_of_samples = 0;
40  dest_ratio = 0;
41  source_ratio = 0;
42  resampled_buffer = NULL;
43  isPrepared = false;
44 
45  // Init buffer source
46  buffer_source = new AudioBufferSource(buffer);
47 
48  // Init resampling source
49  resample_source = new ResamplingAudioSource(buffer_source, false, 2);
50 
51  // Init resampled buffer
52  resampled_buffer = new AudioSampleBuffer(2, 1);
53  resampled_buffer->clear();
54 
55  // Init callback buffer
56  resample_callback_buffer.buffer = resampled_buffer;
57  resample_callback_buffer.numSamples = 1;
58  resample_callback_buffer.startSample = 0;
59 }
60 
61 // Descructor
62 AudioResampler::~AudioResampler()
63 {
64  // Clean up
65  if (buffer_source)
66  delete buffer_source;
67  if (resample_source)
68  delete resample_source;
69  if (resampled_buffer)
70  delete resampled_buffer;
71 }
72 
73 // Sets the audio buffer and updates the key settings
74 void AudioResampler::SetBuffer(AudioSampleBuffer *new_buffer, double sample_rate, double new_sample_rate)
75 {
76  if (sample_rate <= 0)
77  sample_rate == 44100;
78  if (new_sample_rate <= 0)
79  new_sample_rate == 44100;
80 
81  // Set the sample ratio (the ratio of sample rate change)
82  source_ratio = sample_rate / new_sample_rate;
83 
84  // Call SetBuffer with ratio
85  SetBuffer(new_buffer, source_ratio);
86 }
87 
88 // Sets the audio buffer and key settings
89 void AudioResampler::SetBuffer(AudioSampleBuffer *new_buffer, double ratio)
90 {
91  // Update buffer & buffer source
92  buffer = new_buffer;
93  buffer_source->setBuffer(buffer);
94 
95  // Set the sample ratio (the ratio of sample rate change)
96  source_ratio = ratio;
97  dest_ratio = 1.0 / ratio;
98  num_of_samples = buffer->getNumSamples();
99  new_num_of_samples = round(num_of_samples * dest_ratio) - 1;
100 
101  // Set resample ratio
102  resample_source->setResamplingRatio(source_ratio);
103 
104  // Prepare to play resample source
105  if (!isPrepared)
106  {
107  // Prepare to play the audio sources (and set the # of samples per chunk to a little more than expected)
108  resample_source->prepareToPlay(num_of_samples + 10, 0);
109  isPrepared = true;
110  }
111 
112  // Resize buffer for the newly resampled data
113  resampled_buffer->setSize(buffer->getNumChannels(), new_num_of_samples, true, true, true);
114  resample_callback_buffer.numSamples = new_num_of_samples;
115  resample_callback_buffer.startSample = 0;
116  resample_callback_buffer.clearActiveBufferRegion();
117 }
118 
119 // Get the resampled audio buffer
120 AudioSampleBuffer* AudioResampler::GetResampledBuffer()
121 {
122  // Resample the current frame's audio buffer (into the temp callback buffer)
123  resample_source->getNextAudioBlock(resample_callback_buffer);
124 
125  // Return buffer pointer to this newly resampled buffer
126  return resampled_buffer;
127 }
This class is used to expose an AudioSampleBuffer as an AudioSource in JUCE.
This namespace is the default namespace for all code in the openshot library.