2013-09-08 23:09:54 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for AudioBufferSource class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-16 01:26:26 -04:00
|
|
|
// Copyright (c) 2008-2019 OpenShot Studios, LLC
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2013-09-08 23:09:54 -05:00
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "AudioBufferSource.h"
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
|
|
|
|
// Default constructor
|
2021-10-27 14:34:05 -04:00
|
|
|
AudioBufferSource::AudioBufferSource(juce::AudioBuffer<float> *audio_buffer)
|
2020-04-22 02:02:55 -04:00
|
|
|
: position(0), repeat(false), buffer(audio_buffer)
|
2012-10-21 05:29:29 -05:00
|
|
|
{ }
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
|
AudioBufferSource::~AudioBufferSource()
|
|
|
|
|
{
|
2021-10-27 14:34:05 -04:00
|
|
|
// forget the AudioBuffer<float>. It still exists; we just don't know about it.
|
2011-10-11 08:44:27 -05:00
|
|
|
buffer = NULL;
|
2019-12-27 01:01:48 -05:00
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Get the next block of audio samples
|
2019-10-27 03:56:13 -04:00
|
|
|
void AudioBufferSource::getNextAudioBlock (const juce::AudioSourceChannelInfo& info)
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
|
|
|
|
int buffer_samples = buffer->getNumSamples();
|
|
|
|
|
int buffer_channels = buffer->getNumChannels();
|
|
|
|
|
|
|
|
|
|
if (info.numSamples > 0) {
|
|
|
|
|
int start = position;
|
|
|
|
|
int number_to_copy = 0;
|
|
|
|
|
|
|
|
|
|
// Determine how many samples to copy
|
|
|
|
|
if (start + info.numSamples <= buffer_samples)
|
|
|
|
|
{
|
|
|
|
|
// copy the full amount requested
|
|
|
|
|
number_to_copy = info.numSamples;
|
|
|
|
|
}
|
|
|
|
|
else if (start > buffer_samples)
|
|
|
|
|
{
|
|
|
|
|
// copy nothing
|
|
|
|
|
number_to_copy = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (buffer_samples - start > 0)
|
|
|
|
|
{
|
|
|
|
|
// only copy what is left in the buffer
|
|
|
|
|
number_to_copy = buffer_samples - start;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// copy nothing
|
|
|
|
|
number_to_copy = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine if any samples need to be copied
|
|
|
|
|
if (number_to_copy > 0)
|
|
|
|
|
{
|
|
|
|
|
// Loop through each channel and copy some samples
|
|
|
|
|
for (int channel = 0; channel < buffer_channels; channel++)
|
|
|
|
|
info.buffer->copyFrom(channel, info.startSample, *buffer, channel, start, number_to_copy);
|
|
|
|
|
|
|
|
|
|
// Update the position of this audio source
|
|
|
|
|
position += number_to_copy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare to play this audio source
|
2012-08-05 15:17:37 -05:00
|
|
|
void AudioBufferSource::prepareToPlay(int, double) { }
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Release all resources
|
2012-08-05 15:17:37 -05:00
|
|
|
void AudioBufferSource::releaseResources() { }
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Set the next read position of this source
|
2019-10-27 03:56:13 -04:00
|
|
|
void AudioBufferSource::setNextReadPosition (juce::int64 newPosition)
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
|
|
|
|
// set position (if the new position is in range)
|
2012-08-05 15:17:37 -05:00
|
|
|
if (newPosition >= 0 && newPosition < buffer->getNumSamples())
|
2011-10-11 08:44:27 -05:00
|
|
|
position = newPosition;
|
2012-08-05 15:17:37 -05:00
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Get the next read position of this source
|
2019-10-27 03:56:13 -04:00
|
|
|
juce::int64 AudioBufferSource::getNextReadPosition() const
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
|
|
|
|
// return the next read position
|
|
|
|
|
return position;
|
2012-08-05 15:17:37 -05:00
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Get the total length (in samples) of this audio source
|
2019-10-27 03:56:13 -04:00
|
|
|
juce::int64 AudioBufferSource::getTotalLength() const
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
|
|
|
|
// Get the length
|
|
|
|
|
return buffer->getNumSamples();
|
2012-08-05 15:17:37 -05:00
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Determines if this audio source should repeat when it reaches the end
|
|
|
|
|
bool AudioBufferSource::isLooping() const
|
|
|
|
|
{
|
|
|
|
|
// return if this source is looping
|
|
|
|
|
return repeat;
|
2012-08-05 15:17:37 -05:00
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Set if this audio source should repeat when it reaches the end
|
|
|
|
|
void AudioBufferSource::setLooping (bool shouldLoop)
|
|
|
|
|
{
|
|
|
|
|
// Set the repeat flag
|
|
|
|
|
repeat = shouldLoop;
|
2012-08-05 15:17:37 -05:00
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2021-10-27 14:34:05 -04:00
|
|
|
// Use a different AudioBuffer<float> for this source
|
|
|
|
|
void AudioBufferSource::setBuffer (juce::AudioBuffer<float> *audio_buffer)
|
2012-08-05 15:17:37 -05:00
|
|
|
{
|
|
|
|
|
buffer = audio_buffer;
|
|
|
|
|
setNextReadPosition(0);
|
|
|
|
|
}
|