Source

pygame / src / openal / buffers.c

  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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
  pygame - Python Game Library
  Copyright (C) 2010 Marcus von Appen

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library 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
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/
#define PYGAME_OPENALBUFFERS_INTERNAL

#include "openalmod.h"
#include "pgopenal.h"

static int _buffers_init (PyObject *self, PyObject *args, PyObject *kwds);
static void _buffers_dealloc (PyBuffers *self);
static PyObject* _buffers_repr (PyObject *self);

static PyObject* _buffers_setprop (PyObject *self, PyObject *args);
static PyObject* _buffers_getprop (PyObject *self, PyObject *args);
static PyObject* _buffers_bufferdata (PyObject *self, PyObject *args);

static PyObject* _buffers_getcount (PyObject* self, void *closure);
static PyObject* _buffers_getbuffers (PyObject* self, void *closure);

/**
 */
static PyMethodDef _buffers_methods[] = {
    { "set_prop", _buffers_setprop, METH_VARARGS, NULL },
    { "get_prop", _buffers_getprop, METH_VARARGS, NULL },
    { "buffer_data", _buffers_bufferdata, METH_VARARGS, NULL },
    { NULL, NULL, 0, NULL }
};

/**
 */
static PyGetSetDef _buffers_getsets[] = {
    { "count", _buffers_getcount, NULL, NULL, NULL },
    { "buffers", _buffers_getbuffers, NULL, NULL, NULL },
    { NULL, NULL, NULL, NULL, NULL }
};

/**
 */
PyTypeObject PyBuffers_Type =
{
    TYPE_HEAD(NULL, 0)
    "base.Buffers",              /* tp_name */
    sizeof (PyBuffers),          /* tp_basicsize */
    0,                          /* tp_itemsize */
    (destructor) _buffers_dealloc, /* tp_dealloc */
    0,                          /* tp_print */
    0,                          /* tp_getattr */
    0,                          /* tp_setattr */
    0,                          /* tp_compare */
    (reprfunc)_buffers_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 | Py_TPFLAGS_BASETYPE, /* tp_flags */
    0/*DOC_BASE_DEVICE*/,
    0,                          /* tp_traverse */
    0,                          /* tp_clear */
    0,                          /* tp_richcompare */
    0,                          /* tp_weaklistoffset */
    0,                          /* tp_iter */
    0,                          /* tp_iternext */
    _buffers_methods,            /* tp_methods */
    0,                          /* tp_members */
    _buffers_getsets,            /* tp_getset */
    0,                          /* tp_base */
    0,                          /* tp_dict */
    0,                          /* tp_descr_get */
    0,                          /* tp_descr_set */
    0,                          /* tp_dictoffset */
    (initproc) _buffers_init,   /* tp_init */
    0,                          /* tp_alloc */
    0,                          /* tp_new */
    0,                          /* tp_free */
    0,                          /* tp_is_gc */
    0,                          /* tp_bases */
    0,                          /* tp_mro */
    0,                          /* tp_cache */
    0,                          /* tp_subclasses */
    0,                          /* tp_weaklist */
    0,                          /* tp_del */
#if PY_VERSION_HEX >= 0x02060000
    0                           /* tp_version_tag */
#endif
};

static void
_buffers_dealloc (PyBuffers *self)
{
    int switched = 0;

    if (self->buffers != NULL)
    {
        ALCcontext *ctxt = alcGetCurrentContext ();
        if (ctxt != PyContext_AsContext (self->context))
        {
            /* Switch the current context to release the correct buffers. */
            alcMakeContextCurrent (PyContext_AsContext (self->context));
            switched = 1;
        }
        alDeleteBuffers (self->count, self->buffers);
        PyMem_Free (self->buffers);
        if (switched)
            alcMakeContextCurrent (ctxt);
    }
    self->buffers = NULL;
    self->count = 0;

    ((PyObject*)self)->ob_type->tp_free ((PyObject *) self);
}

static int
_buffers_init (PyObject *self, PyObject *args, PyObject *kwds)
{
    PyErr_SetString (PyExc_NotImplementedError,
        "Buffers cannot be created dirrectly - use the Context instead");
    return -1;
}

