1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
MemoryAudioSource::MemoryAudioSource (AudioBuffer<float>& bufferToUse, bool copyMemory, bool shouldLoop)
: isCurrentlyLooping (shouldLoop)
{
if (copyMemory)
buffer.makeCopyOf (bufferToUse);
else
buffer.setDataToReferTo (bufferToUse.getArrayOfWritePointers(),
bufferToUse.getNumChannels(),
bufferToUse.getNumSamples());
}
//==============================================================================
void MemoryAudioSource::prepareToPlay (int /*samplesPerBlockExpected*/, double /*sampleRate*/)
{
position = 0;
}
void MemoryAudioSource::releaseResources() {}
void MemoryAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
{
if (buffer.getNumSamples() == 0)
{
bufferToFill.clearActiveBufferRegion();
return;
}
auto& dst = *bufferToFill.buffer;
auto channels = jmin (dst.getNumChannels(), buffer.getNumChannels());
int max = 0, pos = 0;
auto n = buffer.getNumSamples();
auto m = bufferToFill.numSamples;
int i = position;
for (; (i < n || isCurrentlyLooping) && (pos < m); i += max)
{
max = jmin (m - pos, n - (i % n));
int ch = 0;
for (; ch < channels; ++ch)
dst.copyFrom (ch, bufferToFill.startSample + pos, buffer, ch, i % n, max);
for (; ch < dst.getNumChannels(); ++ch)
dst.clear (ch, bufferToFill.startSample + pos, max);
pos += max;
}
if (pos < m)
dst.clear (bufferToFill.startSample + pos, m - pos);
position = i;
}
//==============================================================================
void MemoryAudioSource::setNextReadPosition (int64 newPosition)
{
position = (int) newPosition;
}
int64 MemoryAudioSource::getNextReadPosition() const
{
return position;
}
int64 MemoryAudioSource::getTotalLength() const
{
return buffer.getNumSamples();
}
//==============================================================================
bool MemoryAudioSource::isLooping() const
{
return isCurrentlyLooping;
}
void MemoryAudioSource::setLooping (bool shouldLoop)
{
isCurrentlyLooping = shouldLoop;
}
//==============================================================================
//==============================================================================
#if JUCE_UNIT_TESTS
struct MemoryAudioSourceTests final : public UnitTest
{
MemoryAudioSourceTests() : UnitTest ("MemoryAudioSource", UnitTestCategories::audio) {}
void runTest() override
{
constexpr int blockSize = 512;
AudioBuffer<float> bufferToFill { 2, blockSize };
AudioSourceChannelInfo channelInfo { bufferToFill };
beginTest ("A zero-length buffer produces silence, whether or not looping is enabled");
{
for (const bool enableLooping : { false, true })
{
AudioBuffer<float> buffer;
MemoryAudioSource source { buffer, true, false };
source.setLooping (enableLooping);
source.prepareToPlay (blockSize, 44100.0);
for (int i = 0; i < 2; ++i)
{
play (source, channelInfo);
expect (isSilent (bufferToFill));
}
}
}
beginTest ("A short buffer without looping is played once and followed by silence");
{
auto buffer = getShortBuffer();
MemoryAudioSource source { buffer, true, false };
source.setLooping (false);
source.prepareToPlay (blockSize, 44100.0);
play (source, channelInfo);
auto copy = buffer;
copy.setSize (buffer.getNumChannels(), blockSize, true, true, false);
expect (bufferToFill == copy);
play (source, channelInfo);
expect (isSilent (bufferToFill));
}
beginTest ("A short buffer with looping is played multiple times");
{
auto buffer = getShortBuffer();
MemoryAudioSource source { buffer, true, false };
source.setLooping (true);
source.prepareToPlay (blockSize, 44100.0);
play (source, channelInfo);
for (int sample = 0; sample < buffer.getNumSamples(); ++sample)
expectEquals (bufferToFill.getSample (0, sample + buffer.getNumSamples()), buffer.getSample (0, sample));
expect (! isSilent (bufferToFill));
}
beginTest ("A long buffer without looping is played once");
{
auto buffer = getLongBuffer();
MemoryAudioSource source { buffer, true, false };
source.setLooping (false);
source.prepareToPlay (blockSize, 44100.0);
play (source, channelInfo);
auto copy = buffer;
copy.setSize (buffer.getNumChannels(), blockSize, true, true, false);
expect (bufferToFill == copy);
for (int i = 0; i < 10; ++i)
play (source, channelInfo);
expect (isSilent (bufferToFill));
}
beginTest ("A long buffer with looping is played multiple times");
{
auto buffer = getLongBuffer();
MemoryAudioSource source { buffer, true, false };
source.setLooping (true);
source.prepareToPlay (blockSize, 44100.0);
for (int i = 0; i < 100; ++i)
{
play (source, channelInfo);
expectEquals (bufferToFill.getSample (0, 0), buffer.getSample (0, (i * blockSize) % buffer.getNumSamples()));
}
}
}
static AudioBuffer<float> getTestBuffer (int length)
{
AudioBuffer<float> buffer { 2, length };
for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
for (int sample = 0; sample < buffer.getNumSamples(); ++sample)
buffer.setSample (channel, sample, jmap ((float) sample, 0.0f, (float) length, -1.0f, 1.0f));
return buffer;
}
static AudioBuffer<float> getShortBuffer() { return getTestBuffer (5); }
static AudioBuffer<float> getLongBuffer() { return getTestBuffer (1000); }
static void play (MemoryAudioSource& source, AudioSourceChannelInfo& info)
{
info.clearActiveBufferRegion();
source.getNextAudioBlock (info);
}
static bool isSilent (const AudioBuffer<float>& b)
{
for (int channel = 0; channel < b.getNumChannels(); ++channel)
if (b.findMinMax (channel, 0, b.getNumSamples()) != Range<float>{})
return false;
return true;
}
};
static MemoryAudioSourceTests memoryAudioSourceTests;
#endif
} // namespace juce
|