blob: 64fb9c7ab1ebbc1d3e0e4ef51d77303b651757ef [file] [log] [blame]
peah01973632016-03-03 11:21:51 -08001/*
2 * Copyright (c) 2015 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/test/audio_buffer_tools.h"
peah01973632016-03-03 11:21:51 -080012
peah55850012016-03-19 18:01:09 -070013#include <string.h>
14
peah01973632016-03-03 11:21:51 -080015namespace webrtc {
16namespace test {
17
peah55850012016-03-19 18:01:09 -070018void SetupFrame(const StreamConfig& stream_config,
peah01973632016-03-03 11:21:51 -080019 std::vector<float*>* frame,
20 std::vector<float>* frame_samples) {
21 frame_samples->resize(stream_config.num_channels() *
22 stream_config.num_frames());
23 frame->resize(stream_config.num_channels());
24 for (size_t ch = 0; ch < stream_config.num_channels(); ++ch) {
25 (*frame)[ch] = &(*frame_samples)[ch * stream_config.num_frames()];
26 }
27}
28
29void CopyVectorToAudioBuffer(const StreamConfig& stream_config,
peah55850012016-03-19 18:01:09 -070030 rtc::ArrayView<const float> source,
peah01973632016-03-03 11:21:51 -080031 AudioBuffer* destination) {
32 std::vector<float*> input;
33 std::vector<float> input_samples;
34
35 SetupFrame(stream_config, &input, &input_samples);
36
peah55850012016-03-19 18:01:09 -070037 RTC_CHECK_EQ(input_samples.size(), source.size());
38 memcpy(input_samples.data(), source.data(),
39 source.size() * sizeof(source[0]));
peah01973632016-03-03 11:21:51 -080040
41 destination->CopyFrom(&input[0], stream_config);
42}
43
peah55850012016-03-19 18:01:09 -070044void ExtractVectorFromAudioBuffer(const StreamConfig& stream_config,
45 AudioBuffer* source,
46 std::vector<float>* destination) {
peah01973632016-03-03 11:21:51 -080047 std::vector<float*> output;
peah01973632016-03-03 11:21:51 -080048
peah55850012016-03-19 18:01:09 -070049 SetupFrame(stream_config, &output, destination);
peah01973632016-03-03 11:21:51 -080050
51 source->CopyTo(stream_config, &output[0]);
peah01973632016-03-03 11:21:51 -080052}
53
Per Ã…hgrendb5d7282021-03-15 16:31:04 +000054void FillBuffer(float value, AudioBuffer& audio_buffer) {
55 for (size_t ch = 0; ch < audio_buffer.num_channels(); ++ch) {
56 FillBufferChannel(value, ch, audio_buffer);
57 }
58}
59
60void FillBufferChannel(float value, int channel, AudioBuffer& audio_buffer) {
61 RTC_CHECK_LT(channel, audio_buffer.num_channels());
62 for (size_t i = 0; i < audio_buffer.num_frames(); ++i) {
63 audio_buffer.channels()[channel][i] = value;
64 }
65}
66
peah01973632016-03-03 11:21:51 -080067} // namespace test
68} // namespace webrtc