WebAudio AudioWorklets in C++: sample rate?

69 views
Skip to first unread message

Luis Torres

unread,
Nov 21, 2024, 5:19:12 PM11/21/24
to emscripten-discuss
I'm following this nice tutorial to get an AudioWorklet going in C++:


But how do I figure out what the audio sample rate is? If I want to generate a tone in the audio callback, it will need to know the audio sample rate.

Mehdi Sabwat

unread,
Nov 22, 2024, 11:24:38 AM11/22/24
to emscripte...@googlegroups.com
Hi,

You get to set the sampleRate in the audio context you use, for audio worklets samplerate is read only,

```
typedef int EMSCRIPTEN_WEBAUDIO_T;

typedef struct EmscriptenWebAudioCreateAttributes
{
        const char *latencyHint; // Specify one of "balanced", "interactive" or "playback"
        uint32_t sampleRate; // E.g. 44100 or 48000
} EmscriptenWebAudioCreateAttributes;

// Creates a new Web Audio AudioContext, and returns a handle to it.
EMSCRIPTEN_WEBAUDIO_T emscripten_create_audio_context(const EmscriptenWebAudioCreateAttributes *options);
```
from system/include/emscripten/webaudio.h

you can also have it EM_ASM_INT{console.log(sampleRate)} or something equivalent (check out https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html), but you need to be careful and run this in the audio worklet scope not the main one.

Hope it helps,

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/981b4bd4-64c4-4747-8c09-6a63e9c69e3an%40googlegroups.com.

Carl Woffenden

unread,
Nov 22, 2024, 11:24:42 AM11/22/24
to emscripten-discuss
But how do I figure out what the audio sample rate is?

It's a property of the audio worklet global scope, but you can also query it from the returned context, something like:

emscriptenGetAudioObject(context).sampleRate

I'm going to be proposing API changes so that it's passed to the callback (in a struct with other data).

Mehdi Sabwat

unread,
Nov 22, 2024, 12:15:05 PM11/22/24
to emscripten-discuss
I wonder how reliable it would be to do this since it can change, for example you could ask for a sampleRate and get less. What happens if it changes during playback?

As I see it it should be read from the callback

Mehdi Sabwat

unread,
Nov 22, 2024, 12:28:00 PM11/22/24
to emscripte...@googlegroups.com
It shouldn't be passed to the callback because it is readonly (https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletGlobalScope/sampleRate)

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

Carl Woffenden

unread,
Nov 22, 2024, 12:28:04 PM11/22/24
to emscripten-discuss
Setting it creation time is only a suggestion, AFAIK, but reading this after creation should be good. You can read the same in the callback too, but the changes I'd like to make report this per callback.

Carl Woffenden

unread,
Nov 22, 2024, 2:15:09 PM11/22/24
to emscripten-discuss
Readonly, so you can read the value valid for that audio frame.
Reply all
Reply to author
Forward
0 new messages