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