blob: 6ecc07ab53ee91ac540bcefb1b8ce59ead7281f3 [file] [log] [blame]
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +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_CHANNEL_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_
13
aluebs@webrtc.org87893762014-11-27 23:40:25 +000014#include <string.h>
15
16#include "webrtc/base/checks.h"
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000017#include "webrtc/common_audio/include/audio_util.h"
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000018
19namespace webrtc {
20
aluebs@webrtc.org87893762014-11-27 23:40:25 +000021// Helper to encapsulate a contiguous data buffer with access to a pointer
22// array of the deinterleaved channels.
23template <typename T>
24class ChannelBuffer {
25 public:
26 ChannelBuffer(int samples_per_channel, int num_channels)
27 : data_(new T[samples_per_channel * num_channels]),
28 channels_(new T*[num_channels]),
29 samples_per_channel_(samples_per_channel),
30 num_channels_(num_channels) {
31 Initialize();
32 }
33
34 ChannelBuffer(const T* data, int samples_per_channel, int num_channels)
35 : data_(new T[samples_per_channel * num_channels]),
36 channels_(new T*[num_channels]),
37 samples_per_channel_(samples_per_channel),
38 num_channels_(num_channels) {
39 Initialize();
40 memcpy(data_.get(), data, length() * sizeof(T));
41 }
42
43 ChannelBuffer(const T* const* channels, int samples_per_channel,
44 int num_channels)
45 : data_(new T[samples_per_channel * num_channels]),
46 channels_(new T*[num_channels]),
47 samples_per_channel_(samples_per_channel),
48 num_channels_(num_channels) {
49 Initialize();
50 for (int i = 0; i < num_channels_; ++i)
51 CopyFrom(channels[i], i);
52 }
53
54 ~ChannelBuffer() {}
55
56 void CopyFrom(const void* channel_ptr, int i) {
57 DCHECK_LT(i, num_channels_);
58 memcpy(channels_[i], channel_ptr, samples_per_channel_ * sizeof(T));
59 }
60
61 T* data() { return data_.get(); }
62 const T* data() const { return data_.get(); }
63
64 const T* channel(int i) const {
65 DCHECK_GE(i, 0);
66 DCHECK_LT(i, num_channels_);
67 return channels_[i];
68 }
69 T* channel(int i) {
70 const ChannelBuffer<T>* t = this;
71 return const_cast<T*>(t->channel(i));
72 }
73
74 T* const* channels() { return channels_.get(); }
75 const T* const* channels() const { return channels_.get(); }
76
77 int samples_per_channel() const { return samples_per_channel_; }
78 int num_channels() const { return num_channels_; }
79 int length() const { return samples_per_channel_ * num_channels_; }
80
81 private:
82 void Initialize() {
83 memset(data_.get(), 0, sizeof(T) * length());
84 for (int i = 0; i < num_channels_; ++i)
85 channels_[i] = &data_[i * samples_per_channel_];
86 }
87
88 scoped_ptr<T[]> data_;
89 scoped_ptr<T*[]> channels_;
90 const int samples_per_channel_;
91 const int num_channels_;
92};
93
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000094// One int16_t and one float ChannelBuffer that are kept in sync. The sync is
95// broken when someone requests write access to either ChannelBuffer, and
96// reestablished when someone requests the outdated ChannelBuffer. It is
97// therefore safe to use the return value of ibuf_const() and fbuf_const()
98// until the next call to ibuf() or fbuf(), and the return value of ibuf() and
99// fbuf() until the next call to any of the other functions.
100class IFChannelBuffer {
101 public:
102 IFChannelBuffer(int samples_per_channel, int num_channels);
103
104 ChannelBuffer<int16_t>* ibuf();
105 ChannelBuffer<float>* fbuf();
106 const ChannelBuffer<int16_t>* ibuf_const() const;
107 const ChannelBuffer<float>* fbuf_const() const;
108
109 int num_channels() const { return ibuf_.num_channels(); }
110 int samples_per_channel() const { return ibuf_.samples_per_channel(); }
111
112 private:
113 void RefreshF() const;
114 void RefreshI() const;
115
116 mutable bool ivalid_;
117 mutable ChannelBuffer<int16_t> ibuf_;
118 mutable bool fvalid_;
119 mutable ChannelBuffer<float> fbuf_;
120};
121
122} // namespace webrtc
123
124#endif // WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_