OpenShot Audio Library | OpenShotAudio  0.3.1
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 
23 namespace juce
24 {
25 
26 MemoryAudioSource::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 //==============================================================================
38 void 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
Type ** getArrayOfWritePointers() noexcept
void makeCopyOf(const AudioBuffer< OtherType > &other, bool avoidReallocating=false)
int getNumChannels() const noexcept
int getNumSamples() const noexcept
void setDataToReferTo(Type **dataToReferTo, int newNumChannels, int newStartSample, int newNumSamples)
MemoryAudioSource(AudioBuffer< float > &audioBuffer, bool copyMemory, bool shouldLoop=false)
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
void getNextAudioBlock(const AudioSourceChannelInfo &bufferToFill) override
AudioBuffer< float > * buffer