-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add -sAUDIO_WORKLET=2 build mode #20630
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
Draft
juj
wants to merge
1
commit into
emscripten-core:main
Choose a base branch
from
juj:audio_worklet_2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
#include <emscripten.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
const unsigned kRenderQuantumFrames = 128; | ||
const unsigned kBytesPerChannel = kRenderQuantumFrames * sizeof(float); | ||
|
||
// The "kernel" is an object that processes a audio stream, which contains | ||
// one or more channels. It is supposed to obtain the frame data from an | ||
// |input|, process and fill an |output| of the AudioWorkletProcessor. | ||
// | ||
// AudioWorkletProcessor Input(multi-channel, 128-frames) | ||
// | | ||
// V | ||
// Kernel | ||
// | | ||
// V | ||
// AudioWorkletProcessor Output(multi-channel, 128-frames) | ||
// | ||
// In this implementation, the kernel operates based on 128-frames, which is | ||
// the render quantum size of Web Audio API. | ||
extern "C" void EMSCRIPTEN_KEEPALIVE SimpleKernel_Process(uintptr_t input_ptr, uintptr_t output_ptr, | ||
unsigned channel_count) { | ||
float* input_buffer = reinterpret_cast<float*>(input_ptr); | ||
float* output_buffer = reinterpret_cast<float*>(output_ptr); | ||
|
||
// Bypasses the data. By design, the channel count will always be the same | ||
// for |input_buffer| and |output_buffer|. | ||
for (unsigned channel = 0; channel < channel_count; ++channel) { | ||
float* destination = output_buffer + channel * kRenderQuantumFrames; | ||
float* source = input_buffer + channel * kRenderQuantumFrames; | ||
memcpy(destination, source, kBytesPerChannel); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
test/webaudio/singlethreaded_audioworklet/export_sync_es6_module.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Emscripten has a -sEXPORT_ES6=1 option to export the generated program as a 'Module' | ||
// object. However, that module is asynchronously loaded, which does not work well | ||
// for Audio Worklets, which require synchronous loading. So instead we manually | ||
// build the page without EXPORT_ES6, and just add an export here. | ||
|
||
export default Module; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<!doctype html> | ||
<html lang="en-us"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
<title>Emscripten-Generated Code</title> | ||
</head> | ||
<body> | ||
<h1>Audio Worklet and WebAssembly</h1> | ||
<p>A basic pattern to use Audio Worklet with WebAssembly. The | ||
AudioWorkletProcessor simply bypasses (copies) the audio via the WebAssembly | ||
function.</p> | ||
<p>See | ||
<a href="https://developer.chrome.com/blog/audio-worklet-design-pattern/" | ||
target="_blank">Chrome Developers Article: Audio Worklet Design Pattern</a> | ||
for more details.</p> | ||
|
||
<div class="demo-box"> | ||
<button id="button-start" disabled>START</button> | ||
</div> | ||
|
||
<script type='text/javascript'> | ||
// Copyright (c) 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
const audioContext = new AudioContext(); | ||
|
||
const startAudio = async (context) => { | ||
await context.audioWorklet.addModule('wasm-worklet-processor.js'); | ||
const oscillator = new OscillatorNode(context); | ||
const bypasser = new AudioWorkletNode(context, 'wasm-worklet-processor'); | ||
oscillator.connect(bypasser).connect(context.destination); | ||
oscillator.start(); | ||
}; | ||
|
||
// A simple onLoad handler. It also handles user gesture to unlock the audio | ||
// playback. | ||
window.addEventListener('load', async () => { | ||
const buttonEl = document.getElementById('button-start'); | ||
buttonEl.disabled = false; | ||
buttonEl.addEventListener('click', async () => { | ||
await startAudio(audioContext); | ||
audioContext.resume(); | ||
buttonEl.disabled = true; | ||
buttonEl.textContent = 'Playing...'; | ||
|
||
// report success | ||
setTimeout(function() { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', 'http://localhost:8888/report_result?0', true); | ||
xhr.send(); | ||
audioContext.close(); // stop the audio output after 1 second, should be enough to verify output and not be annoying | ||
window.close(); | ||
}, 1000); | ||
}, false); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this needs to be a requirement. I think a better pattern is to fetch the wasm binary on the main thread and then transfer it to the AudioWorklet processor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be very neat