/*
    Copyright (c) 2007 SIEGE Development Team
    All rights reserved.
    
    This file is part of libSIEGE.
    
    This software is copyrighted work licensed under the terms of the
    2-clause BSD license. Please consult the file "license.txt" for
    details.
    
    If you did not recieve the file with this program, please email
    Tim Chas <darkuranium@gmail.com>.
*/

#include "buffer.h"

static void pysBufferDealloc(PysBuffer* self)
{
    sgAudioBufferDestroy(self->buffer);
    self->ob_type->tp_free(self);
}
static PyObject* pysBufferNew(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
    PysBuffer* self = (PysBuffer*)type->tp_alloc(type, 0);
    if(self != NULL)
        self->buffer = NULL;
    return (PyObject*)self;
}
static int pysBufferInit(PysBuffer* self, PyObject* args, PyObject* kwargs)
{
    char* fname;
    if(!PyArg_ParseTuple(args, "s", &fname));
        return -1;

    self->buffer = sgAudioBufferCreate(fname);
    return 0;
}

static PyObject* pysBufferSetData(PysBuffer* self, PyObject* args)
{
    SGuint channels, format, frequency;
    PyObject* data;

    if(!PyArg_ParseTuple(args, "IIIO", &channels, &format, &frequency, &data))
        return NULL;

    if(data != NULL)
    {
        if(!PyByteArray_Check(data))
            return NULL;

        sgAudioBufferSetData(self->buffer, channels, format, frequency, PyByteArray_AS_STRING(data), PyByteArray_GET_SIZE(data));
    }
    Py_RETURN_NONE;
}

static PyMethodDef PysBufferMethods[] = {
    {"setData", (PyCFunction)pysBufferSetData, METH_VARARGS, NULL},
    {NULL, NULL, 0, NULL}
};

static PyMemberDef PysBufferMembers[] = {
    {NULL, 0, 0, 0, NULL}
};

static PyTypeObject PysBufferType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "siege.audio.Buffer",      /*tp_name*/
    sizeof(PysBuffer),         /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)pysBufferDealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*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*/
    "",                        /* tp_doc */
    0,		                   /* tp_traverse */
    0,		                   /* tp_clear */
    0,		                   /* tp_richcompare */
    0,		                   /* tp_weaklistoffset */
    0,		                   /* tp_iter */
    0,		                   /* tp_iternext */
    PysBufferMethods,          /* tp_methods */
    PysBufferMembers,          /* tp_members */
    0,                         /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)pysBufferInit,   /* tp_init */
    0,                         /* tp_alloc */
    pysBufferNew,                 /* tp_new */
};

int pysBufferCheck(PyObject* o)
{
    return PyObject_TypeCheck(o, &PysBufferType);
}
void sgpyInitAudioBuffer(PyObject* parent)
{
    PysBufferType.tp_new = PyType_GenericNew;
    if(PyType_Ready(&PysBufferType) < 0)
        return;

    Py_INCREF(&PysBufferType);
    PyModule_AddObject(parent, "Buffer", (PyObject*)&PysBufferType);
}
