OpenShot Library | libopenshot  0.3.3
AudioBufferSource.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 "AudioBufferSource.h"
14 
15 using namespace std;
16 using namespace openshot;
17 
18 // Default constructor
19 AudioBufferSource::AudioBufferSource(juce::AudioBuffer<float> *audio_buffer)
20  : position(0), repeat(false), buffer(audio_buffer)
21 { }
22 
23 // Destructor
25 {
26  // forget the AudioBuffer<float>. It still exists; we just don't know about it.
27  buffer = NULL;
28 }
29 
30 // Get the next block of audio samples
31 void AudioBufferSource::getNextAudioBlock (const juce::AudioSourceChannelInfo& info)
32 {
33  int buffer_samples = buffer->getNumSamples();
34  int buffer_channels = buffer->getNumChannels();
35 
36  if (info.numSamples > 0) {
37  int start = position;
38  int number_to_copy = 0;
39 
40  // Determine how many samples to copy
41  if (start + info.numSamples <= buffer_samples)
42  {
43  // copy the full amount requested
44  number_to_copy = info.numSamples;
45  }
46  else if (start > buffer_samples)
47  {
48  // copy nothing
49  number_to_copy = 0;
50  }
51  else if (buffer_samples - start > 0)
52  {
53  // only copy what is left in the buffer
54  number_to_copy = buffer_samples - start;
55  }
56  else
57  {
58  // copy nothing
59  number_to_copy = 0;
60  }
61 
62  // Determine if any samples need to be copied
63  if (number_to_copy > 0)
64  {
65  // Loop through each channel and copy some samples
66  for (int channel = 0; channel < buffer_channels; channel++)
67  info.buffer->copyFrom(channel, info.startSample, *buffer, channel, start, number_to_copy);
68 
69  // Update the position of this audio source
70  position += number_to_copy;
71  }
72 
73  }
74 }
75 
76 // Prepare to play this audio source
77 void AudioBufferSource::prepareToPlay(int, double) { }
78 
79 // Release all resources
81 
82 // Set the next read position of this source
84 {
85  // set position (if the new position is in range)
86  if (newPosition >= 0 && newPosition < buffer->getNumSamples())
87  position = newPosition;
88 }
89 
90 // Get the next read position of this source
92 {
93  // return the next read position
94  return position;
95 }
96 
97 // Get the total length (in samples) of this audio source
99 {
100  // Get the length
101  return buffer->getNumSamples();
102 }
103 
104 // Determines if this audio source should repeat when it reaches the end
106 {
107  // return if this source is looping
108  return repeat;
109 }
110 
111 // Set if this audio source should repeat when it reaches the end
112 void AudioBufferSource::setLooping (bool shouldLoop)
113 {
114  // Set the repeat flag
115  repeat = shouldLoop;
116 }
117 
118 // Use a different AudioBuffer<float> for this source
120 {
121  buffer = audio_buffer;
123 }
openshot::AudioBufferSource::getNextReadPosition
juce::int64 getNextReadPosition() const
Get the next read position of this source.
Definition: AudioBufferSource.cpp:91
openshot::AudioBufferSource::getNextAudioBlock
void getNextAudioBlock(const juce::AudioSourceChannelInfo &info)
Get the next block of audio samples.
Definition: AudioBufferSource.cpp:31
openshot::AudioBufferSource::setLooping
void setLooping(bool shouldLoop)
Set if this audio source should repeat when it reaches the end.
Definition: AudioBufferSource.cpp:112
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
juce::AudioBuffer< float >
AudioBufferSource.h
Header file for AudioBufferSource class.
int64
#define int64
Definition: Clip.h:17
openshot::AudioBufferSource::isLooping
bool isLooping() const
Determines if this audio source should repeat when it reaches the end.
Definition: AudioBufferSource.cpp:105
openshot::AudioBufferSource::setNextReadPosition
void setNextReadPosition(juce::int64 newPosition)
Set the next read position of this source.
Definition: AudioBufferSource.cpp:83
openshot::AudioBufferSource::setBuffer
void setBuffer(juce::AudioBuffer< float > *audio_buffer)
Update the internal buffer used by this source.
Definition: AudioBufferSource.cpp:119
openshot::AudioBufferSource::releaseResources
void releaseResources()
Release all resources.
Definition: AudioBufferSource.cpp:80
openshot::AudioBufferSource::getTotalLength
juce::int64 getTotalLength() const
Get the total length (in samples) of this audio source.
Definition: AudioBufferSource.cpp:98
openshot::AudioBufferSource::prepareToPlay
void prepareToPlay(int, double)
Prepare to play this audio source.
Definition: AudioBufferSource.cpp:77
openshot::AudioBufferSource::~AudioBufferSource
~AudioBufferSource()
Destructor.
Definition: AudioBufferSource.cpp:24