-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyrtutils.h
103 lines (90 loc) · 2.6 KB
/
pyrtutils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef _PYRTUTILS_
#define _PYRTUTILS_
#include <Python.h>
#include "RtAudio.h"
void setRuntimeExceptionWithMessage(char const *message) {
PyErr_SetString(PyExc_RuntimeError, message);
}
inline int getByteArray(PyObject **obj, void *buf, Py_ssize_t len) {
*obj = PyByteArray_FromStringAndSize((char *) buf, len);
if (!*obj) {
PyErr_SetString(PyExc_RuntimeError,
"Internal error: could not create byte array from input buffer");
return 2;
}
return 0;
}
inline int getArgList(PyObject **args, PyObject **bytearray) {
*args = Py_BuildValue("(O)", *bytearray);
if (!*args) {
return 2;
}
return 0;
}
inline int isNone(PyObject *o) {
if (Py_None == 0)
return 1;
return 0;
}
inline int isBuffer(PyObject *o) {
if (PyObject_CheckBuffer(o))
return 0;
PyErr_SetString(PyExc_BufferError,
"The returned object does not support the buffer interface");
return 2;
}
inline int getBuffer(PyObject *o, Py_buffer **view) {
int error = PyObject_GetBuffer(o, *view, PyBUF_SIMPLE);
if (error) {
PyErr_SetString(PyExc_BufferError,
"Could not extract buffer from object");
return 2;
}
return 0;
}
inline int checkLength(unsigned long expected, Py_ssize_t actual) {
if (expected != actual) {
PyErr_SetString(PyExc_BufferError,
"Got buffer of unexpected length");
return 2;
}
return 0;
}
inline unsigned int widthFromFormat(RtAudioFormat fmt) {
unsigned int w = 1;
switch (fmt) {
case RTAUDIO_SINT8:
w = 1;
break;
case RTAUDIO_SINT16:
w = 2;
break;
case RTAUDIO_SINT24:
case RTAUDIO_SINT32:
case RTAUDIO_FLOAT32:
w = 4;
break;
case RTAUDIO_FLOAT64:
w = 8;
break;
default:
w = 1;
break;
}
return w;
}
RtAudio::StreamParameters *populateStreamParameters(PyObject *dict) {
PyObject *device = PyDict_GetItemString(dict, "device_id");
PyObject *channels = PyDict_GetItemString(dict, "channels");
PyObject *first = PyDict_GetItemString(dict, "first_channel");
if (!device || !channels || !first)
return NULL;
if (!PyInt_Check(device) || !PyInt_Check(channels) || !PyInt_Check(first))
return NULL;
RtAudio::StreamParameters *params = new RtAudio::StreamParameters;
params->deviceId = PyInt_AsLong(device);
params->nChannels = PyInt_AsLong(channels);
params->firstChannel = PyInt_AsLong(first);
return params;
}
#endif