Skip to content

Allow tables to be resized and accessed as FloatBuffers#80

Open
mhroth wants to merge 4 commits into
libpd:masterfrom
mhroth:table
Open

Allow tables to be resized and accessed as FloatBuffers#80
mhroth wants to merge 4 commits into
libpd:masterfrom
mhroth:table

Conversation

@mhroth

@mhroth mhroth commented Aug 15, 2014

Copy link
Copy Markdown

These functions expose additional functionality to the Java domain. Tables may be resized. And instead of reading and writing to them via a Java-array intermediary, they may now be accessed directly as java.nio.FloatBuffers. Finally, the ability to execute a generic Runnable in a thread-safe way has been added, ensuring that the audio thread is not running.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about the real-time implications here. The methods of libpd are generally written in such a way that they only take the lock outside of Java, and only for very short operations.

This one would be a deviation from this pattern because the runnable might do arbitrarily complex things and cause glitches. And even if we assume that developers will only pass in computationally cheap runnables, I still wouldn't trust Java with the native lock because the garbage collector might decide that this is an awesome time to do its thing and freeze everything for umpteen milliseconds.

@nettoyeurny

Copy link
Copy Markdown
Member

This pull requests reminds me of a conversation we had a while ago, and my reaction now is pretty much what it was then: http://createdigitalnoise.com/discussion/334/accessing-arrays-in-pd

The upshot is, these changes don't seem quite safe, and I'm not sure what the use case is. I suggest we start with the use case and see how we can address it.

@danomatika

Copy link
Copy Markdown
Member

I agree that it's reasonable to require client code to make a defensive copy. Yeah, it's technically slower but I should be fine in most cases and keeps thread issues to a minimum.

@mhroth

mhroth commented Aug 18, 2014

Copy link
Copy Markdown
Author

Regarding the use of resizeArray and getArrayAsBuffer, @nettoyeurny is certainly correct that they can be misused to the point of causing a segfault. They definitely require programmer care, separately and when used together. The best the can I could offer is to add more detailed documentation on what the constraints are and that they are advanced methods.

My personal use case is reading an audio file outside of Pd and writing the sample data into a table. In the patch I ensure that the table is not being accessed, I resize the table, then get a FloatBuffer representation to the table and begin to fill it while reading the file. This can be done without having to read the entire file into memory first and then copying it into Pd's table. The use of FloatBuffer and ShortBuffer also allow byte arrays to be easily accessed as floats or shorts, without having to do any awkward bitshifting.

@mhroth

mhroth commented Aug 18, 2014

Copy link
Copy Markdown
Author

Similarly, @nettoyeurny's thoughts are also correct regarding executeSynchronizedRunnable. And again, yes it's advanced, I can offer to add more documentation explaining its use.

My use case is that I have an external which runs in Pd, but also has a Java component which has its own threads, independently of Pd. At some point the Java thread needs to update state in the native external struct, and I need to be able to guarantee that the audio thread (i.e. Pd) isn't in there doing something. Thus, executeSynchronizedRunnable.

@danomatika

danomatika commented Jan 10, 2018

Copy link
Copy Markdown
Member

@nettoyeurny What's your take on this now that locking is built into the C core? Access to filling arrays directly would solve the problem of missing native file format support in libpd for iOS, etc.

Also, I'd like to see this in the C core.

@nettoyeurny

nettoyeurny commented Jan 10, 2018 via email

Copy link
Copy Markdown
Member

@danomatika

danomatika commented Dec 9, 2019

Copy link
Copy Markdown
Member

I agree with Peter regarding the direct buffer access. It's probably best done manually outside of libpd when you really need it. Also, newer capabilities such as integrating double support internally would mean we couldn't guarantee the buffer type you are accessing, at least not trivially for most cases.

However, I think being able to resize an array is definitely useful and ported your implementation to #281.

@danomatika

danomatika commented Dec 9, 2019

Copy link
Copy Markdown
Member

What about the idea of swapping arrays, so that read/write operations can be done without the lock?

I feel this takes use more into wrapper territory. The base C API should be simple and straight forward. I could see something a little more complicated like this being introduced as an additional helper utility, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants