andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_COMMON_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_PROCESSING_COMMON_H_ |
| 13 | |
henrike@webrtc.org | f2aafe4 | 2014-04-29 17:54:17 +0000 | [diff] [blame] | 14 | #include <assert.h> |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 15 | #include <string.h> |
| 16 | |
| 17 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
| 18 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | static inline int ChannelsFromLayout(AudioProcessing::ChannelLayout layout) { |
| 23 | switch (layout) { |
| 24 | case AudioProcessing::kMono: |
| 25 | case AudioProcessing::kMonoAndKeyboard: |
| 26 | return 1; |
| 27 | case AudioProcessing::kStereo: |
| 28 | case AudioProcessing::kStereoAndKeyboard: |
| 29 | return 2; |
| 30 | } |
| 31 | assert(false); |
| 32 | return -1; |
| 33 | } |
| 34 | |
| 35 | // Helper to encapsulate a contiguous data buffer with access to a pointer |
| 36 | // array of the deinterleaved channels. |
| 37 | template <typename T> |
| 38 | class ChannelBuffer { |
| 39 | public: |
| 40 | ChannelBuffer(int samples_per_channel, int num_channels) |
| 41 | : data_(new T[samples_per_channel * num_channels]), |
| 42 | channels_(new T*[num_channels]), |
| 43 | samples_per_channel_(samples_per_channel), |
| 44 | num_channels_(num_channels) { |
| 45 | memset(data_.get(), 0, sizeof(T) * samples_per_channel * num_channels); |
| 46 | for (int i = 0; i < num_channels; ++i) |
| 47 | channels_[i] = &data_[i * samples_per_channel]; |
| 48 | } |
| 49 | ~ChannelBuffer() {} |
| 50 | |
| 51 | void CopyFrom(const void* channel_ptr, int i) { |
| 52 | assert(i < num_channels_); |
| 53 | memcpy(channels_[i], channel_ptr, samples_per_channel_ * sizeof(T)); |
| 54 | } |
| 55 | |
| 56 | T* data() { return data_.get(); } |
| 57 | T* channel(int i) { |
| 58 | assert(i < num_channels_); |
| 59 | return channels_[i]; |
| 60 | } |
| 61 | T** channels() { return channels_.get(); } |
| 62 | |
| 63 | int samples_per_channel() { return samples_per_channel_; } |
| 64 | int num_channels() { return num_channels_; } |
| 65 | int length() { return samples_per_channel_ * num_channels_; } |
| 66 | |
| 67 | private: |
| 68 | scoped_ptr<T[]> data_; |
| 69 | scoped_ptr<T*[]> channels_; |
| 70 | int samples_per_channel_; |
| 71 | int num_channels_; |
| 72 | }; |
| 73 | |
| 74 | } // namespace webrtc |
| 75 | |
| 76 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_COMMON_H_ |