Python Bindings

View: New views
1 Messages — Rating Filter:   Alert me  

Python Bindings

by Christopher Brown :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi List,

I am adding features to the python bindings for audiere. I have added a
function that accepts an array (numpy) as input, and plays it out to the
sound card. It works, but there is a memory leak, in that the buffer
created (using CreateSampleBuffer) is not destroyed until python closes.
But, I don't know enough about C to be able to fix it. The buffer should
be destroyed when the stream is. Can anyone help? Here is the function
(goes in AudioDevice.cpp):

PyObject* AudioDevice_OpenArray(PyObject* self, PyObject* args) {
   audiere_AudioDeviceObject* object = (audiere_AudioDeviceObject*)self;

   PyObject *input;
   long int sample_rate = 44100;
   int streaming = 0;
   if (!PyArg_ParseTuple(args, "O|l", &input, &sample_rate))
     return 0;
   PyArrayObject *array = (PyArrayObject *)
PyArray_ContiguousFromObject(input, PyArray_DOUBLE, 1, 2);
   unsigned long int n;
   int m;

   n = array->dimensions[0];
   m = array->nd;
   short *data = (short *) malloc((n * m) * sizeof(short));
   double t;
   for (unsigned long int i = 0; i < n; i++){
     for (int j = 0; j < m; j++){
       t = * (double *)(array->data + i*array->strides[0] +
j*array->strides[1]);
       t = std::max(t, -1.0);
       t = std::min(t, 1.0);
       t = t * 32767;
       data[(i*m)+j] = (short) t;
     };
   };

   Py_DECREF(array);

   buffer = CreateSampleBuffer(data, n, m, sample_rate, audiere::SF_S16);
   audiere::RefPtr<audiere::OutputStream>
stream(audiere::OpenSound(object->device,
                                                                   buffer->openStream(),
                                                                   streaming != 0));
   if (!stream) {
     PyErr_SetString(PyExc_RuntimeError, "Could not open sound buffer");
     return 0;
   }

   return NewPyOutputStream(stream.get());
}


--
Chris

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Audiere-users mailing list
Audiere-users@...
https://lists.sourceforge.net/lists/listinfo/audiere-users
LightInTheBox - Buy quality products at wholesale price