Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 629057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:42:14+00:00 2026-05-13T19:42:14+00:00

I am working on a real time audio processing dynamically linked library where I

  • 0

I am working on a real time audio processing dynamically linked library where I have a 2 dimensional C array of floating point data which represents the audio buffer. One dimension is time (samples) and the other is channel. I would like to pass this to a python script as a numpy array for the DSP processing and then I would like to pass this back to C so the data can carry on down the processing chain in C. The member function in C++ which does the processing looks like this:

void myEffect::process (float** inputs, float** outputs, int buffersize)
{
    //Some processing stuff
}

The arrays inputs and outputs are of equal size. The integer buffersize is the number of columns in the inputs and outputs arrays. On the python side I would like the processing to be carried out by a function which looks like the following:

class myPyEffect
    ...
    ...
    def process(self,inBuff):
    #inBuff and outBuff should be numpy arrays
        outBuff = inBuff * self.whatever # some DSP stuff
        return outBuff
    ...
    ...

Now, my question is, how can I go about getting the data in and out of C in the most efficient way possible (avoiding unnecessary memory copying etc.)? So far, for simple parameter changes I have been using C-API calls like the following:

pValue = PyObject_CallMethod(pInstance, "setParameter", "(f)", value);

Do I use something similar for my numpy arrays or is there a better way? Thanks for reading.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:42:14+00:00Added an answer on May 13, 2026 at 7:42 pm

    You may be able to avoid dealing with the NumPy C API entirely. Python can call C code using the ctypes module, and you can access pointers into the numpy data using the array’s ctypes attribute.

    Here’s a minimal example showing the process for a 1d sum-of-squares function.

    ctsquare.c

    #include <stdlib.h>
    
    float mysumsquares(float * array, size_t size) {
        float total = 0.0f;
        size_t idx;
        for (idx = 0; idx < size; ++idx) {
            total += array[idx]*array[idx];
        }
        return total;
    }
    

    compilation to ctsquare.so

    These command lines are for OS X, your OS may vary.

    $ gcc -O3 -fPIC -c ctsquare.c -o ctsquare.o
    $ ld -dylib -o ctsquare.so -lc ctsquare.o
    

    ctsquare.py

    import numpy
    import ctypes
    
    # pointer to float type, for convenience
    c_float_p = ctypes.POINTER(ctypes.c_float)
    
    # load the library
    ctsquarelib = ctypes.cdll.LoadLibrary("ctsquare.so")
    
    # define the return type and arguments of the function
    ctsquarelib.mysumsquares.restype = ctypes.c_float
    ctsquarelib.mysumsquares.argtypes = [c_float_p, ctypes.c_size_t]
    
    # python front-end function, takes care of the ctypes interface
    def myssq(arr):
        # make sure that the array is contiguous and the right data type
        arr = numpy.ascontiguousarray(arr, dtype='float32')
    
        # grab a pointer to the array's data
        dataptr = arr.ctypes.data_as(c_float_p)
    
        # this assumes that the array is 1-dimensional. 2d is more complex.
        datasize = arr.ctypes.shape[0]
    
        # call the C function
        ret = ctsquarelib.mysumsquares(dataptr, datasize)
    
        return ret
    
    if __name__ == '__main__':
        a = numpy.array([1,2,3,4])
        print 'sum of squares of [1,2,3,4] =', myssq(a)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm guessing you are encoding them because the punctuation is… May 14, 2026 at 4:45 pm
  • Editorial Team
    Editorial Team added an answer If you don't have a test machine, can you at… May 14, 2026 at 4:44 pm
  • Editorial Team
    Editorial Team added an answer When you Create the Threads, you can Set a Name… May 14, 2026 at 4:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.