summaryrefslogtreecommitdiff
path: root/cras/src/tests/audio_area_unittest.cc
blob: 091d8decfaec65f792fbc21ffcdb6853825b00c7 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <gtest/gtest.h>
#include <stdio.h>

extern "C" {
#include "cras_audio_area.h"
#include "cras_audio_format.h"
}

static const int8_t stereo[CRAS_CH_MAX] = {
    0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
static const int8_t mono[CRAS_CH_MAX] = {
    -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1,
};
static const int8_t kb_mic[CRAS_CH_MAX] = {
    0, 1, -1, -1, 2, -1, -1, -1, -1, -1, -1,
};

static uint16_t buf1[32];
static uint16_t buf2[32];
struct cras_audio_area* a1;
struct cras_audio_area* a2;

namespace {

TEST(AudioArea, CopyAudioArea) {
  struct cras_audio_format fmt;
  int i;

  fmt.num_channels = 2;
  fmt.format = SND_PCM_FORMAT_S16_LE;
  for (i = 0; i < CRAS_CH_MAX; i++)
    fmt.channel_layout[i] = stereo[i];

  a1 = cras_audio_area_create(2);
  a2 = cras_audio_area_create(2);
  cras_audio_area_config_channels(a1, &fmt);
  cras_audio_area_config_channels(a2, &fmt);
  cras_audio_area_config_buf_pointers(a1, &fmt, (uint8_t*)buf1);
  cras_audio_area_config_buf_pointers(a2, &fmt, (uint8_t*)buf2);
  a1->frames = 16;
  a2->frames = 16;

  memset(buf1, 0, 32 * 2);
  for (i = 0; i < 32; i++)
    buf2[i] = rand();
  cras_audio_area_copy(a1, 0, &fmt, a2, 0, 1.0);
  for (i = 0; i < 32; i++)
    EXPECT_EQ(buf1[i], buf2[i]);

  cras_audio_area_destroy(a1);
  cras_audio_area_destroy(a2);
}

TEST(AudioArea, CopyAudioAreaWithGain) {
  struct cras_audio_format fmt;
  int i;
  /* Check a gain of 10x can be applied. */
  float gain_scaler = 10.0f;

  fmt.num_channels = 2;
  fmt.format = SND_PCM_FORMAT_S16_LE;
  for (i = 0; i < CRAS_CH_MAX; i++)
    fmt.channel_layout[i] = stereo[i];

  a1 = cras_audio_area_create(2);
  a2 = cras_audio_area_create(2);
  cras_audio_area_config_channels(a1, &fmt);
  cras_audio_area_config_channels(a2, &fmt);
  cras_audio_area_config_buf_pointers(a1, &fmt, (uint8_t*)buf1);
  cras_audio_area_config_buf_pointers(a2, &fmt, (uint8_t*)buf2);
  a1->frames = 16;
  a2->frames = 16;

  memset(buf1, 0, 32 * 2);
  /* Let src has some samples smaller than 32768/10 and some samples larger than
   * 32768/10 to test clipping. */
  for (i = 0; i < 16; i++)
    buf2[i] = rand() % 3270;
  for (i = 17; i < 32; i++)
    buf2[i] = 3280 + rand() % 3200;
  cras_audio_area_copy(a1, 0, &fmt, a2, 0, gain_scaler);
  for (i = 0; i < 32; i++) {
    int32_t expected_value = buf2[i] * gain_scaler;
    if (expected_value > INT16_MAX)
      expected_value = INT16_MAX;
    EXPECT_EQ(buf1[i], expected_value);
  }

  cras_audio_area_destroy(a1);
  cras_audio_area_destroy(a2);
}
TEST(AudioArea, CopyAudioAreaOffset) {
  struct cras_audio_format fmt;
  int i;

  fmt.num_channels = 2;
  fmt.format = SND_PCM_FORMAT_S16_LE;
  for (i = 0; i < CRAS_CH_MAX; i++)
    fmt.channel_layout[i] = stereo[i];

  a1 = cras_audio_area_create(2);
  a2 = cras_audio_area_create(2);
  cras_audio_area_config_channels(a1, &fmt);
  cras_audio_area_config_channels(a2, &fmt);
  cras_audio_area_config_buf_pointers(a1, &fmt, (uint8_t*)buf1);
  cras_audio_area_config_buf_pointers(a2, &fmt, (uint8_t*)buf2);
  a1->frames = 16;
  a2->frames = 14;

  memset(buf1, 0, 32 * 2);
  for (i = 0; i < 32; i++)
    buf2[i] = rand();
  cras_audio_area_copy(a1, 2, &fmt, a2, 0, 1.0);
  EXPECT_EQ(buf1[0], 0);
  EXPECT_EQ(buf1[1], 0);
  EXPECT_EQ(buf1[2], 0);
  EXPECT_EQ(buf1[3], 0);
  for (i = 4; i < 32; i++)
    EXPECT_EQ(buf1[i], buf2[i - 4]);

  cras_audio_area_destroy(a1);
  cras_audio_area_destroy(a2);
}

TEST(AudioArea, CopyAudioAreaOffsetLimit) {
  struct cras_audio_format fmt;
  int i;

  fmt.num_channels = 2;
  fmt.format = SND_PCM_FORMAT_S16_LE;
  for (i = 0; i < CRAS_CH_MAX; i++)
    fmt.channel_layout[i] = stereo[i];

  a1 = cras_audio_area_create(2);
  a2 = cras_audio_area_create(2);
  cras_audio_area_config_channels(a1, &fmt);
  cras_audio_area_config_channels(a2, &fmt);
  cras_audio_area_config_buf_pointers(a1, &fmt, (uint8_t*)buf1);
  cras_audio_area_config_buf_pointers(a2, &fmt, (uint8_t*)buf2);
  a1->frames = 14;
  a2->frames = 14;

  memset(buf1, 0, 32 * 2);
  for (i = 0; i < 32; i++)
    buf2[i] = rand();
  cras_audio_area_copy(a1, 2, &fmt, a2, 0, 1.0);
  EXPECT_EQ(buf1[0], 0);
  EXPECT_EQ(buf1[1], 0);
  EXPECT_EQ(buf1[2], 0);
  EXPECT_EQ(buf1[3], 0);
  for (i = 4; i < 28; i++)
    EXPECT_EQ(buf1[i], buf2[i - 4]);
  EXPECT_EQ(buf1[28], 0);
  EXPECT_EQ(buf1[29], 0);
  EXPECT_EQ(buf1[30], 0);
  EXPECT_EQ(buf1[31], 0);

  cras_audio_area_destroy(a1);
  cras_audio_area_destroy(a2);
}

TEST(AudioArea, CopyMonoToStereo) {
  struct cras_audio_format dst_fmt;
  struct cras_audio_format src_fmt;
  int i;

  dst_fmt.num_channels = 2;
  dst_fmt.format = SND_PCM_FORMAT_S16_LE;
  for (i = 0; i < CRAS_CH_MAX; i++)
    dst_fmt.channel_layout[i] = stereo[i];
  a1 = cras_audio_area_create(2);
  a1->frames = 16;
  cras_audio_area_config_channels(a1, &dst_fmt);
  cras_audio_area_config_buf_pointers(a1, &dst_fmt, (uint8_t*)buf1);

  src_fmt.num_channels = 1;
  src_fmt.format = SND_PCM_FORMAT_S16_LE;
  for (i = 0; i < CRAS_CH_MAX; i++)
    src_fmt.channel_layout[i] = mono[i];
  a2 = cras_audio_area_create(1);
  a2->frames = 16;
  cras_audio_area_config_channels(a2, &src_fmt);
  cras_audio_area_config_buf_pointers(a2, &src_fmt, (uint8_t*)buf2);

  memset(buf1, 0, 32 * 2);
  for (i = 0; i < 32; i++)
    buf2[i] = rand();
  cras_audio_area_copy(a1, 0, &dst_fmt, a2, 0, 1.0);
  for (i = 0; i < 16; i++) {
    EXPECT_EQ(buf1[i * 2], buf2[i]);
    EXPECT_EQ(buf1[i * 2 + 1], buf2[i]);
  }

  cras_audio_area_destroy(a1);
  cras_audio_area_destroy(a2);
}

TEST(AudioArea, CopyStereoToMono) {
  struct cras_audio_format fmt;
  int i;

  fmt.num_channels = 1;
  fmt.format = SND_PCM_FORMAT_S16_LE;
  for (i = 0; i < CRAS_CH_MAX; i++)
    fmt.channel_layout[i] = mono[i];
  a1 = cras_audio_area_create(1);
  a1->frames = 16;
  cras_audio_area_config_channels(a1, &fmt);
  cras_audio_area_config_buf_pointers(a1, &fmt, (uint8_t*)buf1);

  fmt.num_channels = 2;
  for (i = 0; i < CRAS_CH_MAX; i++)
    fmt.channel_layout[i] = stereo[i];
  a2 = cras_audio_area_create(2);
  a2->frames = 16;
  cras_audio_area_config_channels(a2, &fmt);
  cras_audio_area_config_buf_pointers(a2, &fmt, (uint8_t*)buf2);

  memset(buf1, 0, 32 * 2);
  for (i = 0; i < 32; i++)
    buf2[i] = rand() % 10000;
  cras_audio_area_copy(a1, 0, &fmt, a2, 0, 1.0);
  for (i = 0; i < 16; i++)
    EXPECT_EQ(buf1[i], buf2[i * 2] + buf2[i * 2 + 1]);

  cras_audio_area_destroy(a1);
  cras_audio_area_destroy(a2);
}

TEST(AudioArea, KeyboardMicCopyStereo) {
  struct cras_audio_format fmt;
  int i;

  fmt.num_channels = 3;
  fmt.format = SND_PCM_FORMAT_S16_LE;
  for (i = 0; i < CRAS_CH_MAX; i++)
    fmt.channel_layout[i] = kb_mic[i];
  a1 = cras_audio_area_create(3);
  a1->frames = 10;
  cras_audio_area_config_channels(a1, &fmt);
  cras_audio_area_config_buf_pointers(a1, &fmt, (uint8_t*)buf1);

  fmt.num_channels = 2;
  for (i = 0; i < CRAS_CH_MAX; i++)
    fmt.channel_layout[i] = stereo[i];
  a2 = cras_audio_area_create(2);
  a2->frames = 10;
  cras_audio_area_config_channels(a2, &fmt);
  cras_audio_area_config_buf_pointers(a2, &fmt, (uint8_t*)buf2);

  memset(buf1, 0, 32 * 2);
  for (i = 0; i < 32; i++)
    buf2[i] = rand();
  cras_audio_area_copy(a1, 0, &fmt, a2, 0, 1.0);
  for (i = 0; i < 10; i++) {
    EXPECT_EQ(buf1[i * 3], buf2[i * 2]);
    EXPECT_EQ(buf1[i * 3 + 1], buf2[i * 2 + 1]);
    EXPECT_EQ(buf1[i * 3 + 2], 0);
  }

  cras_audio_area_destroy(a1);
  cras_audio_area_destroy(a2);
}

TEST(AudioArea, KeyboardMicCopyFrontCenter) {
  struct cras_audio_format dst_fmt;
  struct cras_audio_format src_fmt;
  int i;

  dst_fmt.num_channels = 3;
  dst_fmt.format = SND_PCM_FORMAT_S16_LE;
  for (i = 0; i < CRAS_CH_MAX; i++)
    dst_fmt.channel_layout[i] = kb_mic[i];
  a1 = cras_audio_area_create(3);
  a1->frames = 10;
  cras_audio_area_config_channels(a1, &dst_fmt);
  cras_audio_area_config_buf_pointers(a1, &dst_fmt, (uint8_t*)buf1);

  /* Test 2 channels area with only front center in layout. */
  src_fmt.num_channels = 2;
  src_fmt.format = SND_PCM_FORMAT_S16_LE;
  for (i = 0; i < CRAS_CH_MAX; i++)
    src_fmt.channel_layout[i] = -1;
  src_fmt.channel_layout[CRAS_CH_FC] = 0;
  a2 = cras_audio_area_create(2);
  a2->frames = 10;
  cras_audio_area_config_channels(a2, &src_fmt);
  cras_audio_area_config_buf_pointers(a2, &src_fmt, (uint8_t*)buf2);

  memset(buf1, 0, 32 * 2);
  for (i = 0; i < 32; i++)
    buf2[i] = rand();
  cras_audio_area_copy(a1, 0, &dst_fmt, a2, 0, 1.0);
  for (i = 0; i < 10; i++) {
    EXPECT_EQ(buf1[i * 3], 0);
    EXPECT_EQ(buf1[i * 3 + 1], 0);
    EXPECT_EQ(buf1[i * 3 + 2], buf2[i * 2]);
  }

  cras_audio_area_destroy(a1);
  cras_audio_area_destroy(a2);
}

}  //  namespace

extern "C" {

void cras_mix_add_scale_stride(snd_pcm_format_t fmt,
                               uint8_t* dst,
                               uint8_t* src,
                               unsigned int count,
                               unsigned int dst_stride,
                               unsigned int src_stride,
                               float scaler) {
  unsigned int i;

  for (i = 0; i < count; i++) {
    int32_t sum;
    sum = *(int16_t*)dst + *(int16_t*)src * scaler;
    if (sum > INT16_MAX)
      sum = INT16_MAX;
    else if (sum < INT16_MIN)
      sum = INT16_MIN;
    *(int16_t*)dst = sum;
    dst += dst_stride;
    src += src_stride;
  }
}

}  //  extern "C"

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}