|
| 1 | +/* |
| 2 | + ============================================================================== |
| 3 | +
|
| 4 | + This file is part of the YUP library. |
| 5 | + Copyright (c) 2026 - kunitoki@gmail.com |
| 6 | +
|
| 7 | + YUP is an open source library subject to open-source licensing. |
| 8 | +
|
| 9 | + The code included in this file is provided under the terms of the ISC license |
| 10 | + http://www.isc.org/downloads/software-support-policy/isc-license. Permission |
| 11 | + to use, copy, modify, and/or distribute this software for any purpose with or |
| 12 | + without fee is hereby granted provided that the above copyright notice and |
| 13 | + this permission notice appear in all copies. |
| 14 | +
|
| 15 | + YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER |
| 16 | + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE |
| 17 | + DISCLAIMED. |
| 18 | +
|
| 19 | + ============================================================================== |
| 20 | +*/ |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | + |
| 24 | +#include <yup_audio_processors/yup_audio_processors.h> |
| 25 | + |
| 26 | +using namespace yup; |
| 27 | + |
| 28 | +TEST (ParameterChangeBufferTests, AddsAndClearsReservedChanges) |
| 29 | +{ |
| 30 | + ParameterChangeBuffer changes; |
| 31 | + |
| 32 | + EXPECT_TRUE (changes.isEmpty()); |
| 33 | + EXPECT_EQ (0, changes.getNumChanges()); |
| 34 | + |
| 35 | + changes.reserve (2); |
| 36 | + |
| 37 | + EXPECT_TRUE (changes.addChange (3, 0.25f, 12)); |
| 38 | + EXPECT_TRUE (changes.addChange (4, 0.5f, 24)); |
| 39 | + |
| 40 | + EXPECT_FALSE (changes.isEmpty()); |
| 41 | + EXPECT_EQ (2, changes.getNumChanges()); |
| 42 | + |
| 43 | + ASSERT_NE (changes.begin(), changes.end()); |
| 44 | + EXPECT_EQ (3, changes.begin()->parameterIndex); |
| 45 | + EXPECT_FLOAT_EQ (0.25f, changes.begin()->normalizedValue); |
| 46 | + EXPECT_EQ (12, changes.begin()->sampleOffset); |
| 47 | + |
| 48 | + changes.clear(); |
| 49 | + |
| 50 | + EXPECT_TRUE (changes.isEmpty()); |
| 51 | + EXPECT_EQ (0, changes.getNumChanges()); |
| 52 | +} |
| 53 | + |
| 54 | +TEST (ParameterChangeBufferTests, SortOrdersChangesBySampleOffset) |
| 55 | +{ |
| 56 | + ParameterChangeBuffer changes; |
| 57 | + changes.reserve (4); |
| 58 | + |
| 59 | + EXPECT_TRUE (changes.addChange (1, 0.3f, 24)); |
| 60 | + EXPECT_TRUE (changes.addChange (2, 0.4f, 4)); |
| 61 | + EXPECT_TRUE (changes.addChange (1, 0.2f, 12)); |
| 62 | + |
| 63 | + changes.sort(); |
| 64 | + |
| 65 | + ASSERT_EQ (3, changes.getNumChanges()); |
| 66 | + |
| 67 | + const auto* change = changes.begin(); |
| 68 | + EXPECT_EQ (4, change[0].sampleOffset); |
| 69 | + EXPECT_EQ (12, change[1].sampleOffset); |
| 70 | + EXPECT_EQ (24, change[2].sampleOffset); |
| 71 | +} |
| 72 | + |
| 73 | +TEST (ParameterChangeBufferTests, FindsNextChangeAtOrAfterSamplePosition) |
| 74 | +{ |
| 75 | + ParameterChangeBuffer changes; |
| 76 | + changes.reserve (3); |
| 77 | + |
| 78 | + EXPECT_TRUE (changes.addChange (1, 0.1f, 3)); |
| 79 | + EXPECT_TRUE (changes.addChange (1, 0.2f, 9)); |
| 80 | + EXPECT_TRUE (changes.addChange (1, 0.3f, 14)); |
| 81 | + changes.sort(); |
| 82 | + |
| 83 | + EXPECT_EQ (3, changes.findNextSamplePosition (0)->sampleOffset); |
| 84 | + EXPECT_EQ (3, changes.findNextSamplePosition (3)->sampleOffset); |
| 85 | + EXPECT_EQ (9, changes.findNextSamplePosition (4)->sampleOffset); |
| 86 | + EXPECT_EQ (14, changes.findNextSamplePosition (14)->sampleOffset); |
| 87 | + EXPECT_EQ (changes.end(), changes.findNextSamplePosition (15)); |
| 88 | +} |
| 89 | + |
| 90 | +TEST (AudioParameterHandleTests, AdvanceToSampleAppliesOnlyMatchingParameterChanges) |
| 91 | +{ |
| 92 | + auto parameter = AudioParameterBuilder() |
| 93 | + .withID ("gain") |
| 94 | + .withName ("Gain") |
| 95 | + .withRange (0.0f, 100.0f) |
| 96 | + .withDefault (10.0f) |
| 97 | + .build(); |
| 98 | + |
| 99 | + parameter->setIndexInContainer (2); |
| 100 | + |
| 101 | + AudioParameterHandle handle (*parameter, 48000.0); |
| 102 | + |
| 103 | + ParameterChangeBuffer changes; |
| 104 | + changes.reserve (4); |
| 105 | + EXPECT_TRUE (changes.addChange (1, 0.5f, 0)); |
| 106 | + EXPECT_TRUE (changes.addChange (2, 0.25f, 4)); |
| 107 | + EXPECT_TRUE (changes.addChange (2, 0.75f, 8)); |
| 108 | + changes.sort(); |
| 109 | + |
| 110 | + handle.prepareBlock (changes, parameter->getIndexInContainer()); |
| 111 | + |
| 112 | + EXPECT_FALSE (handle.advanceToSample (3)); |
| 113 | + EXPECT_FLOAT_EQ (10.0f, parameter->getValue()); |
| 114 | + |
| 115 | + EXPECT_TRUE (handle.advanceToSample (4)); |
| 116 | + EXPECT_FLOAT_EQ (25.0f, parameter->getValue()); |
| 117 | + EXPECT_FLOAT_EQ (25.0f, handle.getCurrentValue()); |
| 118 | + |
| 119 | + EXPECT_FALSE (handle.advanceToSample (7)); |
| 120 | + EXPECT_FLOAT_EQ (25.0f, parameter->getValue()); |
| 121 | + |
| 122 | + EXPECT_TRUE (handle.advanceToSample (8)); |
| 123 | + EXPECT_FLOAT_EQ (75.0f, parameter->getValue()); |
| 124 | + EXPECT_FLOAT_EQ (75.0f, handle.getCurrentValue()); |
| 125 | + |
| 126 | + EXPECT_FALSE (handle.advanceToSample (8)); |
| 127 | +} |
| 128 | + |
| 129 | +TEST (AudioParameterHandleTests, PrepareBlockRestartsAutomationIteration) |
| 130 | +{ |
| 131 | + auto parameter = AudioParameterBuilder() |
| 132 | + .withID ("mix") |
| 133 | + .withName ("Mix") |
| 134 | + .withRange (0.0f, 1.0f) |
| 135 | + .withDefault (0.0f) |
| 136 | + .build(); |
| 137 | + |
| 138 | + parameter->setIndexInContainer (0); |
| 139 | + |
| 140 | + AudioParameterHandle handle (*parameter, 48000.0); |
| 141 | + |
| 142 | + ParameterChangeBuffer changes; |
| 143 | + changes.reserve (1); |
| 144 | + EXPECT_TRUE (changes.addChange (0, 1.0f, 2)); |
| 145 | + |
| 146 | + handle.prepareBlock (changes, parameter->getIndexInContainer()); |
| 147 | + EXPECT_TRUE (handle.advanceToSample (2)); |
| 148 | + EXPECT_FLOAT_EQ (1.0f, parameter->getValue()); |
| 149 | + |
| 150 | + parameter->setValue (0.0f); |
| 151 | + |
| 152 | + handle.prepareBlock (changes, parameter->getIndexInContainer()); |
| 153 | + EXPECT_TRUE (handle.advanceToSample (2)); |
| 154 | + EXPECT_FLOAT_EQ (1.0f, parameter->getValue()); |
| 155 | +} |
0 commit comments