OpenShot Library | libopenshot
1.1.1
Toggle main menu visibility
Loading...
Searching...
No Matches
AudioBufferSource.cpp
Go to the documentation of this file.
1
8
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
24
AudioBufferSource::~AudioBufferSource
()
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
80
void
AudioBufferSource::releaseResources
() { }
81
82
// Set the next read position of this source
83
void
AudioBufferSource::setNextReadPosition
(juce::int64 newPosition)
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
91
juce::int64
AudioBufferSource::getNextReadPosition
()
const
92
{
93
// return the next read position
94
return
position;
95
}
96
97
// Get the total length (in samples) of this audio source
98
juce::int64
AudioBufferSource::getTotalLength
()
const
99
{
100
// Get the length
101
return
buffer->getNumSamples();
102
}
103
104
// Determines if this audio source should repeat when it reaches the end
105
bool
AudioBufferSource::isLooping
()
const
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
119
void
AudioBufferSource::setBuffer
(
juce::AudioBuffer<float>
*audio_buffer)
120
{
121
buffer = audio_buffer;
122
setNextReadPosition
(0);
123
}
AudioBufferSource.h
Header file for AudioBufferSource class.
juce::AudioBuffer
Definition
Frame.h:37
openshot::AudioBufferSource::AudioBufferSource
AudioBufferSource(juce::AudioBuffer< float > *audio_buffer)
Default constructor.
Definition
AudioBufferSource.cpp:19
openshot::AudioBufferSource::~AudioBufferSource
~AudioBufferSource()
Destructor.
Definition
AudioBufferSource.cpp:24
openshot::AudioBufferSource::prepareToPlay
void prepareToPlay(int, double)
Prepare to play this audio source.
Definition
AudioBufferSource.cpp:77
openshot::AudioBufferSource::releaseResources
void releaseResources()
Release all resources.
Definition
AudioBufferSource.cpp:80
openshot::AudioBufferSource::setBuffer
void setBuffer(juce::AudioBuffer< float > *audio_buffer)
Update the internal buffer used by this source.
Definition
AudioBufferSource.cpp:119
openshot::AudioBufferSource::getNextAudioBlock
void getNextAudioBlock(const juce::AudioSourceChannelInfo &info)
Get the next block of audio samples.
Definition
AudioBufferSource.cpp:31
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::getNextReadPosition
juce::int64 getNextReadPosition() const
Get the next read position of this source.
Definition
AudioBufferSource.cpp:91
openshot::AudioBufferSource::getTotalLength
juce::int64 getTotalLength() const
Get the total length (in samples) of this audio source.
Definition
AudioBufferSource.cpp:98
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
AnimatedCurve.h:24
AudioBufferSource.cpp
Generated by
1.17.0