1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <iostream> 18 19#include <gtest/gtest.h> 20 21#include "utility/AAudioUtilities.h" 22#include "utility/LinearRamp.h" 23 24 25TEST(test_linear_ramp, linear_ramp_segments) { 26 LinearRamp ramp; 27 const float source[4] = {1.0f, 1.0f, 1.0f, 1.0f }; 28 float destination[4] = {1.0f, 1.0f, 1.0f, 1.0f }; 29 30 float levelFrom = -1.0f; 31 float levelTo = -1.0f; 32 ramp.setLengthInFrames(8); 33 ramp.setTarget(8.0f); 34 35 ASSERT_EQ(8, ramp.getLengthInFrames()); 36 37 bool ramping = ramp.nextSegment(4, &levelFrom, &levelTo); 38 ASSERT_EQ(1, ramping); 39 ASSERT_EQ(0.0f, levelFrom); 40 ASSERT_EQ(4.0f, levelTo); 41 42 AAudio_linearRamp(source, destination, 4, 1, levelFrom, levelTo); 43 ASSERT_EQ(0.0f, destination[0]); 44 ASSERT_EQ(1.0f, destination[1]); 45 ASSERT_EQ(2.0f, destination[2]); 46 ASSERT_EQ(3.0f, destination[3]); 47 48 ramping = ramp.nextSegment(4, &levelFrom, &levelTo); 49 ASSERT_EQ(1, ramping); 50 ASSERT_EQ(4.0f, levelFrom); 51 ASSERT_EQ(8.0f, levelTo); 52 53 AAudio_linearRamp(source, destination, 4, 1, levelFrom, levelTo); 54 ASSERT_EQ(4.0f, destination[0]); 55 ASSERT_EQ(5.0f, destination[1]); 56 ASSERT_EQ(6.0f, destination[2]); 57 ASSERT_EQ(7.0f, destination[3]); 58 59 ramping = ramp.nextSegment(4, &levelFrom, &levelTo); 60 ASSERT_EQ(0, ramping); 61 ASSERT_EQ(8.0f, levelFrom); 62 ASSERT_EQ(8.0f, levelTo); 63 64 AAudio_linearRamp(source, destination, 4, 1, levelFrom, levelTo); 65 ASSERT_EQ(8.0f, destination[0]); 66 ASSERT_EQ(8.0f, destination[1]); 67 ASSERT_EQ(8.0f, destination[2]); 68 ASSERT_EQ(8.0f, destination[3]); 69 70}; 71 72 73TEST(test_linear_ramp, linear_ramp_forced) { 74 LinearRamp ramp; 75 const float source[4] = {1.0f, 1.0f, 1.0f, 1.0f }; 76 float destination[4] = {1.0f, 1.0f, 1.0f, 1.0f }; 77 78 float levelFrom = -1.0f; 79 float levelTo = -1.0f; 80 ramp.setLengthInFrames(4); 81 ramp.setTarget(8.0f); 82 ramp.forceCurrent(4.0f); 83 ASSERT_EQ(4.0f, ramp.getCurrent()); 84 85 bool ramping = ramp.nextSegment(4, &levelFrom, &levelTo); 86 ASSERT_EQ(1, ramping); 87 ASSERT_EQ(4.0f, levelFrom); 88 ASSERT_EQ(8.0f, levelTo); 89 90 AAudio_linearRamp(source, destination, 4, 1, levelFrom, levelTo); 91 ASSERT_EQ(4.0f, destination[0]); 92 ASSERT_EQ(5.0f, destination[1]); 93 ASSERT_EQ(6.0f, destination[2]); 94 ASSERT_EQ(7.0f, destination[3]); 95 96 ramping = ramp.nextSegment(4, &levelFrom, &levelTo); 97 ASSERT_EQ(0, ramping); 98 ASSERT_EQ(8.0f, levelFrom); 99 ASSERT_EQ(8.0f, levelTo); 100 101 AAudio_linearRamp(source, destination, 4, 1, levelFrom, levelTo); 102 ASSERT_EQ(8.0f, destination[0]); 103 ASSERT_EQ(8.0f, destination[1]); 104 ASSERT_EQ(8.0f, destination[2]); 105 ASSERT_EQ(8.0f, destination[3]); 106 107}; 108 109