summaryrefslogtreecommitdiffstats
path: root/audio_utils/tests/balance_tests.cpp
blob: aa115d72ad4dc1bf110743690847de9cf21cc820 (plain)
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
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <audio_utils/Balance.h>
#include <gtest/gtest.h>
#include <vector>

TEST(audio_utils_balance, stereo) {
  // disable ramping so we can check single frame processing.
  android::audio_utils::Balance balance(false /* ramp */);
  ASSERT_EQ(false, balance.getRamp());

  balance.setChannelMask(AUDIO_CHANNEL_OUT_STEREO);
  std::vector<float> buffer = {1.f, -1.f};

  // balance of 0 is no change.
  ASSERT_EQ(0.f, balance.getBalance());
  balance.process(buffer.data(), 1 /* frames */);
  ASSERT_EQ((std::vector<float>{1.f, -1.f}), buffer);

  // balance of 1.f is right.
  balance.setBalance(1.f);
  ASSERT_EQ(1.f, balance.getBalance());
  balance.process(buffer.data(), 1 /* frames */);
  ASSERT_EQ((std::vector<float>{0.f, -1.f}), buffer);

  // balance of -1.f is left.
  buffer = {1.f, -1.f};
  balance.setBalance(-1.f); // to left
  ASSERT_EQ(-1.f, balance.getBalance());
  balance.process(buffer.data(), 1 /* frames */);
  ASSERT_EQ((std::vector<float>{1.f, 0.f}), buffer);
}

TEST(audio_utils_balance, 7point1) {
  // disable ramping so we can check single frame processing.
  android::audio_utils::Balance balance(false /* ramp */);
  ASSERT_EQ(false, balance.getRamp());

  balance.setChannelMask(AUDIO_CHANNEL_OUT_7POINT1);
  // FL, FR, FC, LFE, BL, BR, SL, SR
  std::vector<float> buffer = {1.f, -1.f, 0.5f, 0.25f, 0.75f, 0.75f, 0.125f, 0.125f};

  // balance of 0 is no change.
  ASSERT_EQ(0.f, balance.getBalance());
  balance.process(buffer.data(), 1 /* frames */);
  ASSERT_EQ((std::vector<float>{1.f, -1.f, 0.5f, 0.25f, 0.75f, 0.75f, 0.125f, 0.125f}), buffer);

  // balance of 1.f is right.
  balance.setBalance(1.f);
  balance.process(buffer.data(), 1 /* frames */);
  ASSERT_EQ((std::vector<float>{0.f, -1.f, 0.5f, 0.25f, 0.f, 0.75f, 0.f, 0.125f}), buffer);

  // balance of -1.f is left.
  buffer = {1.f, -1.f, 0.5f, 0.25f, 0.75f, 0.75f, 0.125f, 0.125f};
  balance.setBalance(-1.f); // to left
  ASSERT_EQ(-1.f, balance.getBalance());
  balance.process(buffer.data(), 1 /* frames */);
  ASSERT_EQ((std::vector<float>{1.f, 0.f, 0.5f, 0.25f, 0.75f, 0.f, 0.125f, 0.f}), buffer);
}

TEST(audio_utils_balance, lfe) {
  // disable ramping so we can check single frame processing.
  android::audio_utils::Balance balance(false /* ramp */);
  ASSERT_EQ(false, balance.getRamp());
  std::vector<float> buffer = {1.f, -1.f};

  // NOTE: single channel falls under mono exception (we ignore balance)
  // so we pair with another "center" channel.
  // LFE by itself is considered "center".
  for (auto channelMask : {
        (AUDIO_CHANNEL_OUT_FRONT_CENTER | AUDIO_CHANNEL_OUT_LOW_FREQUENCY),
        (AUDIO_CHANNEL_OUT_FRONT_CENTER | AUDIO_CHANNEL_OUT_LOW_FREQUENCY_2),
      }) {
    balance.setChannelMask((audio_channel_mask_t)channelMask);

      // balance of 0 is no change.
      balance.setBalance(0.f);
      balance.process(buffer.data(), 1 /* frames */);
      ASSERT_EQ((std::vector<float>{1.f, -1.f}), buffer);

      // balance of 1.f is right. (center unaffected)
      balance.setBalance(1.f);
      balance.process(buffer.data(), 1 /* frames */);
      ASSERT_EQ((std::vector<float>{1.f, -1.f}), buffer);

      // balance of -1.f is left. (center unaffected)
      balance.setBalance(-1.f);
      balance.process(buffer.data(), 1 /* frames */);
      ASSERT_EQ((std::vector<float>{1.f, -1.f}), buffer);
  }

  // If both LFE and LFE2 are present, we assume L/R.
  balance.setChannelMask((audio_channel_mask_t)
      (AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_LOW_FREQUENCY_2));
  // balance of 0 is no change.
  balance.setBalance(0.f); // to left
  balance.process(buffer.data(), 1 /* frames */);
  ASSERT_EQ((std::vector<float>{1.f, -1.f}), buffer);

  // balance of 1.f is right.
  balance.setBalance(1.f);
  balance.process(buffer.data(), 1 /* frames */);
  ASSERT_EQ((std::vector<float>{0.f, -1.f}), buffer);

  // balance of -1.f is left.
  buffer = {1.f, -1.f};
  balance.setBalance(-1.f); // to left
  balance.process(buffer.data(), 1 /* frames */);
  ASSERT_EQ((std::vector<float>{1.f, 0.f}), buffer);
}