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