comparison PyTypeInterface.h @ 47:8b2eddf686da

Fix blockSize+2 for frequency domin plugins
author fazekasgy
date Tue, 06 Oct 2009 09:27:20 +0000
parents 27bab3a16c9a
children c1e4f706ca9a
comparison
equal deleted inserted replaced
46:af9c4cee95a8 47:8b2eddf686da
150 std::vector<float> PyValue_To_FloatVector (PyObject*) const; 150 std::vector<float> PyValue_To_FloatVector (PyObject*) const;
151 std::vector<float> PyList_To_FloatVector (PyObject*) const; 151 std::vector<float> PyList_To_FloatVector (PyObject*) const;
152 152
153 // Input buffers to Python 153 // Input buffers to Python
154 PyObject* InputBuffers_As_PythonLists(const float *const *inputBuffers,const size_t& channels, const size_t& blockSize, const Vamp::Plugin::InputDomain& dtype); 154 PyObject* InputBuffers_As_PythonLists(const float *const *inputBuffers,const size_t& channels, const size_t& blockSize, const Vamp::Plugin::InputDomain& dtype);
155 PyObject* InputBuffers_As_SharedMemoryList(const float *const *inputBuffers,const size_t& channels, const size_t& blockSize); 155 PyObject* InputBuffers_As_SharedMemoryList(const float *const *inputBuffers,const size_t& channels, const size_t& blockSize, const Vamp::Plugin::InputDomain& dtype);
156 156
157 // Numpy types 157 // Numpy types
158 #ifdef HAVE_NUMPY 158 #ifdef HAVE_NUMPY
159 std::vector<float> PyArray_To_FloatVector (PyObject *pyValue) const; 159 std::vector<float> PyArray_To_FloatVector (PyObject *pyValue) const;
160 PyObject* InputBuffers_As_NumpyArray(const float *const *inputBuffers, const size_t&, const size_t&, const Vamp::Plugin::InputDomain& dtype); 160 PyObject* InputBuffers_As_NumpyArray(const float *const *inputBuffers, const size_t&, const size_t&, const Vamp::Plugin::InputDomain& dtype);
447 PyObject **pyChannelListArray = PySequence_Fast_ITEMS(pyChannelList); 447 PyObject **pyChannelListArray = PySequence_Fast_ITEMS(pyChannelList);
448 for (size_t i=0; i < channels; ++i) { 448 for (size_t i=0; i < channels; ++i) {
449 449
450 PyObject *pySampleList = PyList_New((Py_ssize_t) blockSize); 450 PyObject *pySampleList = PyList_New((Py_ssize_t) blockSize);
451 PyObject **pySampleListArray = PySequence_Fast_ITEMS(pySampleList); 451 PyObject **pySampleListArray = PySequence_Fast_ITEMS(pySampleList);
452 size_t arraySize;
453
454 if (dtype==Vamp::Plugin::FrequencyDomain)
455 arraySize = blockSize + 2;
456 else
457 arraySize = blockSize;
452 458
453 // Note: passing a complex list crashes the C-style plugin 459 // Note: passing a complex list crashes the C-style plugin
454 // when it tries to convert it to a numpy array directly. 460 // when it tries to convert it to a numpy array directly.
455 // This plugin will be obsolete, but we have to find a way 461 // This plugin will be obsolete, but we have to find a way
456 // to prevent such crash. 462 // to prevent such crash.
457 463
458 switch (Vamp::Plugin::TimeDomain) //(dtype) 464 switch (Vamp::Plugin::TimeDomain) //(dtype)
459 { 465 {
460 case Vamp::Plugin::TimeDomain : 466 case Vamp::Plugin::TimeDomain :
461 467
462 for (size_t j = 0; j < blockSize; ++j) { 468 for (size_t j = 0; j < arraySize; ++j) {
463 PyObject *pyFloat=PyFloat_FromDouble( 469 PyObject *pyFloat=PyFloat_FromDouble(
464 (double) inputBuffers[i][j]); 470 (double) inputBuffers[i][j]);
465 pySampleListArray[j] = pyFloat; 471 pySampleListArray[j] = pyFloat;
466 } 472 }
467 break; 473 break;
468 474
469 case Vamp::Plugin::FrequencyDomain : 475 case Vamp::Plugin::FrequencyDomain :
470 476
471 size_t k = 0; 477 size_t k = 0;
472 for (size_t j = 0; j < blockSize/2; ++j) { 478 for (size_t j = 0; j < arraySize/2; ++j) {
473 PyObject *pyComplex=PyComplex_FromDoubles( 479 PyObject *pyComplex=PyComplex_FromDoubles(
474 (double) inputBuffers[i][k], 480 (double) inputBuffers[i][k],
475 (double) inputBuffers[i][k+1]); 481 (double) inputBuffers[i][k+1]);
476 pySampleListArray[j] = pyComplex; 482 pySampleListArray[j] = pyComplex;
477 k += 2; 483 k += 2;
485 } 491 }
486 492
487 /// numpy buffer interface: passing the sample buffers as shared memory buffers 493 /// numpy buffer interface: passing the sample buffers as shared memory buffers
488 /// Optimization: using sequence protocol for creating the buffer list 494 /// Optimization: using sequence protocol for creating the buffer list
489 inline PyObject* 495 inline PyObject*
490 PyTypeInterface::InputBuffers_As_SharedMemoryList(const float *const *inputBuffers,const size_t& channels, const size_t& blockSize) 496 PyTypeInterface::InputBuffers_As_SharedMemoryList(const float *const *inputBuffers,const size_t& channels, const size_t& blockSize, const Vamp::Plugin::InputDomain& dtype)
491 { 497 {
492 //create a list of buffers (returns new references) 498 //create a list of buffers (returns new references)
493 PyObject *pyChannelList = PyList_New((Py_ssize_t) channels); 499 PyObject *pyChannelList = PyList_New((Py_ssize_t) channels);
494 PyObject **pyChannelListArray = PySequence_Fast_ITEMS(pyChannelList); 500 PyObject **pyChannelListArray = PySequence_Fast_ITEMS(pyChannelList);
495 501
496 // Expose memory using the Buffer Interface. 502 // Expose memory using the Buffer Interface.
497 // This will pass a pointer which can be recasted in Python code 503 // This will pass a pointer which can be recasted in Python code
498 // as complex or float array using Numpy's frombuffer() method 504 // as complex or float array using Numpy's frombuffer() method
499 // (this will not copy values just keep the starting adresses 505 // (this will not copy values just keep the starting adresses
500 // for each channel in a list) 506 // for each channel in a list)
501 Py_ssize_t bufferSize = (Py_ssize_t) sizeof(float) * blockSize; 507 Py_ssize_t bufferSize;
502 508
509 if (dtype==Vamp::Plugin::FrequencyDomain)
510 bufferSize = (Py_ssize_t) sizeof(float) * (blockSize+2);
511 else
512 bufferSize = (Py_ssize_t) sizeof(float) * blockSize;
513
503 for (size_t i=0; i < channels; ++i) { 514 for (size_t i=0; i < channels; ++i) {
504 PyObject *pyBuffer = PyBuffer_FromMemory 515 PyObject *pyBuffer = PyBuffer_FromMemory
505 ((void *) (float *) inputBuffers[i],bufferSize); 516 ((void *) (float *) inputBuffers[i],bufferSize);
506 pyChannelListArray[i] = pyBuffer; 517 pyChannelListArray[i] = pyBuffer;
507 } 518 }
545 arraySize = (int) blockSize; 556 arraySize = (int) blockSize;
546 break; 557 break;
547 558
548 case Vamp::Plugin::FrequencyDomain : 559 case Vamp::Plugin::FrequencyDomain :
549 typenum = dtype_complex64; //NPY_CFLOAT; 560 typenum = dtype_complex64; //NPY_CFLOAT;
550 arraySize = (int) blockSize / 2; 561 arraySize = (int) (blockSize / 2) + 1;
551 break; 562 break;
552 563
553 default : 564 default :
554 cerr << "PyTypeInterface::InputBuffers_As_NumpyArray: Error: Unsupported numpy array data type." << endl; 565 cerr << "PyTypeInterface::InputBuffers_As_NumpyArray: Error: Unsupported numpy array data type." << endl;
555 return pyChannelList; 566 return pyChannelList;