Audio Processing Framework (APF) version 0.5.0
portaudio_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_PORTAUDIO_POLICY_H
31#define APF_PORTAUDIO_POLICY_H
32
33#include <portaudio.h>
34#include <cassert> // for assert()
35
36#include "apf/parameter_map.h"
37#include "apf/stringtools.h"
38#include "apf/iterator.h" // for has_begin_and_end
39
40#ifndef APF_MIMOPROCESSOR_INTERFACE_POLICY
41#define APF_MIMOPROCESSOR_INTERFACE_POLICY apf::portaudio_policy
42#endif
43
44namespace apf
45{
46
51{
52 public:
53 using sample_type = float;
54 class Input;
55 class Output;
56
57 struct portaudio_error : std::runtime_error
58 {
59 portaudio_error(PaError error)
60 : std::runtime_error("PortAudio: "+std::string(Pa_GetErrorText(error)))
61 {}
62 };
63
64 // const PaStreamInfo * Pa_GetStreamInfo (PaStream *stream)
65
66 static std::string device_info()
67 {
68 auto err = Pa_Initialize();
69 if (err != paNoError) throw portaudio_error(err);
70
71 std::string result;
72 for (int i = 0; i < Pa_GetDeviceCount(); ++i)
73 {
74 result = result + "Device ID " + str::A2S(i) + ": "
75 + Pa_GetDeviceInfo(i)->name + "\n";
76 }
77 return result;
78 }
79
80 bool activate()
81 {
82 // the original definition causes a warning message (old-style cast).
83 // 32bit float, non-interleaved
84 unsigned long sample_format = 0x00000001 | 0x80000000;
85
86 auto inputParameters = PaStreamParameters();
87 auto outputParameters = PaStreamParameters();
88
89 inputParameters.channelCount = _next_input_id;
90 inputParameters.device = _device_id;
91 inputParameters.hostApiSpecificStreamInfo = nullptr;
92 inputParameters.sampleFormat = sample_format;
93 inputParameters.suggestedLatency
94 = 0; //Pa_GetDeviceInfo(_device_id)->defaultLowInputLatency ;
95
96 outputParameters.channelCount = _next_output_id;
97 outputParameters.device = _device_id;
98 outputParameters.hostApiSpecificStreamInfo = nullptr;
99 outputParameters.sampleFormat = sample_format;
100 outputParameters.suggestedLatency
101 = 0; //Pa_GetDeviceInfo(_device_id)->defaultLowOutputLatency ;
102
103 auto err = Pa_OpenStream(&_stream, &inputParameters, &outputParameters
104 , _sample_rate, _block_size, 0, _pa_callback, this);
105
106 if (err != paNoError) throw portaudio_error(err);
107
108 err = Pa_StartStream(_stream);
109 if (err != paNoError) throw portaudio_error(err);
110 return true;
111 }
112
113 bool deactivate()
114 {
115 auto err = Pa_StopStream(_stream);
116 if (err != paNoError) throw portaudio_error(err);
117 return true;
118 }
119
120 unsigned long block_size() const { return _block_size; }
121 int sample_rate() const { return _sample_rate; }
122
123 int in_channels() const { return _next_input_id; }
124 int out_channels() const { return _next_output_id; }
125
126 // this is just temporary!
127 // TODO: find a clean solution regarding audio and thread policies
128 int get_real_time_priority() const { return -1; }
129
130 protected:
133 : _sample_rate(p.get<int>("sample_rate"))
134 , _block_size(p.get<unsigned long>("block_size"))
135 , _device_id(p.get("device_id", 0))
136 , _next_input_id(0)
137 , _next_output_id(0)
138 {
139 auto err = Pa_Initialize();
140 if (err != paNoError) throw portaudio_error(err);
141 }
142
145 {
146 Pa_CloseStream(_stream); // ignore errors
147 Pa_Terminate(); // ignore errors
148 }
149
150 private:
151 static int _pa_callback(const void *input, void *output
152 , unsigned long frameCount, const PaStreamCallbackTimeInfo* timeInfo
153 , PaStreamCallbackFlags statusFlags, void *userData)
154 {
155 (void)timeInfo; // not used
156 (void)statusFlags; // not used
157
158 return static_cast<portaudio_policy*>(userData)->pa_callback(input
159 , output, frameCount);
160 }
161
162 int pa_callback(const void *input, void *output, unsigned long frameCount)
163 {
164 (void)frameCount;
165 assert(frameCount == this->block_size());
166
167 _in = static_cast<sample_type* const*>(input);
168 _out = static_cast<sample_type* const*>(output);
169
170 this->process();
171
172 return paContinue;
173 // possible return values: paContinue, paComplete, paAbort
174 }
175
176 virtual void process() = 0;
177
180 int get_next_input_id() { return _next_input_id++; }
181
183 int get_next_output_id() { return _next_output_id++; }
184
185 const int _sample_rate;
186 const unsigned long _block_size;
187 const int _device_id;
188
189 int _next_input_id;
190 int _next_output_id;
191 sample_type* const* _in;
192 sample_type* const* _out;
193
194 PaStream* _stream;
195};
196
197class portaudio_policy::Input
198{
199 public:
200 using iterator = sample_type const*;
201
202 struct buffer_type : has_begin_and_end<iterator> { friend class Input; };
203
204 void fetch_buffer()
205 {
206 this->buffer._begin = _parent._in[_id];
207 this->buffer._end = this->buffer._begin + _parent.block_size();
208 }
209
210 buffer_type buffer;
211
212 protected:
213 Input(portaudio_policy& parent, const parameter_map&)
214 : _parent(parent)
215 , _id(_parent.get_next_input_id())
216 {}
217
218 ~Input() = default;
219
220 private:
221 portaudio_policy& _parent;
222 const int _id;
223};
224
225class portaudio_policy::Output
226{
227 public:
228 using iterator = sample_type*;
229
230 struct buffer_type : has_begin_and_end<iterator> { friend class Output; };
231
232 void fetch_buffer()
233 {
234 this->buffer._begin = _parent._out[_id];
235 this->buffer._end = this->buffer._begin + _parent.block_size();
236 }
237
238 buffer_type buffer;
239
240 protected:
241 Output(portaudio_policy& parent, const parameter_map&)
242 : _parent(parent)
243 , _id(_parent.get_next_output_id())
244 {}
245
246 ~Output() = default;
247
248 private:
249 portaudio_policy& _parent;
250 const int _id;
251};
252
253} // namespace apf
254
255#endif
interface_policy using PortAudio.
portaudio_policy(const parameter_map &p=parameter_map())
Constructor.
~portaudio_policy()
Protected destructor.
Several more or less useful iterators and some macros.
std::string A2S(const T &input)
Converter "Anything to String".
Definition: stringtools.h:52
Audio Processing Framework.
Definition: iterator.h:61
A "dictionary" for parameters.
Helper functions for string conversion.
A "dictionary" for parameters.
Definition: parameter_map.h:68