OpenShot Library | libopenshot  0.5.0
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 using namespace std;
18 using namespace openshot;
19 
20 // Constructor that reads samples from a reader
21 AudioReaderSource::AudioReaderSource(ReaderBase *audio_reader, int64_t starting_frame_number)
22  : reader(audio_reader), frame_position(starting_frame_number), videoCache(NULL), frame(NULL),
23  sample_position(0), speed(1), stream_position(0) {
24 }
25 
26 // Destructor
28 {
29 }
30 
31 // Get the next block of audio samples
32 void AudioReaderSource::getNextAudioBlock(const juce::AudioSourceChannelInfo& info)
33 {
34  if (info.numSamples > 0) {
35  int remaining_samples = info.numSamples;
36  int remaining_position = info.startSample;
37 
38  // Pause and fill buffer with silence (wait for pre-roll)
39  if (speed != 1 || !videoCache->isReady()) {
40  info.buffer->clear();
41  return;
42  }
43 
44  while (remaining_samples > 0) {
45  try {
46  // Get current frame object
47  if (reader) {
48  frame = reader->GetFrame(frame_position);
49  }
50  }
51  catch (const ReaderClosed & e) { }
52  catch (const OutOfBoundsFrame & e) { }
53 
54  // Get audio samples
55  if (reader && frame) {
56  if (sample_position + remaining_samples <= frame->GetAudioSamplesCount()) {
57  // Success, we have enough samples
58  for (int channel = 0; channel < frame->GetAudioChannelsCount(); channel++) {
59  if (channel < info.buffer->getNumChannels()) {
60  info.buffer->addFrom(channel, remaining_position, *frame->GetAudioSampleBuffer(),
61  channel, sample_position, remaining_samples);
62  }
63  }
64  sample_position += remaining_samples;
65  remaining_position += remaining_samples;
66  remaining_samples = 0;
67 
68  } else if (sample_position + remaining_samples > frame->GetAudioSamplesCount()) {
69  // Not enough samples, take what we can
70  int amount_to_copy = frame->GetAudioSamplesCount() - sample_position;
71  for (int channel = 0; channel < frame->GetAudioChannelsCount(); channel++) {
72  if (channel < info.buffer->getNumChannels()) {
73  info.buffer->addFrom(channel, remaining_position, *frame->GetAudioSampleBuffer(), channel,
74  sample_position, amount_to_copy);
75  }
76  }
77  sample_position += amount_to_copy;
78  remaining_position += amount_to_copy;
79  remaining_samples -= amount_to_copy;
80  }
81 
82  // Increment frame position (if samples are all used up)
83  if (sample_position == frame->GetAudioSamplesCount()) {
84  frame_position += speed;
85  sample_position = 0; // reset for new frame
86  }
87 
88  }
89  }
90  }
91 }
92 
93 // Prepare to play this audio source
95 
96 // Release all resources
98 
99 // Get the total length (in samples) of this audio source
101 {
102  // Get the length
103  if (reader)
104  return reader->info.sample_rate * reader->info.duration;
105  else
106  return 0;
107 }
Header file for AudioReaderSource class.
#define int64
Definition: Clip.h:17
Header file for all Exception classes.
Header file for Frame class.
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
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