peah | 0197363 | 2016-03-03 11:21:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "modules/audio_processing/test/audio_buffer_tools.h" |
peah | 0197363 | 2016-03-03 11:21:51 -0800 | [diff] [blame] | 12 | |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
peah | 0197363 | 2016-03-03 11:21:51 -0800 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | namespace test { |
| 17 | |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 18 | void SetupFrame(const StreamConfig& stream_config, |
peah | 0197363 | 2016-03-03 11:21:51 -0800 | [diff] [blame] | 19 | 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 | |
| 29 | void CopyVectorToAudioBuffer(const StreamConfig& stream_config, |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 30 | rtc::ArrayView<const float> source, |
peah | 0197363 | 2016-03-03 11:21:51 -0800 | [diff] [blame] | 31 | AudioBuffer* destination) { |
| 32 | std::vector<float*> input; |
| 33 | std::vector<float> input_samples; |
| 34 | |
| 35 | SetupFrame(stream_config, &input, &input_samples); |
| 36 | |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 37 | RTC_CHECK_EQ(input_samples.size(), source.size()); |
| 38 | memcpy(input_samples.data(), source.data(), |
| 39 | source.size() * sizeof(source[0])); |
peah | 0197363 | 2016-03-03 11:21:51 -0800 | [diff] [blame] | 40 | |
| 41 | destination->CopyFrom(&input[0], stream_config); |
| 42 | } |
| 43 | |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 44 | void ExtractVectorFromAudioBuffer(const StreamConfig& stream_config, |
| 45 | AudioBuffer* source, |
| 46 | std::vector<float>* destination) { |
peah | 0197363 | 2016-03-03 11:21:51 -0800 | [diff] [blame] | 47 | std::vector<float*> output; |
peah | 0197363 | 2016-03-03 11:21:51 -0800 | [diff] [blame] | 48 | |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 49 | SetupFrame(stream_config, &output, destination); |
peah | 0197363 | 2016-03-03 11:21:51 -0800 | [diff] [blame] | 50 | |
| 51 | source->CopyTo(stream_config, &output[0]); |
peah | 0197363 | 2016-03-03 11:21:51 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | } // namespace test |
| 55 | } // namespace webrtc |