audio_buffer_tools.cc (2439B)
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 <cstring> 14 #include <vector> 15 16 #include "api/array_view.h" 17 #include "api/audio/audio_processing.h" 18 #include "modules/audio_processing/audio_buffer.h" 19 #include "rtc_base/checks.h" 20 21 namespace webrtc { 22 namespace test { 23 24 void SetupFrame(const StreamConfig& stream_config, 25 std::vector<float*>* frame, 26 std::vector<float>* frame_samples) { 27 frame_samples->resize(stream_config.num_channels() * 28 stream_config.num_frames()); 29 frame->resize(stream_config.num_channels()); 30 for (size_t ch = 0; ch < stream_config.num_channels(); ++ch) { 31 (*frame)[ch] = &(*frame_samples)[ch * stream_config.num_frames()]; 32 } 33 } 34 35 void CopyVectorToAudioBuffer(const StreamConfig& stream_config, 36 ArrayView<const float> source, 37 AudioBuffer* destination) { 38 std::vector<float*> input; 39 std::vector<float> input_samples; 40 41 SetupFrame(stream_config, &input, &input_samples); 42 43 RTC_CHECK_EQ(input_samples.size(), source.size()); 44 memcpy(input_samples.data(), source.data(), 45 source.size() * sizeof(source[0])); 46 47 destination->CopyFrom(&input[0], stream_config); 48 } 49 50 void ExtractVectorFromAudioBuffer(const StreamConfig& stream_config, 51 AudioBuffer* source, 52 std::vector<float>* destination) { 53 std::vector<float*> output; 54 55 SetupFrame(stream_config, &output, destination); 56 57 source->CopyTo(stream_config, &output[0]); 58 } 59 60 void FillBuffer(float value, AudioBuffer& audio_buffer) { 61 for (size_t ch = 0; ch < audio_buffer.num_channels(); ++ch) { 62 FillBufferChannel(value, ch, audio_buffer); 63 } 64 } 65 66 void FillBufferChannel(float value, int channel, AudioBuffer& audio_buffer) { 67 RTC_CHECK_LT(channel, audio_buffer.num_channels()); 68 for (size_t i = 0; i < audio_buffer.num_frames(); ++i) { 69 audio_buffer.channels()[channel][i] = value; 70 } 71 } 72 73 } // namespace test 74 } // namespace webrtc