Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for WASM with +mutable-globals,+atomics target features #656

Open
Oberdiah opened this issue Apr 5, 2022 · 3 comments
Open

Support for WASM with +mutable-globals,+atomics target features #656

Oberdiah opened this issue Apr 5, 2022 · 3 comments

Comments

@Oberdiah
Copy link

Oberdiah commented Apr 5, 2022

I realise this is quite an obscure use-case, but currently if you try to play audio on WASM with the +atomics and +mutable-globals features enabled in Rust Nightly, Firefox and Chrome error out since AudioBuffer.copyToChannel doesn't support SharedArrayBuffers.

Error on Firefox:

panicked at 'Unable to write sample data into the audio context buffer: JsValue(TypeError: AudioBuffer.copyToChannel: Argument 1 can't be a SharedArrayBuffer or an ArrayBufferView backed by a SharedArrayBuffer

Error on Chrome:

web.js:446 panicked at 'Unable to write sample data into the audio context buffer: JsValue(TypeError: Failed to execute 'copyToChannel' on 'AudioBuffer': The provided Float32Array value must not be shared.
TypeError: Failed to execute 'copyToChannel' on 'AudioBuffer': The provided Float32Array value must not be shared.
init/imports.wbg.__wbg_copyToChannel_74f68c0678489450/<@http://localhost/web.js:1244:25
handleError@http://localhost/web.js:310:18
init/imports.wbg.__wbg_copyToChannel_74f68c0678489450@http://localhost/web.js:1243:76
web_sys::features::gen_AudioBuffer::AudioBuffer::copy_to_channel::ha856702a3af061e5@http://localhost/web_bg.wasm:wasm-function[6669]:0x71ba4f
<cpal::host::webaudio::Device as cpal::traits::DeviceTrait>::build_output_stream_raw::{{closure}}::ha128e8e549ebdafb@http://localhost/web_bg.wasm:wasm-function[669]:0x2bcd81

EDIT:

I have managed to get it working by modifying host\webaudio\mod.rs by adding an external javascript call

webaudio\mod.rs:

#[wasm_bindgen(module = "/audio-shim.js")]
extern "C" {
    fn copy_audio_buffer(dest: &AudioBuffer, src: &[f32], channel: i32);
}
// New, working version
copy_audio_buffer(&ctx_buffer, &temporary_channel_buffer, channel as i32);

// Previous, broken version
// ctx_buffer
//     .copy_to_channel(&mut temporary_channel_buffer, channel as i32)
//     .expect("Unable to write sample data into the audio context buffer");

audio-shim.js:

export function copy_audio_buffer(dest, src, channel) {
    // Turn the array view into owned memory.
    var standalone = [...src];
    // Make it a Float32Array.
    var buffer = new Float32Array(standalone);

    // Copy the data.
    dest.copyToChannel(buffer, channel);
}

( Many thanks to this webpage for that trick )

@Nek
Copy link

Nek commented Sep 26, 2022

Huge enormous thanks! You've saved my week.

@Davidster
Copy link

Davidster commented Jun 14, 2023

would the maintainers of this project be willing to accept a pull request containing this change?

I want to use this but if it's not added into the main branch then I will have to maintain a fork of cpal

@Oberdiah
Copy link
Author

No idea, but it's worth trying I guess, feel free. The branch I'm using at the moment is the one attempting to be merged in #774, which has this change plus more fixes, so personally I'm hoping that that gets merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants