To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

The primary repository for this project is hosted at https://github.com/sonic-visualiser/sv-dependency-builds .
This repository is a read-only copy which is updated automatically every hour.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / src / portaudio_20161030_catalina_patch / bindings / cpp / source / portaudiocpp / System.cxx @ 164:9fa11135915a

History | View | Annotate | Download (5.79 KB)

1
#include "portaudiocpp/System.hxx"
2

    
3
#include <cstddef>
4
#include <cassert>
5

    
6
#include "portaudiocpp/HostApi.hxx"
7
#include "portaudiocpp/Device.hxx"
8
#include "portaudiocpp/Stream.hxx"
9
#include "portaudiocpp/Exception.hxx"
10
#include "portaudiocpp/SystemHostApiIterator.hxx"
11
#include "portaudiocpp/SystemDeviceIterator.hxx"
12

    
13
namespace portaudio
14
{
15
        // -----------------------------------------------------------------------------------
16

    
17
        // Static members:
18
        System *System::instance_ = NULL;
19
        int System::initCount_ = 0;
20
        HostApi **System::hostApis_ = NULL;
21
        Device **System::devices_ = NULL;
22
        Device *System::nullDevice_ = NULL;
23

    
24
        // -----------------------------------------------------------------------------------
25

    
26
        int System::version()
27
        {
28
                return Pa_GetVersion();
29
        }
30

    
31
        const char *System::versionText()
32
        {
33
                return Pa_GetVersionText();
34
        }
35

    
36
        void System::initialize()
37
        {
38
                ++initCount_;
39

    
40
                if (initCount_ == 1)
41
                {
42
                        // Create singleton:
43
                        assert(instance_ == NULL);
44
                        instance_ = new System();
45

    
46
                        // Initialize the PortAudio system:
47
                        {
48
                                PaError err = Pa_Initialize();
49

    
50
                                if (err != paNoError)
51
                                        throw PaException(err);
52
                        }
53

    
54
                        // Create and populate device array:
55
                        {
56
                                int numDevices = instance().deviceCount();
57

    
58
                                devices_ = new Device*[numDevices];
59

    
60
                                for (int i = 0; i < numDevices; ++i)
61
                                        devices_[i] = new Device(i);
62
                        }
63

    
64
                        // Create and populate host api array:
65
                        {
66
                                int numHostApis = instance().hostApiCount();
67

    
68
                                hostApis_ = new HostApi*[numHostApis];
69

    
70
                                for (int i = 0; i < numHostApis; ++i)
71
                                        hostApis_[i] = new HostApi(i);
72
                        }
73
                        
74
                        // Create null device:
75
                        nullDevice_ = new Device(paNoDevice);
76
                }
77
        }
78

    
79
        void System::terminate()
80
        {
81
                PaError err = paNoError;
82

    
83
                if (initCount_ == 1)
84
                {
85
                        // Destroy null device:
86
                        delete nullDevice_;
87

    
88
                        // Destroy host api array:
89
                        {
90
                                if (hostApis_ != NULL)
91
                                {
92
                                        int numHostApis = instance().hostApiCount();
93

    
94
                                        for (int i = 0; i < numHostApis; ++i)
95
                                                delete hostApis_[i];
96

    
97
                                        delete[] hostApis_;
98
                                        hostApis_ = NULL;
99
                                }
100
                        }
101

    
102
                        // Destroy device array:
103
                        {
104
                                if (devices_ != NULL)
105
                                {
106
                                        int numDevices = instance().deviceCount();
107

    
108
                                        for (int i = 0; i < numDevices; ++i)
109
                                                delete devices_[i];
110

    
111
                                        delete[] devices_;
112
                                        devices_ = NULL;
113
                                }
114
                        }
115

    
116
                        // Terminate the PortAudio system:
117
                        assert(instance_ != NULL);
118
                        err = Pa_Terminate();
119

    
120
                        // Destroy singleton:
121
                        delete instance_;
122
                        instance_ = NULL;
123
                }
124

    
125
                if (initCount_ > 0)
126
                        --initCount_;
127

    
128
                if (err != paNoError)
129
                        throw PaException(err);
130
        }
131

    
132

    
133
        System &System::instance()
134
        {
135
                assert(exists());
136

    
137
                return *instance_;
138
        }
139

    
140
        bool System::exists()
141
        {
142
                return (instance_ != NULL);
143
        }
144

    
145
        // -----------------------------------------------------------------------------------
146

    
147
        System::HostApiIterator System::hostApisBegin()
148
        {
149
                System::HostApiIterator tmp;
150
                tmp.ptr_ = &hostApis_[0]; // begin (first element)
151
                return tmp;
152
        }
153

    
154
        System::HostApiIterator System::hostApisEnd()
155
        {
156
                int count = hostApiCount();
157

    
158
                System::HostApiIterator tmp;
159
                tmp.ptr_ = &hostApis_[count]; // end (one past last element)
160
                return tmp;
161
        }
162

    
163
        HostApi &System::defaultHostApi()
164
        {
165
                PaHostApiIndex defaultHostApi = Pa_GetDefaultHostApi();
166

    
167
                if (defaultHostApi < 0)
168
                        throw PaException(defaultHostApi);
169

    
170
                return *hostApis_[defaultHostApi];
171
        }
172

    
173
        HostApi &System::hostApiByTypeId(PaHostApiTypeId type)
174
        {
175
                PaHostApiIndex index = Pa_HostApiTypeIdToHostApiIndex(type);
176

    
177
                if (index < 0)
178
                        throw PaException(index);
179

    
180
                return *hostApis_[index];
181
        }
182

    
183
        HostApi &System::hostApiByIndex(PaHostApiIndex index)
184
        {
185
                if (index < 0 || index >= hostApiCount())
186
                        throw PaException(paInternalError);
187

    
188
                return *hostApis_[index];
189
        }
190

    
191
        int System::hostApiCount()
192
        {
193
                PaHostApiIndex count = Pa_GetHostApiCount();
194

    
195
                if (count < 0)
196
                        throw PaException(count);
197

    
198
                return count;
199
        }
200

    
201
        // -----------------------------------------------------------------------------------
202

    
203
        System::DeviceIterator System::devicesBegin()
204
        {
205
                DeviceIterator tmp;
206
                tmp.ptr_ = &devices_[0];
207

    
208
                return tmp;
209
        }
210

    
211
        System::DeviceIterator System::devicesEnd()
212
        {
213
                int count = deviceCount();
214

    
215
                DeviceIterator tmp;
216
                tmp.ptr_ = &devices_[count];
217

    
218
                return tmp;
219
        }
220

    
221
        //////
222
        /// Returns the System's default input Device, or the null Device if none 
223
        /// was available.
224
        //////
225
        Device &System::defaultInputDevice()
226
        {
227
                PaDeviceIndex index = Pa_GetDefaultInputDevice();
228
                return deviceByIndex(index);
229
        }
230

    
231
        //////
232
        /// Returns the System's default output Device, or the null Device if none 
233
        /// was available.
234
        //////
235
        Device &System::defaultOutputDevice()
236
        {
237
                PaDeviceIndex index = Pa_GetDefaultOutputDevice();
238
                return deviceByIndex(index);
239
        }
240

    
241
        //////
242
        /// Returns the Device for the given index.
243
        /// Will throw a paInternalError equivalent PaException if the given index 
244
        /// is out of range.
245
        //////
246
        Device &System::deviceByIndex(PaDeviceIndex index)
247
        {
248
                if (index < -1 || index >= deviceCount())
249
                {
250
                        throw PaException(paInternalError);
251
                }
252

    
253
                if (index == -1)
254
                        return System::instance().nullDevice();
255

    
256
                return *devices_[index];
257
        }
258

    
259
        int System::deviceCount()
260
        {
261
                PaDeviceIndex count = Pa_GetDeviceCount();
262

    
263
                if (count < 0)
264
                        throw PaException(count);
265

    
266
                return count;
267
        }
268

    
269
        Device &System::nullDevice()
270
        {
271
                return *nullDevice_;
272
        }
273

    
274
        // -----------------------------------------------------------------------------------
275

    
276
        void System::sleep(long msec)
277
        {
278
                Pa_Sleep(msec);
279
        }
280

    
281
        int System::sizeOfSample(PaSampleFormat format)
282
        {
283
                PaError err = Pa_GetSampleSize(format);
284
                if (err < 0)
285
                {
286
                        throw PaException(err);
287
                        return 0;
288
                }
289

    
290
                return err;
291
        }
292

    
293
        // -----------------------------------------------------------------------------------
294

    
295
        System::System()
296
        {
297
                // (left blank intentionally)
298
        }
299

    
300
        System::~System()
301
        {
302
                // (left blank intentionally)
303
        }
304

    
305
        // -----------------------------------------------------------------------------------
306

    
307
} // namespace portaudio
308