Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions modules/yup_audio_basics/buffers/yup_AudioSampleBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,56 @@ class AudioBuffer
*/
void setNotClear() noexcept { isClear = false; }

//==============================================================================
/** Fills all the samples in all channels by a value.

This method will do nothing if the buffer has been marked as cleared (i.e. the
hasBeenCleared method returns true.)
*/
void fill (Type value) noexcept
{
for (int i = 0; i < numChannels; ++i)
FloatVectorOperations::fill (channels[i], value, size);

isClear = false;
}

/** Fills a specified region of all the channels.

For speed, this doesn't check whether the channel and sample number
are in-range, so be careful!
*/
void fill (int startSample, int numSamples, Type value) noexcept
{
jassert (startSample >= 0 && startSample + numSamples <= size);

if (numSamples <= 0)
return;

for (int i = 0; i < numChannels; ++i)
FloatVectorOperations::fill (channels[i] + startSample, value, numSamples);

isClear = false;
}

/** Fills a specified region of just one channel.

For speed, this doesn't check whether the channel and sample number
are in-range, so be careful!
*/
void fill (int channel, int startSample, int numSamples, Type value) noexcept
{
jassert (isPositiveAndBelow (channel, numChannels));
jassert (startSample >= 0 && startSample + numSamples <= size);

if (numSamples <= 0)
return;

FloatVectorOperations::fill (channels[channel] + startSample, value, numSamples);

isClear = false;
}

//==============================================================================
/** Returns a sample from the buffer.

Expand Down
81 changes: 81 additions & 0 deletions tests/yup_audio_basics/yup_AudioSampleBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,87 @@ TYPED_TEST (AudioBufferTests, ClearAndHasBeenCleared)
EXPECT_TRUE (approximatelyEqual (buffer.getSample (0, 0), static_cast<TypeParam> (0)));
}

// Test fill methods
TYPED_TEST (AudioBufferTests, FillAllChannels)
{
using BufferType = typename TestFixture::BufferType;

BufferType buffer (3, 5);
buffer.clear();
EXPECT_TRUE (buffer.hasBeenCleared());

buffer.fill (static_cast<TypeParam> (2.5));

EXPECT_FALSE (buffer.hasBeenCleared());

for (int ch = 0; ch < buffer.getNumChannels(); ++ch)
for (int i = 0; i < buffer.getNumSamples(); ++i)
EXPECT_TRUE (approximatelyEqual (buffer.getSample (ch, i), static_cast<TypeParam> (2.5)));
}

TYPED_TEST (AudioBufferTests, FillRegionInAllChannels)
{
using BufferType = typename TestFixture::BufferType;

BufferType buffer;
this->initializeBuffer (buffer, 2, 6);

buffer.fill (2, 3, static_cast<TypeParam> (-4.0));

for (int ch = 0; ch < buffer.getNumChannels(); ++ch)
{
EXPECT_TRUE (approximatelyEqual (buffer.getSample (ch, 0), static_cast<TypeParam> (1.0)));
EXPECT_TRUE (approximatelyEqual (buffer.getSample (ch, 1), static_cast<TypeParam> (2.0)));
EXPECT_TRUE (approximatelyEqual (buffer.getSample (ch, 2), static_cast<TypeParam> (-4.0)));
EXPECT_TRUE (approximatelyEqual (buffer.getSample (ch, 3), static_cast<TypeParam> (-4.0)));
EXPECT_TRUE (approximatelyEqual (buffer.getSample (ch, 4), static_cast<TypeParam> (-4.0)));
EXPECT_TRUE (approximatelyEqual (buffer.getSample (ch, 5), static_cast<TypeParam> (6.0)));
}

EXPECT_FALSE (buffer.hasBeenCleared());
}

TYPED_TEST (AudioBufferTests, FillRegionInSingleChannel)
{
using BufferType = typename TestFixture::BufferType;

BufferType buffer;
this->initializeBuffer (buffer, 3, 5);

buffer.fill (1, 1, 3, static_cast<TypeParam> (9.0));

for (int ch = 0; ch < buffer.getNumChannels(); ++ch)
{
for (int i = 0; i < buffer.getNumSamples(); ++i)
{
const auto expected = (ch == 1 && i >= 1 && i <= 3) ? static_cast<TypeParam> (9.0)
: static_cast<TypeParam> (i + 1);

EXPECT_TRUE (approximatelyEqual (buffer.getSample (ch, i), expected));
}
}

EXPECT_FALSE (buffer.hasBeenCleared());
}

TYPED_TEST (AudioBufferTests, FillWithZeroSamplesDoesNothing)
{
using BufferType = typename TestFixture::BufferType;

BufferType buffer (2, 4);
buffer.clear();
EXPECT_TRUE (buffer.hasBeenCleared());

buffer.fill (1, 0, static_cast<TypeParam> (3.0));
buffer.fill (0, 1, 0, static_cast<TypeParam> (4.0));

EXPECT_TRUE (buffer.hasBeenCleared());

for (int ch = 0; ch < buffer.getNumChannels(); ++ch)
for (int i = 0; i < buffer.getNumSamples(); ++i)
EXPECT_TRUE (approximatelyEqual (buffer.getSample (ch, i), static_cast<TypeParam> (0)));
}

// Test getSample and setSample methods
TYPED_TEST (AudioBufferTests, GetAndSetSample)
{
Expand Down
2 changes: 2 additions & 0 deletions tests/yup_audio_formats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#endif

#include "yup_audio_formats/yup_AudioFormatManager.cpp"
#include "yup_audio_formats/yup_AudioFormatReader.cpp"
#include "yup_audio_formats/yup_AudioFormatWriter.cpp"

#if YUP_MODULE_AVAILABLE_dr_libs && YUP_AUDIO_FORMAT_WAVE
#include "yup_audio_formats/yup_WaveAudioFormat.cpp"
Expand Down
153 changes: 153 additions & 0 deletions tests/yup_audio_formats/yup_AudioFormatReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
==============================================================================

This file is part of the YUP library.
Copyright (c) 2026 - kunitoki@gmail.com

YUP is an open source library subject to 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.

YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.

==============================================================================
*/

