I've begun working on my first project using Libuv and I've reached the first hangup I couldn't resolve by either trying a million things or digging into the source code.
My project runs in the Max/MSP coding environment (https://cycling74.com/
). Their SDK API documentation can be found here
. Basically, I'm copying the audio signal (chunked into signal vectors by Max) from a callback that the audio card uses into a uv_buf_t and then using uv_udp_send to send that to a receiving program that copies the buffer in reverse to be used as playback on their separate machine/soundcard . So I'm trying to send audio from one user running Max/MSP to another, also running Max/MSP over the internet.
I've been successful sending small, single, test messages between the programs, and also successful copying the audio signal, which is an array of doubles representing the sample data, and using that for playback on the individual programs. But when I try to send the copied sample data via udp_send from one to the other, the programs crash. If I run _just_ the sending program, it is fine and reports no errors. When I instantiate the receiving program by itself, it also seems fine. What this tells me is that it has to do with memory allocation in either the on_alloc or on_recv callbacks that udp_recv_start uses. I've allocated memory for the size of the signal vector in on_alloc (sys_getblksize() being part of the Max API which reports the signal vector size:
buf->base = (char*)malloc(sizeof(double) * sys_getblksize());
buf->len = sys_getblksize() * sizeof(double);
and free the buffer's base : free(buf->base); in the on_recv callback as per the documentation, but something about that doesn't seem right to me. How is the buffer getting passed between the two and does the freeing of memory in the recv_cb free the memory in the alloc_cb, or am I just VERY rapidly allocating memory without freeing it?
I've got all of the code on GitHub here, for anyone that wants to take a look: https://github.com/dlandon86?tab=repositories
. The send side of things is dl.netsend~ and receive dl.netreceive~. Its my first coding project... ever, so feel free to pick it apart! And I'm happy to give more info if it's required. I just don't want to make this post any longer than it needs to be.
Thank you for your help!