static PyObject*
_buffers_repr (PyObject *self)
{
    return Text_FromUTF8 ("<Buffers>");
}

/* Buffers getters/setters */
static PyObject*
_buffers_getcount (PyObject* self, void *closure)
{
    return PyInt_FromLong ((long)(((PyBuffers*)self)->count));
}

static PyObject*
_buffers_getbuffers (PyObject* self, void *closure)
{
    PyBuffers *buffers = (PyBuffers*)self;
    PyObject *tuple, *item;
    ALsizei i;

    tuple = PyTuple_New ((Py_ssize_t)(buffers->count));
    if (!tuple)
        return NULL;

    for (i = 0; i < buffers->count; i++)
    {
        item = PyInt_FromLong ((long)buffers->buffers[i]);
        if (!item)
        {
            Py_DECREF (tuple);
            return NULL;
        }
        PyTuple_SET_ITEM (tuple, (Py_ssize_t)i, item);
    }
    return tuple;
}


/* Buffers methods */
static PyObject*
_buffers_setprop (PyObject *self, PyObject *args)
{
    long bufnum;
    ALenum param;
    PyObject *values;
    char *type;
    PropType ptype = INVALID;

    if (!CONTEXT_IS_CURRENT (((PyBuffers*)self)->context))
    {
        PyErr_SetString (PyExc_PyGameError, "buffer context is not current");
        return NULL;
    }

    if (!PyArg_ParseTuple (args, "llO|s:set_prop", &bufnum, &param, &values,
            &type))
        return NULL;

    if (bufnum < 0 || bufnum > ((PyBuffers*)self)->count)
    {
        PyErr_SetString (PyExc_ValueError, "buffer index out of range");
        return NULL;
    }

    if (type)
    {
        ptype = GetPropTypeFromStr (type);
        if (ptype == INVALID)
        {
            PyErr_SetString (PyExc_RuntimeError,
                "passing a sequence requires passing a type specifier");
            return NULL;
        }
    }

    if (PySequence_Check (values))
    {
        Py_ssize_t size, cnt;
        if (!type)
        {
            PyErr_SetString (PyExc_RuntimeError,
                "passing a sequence requires passing a type specifier");
            return NULL;
        }
        if (ptype == INT || ptype == FLOAT)
        {
            PyErr_SetString (PyExc_TypeError,
                "cannot use single value type and sequence together");
            return NULL;
        }

        size = PySequence_Size (values);
        switch (ptype)
        {
        case INT3:
        case INTARRAY:
        {
            ALint *vals;
            int tmp;
            if (ptype == INT3 && size < 3)
            {
                PyErr_SetString (PyExc_ValueError,
                    "sequence too small for 'i3'");
                return NULL;
            }
            vals = PyMem_New (ALint, size);
            if (!vals)
                return NULL;
            for (cnt = 0; cnt < size; cnt++)
            {
                if (!IntFromSeqIndex (values, cnt, &tmp))
                {
                    PyMem_Free (vals);
                    return NULL;
                }
                vals[cnt] = (ALint) tmp;
            }

            CLEAR_ALERROR_STATE ();
            if (ptype == INT3)
                alBuffer3i ((ALuint)bufnum, param, vals[0], vals[1], vals[2]);
            else
                alBufferiv ((ALuint)bufnum, param, vals);
            PyMem_Free (vals);
            /* Error will be set at the end */
            break;
        }
        case FLOAT3:
        case FLOATARRAY:
        {
            ALfloat *vals;
            double tmp;
            if (ptype == FLOAT3 && size < 3)
            {
                PyErr_SetString (PyExc_ValueError,
                    "sequence too small for 'f3'");
                return NULL;
            }
            vals = PyMem_New (ALfloat, size);
            if (!vals)
                return NULL;
            for (cnt = 0; cnt < size; cnt++)
            {
                if (!DoubleFromSeqIndex (values, cnt, &tmp))
                {
                    PyMem_Free (vals);
                    return NULL;
                }
                vals[cnt] = (ALfloat) tmp;
            }

            CLEAR_ALERROR_STATE ();
            if (ptype == FLOAT3)
                alBuffer3f ((ALuint)bufnum, param, vals[0], vals[1], vals[2]);
            else
                alBufferfv ((ALuint)bufnum, param, vals);
            PyMem_Free (vals);
            /* Error will be set at the end */
            break;
        }
        default:
            PyErr_SetString (PyExc_TypeError, "unsupported value");
            return NULL;
        }
    }
    else
    {
        int ival = 0;
        double fval = 0;

        if (!type)
        {
            if (IntFromObj (values, &ival))
                ptype = INT;
            else
                PyErr_Clear ();
            if (DoubleFromObj (values, &fval))
                ptype = FLOAT;
            else
            {
                PyErr_Clear ();
                PyErr_SetString (PyExc_TypeError, "unsupported value");
                return NULL;
            }
        }
        
        switch (ptype)
        {
        case INT:
            if (!IntFromObj (values, &ival))
                return NULL;
            CLEAR_ALERROR_STATE ();
            alBufferi ((ALuint)bufnum, param, (ALint) ival);
            break;
        case FLOAT:
            if (!DoubleFromObj (values, &fval))
                return NULL;
            CLEAR_ALERROR_STATE ();
            alBufferf ((ALuint)bufnum, param, (ALfloat) fval);
            break;
        default:
            PyErr_SetString (PyExc_TypeError, "value type mismatch");
            return NULL;
        }
    }

    if (SetALErrorException (alGetError (), 0))
        return NULL;
    Py_RETURN_NONE;
}

static PyObject*
_buffers_getprop (PyObject *self, PyObject *args)
{
    long bufnum;
    ALenum param;
    char *type;
    int size = 0, cnt;
    PropType ptype = INVALID;

    if (!CONTEXT_IS_CURRENT (((PyBuffers*)self)->context))
    {
        PyErr_SetString (PyExc_PyGameError, "buffer context is not current");
        return NULL;
    }

    if (!PyArg_ParseTuple (args, "lls:get_prop", &bufnum, &param, &type))
    {
        PyErr_Clear ();
        if (!PyArg_ParseTuple (args, "ll|si:get_prop", &bufnum, &param, &type,
            &size))
            return NULL;
        if (size <= 0)
        {
            PyErr_SetString (PyExc_ValueError, "size must not smaller than 0");
            return NULL;
        }
    }

    if (bufnum < 0 || bufnum > ((PyBuffers*)self)->count)
    {
        PyErr_SetString (PyExc_ValueError, "buffer index out of range");
        return NULL;
    }

    ptype = GetPropTypeFromStr (type);
    CLEAR_ALERROR_STATE ();
    switch (ptype)
    {
    case INT:
    {
        ALint val;
        alGetBufferi ((ALuint)bufnum, param, &val);
        if (SetALErrorException (alGetError (), 0))
            return NULL;
        return PyLong_FromLong ((long)val);
    }
    case FLOAT:
    {
        ALfloat val;
        alGetBufferf ((ALuint)bufnum, param, &val);
        if (SetALErrorException (alGetError (), 0))
            return NULL;
        return PyFloat_FromDouble ((double)val);
    }
    case INT3:
    {
        ALint val[3];
        alGetBuffer3i ((ALuint)bufnum, param, &val[0], &val[1], &val[2]);
        if (SetALErrorException (alGetError (), 0))
            return NULL;
        return Py_BuildValue ("(lll)", (long)val[0], (long)val[1],
            (long)val[2]);
    }
    case FLOAT3:
    {
        ALfloat val[3];
        alGetBuffer3f ((ALuint)bufnum, param, &val[0], &val[1], &val[2]);
        if (SetALErrorException (alGetError (), 0))
            return NULL;
        return Py_BuildValue ("(ddd)", (double)val[0], (double)val[1],
            (double)val[2]);
    }
    case INTARRAY:
    {
        PyObject *tuple, *item;
        ALint* val = PyMem_New (ALint, size);
        if (!val)
            return NULL;
        alGetBufferiv ((ALuint)bufnum, param, val);
        if (SetALErrorException (alGetError (), 0))
        {
            PyMem_Free (val);
            return NULL;
        }
        tuple = PyTuple_New ((Py_ssize_t) size);
        if (!tuple)
            return NULL;
        for (cnt = 0; cnt < size; cnt++)
        {
            item = PyLong_FromLong ((long)val[cnt]);
            if (!item)
            {
                PyMem_Free (val);
                Py_DECREF (tuple);
                return NULL;
            }
            PyTuple_SET_ITEM (tuple, (Py_ssize_t) cnt, item);
        }
        return tuple;
    }
    case FLOATARRAY:
    {
        PyObject *tuple, *item;
        ALfloat* val = PyMem_New (ALfloat, size);
        if (!val)
            return NULL;
        alGetBufferfv ((ALuint)bufnum, param, val);
        if (SetALErrorException (alGetError (), 0))
        {
            PyMem_Free (val);
            return NULL;
        }
        tuple = PyTuple_New ((Py_ssize_t) size);
        if (!tuple)
            return NULL;
        for (cnt = 0; cnt < size; cnt++)
        {
            item = PyFloat_FromDouble ((double)val[cnt]);
            if (!item || PyTuple_SET_ITEM (tuple, (Py_ssize_t) cnt, item) != 0)
            {
                PyMem_Free (val);
                Py_XDECREF (item);
                Py_DECREF (tuple);
                return NULL;
            }
        }
        return tuple;
    }
    default:
        PyErr_SetString (PyExc_ValueError, "invalid type specifier");
        return NULL;
    }
}

