Loading...
Searching...
No Matches
AudioReaderSource.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "AudioReaderSource.h"
#include "Exceptions.h"
#include "Frame.h"
using namespace std;
using namespace openshot;
// Constructor that reads samples from a reader
AudioReaderSource::AudioReaderSource(ReaderBase *audio_reader, int64_t starting_frame_number)
: reader(audio_reader), frame_position(starting_frame_number), videoCache(NULL), frame(NULL),
sample_position(0), speed(1), stream_position(0) {
}
// Destructor
AudioReaderSource::~AudioReaderSource()
{
}
// Get the next block of audio samples
void AudioReaderSource::getNextAudioBlock(const juce::AudioSourceChannelInfo& info)
{
if (info.numSamples > 0) {
int remaining_samples = info.numSamples;
int remaining_position = info.startSample;
// Pause and fill buffer with silence (wait for pre-roll)
if (speed != 1 || !videoCache->isReady()) {
info.buffer->clear();
return;
}
while (remaining_samples > 0) {
try {
// Get current frame object
if (reader) {
frame = reader->GetFrame(frame_position);
}
}
catch (const ReaderClosed & e) { }
catch (const OutOfBoundsFrame & e) { }
// Get audio samples
if (reader && frame) {
if (sample_position + remaining_samples <= frame->GetAudioSamplesCount()) {
// Success, we have enough samples
for (int channel = 0; channel < frame->GetAudioChannelsCount(); channel++) {
if (channel < info.buffer->getNumChannels()) {
info.buffer->addFrom(channel, remaining_position, *frame->GetAudioSampleBuffer(),
channel, sample_position, remaining_samples);
}
}
sample_position += remaining_samples;
remaining_position += remaining_samples;
remaining_samples = 0;
} else if (sample_position + remaining_samples > frame->GetAudioSamplesCount()) {
// Not enough samples, take what we can
int amount_to_copy = frame->GetAudioSamplesCount() - sample_position;
for (int channel = 0; channel < frame->GetAudioChannelsCount(); channel++) {
if (channel < info.buffer->getNumChannels()) {
info.buffer->addFrom(channel, remaining_position, *frame->GetAudioSampleBuffer(), channel,
sample_position, amount_to_copy);
}
}
sample_position += amount_to_copy;
remaining_position += amount_to_copy;
remaining_samples -= amount_to_copy;
}
// Increment frame position (if samples are all used up)
if (sample_position == frame->GetAudioSamplesCount()) {
frame_position += speed;
sample_position = 0; // reset for new frame
}
}
}
}
}
// Prepare to play this audio source
void AudioReaderSource::prepareToPlay(int, double) {}
// Release all resources
void AudioReaderSource::releaseResources() { }
// Get the total length (in samples) of this audio source
juce::int64 AudioReaderSource::getTotalLength() const
{
// Get the length
if (reader)
return reader->info.sample_rate * reader->info.duration;
else
return 0;
}
AudioReaderSource.h
Header file for AudioReaderSource class.
Exceptions.h
Header file for all Exception classes.
Frame.h
Header file for Frame class.
openshot::AudioReaderSource::AudioReaderSource
AudioReaderSource(ReaderBase *audio_reader, int64_t starting_frame_number)
The cache thread (for pre-roll checking).
Definition AudioReaderSource.cpp:21
openshot::AudioReaderSource::releaseResources
void releaseResources()
Release all resources.
Definition AudioReaderSource.cpp:97
openshot::AudioReaderSource::getTotalLength
juce::int64 getTotalLength() const
Get the total length (in samples) of this audio source.
Definition AudioReaderSource.cpp:100
openshot::AudioReaderSource::prepareToPlay
void prepareToPlay(int, double)
Prepare to play this audio source.
Definition AudioReaderSource.cpp:94
openshot::AudioReaderSource::getNextAudioBlock
void getNextAudioBlock(const juce::AudioSourceChannelInfo &info)
Get the next block of audio samples.
Definition AudioReaderSource.cpp:32
openshot::AudioReaderSource::~AudioReaderSource
~AudioReaderSource()
Destructor.
Definition AudioReaderSource.cpp:27
openshot::OutOfBoundsFrame
Exception for frames that are out of bounds.
Definition Exceptions.h:301
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
openshot::ReaderClosed
Exception when a reader is closed, and a frame is requested.
Definition Exceptions.h:364
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29