OpenShot Library | OpenShotAudio 0.2.2
juce_MemoryAudioSource.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26MemoryAudioSource::MemoryAudioSource (AudioBuffer<float>& bufferToUse, bool copyMemory, bool shouldLoop)
27 : isLooping (shouldLoop)
28{
29 if (copyMemory)
30 buffer.makeCopyOf (bufferToUse);
31 else
32 buffer.setDataToReferTo (bufferToUse.getArrayOfWritePointers(),
33 bufferToUse.getNumChannels(),
34 bufferToUse.getNumSamples());
35}
36
37//==============================================================================
38void MemoryAudioSource::prepareToPlay (int /*samplesPerBlockExpected*/, double /*sampleRate*/)
39{
40 position = 0;
41}
42
44
46{
47 auto& dst = *bufferToFill.buffer;
48 auto channels = jmin (dst.getNumChannels(), buffer.getNumChannels());
49 auto max = 0, pos = 0;
50 auto n = buffer.getNumSamples(), m = bufferToFill.numSamples;
51
52 int i;
53 for (i = position; (i < n || isLooping) && (pos < m); i += max)
54 {
55 max = jmin (m - pos, n - (i % n));
56
57 int ch = 0;
58 for (; ch < channels; ++ch)
59 dst.copyFrom (ch, bufferToFill.startSample + pos, buffer, ch, i % n, max);
60
61 for (; ch < dst.getNumChannels(); ++ch)
62 dst.clear (ch, bufferToFill.startSample + pos, max);
63
64 pos += max;
65 }
66
67 if (pos < m)
68 dst.clear (bufferToFill.startSample + pos, m - pos);
69
70 position = (i % n);
71}
72
73} // namespace juce
void makeCopyOf(const AudioBuffer< OtherType > &other, bool avoidReallocating=false)
Resizes this buffer to match the given one, and copies all of its content across.
int getNumChannels() const noexcept
Returns the number of channels of audio data that this buffer contains.
int getNumSamples() const noexcept
Returns the number of samples allocated in each of the buffer's channels.
void setDataToReferTo(Type **dataToReferTo, int newNumChannels, int newStartSample, int newNumSamples)
Makes this buffer point to a pre-allocated set of channel data arrays.
Type ** getArrayOfWritePointers() noexcept
Returns an array of pointers to the channels in the buffer.
MemoryAudioSource(AudioBuffer< float > &audioBuffer, bool copyMemory, bool shouldLoop=false)
Creates a MemoryAudioSource by providing an audio buffer.
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
Implementation of the AudioSource method.
void getNextAudioBlock(const AudioSourceChannelInfo &bufferToFill) override
Implementation of the AudioSource method.
void releaseResources() override
Implementation of the AudioSource method.
Used by AudioSource::getNextAudioBlock().
int numSamples
The number of samples in the buffer which the callback is expected to fill with data.
AudioBuffer< float > * buffer
The destination buffer to fill with audio data.
int startSample
The first sample in the buffer from which the callback is expected to write data.