I am trying to call some audio rendering code in K...
# webassembly
p
I am trying to call some audio rendering code in Kotlin from a WebAudio AudioWorkletProcessor in JavaScript. I tried this: https://kotlinlang.org/docs/wasm-js-interop.html#functions-with-the-jsexport-annotation
Copy code
//import exports from "./module.mjs"
import exports from "./composeApp.js"
console.log("Kotlin random code is " + exports.renderKotlinAudio());
But I always get: SyntaxError: Unexpected identifier 'exports' I am using Kotlin/Compose multi-platform code. My Kotlin WASM code has:
Copy code
@OptIn(ExperimentalJsExport::class)
@JsExport
fun renderKotlinAudio(): Float {
    return Random.nextFloat()
}
I spent a lot of time with Gemini giving me increasingly complex solutions that did not work. Has anyone actually done this successfully?
t
AFAIK for
AudioWorkletProcessor
you should have separate JS file
p
Yes, when I use AudioWorkletProcessor it is in a separate JS file. But first I am just trying to do a minimal call from JavaScript into Kotlin. Is it possible? Any examples?
With Gemini's help, I got this to work. The composeApp had not loaded yet so all I got was a Promise. I had to wait for the Promise to load.
Copy code
const kotlinModulePromise = window.composeApp;
kotlinModulePromise.then(kotlinModule => {
        console.log("renderKotlinAudio returns " + kotlinModule.renderKotlinAudio);
        console.log("renderKotlinAudio returns " + kotlinModule.renderKotlinAudio);
    }).catch(e => {
        // It's good practice to catch potential loading errors
        console.error("Error loading Kotlin module:", e);
        document.getElementById('output').textContent = "Error loading the application module.";
    });
t
AudioWorkletProcessor
on Kotlin/JS example you can find here.
1. Kotlin/JS do per-file compilation - provides you separate JS file 2. Kotlin/JS supports extending from external
p
import web.audio.AudioWorkletModule !!! What is this? Is there existing support for AudioWorkletProcessor in Kotlin?! Is this something you wrote? I found this: https://github.com/JetBrains/kotlin-wrappers/tree/master/kotlin-browser I was able to call Kotlin from JavaScript. But I cannot get that to work yet from an AudioWorkletProcessor. I have been working on this but there may be an easier way... https://github.com/philburk/kc-audio-bridge/
I cloned Seskar and loaded it in Android Studio. The README says it is a "Gradle plugin". It contains a test that uses a Kotlin wrapper for the AudioWorkletProcessor. The relationship is not clear. Is Seskar needed to use the Kotlin wrappers for audio? This might be what I am looking for. But the docs are not clear.
t
Is Seskar needed to use the Kotlin wrappers for audio?
Yes, Seskar supports Kotlin Wrappers declarations only
Is there
AudioWorkletProcessor
in other declarations?
p
OK. I now think that the restrictions on sharing data between the AudioWorkletProcessor and the Main thread are too severe. They would require significant changes to my Audio API to support generic message passing on all platforms. The advantage would be that I could support callbacks for WebAudio. But I can only do fake callbacks for Android AudioTrack and JavaSound. So I will stick with my blocking-write API. Thanks for your help.
t
Can it be
suspend
calls?
p
t
Will it be AudioBuffer sync calls?