OpenShot Library | libopenshot 0.3.3
Loading...
Searching...
No Matches
AudioReaderSource.cpp
Go to the documentation of this file.
1
9// Copyright (c) 2008-2019 OpenShot Studios, LLC
10//
11// SPDX-License-Identifier: LGPL-3.0-or-later
12
13#include "AudioReaderSource.h"
14#include "Exceptions.h"
15#include "Frame.h"
16
17
18using namespace std;
19using namespace openshot;
20
21// Constructor that reads samples from a reader
22AudioReaderSource::AudioReaderSource(ReaderBase *audio_reader, int64_t starting_frame_number)
23 : reader(audio_reader), frame_position(starting_frame_number), videoCache(NULL), frame(NULL),
24 sample_position(0), speed(1), stream_position(0) {
25}
26
27// Destructor
31
32// Get the next block of audio samples
33void AudioReaderSource::getNextAudioBlock(const juce::AudioSourceChannelInfo& info)
34{
35 if (info.numSamples > 0) {
36 int remaining_samples = info.numSamples;
37 int remaining_position = info.startSample;
38
39 // Pause and fill buffer with silence (wait for pre-roll)
40 if (speed != 1 || !videoCache->isReady()) {
41 info.buffer->clear();
42 return;
43 }
44
45 while (remaining_samples > 0) {
46
47 try {
48 // Get current frame object
49 if (reader) {
50 frame = reader->GetFrame(frame_position);
51 }
52 }
53 catch (const ReaderClosed & e) { }
54 catch (const OutOfBoundsFrame & e) { }
55
56 // Get audio samples
57 if (reader && frame) {
58 if (sample_position + remaining_samples <= frame->GetAudioSamplesCount()) {
59 // Success, we have enough samples
60 for (int channel = 0; channel < frame->GetAudioChannelsCount(); channel++) {
61 if (channel < info.buffer->getNumChannels()) {
62 info.buffer->addFrom(channel, remaining_position, *frame->GetAudioSampleBuffer(),
63 channel, sample_position, remaining_samples);
64 }
65 }
66 sample_position += remaining_samples;
67 remaining_position += remaining_samples;
68 remaining_samples = 0;
69
70 } else if (sample_position + remaining_samples > frame->GetAudioSamplesCount()) {
71 // Not enough samples, take what we can
72 int amount_to_copy = frame->GetAudioSamplesCount() - sample_position;
73 for (int channel = 0; channel < frame->GetAudioChannelsCount(); channel++) {
74 if (channel < info.buffer->getNumChannels()) {
75 info.buffer->addFrom(channel, remaining_position, *frame->GetAudioSampleBuffer(), channel,
76 sample_position, amount_to_copy);
77 }
78 }
79 sample_position += amount_to_copy;
80 remaining_position += amount_to_copy;
81 remaining_samples -= amount_to_copy;
82 }
83
84 // Increment frame position (if samples are all used up)
85 if (sample_position == frame->GetAudioSamplesCount()) {
86 frame_position += speed;
87 sample_position = 0; // reset for new frame
88 }
89
90 }
91 }
92 }
93}
94
95// Prepare to play this audio source
97
98// Release all resources
100
101// Get the total length (in samples) of this audio source
103{
104 // Get the length
105 if (reader)
106 return reader->info.sample_rate * reader->info.duration;
107 else
108 return 0;
109}
Header file for AudioReaderSource class.
Header file for all Exception classes.
Header file for Frame class.
AudioReaderSource(ReaderBase *audio_reader, int64_t starting_frame_number)
The cache thread (for pre-roll checking)
void releaseResources()
Release all resources.
juce::int64 getTotalLength() const
Get the total length (in samples) of this audio source.
void prepareToPlay(int, double)
Prepare to play this audio source.
void getNextAudioBlock(const juce::AudioSourceChannelInfo &info)
Get the next block of audio samples.
Exception for frames that are out of bounds.
Definition Exceptions.h:301
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
openshot::ReaderInfo info
Information about the current media file.
Definition ReaderBase.h:88
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
Exception when a reader is closed, and a frame is requested.
Definition Exceptions.h:364
bool isReady()
Is cache ready for video/audio playback.
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
float duration
Length of time (in seconds)
Definition ReaderBase.h:43
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition ReaderBase.h:60