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 
11 #include "modules/audio_processing/test/audio_buffer_tools.h"
12 
13 #include <string.h>
14 
15 namespace webrtc {
16 namespace test {
17 
SetupFrame(const StreamConfig & stream_config,std::vector<float * > * frame,std::vector<float> * frame_samples)18 void SetupFrame(const StreamConfig& stream_config,
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 
CopyVectorToAudioBuffer(const StreamConfig & stream_config,rtc::ArrayView<const float> source,AudioBuffer * destination)29 void CopyVectorToAudioBuffer(const StreamConfig& stream_config,
30                              rtc::ArrayView<const float> source,
31                              AudioBuffer* destination) {
32   std::vector<float*> input;
33   std::vector<float> input_samples;
34 
35   SetupFrame(stream_config, &input, &input_samples);
36 
37   RTC_CHECK_EQ(input_samples.size(), source.size());
38   memcpy(input_samples.data(), source.data(),
39          source.size() * sizeof(source[0]));
40 
41   destination->CopyFrom(&input[0], stream_config);
42 }
43 
ExtractVectorFromAudioBuffer(const StreamConfig & stream_config,AudioBuffer * source,std::vector<float> * destination)44 void ExtractVectorFromAudioBuffer(const StreamConfig& stream_config,
45                                   AudioBuffer* source,
46                                   std::vector<float>* destination) {
47   std::vector<float*> output;
48 
49   SetupFrame(stream_config, &output, destination);
50 
51   source->CopyTo(stream_config, &output[0]);
52 }
53 
54 }  // namespace test
55 }  // namespace webrtc
56