#include <yup_audio_formats/yup_audio_formats.h>

#include <gtest/gtest.h>

#include <algorithm>
#include <array>

using namespace yup;

namespace
{
class MockAudioFormatReader : public AudioFormatReader
{
public:
MockAudioFormatReader (int numChannelsToUse, int numSamples)
: AudioFormatReader (nullptr, "MockAudioFormatReader")
, buffer (numChannelsToUse, numSamples)
{
sampleRate = 48000.0;
bitsPerSample = 32;
lengthInSamples = numSamples;
numChannels = numChannelsToUse;
usesFloatingPointData = true;
}

bool readSamples (float* const* destChannels,
int numDestChannels,
int startOffsetInDestBuffer,
int64 startSampleInFile,
int numSamples) override
{
if (startSampleInFile < 0 || startSampleInFile + numSamples > lengthInSamples)
return false;

const auto channelsToCopy = jmin (numDestChannels, numChannels);

for (int ch = 0; ch < channelsToCopy; ++ch)
{
if (destChannels[ch] == nullptr)
continue;

const auto* source = buffer.getReadPointer (ch) + startSampleInFile;
auto* destination = destChannels[ch] + startOffsetInDestBuffer;
std::copy (source, source + numSamples, destination);
}

return true;
}

AudioBuffer<float> buffer;
};

static void fillChannel (AudioBuffer<float>& buffer, int channel, std::initializer_list<float> values)
{
auto* data = buffer.getWritePointer (channel);

int index = 0;
for (float value : values)
data[index++] = value;
}
} // namespace

TEST (AudioFormatReaderTests, ReadCopiesMonoSourceToAllDestinationChannels)
{
MockAudioFormatReader reader (1, 4);
fillChannel (reader.buffer, 0, { 0.1f, -0.25f, 0.75f, 0.0f });

AudioBuffer<float> destination (3, 4);
destination.clear();

EXPECT_TRUE (reader.read (&destination, 0, 4, 0, true, false));

for (int channel = 0; channel < destination.getNumChannels(); ++channel)
{
for (int sample = 0; sample < destination.getNumSamples(); ++sample)
EXPECT_FLOAT_EQ (reader.buffer.getSample (0, sample), destination.getSample (channel, sample));
}
}

TEST (AudioFormatReaderTests, ReadClearsDestinationWhenNoChannelsAreRequested)
{
MockAudioFormatReader reader (2, 4);
fillChannel (reader.buffer, 0, { 0.1f, -0.25f, 0.75f, 0.0f });
fillChannel (reader.buffer, 1, { -0.8f, 0.5f, 0.2f, -0.1f });

AudioBuffer<float> destination (2, 4);
destination.fill (1.0f);

EXPECT_TRUE (reader.read (&destination, 0, 4, 0, false, false));

for (int channel = 0; channel < destination.getNumChannels(); ++channel)
{
for (int sample = 0; sample < destination.getNumSamples(); ++sample)
EXPECT_FLOAT_EQ (0.0f, destination.getSample (channel, sample));
}
}

TEST (AudioFormatReaderTests, ReadMaxLevelsReturnsChannelExtremes)
{
MockAudioFormatReader reader (2, 4);
fillChannel (reader.buffer, 0, { 0.1f, -0.4f, 0.25f, 0.05f });
fillChannel (reader.buffer, 1, { -0.9f, 0.2f, 0.6f, -0.1f });

Range<float> levels[2];
reader.readMaxLevels (0, 4, levels, 2);

EXPECT_NEAR (-0.4f, levels[0].getStart(), 1.0e-6f);
EXPECT_NEAR (0.25f, levels[0].getEnd(), 1.0e-6f);
EXPECT_NEAR (-0.9f, levels[1].getStart(), 1.0e-6f);
EXPECT_NEAR (0.6f, levels[1].getEnd(), 1.0e-6f);
}

TEST (AudioFormatReaderTests, SearchForLevelFindsFirstMatchingRun)
{
MockAudioFormatReader reader (2, 5);
fillChannel (reader.buffer, 0, { 0.1f, 0.55f, -0.6f, 0.2f, 0.1f });
fillChannel (reader.buffer, 1, { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f });

EXPECT_EQ (1, reader.searchForLevel (0, 5, 0.5, 0.7, 2));
EXPECT_EQ (-1, reader.searchForLevel (0, 5, 0.8, 0.9, 1));
}

TEST (AudioFormatReaderTests, GetChannelLayoutMatchesChannelCount)
{
MockAudioFormatReader monoReader (1, 1);
MockAudioFormatReader stereoReader (2, 1);
MockAudioFormatReader surroundReader (4, 1);

EXPECT_EQ (AudioChannelSet::mono(), monoReader.getChannelLayout());
EXPECT_EQ (AudioChannelSet::stereo(), stereoReader.getChannelLayout());
EXPECT_EQ (AudioChannelSet::discreteChannels (4), surroundReader.getChannelLayout());
}
Loading
Loading