OpenShot Library | libopenshot  0.2.7
AudioBufferSource.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for AudioBufferSource class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "AudioBufferSource.h"
32 
33 using namespace std;
34 using namespace openshot;
35 
36 // Default constructor
37 AudioBufferSource::AudioBufferSource(juce::AudioSampleBuffer *audio_buffer)
38  : position(0), repeat(false), buffer(audio_buffer)
39 { }
40 
41 // Destructor
43 {
44  // forget the AudioSampleBuffer. It still exists; we just don't know about it.
45  buffer = NULL;
46 }
47 
48 // Get the next block of audio samples
49 void AudioBufferSource::getNextAudioBlock (const juce::AudioSourceChannelInfo& info)
50 {
51  int buffer_samples = buffer->getNumSamples();
52  int buffer_channels = buffer->getNumChannels();
53 
54  if (info.numSamples > 0) {
55  int start = position;
56  int number_to_copy = 0;
57 
58  // Determine how many samples to copy
59  if (start + info.numSamples <= buffer_samples)
60  {
61  // copy the full amount requested
62  number_to_copy = info.numSamples;
63  }
64  else if (start > buffer_samples)
65  {
66  // copy nothing
67  number_to_copy = 0;
68  }
69  else if (buffer_samples - start > 0)
70  {
71  // only copy what is left in the buffer
72  number_to_copy = buffer_samples - start;
73  }
74  else
75  {
76  // copy nothing
77  number_to_copy = 0;
78  }
79 
80  // Determine if any samples need to be copied
81  if (number_to_copy > 0)
82  {
83  // Loop through each channel and copy some samples
84  for (int channel = 0; channel < buffer_channels; channel++)
85  info.buffer->copyFrom(channel, info.startSample, *buffer, channel, start, number_to_copy);
86 
87  // Update the position of this audio source
88  position += number_to_copy;
89  }
90 
91  }
92 }
93 
94 // Prepare to play this audio source
95 void AudioBufferSource::prepareToPlay(int, double) { }
96 
97 // Release all resources
99 
100 // Set the next read position of this source
102 {
103  // set position (if the new position is in range)
104  if (newPosition >= 0 && newPosition < buffer->getNumSamples())
105  position = newPosition;
106 }
107 
108 // Get the next read position of this source
110 {
111  // return the next read position
112  return position;
113 }
114 
115 // Get the total length (in samples) of this audio source
117 {
118  // Get the length
119  return buffer->getNumSamples();
120 }
121 
122 // Determines if this audio source should repeat when it reaches the end
124 {
125  // return if this source is looping
126  return repeat;
127 }
128 
129 // Set if this audio source should repeat when it reaches the end
130 void AudioBufferSource::setLooping (bool shouldLoop)
131 {
132  // Set the repeat flag
133  repeat = shouldLoop;
134 }
135 
136 // Use a different AudioSampleBuffer for this source
137 void AudioBufferSource::setBuffer (juce::AudioSampleBuffer *audio_buffer)
138 {
139  buffer = audio_buffer;
141 }
Header file for AudioBufferSource class.
#define int64
Definition: Clip.h:35
void prepareToPlay(int, double)
Prepare to play this audio source.
void releaseResources()
Release all resources.
void setBuffer(juce::AudioSampleBuffer *audio_buffer)
Update the internal buffer used by this source.
void getNextAudioBlock(const juce::AudioSourceChannelInfo &info)
Get the next block of audio samples.
bool isLooping() const
Determines if this audio source should repeat when it reaches the end.
void setNextReadPosition(juce::int64 newPosition)
Set the next read position of this source.
juce::int64 getNextReadPosition() const
Get the next read position of this source.
juce::int64 getTotalLength() const
Get the total length (in samples) of this audio source.
void setLooping(bool shouldLoop)
Set if this audio source should repeat when it reaches the end.
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47