Audio Processing Framework (APF) version 0.5.0
pointer_policy.h
Go to the documentation of this file.
1/******************************************************************************
2 Copyright (c) 2012-2016 Institut für Nachrichtentechnik, Universität Rostock
3 Copyright (c) 2006-2012 Quality & Usability Lab
4 Deutsche Telekom Laboratories, TU Berlin
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23*******************************************************************************/
24
25// https://AudioProcessingFramework.github.io/
26
29
30#ifndef APF_POINTER_POLICY_H
31#define APF_POINTER_POLICY_H
32
33#include <cassert> // for assert()
34#include "apf/parameter_map.h"
35#include "apf/iterator.h" // for has_begin_and_end
36
37#ifndef APF_MIMOPROCESSOR_SAMPLE_TYPE
38#define APF_MIMOPROCESSOR_SAMPLE_TYPE float
39#endif
40#ifndef APF_MIMOPROCESSOR_INTERFACE_POLICY
41#define APF_MIMOPROCESSOR_INTERFACE_POLICY apf::pointer_policy<APF_MIMOPROCESSOR_SAMPLE_TYPE*>
42#endif
43
44namespace apf
45{
46
47template<typename T> class pointer_policy; // no implementation, use <T*>!
48
52template<typename T>
53class pointer_policy<T*>
54{
55 public:
56 using sample_type = T;
57
58 class Input;
59 class Output;
60
61 void audio_callback(size_t n, T* const* in, T* const* out);
62
63 // for now, do nothing:
64 bool activate() const { return true; }
65 bool deactivate() const { return true; }
66
67 size_t block_size() const { return _block_size; }
68 size_t sample_rate() const { return _sample_rate; }
69
70 int in_channels() const { return _next_input_id; }
71 int out_channels() const { return _next_output_id; }
72
73 protected:
74 explicit pointer_policy(const parameter_map& params = parameter_map())
75 : _sample_rate(params.get<size_t>("sample_rate"))
76 , _block_size(params.get<size_t>("block_size"))
77 , _next_input_id(0)
78 , _next_output_id(0)
79 , _in(0)
80 , _out(0)
81 {}
82
83 virtual ~pointer_policy() = default;
84
85 private:
86 virtual void process() = 0;
87
90 int get_next_input_id() { return _next_input_id++; }
91
93 int get_next_output_id() { return _next_output_id++; }
94
95 const size_t _sample_rate;
96 const size_t _block_size;
97
98 int _next_input_id;
99 int _next_output_id;
100 T* const* _in;
101 T* const* _out;
102};
103
115template<typename T>
116void
117pointer_policy<T*>::audio_callback(size_t n, T* const* in, T* const* out)
118{
119 assert(n == this->block_size());
120 (void)n; // avoid "unused parameter" warning
121
122 _in = in;
123 _out = out;
124 this->process();
125}
126
127template<typename T>
128class pointer_policy<T*>::Input
129{
130 public:
131 using iterator = T const*;
132
133 struct buffer_type : has_begin_and_end<iterator> { friend class Input; };
134
135 void fetch_buffer()
136 {
137 this->buffer._begin = _parent._in[_id];
138 this->buffer._end = this->buffer._begin + _parent.block_size();
139 }
140
141 buffer_type buffer;
142
143 protected:
144 Input(pointer_policy& parent, const parameter_map&)
145 : _parent(parent)
146 , _id(_parent.get_next_input_id())
147 {}
148
149 ~Input() = default;
150
151 private:
152 Input(const Input&); Input& operator=(const Input&); // deactivated
153
154 pointer_policy& _parent;
155 const int _id;
156};
157
158template<typename T>
159class pointer_policy<T*>::Output
160{
161 public:
162 using iterator = T*;
163
164 struct buffer_type : has_begin_and_end<iterator> { friend class Output; };
165
166 void fetch_buffer()
167 {
168 this->buffer._begin = _parent._out[_id];
169 this->buffer._end = this->buffer._begin + _parent.block_size();
170 }
171
172 buffer_type buffer;
173
174 protected:
175 Output(pointer_policy& parent, const parameter_map&)
176 : _parent(parent)
177 , _id(_parent.get_next_output_id())
178 {}
179
180 ~Output() = default;
181
182 private:
183 Output(const Output&); Output& operator=(const Output&); // deactivated
184
185 pointer_policy& _parent;
186 const int _id;
187};
188
189} // namespace apf
190
191#endif
Convenience class providing begin() and end().
Definition: iterator.h:335
void audio_callback(size_t n, T *const *in, T *const *out)
This has to be called for each audio block.
Several more or less useful iterators and some macros.
Audio Processing Framework.
Definition: iterator.h:61
A "dictionary" for parameters.
A "dictionary" for parameters.
Definition: parameter_map.h:68