Calling the Port Audio C API via Panama FFI APIs
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Tue Jan 4 11:16:28 UTC 2022
Hi Carl,
> My build is from the public download https://jdk.java.net/panama/
> <https://urldefense.com/v3/__https://jdk.java.net/panama/__;!!ACWV5N9M2RV99hQ!c43izMm_BeKdqh1M1wZ5FiIRhphPcIqZHa_elCGMN9aAHFJp0953QCbVC2PlX1RkOfx30vk$>
> shown as Build 17-panama+3-167 (2021/5/18)
> I chose the shared scope because when using newConfinedScope() it
> crashes. try (var scope = ResourceScope.newConfinedScope())
> :-)
>
> Not sure if upcalls/callbacks handle scope differently.
I suspect the crash is in reality a Java exception not reported
correctly in that early access version because of:
https://git.openjdk.java.net/panama-foreign/pull/597
That is, I suspect the library you are using is creating threads behind
the curtains, and then using such threads to call some of the upcalls in
your code. Then, if the upcall accesses any segment created with a
confined scope, access fails (as expected, since access comes from
another thread), and the upcall terminates (which means the VM exits, as
there's no other way to recover meanigfully).
After the above PR, such exceptions are reported in a much more friendly
way (e.g. with a stack trace and all). If that's not the issue, then I'd
like to know more because it sounds odd.
>
>
> 2. You are not using a recent Panama build (I see the MemoryAccess
> static methods are used). I think few issues with upcalls have been
> fixed - might be worth to give latest Panama a try.
>
> I'm assuming the latest build of the main branch 'origin/foreign-jextract'
> I'm still in the process, for some reason the configure is complaining
> about the following:
>
> checking if libclang should be enabled... no, not found
>
> configure: error: Cannot locate libclang! You can download pre-built llvm
>
> binary from http://llvm.org/releases/download.html
> <https://urldefense.com/v3/__http://llvm.org/releases/download.html__;!!ACWV5N9M2RV99hQ!c43izMm_BeKdqh1M1wZ5FiIRhphPcIqZHa_elCGMN9aAHFJp0953QCbVC2PlX1RkJJQrzKw$>,
> then specify the
>
> location using --with-libclang
>
> /Users/cdea/projects/panama/panama-foreign/build/.configure-support/generated-configure.sh:
> line 84: 5: Bad file descriptor
>
> configure exiting with result code 1
>
You need to pass the flag --with-libclang=<LLVM_HOME> flag to `sh
configure` where LLVM_HOME should point to a full LLVM install (e.g. one
you downloaded from the LLVM website).
>
>
>
> 3. I see a call to PaSleep. Now, while that _shouldn't_ cause
> issue, I
> wonder if putting your thread to sleep in native-land instead of Java
> land might cause issues.
>
> Hmm.. Did you mean a C lang sleep?
>
> I changed it to Java land (sleep) and no change.
> PortAudio library has a function called Pa_Sleep()...
Ok, that was a wild suggestion :-)
>
> 4. On a very quick look, the pointer arithmetic in the upcall
> seems fine
>
>
> Is there a more elegant way to express this?
Let's try to make it work first, and then worry about the "looks" :-)
Maurizio
>
>
> Thank you!,
> Carl
>
> That's all I have for now, sorry!
>
> Cheers
> Maurizio
>
> On 22/12/2021 04:06, carl dea wrote:
> > Hello friends of JEP 412,
> >
> >
> > I hope I'm not being too forward, but I would love it if I could
> get a
> > second opinion of my *port* of an example from the folks from
> the PortAudio
> > C API project using Project Panama's FFI APIs.
> >
> >
> > I created the project called *portaudio4j* to simply port their
> first
> > example called *examples/paex_saw.c*.
> >
> > https://github.com/carldea/portaudio4j
> <https://urldefense.com/v3/__https://github.com/carldea/portaudio4j__;!!ACWV5N9M2RV99hQ!c43izMm_BeKdqh1M1wZ5FiIRhphPcIqZHa_elCGMN9aAHFJp0953QCbVC2PlX1RkICKUj2E$>
> >
> >
> > *My version:*
> >
> https://github.com/carldea/portaudio4j/blob/main/src/PaexSaw.java
> <https://urldefense.com/v3/__https://github.com/carldea/portaudio4j/blob/main/src/PaexSaw.java__;!!ACWV5N9M2RV99hQ!c43izMm_BeKdqh1M1wZ5FiIRhphPcIqZHa_elCGMN9aAHFJp0953QCbVC2PlX1RkcvIa1mY$>
> >
> >
> > *(Their version) Port Audio C API 's beginner example paex_saw.c
> is here:*
> >
> https://github.com/PortAudio/portaudio/blob/master/examples/paex_saw.c
> <https://urldefense.com/v3/__https://github.com/PortAudio/portaudio/blob/master/examples/paex_saw.c__;!!ACWV5N9M2RV99hQ!c43izMm_BeKdqh1M1wZ5FiIRhphPcIqZHa_elCGMN9aAHFJp0953QCbVC2PlX1RkpfeaeCk$>
> >
> >
> >
> > *The Problem (Issue)*
> >
> > For some reason the C version (*paex_saw.c*) of the example
> compared to the
> > Panama example (*PaexSaw.java*) doesn't sound the same.
> Sometimes it's too
> > loud as if something isn't cleaning up from the prior run. The
> only thing
> > I've changed afaik is the SAMPLE_RATE, because it's so
> frighteningly loud.
> > Maybe I'm not properly doing pointer arithmetic. Or something
> I'm doing
> > that is overly expensive (operation wise) when accessing a
> > MemoryAddress/MemorySegment.
> >
> >
> > So, I thought I'd reach out to those who might be able to shed
> some light
> > on the issues I'm running into.
> >
> >
> > *Why did I choose this C library?*
> >
> > --------------------------------
> >
> > It is a relatively simple and small example that can exercise
> most aspects
> > of Panama (downcall, upcall, typedef void variables, handles,
> pointers,
> > structs, etc.) Everyone has a speaker and mic, so it would be a
> nice way to
> > use the library for all OSes.
> >
> >
> > Of course if there's a way I can pinpoint the cause, I'd be
> happy to report
> > the bug so we can keep an eye on it. Also, it might be a good
> example to
> > add to Sundar's examples here:
> > https://github.com/sundararajana/panama-jextract-samples
> <https://urldefense.com/v3/__https://github.com/sundararajana/panama-jextract-samples__;!!ACWV5N9M2RV99hQ!c43izMm_BeKdqh1M1wZ5FiIRhphPcIqZHa_elCGMN9aAHFJp0953QCbVC2PlX1RkoIXgYGs$>.
> >
> > Thank you, Happy holidays!
> >
> >
> > Carl
> >
>
>
>
> --
> *Carl Dea*
> Principal Software Engineer
> Email: carldea at gmail.com <mailto:carldea at gmail.com>
> Website: https://carlfx.wordpress.com
> <https://urldefense.com/v3/__https://carlfx.wordpress.com__;!!ACWV5N9M2RV99hQ!c43izMm_BeKdqh1M1wZ5FiIRhphPcIqZHa_elCGMN9aAHFJp0953QCbVC2PlX1RkWdH4ptY$>
> Twitter: @carldea
> <https://urldefense.com/v3/__https://twitter.com/carldea__;!!ACWV5N9M2RV99hQ!c43izMm_BeKdqh1M1wZ5FiIRhphPcIqZHa_elCGMN9aAHFJp0953QCbVC2PlX1Rkm1qEovo$>
More information about the panama-dev
mailing list