OpenShot Library | libopenshot  0.3.3
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 
18 using namespace std;
19 using namespace openshot;
20 
21 // Constructor that reads samples from a reader
22 AudioReaderSource::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
29 {
30 }
31 
32 // Get the next block of audio samples
33 void 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 }
openshot::ReaderInfo::sample_rate
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:60
openshot::ReaderBase::GetFrame
virtual std::shared_ptr< openshot::Frame > GetFrame(int64_t number)=0
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::AudioReaderSource::~AudioReaderSource
~AudioReaderSource()
Destructor.
Definition: AudioReaderSource.cpp:28
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:88
openshot::AudioReaderSource::prepareToPlay
void prepareToPlay(int, double)
Prepare to play this audio source.
Definition: AudioReaderSource.cpp:96
int64
#define int64
Definition: Clip.h:17
openshot::ReaderInfo::duration
float duration
Length of time (in seconds)
Definition: ReaderBase.h:43
openshot::OutOfBoundsFrame
Exception for frames that are out of bounds.
Definition: Exceptions.h:300
openshot::AudioReaderSource::getTotalLength
juce::int64 getTotalLength() const
Get the total length (in samples) of this audio source.
Definition: AudioReaderSource.cpp:102
AudioReaderSource.h
Header file for AudioReaderSource class.
Frame.h
Header file for Frame class.
openshot::ReaderClosed
Exception when a reader is closed, and a frame is requested.
Definition: Exceptions.h:363
openshot::AudioReaderSource::getNextAudioBlock
void getNextAudioBlock(const juce::AudioSourceChannelInfo &info)
Get the next block of audio samples.
Definition: AudioReaderSource.cpp:33
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:75
openshot::AudioReaderSource::releaseResources
void releaseResources()
Release all resources.
Definition: AudioReaderSource.cpp:99
openshot::VideoCacheThread::isReady
bool isReady()
Is cache ready for video/audio playback.
Definition: VideoCacheThread.cpp:135
Exceptions.h
Header file for all Exception classes.