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

FreeQueue + WASM example #255

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Format main.js
  • Loading branch information
DivyamAhuja committed Sep 19, 2022
commit b07633510622de5ada3daa04256a475fb233f358
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#define DR_MP3_IMPLEMENTATION
#include "dr_mp3.h"

// A pointer to FreeQueue instance.
struct FreeQueue *queue;
DivyamAhuja marked this conversation as resolved.
Show resolved Hide resolved
// A drmp3 struct to load and store audio file.
drmp3 mp3;

// Function to get FreeQueue address in memory from JavaScript.
Expand All @@ -33,9 +35,8 @@ float* input[] = {
(float[FRAME_SIZE]){}
};

/**
* Reads frames from a MP3 file and pushes them to a queue.
*/

// Reads frames from a MP3 file and pushes them to a queue.
EMSCRIPTEN_KEEPALIVE void audio_loop() {
if (isLastFramePushed) {
framesRead = drmp3_read_pcm_frames_f32(
Expand All @@ -52,7 +53,6 @@ EMSCRIPTEN_KEEPALIVE void audio_loop() {
isLastFramePushed = FreeQueuePush(queue, input, framesRead);
}

DivyamAhuja marked this conversation as resolved.
Show resolved Hide resolved

int main(int argc, char** argv) {
queue = CreateFreeQueue(81920, 2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ to_root_dir: ../../../../

{% block content %}

{# Inject the service worker to workaround COOP/COEP restriction on
SharedArrayBuffer. See https://github.com/gzuidhof/coi-serviceworker
for more details. #}
<script src="./coi-serviceworker.js"></script>
DivyamAhuja marked this conversation as resolved.
Show resolved Hide resolved

<h1>{{ eleventyNavigation.title }}</h1>
Expand Down
47 changes: 23 additions & 24 deletions src/audio-worklet/free-queue/examples/hello-webassembly/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import FreeQueue from "../../src/free-queue.js";
import exampleModule from "./build/example.js";
import FreeQueue from '../../src/free-queue.js';
import exampleModule from './build/example.js';

const toogleButton = document.getElementById("toogle");
const toogleButton = document.getElementById('toogle');
toogleButton.disabled = false;

let audioContext;
Expand All @@ -14,62 +14,62 @@ let isPlaying = false;
*/
const createAudioContext = async (queue) => {
const audioContext = new AudioContext({
sampleRate: 44100
sampleRate: 44100
});
await audioContext.audioWorklet.addModule("sink-processor.js");
await audioContext.audioWorklet.addModule('sink-processor.js');
const oscillator = new OscillatorNode(audioContext);

// Create AudioWorkletNode, while passing queue in parameter options
DivyamAhuja marked this conversation as resolved.
Show resolved Hide resolved
const processorNode =
new AudioWorkletNode(audioContext, 'sink-processor', {
DivyamAhuja marked this conversation as resolved.
Show resolved Hide resolved
processorOptions: {
queue,
},
outputChannelCount: [2],
processorOptions: {
queue,
},
outputChannelCount: [2],
});
oscillator.connect(processorNode).connect(audioContext.destination);
audioContext.suspend();
oscillator.start();
console.log("AudioContext created.");
console.log('AudioContext created.');
return audioContext;
}

(async () => {
// Load WebAssembly Module
const Module = await exampleModule({
locateFile: (path, prefix) => {
if (path.endsWith(".data")) return "./build/" + path;
if (path.endsWith('.data')) return './build/' + path;
return prefix + path;
}
});

// Get FreeQueue address from WebAssembly Module.
// Get the pointer to the FreeQueue object from the WebAssembly module.
const queuePointer = Module._getFreeQueue();

// Get addresses of data members of queue.
// The getter function for the FreeQueue pointers
let getFreeQueuePointerByMember = Module.cwrap(
"GetFreeQueuePointerByMember",
"number",
["number", "string"]
'GetFreeQueuePointerByMember',
'number',
['number', 'string']
);

let bufferLengthPointer =
getFreeQueuePointerByMember(queuePointer, "buffer_length");
getFreeQueuePointerByMember(queuePointer, 'buffer_length');
let channelCountPointer =
getFreeQueuePointerByMember(queuePointer, "channel_count");
getFreeQueuePointerByMember(queuePointer, 'channel_count');
let statePointer =
getFreeQueuePointerByMember(queuePointer, "state");
getFreeQueuePointerByMember(queuePointer, 'state');
let channelsPointer =
getFreeQueuePointerByMember(queuePointer, "channel_data");
getFreeQueuePointerByMember(queuePointer, 'channel_data');

const pointers = {
memory: Module.wasmMemory,
bufferLengthPointer,
channelCountPointer,
statePointer,
channelsPointer
}
};

// Create queue from pointers.
// Creates a JS 'queue' object from pointers.
const queue = FreeQueue.fromPointers(pointers);
/**
DivyamAhuja marked this conversation as resolved.
Show resolved Hide resolved
* Function to run, when toogle button is clicked.
Expand Down Expand Up @@ -100,5 +100,4 @@ const createAudioContext = async (queue) => {
}

toogleButton.onclick = toogleButtonClickHandler;

})();