2012-08-05 15:17:37 -05:00
|
|
|
/**
|
2013-09-08 23:09:54 -05:00
|
|
|
* @file
|
|
|
|
|
* @brief Source file for AudioResampler class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* LICENSE
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2019-06-11 06:48:32 -04:00
|
|
|
* Copyright (c) 2008-2019 OpenShot Studios, LLC
|
2014-03-29 18:49:22 -05:00
|
|
|
* <http://www.openshotstudios.com/>. This file is part of
|
|
|
|
|
* OpenShot Library (libopenshot), an open-source project dedicated to
|
|
|
|
|
* delivering high quality video editing and animation solutions to the
|
|
|
|
|
* world. For more information visit <http://www.openshot.org/>.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is free software: you can redistribute it
|
2014-07-11 16:52:14 -05:00
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* as published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is distributed in the hope that it will be
|
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-07-11 16:52:14 -05:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-07-11 16:52:14 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
2012-08-05 15:17:37 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "../include/AudioResampler.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
// 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)
|
|
|
|
|
AudioResampler::AudioResampler()
|
2012-08-05 15:17:37 -05:00
|
|
|
{
|
2012-10-21 05:29:29 -05:00
|
|
|
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;
|
|
|
|
|
|
2012-08-05 15:17:37 -05:00
|
|
|
// Init buffer source
|
|
|
|
|
buffer_source = new AudioBufferSource(buffer);
|
|
|
|
|
|
|
|
|
|
// Init resampling source
|
|
|
|
|
resample_source = new ResamplingAudioSource(buffer_source, false, 2);
|
|
|
|
|
|
|
|
|
|
// Init resampled buffer
|
|
|
|
|
resampled_buffer = new AudioSampleBuffer(2, 1);
|
2012-10-21 05:29:29 -05:00
|
|
|
resampled_buffer->clear();
|
2012-08-05 15:17:37 -05:00
|
|
|
|
|
|
|
|
// Init callback buffer
|
|
|
|
|
resample_callback_buffer.buffer = resampled_buffer;
|
|
|
|
|
resample_callback_buffer.numSamples = 1;
|
|
|
|
|
resample_callback_buffer.startSample = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Descructor
|
|
|
|
|
AudioResampler::~AudioResampler()
|
|
|
|
|
{
|
|
|
|
|
// Clean up
|
2012-10-21 05:29:29 -05:00
|
|
|
if (buffer_source)
|
|
|
|
|
delete buffer_source;
|
|
|
|
|
if (resample_source)
|
|
|
|
|
delete resample_source;
|
|
|
|
|
if (resampled_buffer)
|
|
|
|
|
delete resampled_buffer;
|
2012-08-05 15:17:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sets the audio buffer and updates the key settings
|
2014-06-30 15:37:50 -05:00
|
|
|
void AudioResampler::SetBuffer(AudioSampleBuffer *new_buffer, double sample_rate, double new_sample_rate)
|
2012-08-05 15:17:37 -05:00
|
|
|
{
|
|
|
|
|
if (sample_rate <= 0)
|
2019-04-27 12:50:31 +02:00
|
|
|
sample_rate = 44100;
|
2012-08-05 15:17:37 -05:00
|
|
|
if (new_sample_rate <= 0)
|
2019-04-27 12:50:31 +02:00
|
|
|
new_sample_rate = 44100;
|
2012-08-05 15:17:37 -05:00
|
|
|
|
2012-10-21 05:29:29 -05:00
|
|
|
// Set the sample ratio (the ratio of sample rate change)
|
|
|
|
|
source_ratio = sample_rate / new_sample_rate;
|
|
|
|
|
|
|
|
|
|
// Call SetBuffer with ratio
|
|
|
|
|
SetBuffer(new_buffer, source_ratio);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sets the audio buffer and key settings
|
2014-06-30 15:37:50 -05:00
|
|
|
void AudioResampler::SetBuffer(AudioSampleBuffer *new_buffer, double ratio)
|
2012-10-21 05:29:29 -05:00
|
|
|
{
|
2012-08-05 15:17:37 -05:00
|
|
|
// Update buffer & buffer source
|
|
|
|
|
buffer = new_buffer;
|
|
|
|
|
buffer_source->setBuffer(buffer);
|
|
|
|
|
|
|
|
|
|
// Set the sample ratio (the ratio of sample rate change)
|
2012-10-21 05:29:29 -05:00
|
|
|
source_ratio = ratio;
|
|
|
|
|
dest_ratio = 1.0 / ratio;
|
2012-08-05 15:17:37 -05:00
|
|
|
num_of_samples = buffer->getNumSamples();
|
|
|
|
|
new_num_of_samples = round(num_of_samples * dest_ratio) - 1;
|
|
|
|
|
|
|
|
|
|
// Set resample ratio
|
|
|
|
|
resample_source->setResamplingRatio(source_ratio);
|
|
|
|
|
|
|
|
|
|
// Prepare to play resample source
|
|
|
|
|
if (!isPrepared)
|
|
|
|
|
{
|
|
|
|
|
// Prepare to play the audio sources (and set the # of samples per chunk to a little more than expected)
|
2012-10-21 05:29:29 -05:00
|
|
|
resample_source->prepareToPlay(num_of_samples + 10, 0);
|
2012-08-05 15:17:37 -05:00
|
|
|
isPrepared = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resize buffer for the newly resampled data
|
|
|
|
|
resampled_buffer->setSize(buffer->getNumChannels(), new_num_of_samples, true, true, true);
|
|
|
|
|
resample_callback_buffer.numSamples = new_num_of_samples;
|
|
|
|
|
resample_callback_buffer.startSample = 0;
|
|
|
|
|
resample_callback_buffer.clearActiveBufferRegion();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the resampled audio buffer
|
|
|
|
|
AudioSampleBuffer* AudioResampler::GetResampledBuffer()
|
|
|
|
|
{
|
|
|
|
|
// Resample the current frame's audio buffer (into the temp callback buffer)
|
|
|
|
|
resample_source->getNextAudioBlock(resample_callback_buffer);
|
|
|
|
|
|
|
|
|
|
// Return buffer pointer to this newly resampled buffer
|
|
|
|
|
return resampled_buffer;
|
|
|
|
|
}
|