tgarc / pastream

Some utilities that build on python-sounddevice
MIT License
5 stars 0 forks source link

What's wrong with RingBuffer.get_write_buffers()? #2

Closed mgeier closed 7 years ago

mgeier commented 7 years ago

I saw that you're using PaUtil_GetRingBufferWriteRegions() and some low-level CFFI buffer juggling. Is there something missing in RingBuffer.get_write_buffers() so that you can't use it instead?

If there's something missing I'd rather add it there ...

tgarc commented 7 years ago

The bottom line of why I started using these functions directly (in one particular piece of my code only) is that indexing into a cffi buffer object always makes a copy, which in my case was not needed.

So when you call get_write_buffers() you get..

numframes, buffer1, buffer2 = get_write_buffers(frames)

In my code I need to read from buffer1/buffer2 incrementally but that isn't possible without copies. Looking back at the code it seems like I could've just passed a size parameter to get_write_buffers but I believe that just made the code harder to understand.

IOW, I couldn't find a general 'fix' for get_write_buffers that would make my code easier to write. Returning pointers would fix it for me but would make the much more common case where you want to get the entire buffer region as a python buffer object, more difficult.

mgeier commented 7 years ago

OK, thanks, I see the issue now.

I'm not sure if passing a size would help, because you don't know where your audio file ends, right?

You could call advance_write_index() a second time (with the updated number of frames) and then get_write_buffers() again, but this also doesn't seem right.

I also don't see a way to fix get_write_buffers(), returning a CFFI pointer doesn't seem like a good idea.

But if you are not trying to support Python 2.6 (are you?), I guess you should use memoryview(). I think that's Python's solution to exactly that problem ...

If you do want to support Python 2.6, you could add a separate implementation using buffer(). This would sure be a bit more complicated, but it may still be simpler than getting into the CFFI stuff.

tgarc commented 7 years ago

I hadn't thought about memoryview, that's a good point! And no I don't really care about python 2.6; and if I really did want to support it, I think I could use buffer to the same effect.

tgarc commented 7 years ago

Switch to using memoryview instead of using pointers. Thanks @mgeier