static PyObject*
_buffers_bufferdata (PyObject *self, PyObject *args)
{
    long bufnum;
    ALenum format;
    const char *buf;
    Py_ssize_t len;
    ALsizei freq;

    if (!CONTEXT_IS_CURRENT (((PyBuffers*)self)->context))
    {
        PyErr_SetString (PyExc_PyGameError, "buffer context is not current");
        return NULL;
    }

    if (!PyArg_ParseTuple (args, "lls#l", &bufnum, &format, &buf, &len, &freq))
        return NULL;
    
    /* TODO */
    if (bufnum < 0 || bufnum > ((PyBuffers*)self)->count)
    {
        PyErr_SetString (PyExc_ValueError, "buffer index out of range");
        return NULL;
    }
    CLEAR_ALERROR_STATE ();
    alBufferData ((ALuint) bufnum, format, (const void*) buf, (ALsizei)len,
        freq);
    if (SetALErrorException (alGetError (), 0))
        return NULL;
    Py_RETURN_NONE;
}

/* C API */
PyObject*
PyBuffers_New (PyObject *context, ALsizei count)
{
    ALuint *buf;
    PyObject *buffers;

    if (!context || !PyContext_Check (context))
    {
        PyErr_SetString (PyExc_TypeError, "context is not a valid Context");
        return NULL;
    }

    if (count < 1)
    {
        PyErr_SetString (PyExc_ValueError, "cannot create less than 1 buffer");
        return NULL;
    }

    buffers = PyBuffers_Type.tp_new (&PyBuffers_Type, NULL, NULL);
    if (!buffers)
        return NULL;
    ((PyBuffers*)buffers)->context = context;
    ((PyBuffers*)buffers)->count = count;
    ((PyBuffers*)buffers)->buffers = NULL;
    
    buf = PyMem_New (ALuint, count);
    if (!buf)
    {
        Py_DECREF (buffers);
        return NULL;
    }
    CLEAR_ALERROR_STATE ();
    alGenBuffers (count, buf);
    if (SetALErrorException (alGetError (), 0))
    {
        Py_DECREF (buffers);
        PyMem_Free (buf);
        return NULL;
    }

    ((PyBuffers*)buffers)->buffers = buf;
    return buffers;
}

void
buffers_export_capi (void **capi)
{
    capi[PYGAME_OPENALBUFFERS_FIRSTSLOT] = &PyBuffers_Type;
}