1 /*
2  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <algorithm>
12 
13 #include "modules/audio_mixer/audio_frame_manipulator.h"
14 #include "modules/include/module_common_types.h"
15 #include "test/gtest.h"
16 
17 namespace webrtc {
18 namespace {
19 
FillFrameWithConstants(size_t samples_per_channel,size_t number_of_channels,int16_t value,AudioFrame * frame)20 void FillFrameWithConstants(size_t samples_per_channel,
21                             size_t number_of_channels,
22                             int16_t value,
23                             AudioFrame* frame) {
24   frame->num_channels_ = number_of_channels;
25   frame->samples_per_channel_ = samples_per_channel;
26   int16_t* frame_data = frame->mutable_data();
27   std::fill(frame_data,
28             frame_data + samples_per_channel * number_of_channels, value);
29 }
30 }  // namespace
31 
TEST(AudioFrameManipulator,CompareForwardRampWithExpectedResultStereo)32 TEST(AudioFrameManipulator, CompareForwardRampWithExpectedResultStereo) {
33   constexpr int kSamplesPerChannel = 5;
34   constexpr int kNumberOfChannels = 2;
35 
36   // Create a frame with values 5, 5, 5, ... and channels & samples as above.
37   AudioFrame frame;
38   FillFrameWithConstants(kSamplesPerChannel, kNumberOfChannels, 5, &frame);
39 
40   Ramp(0.0f, 1.0f, &frame);
41 
42   const int total_samples = kSamplesPerChannel * kNumberOfChannels;
43   const int16_t expected_result[total_samples] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4};
44   const int16_t* frame_data = frame.data();
45   EXPECT_TRUE(
46       std::equal(frame_data, frame_data + total_samples, expected_result));
47 }
48 
TEST(AudioFrameManipulator,CompareBackwardRampWithExpectedResultMono)49 TEST(AudioFrameManipulator, CompareBackwardRampWithExpectedResultMono) {
50   constexpr int kSamplesPerChannel = 5;
51   constexpr int kNumberOfChannels = 1;
52 
53   // Create a frame with values 5, 5, 5, ... and channels & samples as above.
54   AudioFrame frame;
55   FillFrameWithConstants(kSamplesPerChannel, kNumberOfChannels, 5, &frame);
56 
57   Ramp(1.0f, 0.0f, &frame);
58 
59   const int total_samples = kSamplesPerChannel * kNumberOfChannels;
60   const int16_t expected_result[total_samples] = {5, 4, 3, 2, 1};
61   const int16_t* frame_data = frame.data();
62   EXPECT_TRUE(
63       std::equal(frame_data, frame_data + total_samples, expected_result));
64 }
65 
66 }  // namespace webrtc
67