forked from Malvineous/pyopl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyopl.cpp
214 lines (183 loc) · 5.65 KB
/
pyopl.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* pyopl.cpp - Main OPL wrapper.
*
* Copyright (C) 2011-2012 Adam Nielsen <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Python.h>
#include <cassert>
#include "dbopl.h"
// Size of each sample in bytes (2 == 16-bit)
#define SAMPLE_SIZE 2
// Volume amplication (0 == none, 1 == 2x, 2 == 4x)
#define VOL_AMP 1
// Clipping function to prevent integer wraparound after amplification
#define SAMP_BITS (SAMPLE_SIZE << 3)
#define SAMP_MAX ((1 << (SAMP_BITS-1)) - 1)
#define SAMP_MIN -((1 << (SAMP_BITS-1)))
#define CLIP(v) (((v) > SAMP_MAX) ? SAMP_MAX : (((v) < SAMP_MIN) ? SAMP_MIN : (v)))
class SampleHandler: public MixerChannel {
public:
Py_buffer pybuf;
uint8_t channels;
SampleHandler(uint8_t channels)
: channels(channels)
{
}
virtual ~SampleHandler()
{
}
virtual void AddSamples_m32(Bitu samples, Bit32s *buffer)
{
// Convert samples from mono s32 to stereo s16
int16_t *out = (int16_t *)this->pybuf.buf;
for (unsigned int i = 0; i < samples; i++) {
Bit32s v = buffer[i] << VOL_AMP;
*out++ = CLIP(v);
if (channels == 2) *out++ = CLIP(v);
}
return;
}
virtual void AddSamples_s32(Bitu samples, Bit32s *buffer)
{
// Convert samples from stereo s32 to stereo s16
int16_t *out = (int16_t *)this->pybuf.buf;
for (unsigned int i = 0; i < samples; i++) {
Bit32s v = buffer[i*2] << VOL_AMP;
*out++ = CLIP(v);
if (channels == 2) {
v = buffer[i*2+1] << VOL_AMP;
*out++ = CLIP(v);
}
}
return;
}
};
struct PyOPL {
// Can't put any objects in here (only pointers) as this struct is allocated
// with malloc() instead of operator new (so constructors don't get called.)
PyObject_HEAD
SampleHandler *sh;
DBOPL::Handler *opl;
};
PyObject *opl_writeReg(PyObject *self, PyObject *args, PyObject *keywds)
{
PyOPL *o = (PyOPL *)self;
static const char *kwlist[] = {"reg", "val", NULL};
int reg, val;
if (!PyArg_ParseTupleAndKeywords(args, keywds, "ii", (char **)kwlist, ®, &val)) return NULL;
o->opl->WriteReg(reg, val);
Py_INCREF(Py_None);
return Py_None;
}
PyObject *opl_getSamples(PyObject *self, PyObject *args)
{
PyOPL *o = (PyOPL *)self;
if (!PyArg_ParseTuple(args, "w*", &o->sh->pybuf)) return NULL;
int samples = o->sh->pybuf.len / SAMPLE_SIZE / o->sh->channels;
if (samples > 512) {
PyErr_SetString(PyExc_ValueError, "buffer too large (max 512 samples)");
return NULL;
}
if (samples < 2) {
PyErr_SetString(PyExc_ValueError, "buffer too small (min 2 samples)");
return NULL;
}
o->opl->Generate(o->sh, samples);
PyBuffer_Release(&o->sh->pybuf); // won't use it any more
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef opl_methods[] = {
{"writeReg", (PyCFunction)opl_writeReg, METH_VARARGS | METH_KEYWORDS, "writeReg(reg=, val=): Write a value to an OPL register."},
{"getSamples", (PyCFunction)opl_getSamples, METH_VARARGS, "getSamples(buffer): Fill the supplied buffer with audio samples."},
{NULL, NULL, 0, NULL}
};
void opl_dealloc(PyObject *self)
{
PyOPL *o = (PyOPL *)self;
delete o->opl;
delete o->sh;
PyObject_Del(self);
return;
}
PyObject *opl_getattr(PyObject *self, char *attrname)
{
return Py_FindMethod(opl_methods, self, attrname);
}
int opl_setattr(PyObject *self, char *attrname, PyObject *value)
{
PyErr_SetString(PyExc_AttributeError, attrname);
return -1;
}
PyObject *opl_repr(PyObject *self)
{
return PyString_FromString("<OPL>");
}
static PyTypeObject PyOPLType = {
PyObject_HEAD_INIT(&PyType_Type)
0, // ob_size
"pyopl", // tp_name
sizeof(PyOPL), // tp_basicsize
0, // tp_itemsize
opl_dealloc, // tp_dealloc
0, // tp_print
opl_getattr, // tp_getattr
opl_setattr, // tp_setattr
0, // tp_compare
opl_repr, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
};
PyObject *opl_new(PyObject *self, PyObject *args, PyObject *keywds)
{
static const char *kwlist[] = {"freq", "sampleSize", "channels", NULL};
unsigned int freq;
uint8_t sampleSize;
uint8_t channels;
if (!PyArg_ParseTupleAndKeywords(args, keywds, "Ibb", (char **)kwlist, &freq, &sampleSize, &channels)) return NULL;
if (sampleSize != SAMPLE_SIZE) {
PyErr_SetString(PyExc_ValueError, "invalid sample size (valid values: 2=16-bit)");
return NULL;
}
if ((channels != 1) && (channels != 2)) {
PyErr_SetString(PyExc_ValueError, "invalid channel count (valid values: 1=mono, 2=stereo)");
return NULL;
}
PyOPL *o = PyObject_New(PyOPL, &PyOPLType);
if (o) {
o->sh = new SampleHandler(channels);
o->opl = new DBOPL::Handler();
o->opl->Init(freq);
}
return (PyObject *)o;
}
static PyMethodDef methods[] = {
{"opl", (PyCFunction)opl_new, METH_VARARGS | METH_KEYWORDS, "Create a new OPL emulator instance."},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initpyopl(void)
{
(void) Py_InitModule("pyopl", methods);
}