Best JavaScript code snippet using wpt
library_godot_audio.js
Source:library_godot_audio.js
1/*************************************************************************/2/* library_godot_audio.js */3/*************************************************************************/4/* This file is part of: */5/* GODOT ENGINE */6/* https://godotengine.org */7/*************************************************************************/8/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */9/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */10/* */11/* Permission is hereby granted, free of charge, to any person obtaining */12/* a copy of this software and associated documentation files (the */13/* "Software"), to deal in the Software without restriction, including */14/* without limitation the rights to use, copy, modify, merge, publish, */15/* distribute, sublicense, and/or sell copies of the Software, and to */16/* permit persons to whom the Software is furnished to do so, subject to */17/* the following conditions: */18/* */19/* The above copyright notice and this permission notice shall be */20/* included in all copies or substantial portions of the Software. */21/* */22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */29/*************************************************************************/30const GodotAudio = {31 $GodotAudio__deps: ['$GodotRuntime', '$GodotOS'],32 $GodotAudio: {33 ctx: null,34 input: null,35 driver: null,36 interval: 0,37 init: function (mix_rate, latency, onstatechange, onlatencyupdate) {38 const ctx = new (window.AudioContext || window.webkitAudioContext)({39 sampleRate: mix_rate,40 // latencyHint: latency / 1000 // Do not specify, leave 'interactive' for good performance.41 });42 GodotAudio.ctx = ctx;43 ctx.onstatechange = function () {44 let state = 0;45 switch (ctx.state) {46 case 'suspended':47 state = 0;48 break;49 case 'running':50 state = 1;51 break;52 case 'closed':53 state = 2;54 break;55 // no default56 }57 onstatechange(state);58 };59 ctx.onstatechange(); // Immeditately notify state.60 // Update computed latency61 GodotAudio.interval = setInterval(function () {62 let computed_latency = 0;63 if (ctx.baseLatency) {64 computed_latency += GodotAudio.ctx.baseLatency;65 }66 if (ctx.outputLatency) {67 computed_latency += GodotAudio.ctx.outputLatency;68 }69 onlatencyupdate(computed_latency);70 }, 1000);71 GodotOS.atexit(GodotAudio.close_async);72 return ctx.destination.channelCount;73 },74 create_input: function (callback) {75 if (GodotAudio.input) {76 return 0; // Already started.77 }78 function gotMediaInput(stream) {79 try {80 GodotAudio.input = GodotAudio.ctx.createMediaStreamSource(stream);81 callback(GodotAudio.input);82 } catch (e) {83 GodotRuntime.error('Failed creaating input.', e);84 }85 }86 if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {87 navigator.mediaDevices.getUserMedia({88 'audio': true,89 }).then(gotMediaInput, function (e) {90 GodotRuntime.error('Error getting user media.', e);91 });92 } else {93 if (!navigator.getUserMedia) {94 navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;95 }96 if (!navigator.getUserMedia) {97 GodotRuntime.error('getUserMedia not available.');98 return 1;99 }100 navigator.getUserMedia({101 'audio': true,102 }, gotMediaInput, function (e) {103 GodotRuntime.print(e);104 });105 }106 return 0;107 },108 close_async: function (resolve, reject) {109 const ctx = GodotAudio.ctx;110 GodotAudio.ctx = null;111 // Audio was not initialized.112 if (!ctx) {113 resolve();114 return;115 }116 // Remove latency callback117 if (GodotAudio.interval) {118 clearInterval(GodotAudio.interval);119 GodotAudio.interval = 0;120 }121 // Disconnect input, if it was started.122 if (GodotAudio.input) {123 GodotAudio.input.disconnect();124 GodotAudio.input = null;125 }126 // Disconnect output127 let closed = Promise.resolve();128 if (GodotAudio.driver) {129 closed = GodotAudio.driver.close();130 }131 closed.then(function () {132 return ctx.close();133 }).then(function () {134 ctx.onstatechange = null;135 resolve();136 }).catch(function (e) {137 ctx.onstatechange = null;138 GodotRuntime.error('Error closing AudioContext', e);139 resolve();140 });141 },142 },143 godot_audio_is_available__sig: 'i',144 godot_audio_is_available__proxy: 'sync',145 godot_audio_is_available: function () {146 if (!(window.AudioContext || window.webkitAudioContext)) {147 return 0;148 }149 return 1;150 },151 godot_audio_init__sig: 'iiiii',152 godot_audio_init: function (p_mix_rate, p_latency, p_state_change, p_latency_update) {153 const statechange = GodotRuntime.get_func(p_state_change);154 const latencyupdate = GodotRuntime.get_func(p_latency_update);155 return GodotAudio.init(p_mix_rate, p_latency, statechange, latencyupdate);156 },157 godot_audio_resume__sig: 'v',158 godot_audio_resume: function () {159 if (GodotAudio.ctx && GodotAudio.ctx.state !== 'running') {160 GodotAudio.ctx.resume();161 }162 },163 godot_audio_capture_start__proxy: 'sync',164 godot_audio_capture_start__sig: 'i',165 godot_audio_capture_start: function () {166 return GodotAudio.create_input(function (input) {167 input.connect(GodotAudio.driver.get_node());168 });169 },170 godot_audio_capture_stop__proxy: 'sync',171 godot_audio_capture_stop__sig: 'v',172 godot_audio_capture_stop: function () {173 if (GodotAudio.input) {174 const tracks = GodotAudio.input['mediaStream']['getTracks']();175 for (let i = 0; i < tracks.length; i++) {176 tracks[i]['stop']();177 }178 GodotAudio.input.disconnect();179 GodotAudio.input = null;180 }181 },182};183autoAddDeps(GodotAudio, '$GodotAudio');184mergeInto(LibraryManager.library, GodotAudio);185/**186 * The AudioWorklet API driver, used when threads are available.187 */188const GodotAudioWorklet = {189 $GodotAudioWorklet__deps: ['$GodotAudio', '$GodotConfig'],190 $GodotAudioWorklet: {191 promise: null,192 worklet: null,193 create: function (channels) {194 const path = GodotConfig.locate_file('godot.audio.worklet.js');195 GodotAudioWorklet.promise = GodotAudio.ctx.audioWorklet.addModule(path).then(function () {196 GodotAudioWorklet.worklet = new AudioWorkletNode(197 GodotAudio.ctx,198 'godot-processor',199 {200 'outputChannelCount': [channels],201 },202 );203 return Promise.resolve();204 });205 GodotAudio.driver = GodotAudioWorklet;206 },207 start: function (in_buf, out_buf, state) {208 GodotAudioWorklet.promise.then(function () {209 const node = GodotAudioWorklet.worklet;210 node.connect(GodotAudio.ctx.destination);211 node.port.postMessage({212 'cmd': 'start',213 'data': [state, in_buf, out_buf],214 });215 node.port.onmessage = function (event) {216 GodotRuntime.error(event.data);217 };218 });219 },220 get_node: function () {221 return GodotAudioWorklet.worklet;222 },223 close: function () {224 return new Promise(function (resolve, reject) {225 GodotAudioWorklet.promise.then(function () {226 GodotAudioWorklet.worklet.port.postMessage({227 'cmd': 'stop',228 'data': null,229 });230 GodotAudioWorklet.worklet.disconnect();231 GodotAudioWorklet.worklet = null;232 GodotAudioWorklet.promise = null;233 resolve();234 });235 });236 },237 },238 godot_audio_worklet_create__sig: 'vi',239 godot_audio_worklet_create: function (channels) {240 GodotAudioWorklet.create(channels);241 },242 godot_audio_worklet_start__sig: 'viiiii',243 godot_audio_worklet_start: function (p_in_buf, p_in_size, p_out_buf, p_out_size, p_state) {244 const out_buffer = GodotRuntime.heapSub(HEAPF32, p_out_buf, p_out_size);245 const in_buffer = GodotRuntime.heapSub(HEAPF32, p_in_buf, p_in_size);246 const state = GodotRuntime.heapSub(HEAP32, p_state, 4);247 GodotAudioWorklet.start(in_buffer, out_buffer, state);248 },249 godot_audio_worklet_state_wait__sig: 'iiii',250 godot_audio_worklet_state_wait: function (p_state, p_idx, p_expected, p_timeout) {251 Atomics.wait(HEAP32, (p_state >> 2) + p_idx, p_expected, p_timeout);252 return Atomics.load(HEAP32, (p_state >> 2) + p_idx);253 },254 godot_audio_worklet_state_add__sig: 'iiii',255 godot_audio_worklet_state_add: function (p_state, p_idx, p_value) {256 return Atomics.add(HEAP32, (p_state >> 2) + p_idx, p_value);257 },258 godot_audio_worklet_state_get__sig: 'iii',259 godot_audio_worklet_state_get: function (p_state, p_idx) {260 return Atomics.load(HEAP32, (p_state >> 2) + p_idx);261 },262};263autoAddDeps(GodotAudioWorklet, '$GodotAudioWorklet');264mergeInto(LibraryManager.library, GodotAudioWorklet);265/*266 * The deprecated ScriptProcessorNode API, used when threads are disabled.267 */268const GodotAudioScript = {269 $GodotAudioScript__deps: ['$GodotAudio'],270 $GodotAudioScript: {271 script: null,272 create: function (buffer_length, channel_count) {273 GodotAudioScript.script = GodotAudio.ctx.createScriptProcessor(buffer_length, 2, channel_count);274 GodotAudio.driver = GodotAudioScript;275 return GodotAudioScript.script.bufferSize;276 },277 start: function (p_in_buf, p_in_size, p_out_buf, p_out_size, onprocess) {278 GodotAudioScript.script.onaudioprocess = function (event) {279 // Read input280 const inb = GodotRuntime.heapSub(HEAPF32, p_in_buf, p_in_size);281 const input = event.inputBuffer;282 if (GodotAudio.input) {283 const inlen = input.getChannelData(0).length;284 for (let ch = 0; ch < 2; ch++) {285 const data = input.getChannelData(ch);286 for (let s = 0; s < inlen; s++) {287 inb[s * 2 + ch] = data[s];288 }289 }290 }291 // Let Godot process the input/output.292 onprocess();293 // Write the output.294 const outb = GodotRuntime.heapSub(HEAPF32, p_out_buf, p_out_size);295 const output = event.outputBuffer;296 const channels = output.numberOfChannels;297 for (let ch = 0; ch < channels; ch++) {298 const data = output.getChannelData(ch);299 // Loop through samples and assign computed values.300 for (let sample = 0; sample < data.length; sample++) {301 data[sample] = outb[sample * channels + ch];302 }303 }304 };305 GodotAudioScript.script.connect(GodotAudio.ctx.destination);306 },307 get_node: function () {308 return GodotAudioScript.script;309 },310 close: function () {311 return new Promise(function (resolve, reject) {312 GodotAudioScript.script.disconnect();313 GodotAudioScript.script.onaudioprocess = null;314 GodotAudioScript.script = null;315 resolve();316 });317 },318 },319 godot_audio_script_create__sig: 'iii',320 godot_audio_script_create: function (buffer_length, channel_count) {321 return GodotAudioScript.create(buffer_length, channel_count);322 },323 godot_audio_script_start__sig: 'viiiii',324 godot_audio_script_start: function (p_in_buf, p_in_size, p_out_buf, p_out_size, p_cb) {325 const onprocess = GodotRuntime.get_func(p_cb);326 GodotAudioScript.start(p_in_buf, p_in_size, p_out_buf, p_out_size, onprocess);327 },328};329autoAddDeps(GodotAudioScript, '$GodotAudioScript');...
sound_generator_worklet.js
Source:sound_generator_worklet.js
1/*2 * Copyright (c) 2021 Antonio-R13 * License: https://github.com/Antonio-R1/racing-js/LICENSE | GNU AGPLv34 */5import * as THREE from '../three_js/build/three.module.js';6const SPEED_OF_SOUND = 343; // speed of sound in m/s7class SoundGeneratorAudioListener extends THREE.AudioListener {8 constructor () {9 super();10 this.worldPosition = new THREE.Vector3();11 }12 updateMatrixWorld (force) {13 super.updateMatrixWorld (force);14 this.getWorldPosition(this.worldPosition);15 } 16}17class SoundGenerator extends THREE.PositionalAudio {18 constructor (listener) {19 super(listener);20 this.delayNode = this.context.createDelay(50);21// this.setFilter(this.delayNode);22 this.worklet = null;23 this.lastTimeUpdated = null;24 this.worldPosition = new THREE.Vector3();25 }26 static _load(loadingManager, listener, path) {27 loadingManager.itemStart(path);28 listener.context.audioWorklet.addModule(path)29 .then (() => loadingManager.itemEnd (path));30 }31 _setOutputSource (audioNode, index) {32 this.audioNode.connect (audioNode, index);33 this.hasPlaybackControl = false;34 this.sourceType = 'audioNode';35 this.source = this.delayNode;36 }37 _setNodeSources(audioNodeArray) {38 for (let i=0; i<audioNodeArray.length; i++) {39 audioNodeArray[i].connect (this.delayNode);40 }41 this.hasPlaybackControl = false;42 this.sourceType = 'audioNode';43 this.source = this.delayNode;44 }45 setNodeSource () {46 throw new Error ("not supported");47 }48 setMediaElementSource() {49 throw new Error ("not supported");50 }51 setMediaStreamSource() {52 throw new Error ("not supported");53 }54 setBuffer() {55 throw new Error ("not supported");56 }57 play () {58 this.isPlaying = true;59 return this.connect();60 }61 stop () {62 if (this.isPlaying) {63 this.isPlaying = false;64 return this.disconnect();65 }66 }67 updateMatrixWorld (force) {68 super.updateMatrixWorld (force);69 this.getWorldPosition(this.worldPosition);70 if (!this.worklet) {71 return;72 }73 // doppler effect with delay nodes similar to https://github.com/WebAudio/web-audio-api/issues/372#issuecomment-25002461074 let time = this.context.currentTime;75 let distanceToListener = this.worldPosition.distanceTo(this.listener.worldPosition);76 let dt = time-this.lastTimeUpdated;77 if (this.lastTimeUpdated===null) {78 this.lastTimeUpdated = this.context.currentTime;79 this.delayNode.delayTime.value = distanceToListener/SPEED_OF_SOUND;80 }81 if (dt < 0.1) {82 return;83 }84 this.delayNode.delayTime.linearRampToValueAtTime(distanceToListener/SPEED_OF_SOUND, this.context.currentTime+dt);85 this.lastTimeUpdated = this.context.currentTime;86 }87}88class EngineSoundGenerator extends SoundGenerator {89 constructor ({listener, parameters}) {90 super (listener);91 this.gainIntake = this.context.createGain();92 this.gainIntake.gain.value = 1.0;93 this.gainEngineBlockVibrations = this.context.createGain();94 this.gainEngineBlockVibrations.gain.value = 1.0;95 this.gainOutlet = this.context.createGain();96 this.gainOutlet.gain.value = 1.0;97 let options = {numberOfInputs: 0, numberOfOutputs: 3, processorOptions: parameters};98 this.addWorkletNode(options);99 }100 /* example for parameters:101 {cylinders: 4,102 intakeWaveguideLength: 100,103 exhaustWaveguideLength: 100,104 extractorWaveguideLength: 100,105 intakeOpenReflectionFactor: 0.25,106 intakeClosedReflectionFactor: 0.95,107 exhaustOpenReflectionFactor: 0.25,108 exhaustClosedReflectionFactor: 0.95,109 ignitionTime: 0.016,110 straightPipeWaveguideLength: 128,111 straightPipeReflectionFactor: 0.1,112 mufflerElementsLength: [10, 15, 20, 25],113 action: 0.25,114 outletWaveguideLength: 5,115 outletReflectionFactor: 0.1}116 */117 setParameters (parameters) {118 this.worklet.port.postMessage(parameters);119 }120 addWorkletNode (options) {121 this.worklet = new AudioWorkletNode (this.listener.context, "engine-sound-processor", options);122 this.worklet.connect (this.gainIntake, 0);123 this.worklet.connect (this.gainEngineBlockVibrations, 1);124 this.worklet.connect (this.gainOutlet, 2);125 this._setNodeSources ([this.gainIntake, this.gainEngineBlockVibrations, this.gainOutlet]);126 }127 static load (loadingManager, listener) {128 SoundGenerator._load(loadingManager, listener, "sound/engine_sound_generator_worklet.js");129 }130}131class EngineSoundOutput extends SoundGenerator {132 constructor (audioNode, outputIndex) {133 this._setOutputSource(audioNode, outputIndex);134 }135}136class MultipleOutputsEngineSoundGenerator {137 constructor ({listener, parameters = undefined}) {138 this.listener = listener;139 let options = {numberOfInputs: 0, numberOfOutputs: numberOfOutputs, processorOptions: parameters}140 this.listener.context.audioWorklet.addModule("engine_sound_generator_worklet.js")141 .then (() => this.addWorkletNode(options));142 }143 addWorkletNode(options) {144 this.worklet = new AudioWorkletNode (this.listener.context, "engine-sound-processor", options);145 this.intakePositionalAudio = new EngineSoundOutput(this.worklet, 0);146 this.engineBlockVibationsPositionalAudio = new EngineSoundOutput(this.worklet, 1);147 this.outletPositionalAudio = new EngineSoundOutput(this.worklet, 2);148 }149 setParameters (parameters) {150 this.worklet.port.postMessage(parameters);151 }152}...
Using AI Code Generation
1importScripts('/resources/testharness.js');2importScripts('/resources/testharnessreport.js');3importScripts('/resources/WebIDLParser.js');4importScripts('/resources/idlharness.js');5importScripts('/resources/testharness.js');6importScripts('/resources/testharnessreport.js');7importScripts('/resources/WebIDLParser.js');8importScripts('/resources/idlharness.js');9promise_test(async t => {10 const cssPaint = new CSSPaintWorklet();11 await cssPaint.addModule('paint.js');12 await cssPaint.addModule('paint.js');13}, 'CSSPaintWorklet.addModule() should not throw if a module is added twice');14promise_test(async t => {15 const cssPaint = new CSSPaintWorklet();16 await cssPaint.addModule('paint.js');17 await cssPaint.addModule('paint.js');18}, 'CSSPaintWorklet.addModule() should not throw if a module is added twice');19promise_test(async t => {20 const cssPaint = new CSSPaintWorklet();21 await cssPaint.addModule('paint.js');22 await cssPaint.addModule('paint.js');23}, 'CSSPaintWorklet.addModule() should not throw if a module is added twice');24promise_test(async t => {25 const cssPaint = new CSSPaintWorklet();26 await cssPaint.addModule('paint.js');27 await cssPaint.addModule('paint.js');28}, 'CSSPaintWorklet.addModule() should not throw if a module is added twice');29promise_test(async t => {30 const cssPaint = new CSSPaintWorklet();31 await cssPaint.addModule('paint.js');32 await cssPaint.addModule('paint.js');33}, 'CSSPaintWorklet.addModule() should not throw if a module is added twice');34promise_test(async t => {35 const cssPaint = new CSSPaintWorklet();36 await cssPaint.addModule('paint.js');37 await cssPaint.addModule('paint.js');38}, 'CSSPaintWorklet.addModule() should not throw if a module is added twice');39promise_test(async t => {40 const cssPaint = new CSSPaintWorklet();41 await cssPaint.addModule('paint.js');42 await cssPaint.addModule('paint.js');43}, 'CSSPaintWorklet.addModule() should not throw if a module is added twice');44promise_test(async t => {45 const cssPaint = new CSSPaintWorklet();
Using AI Code Generation
1const worklet = new Worklet('test.js');2const worklet = new Worklet('test2.js');3const worklet = new Worklet('test3.js');4const worklet = new Worklet('test4.js');5const worklet = new Worklet('test5.js');6const worklet = new Worklet('test6.js');7const worklet = new Worklet('test7.js');8const worklet = new Worklet('test8.js');9const worklet = new Worklet('test9.js');10const worklet = new Worklet('test10.js');11const worklet = new Worklet('test11.js');12const worklet = new Worklet('test12.js');
Using AI Code Generation
1importScripts('/resources/testharness.js');2importScripts('/resources/testharnessreport.js');3test(function() {4 assert_equals(typeof CSSPaintWorklet, 'function');5}, 'CSSPaintWorklet interface exists');6test(function() {7 assert_equals(typeof CSSPaintWorklet.prototype.addModule, 'function');8}, 'CSSPaintWorklet.prototype.addModule exists');9test(function() {10 assert_equals(typeof CSSPaintWorklet.prototype.registerPaint, 'function');11}, 'CSSPaintWorklet.prototype.registerPaint exists');12done();
Using AI Code Generation
1async function runTest() {2 let url = new URL("worklet.js", location.href);3 let worklet = new CSSPaintWorklet();4 await worklet.addModule(url.href);5 let paintCtor = await worklet.paintWorklet.constructorFor("worklet");6 let paint = new paintCtor();7 let context = new OffscreenCanvas(100, 100).getContext("2d");8 let paintSize = new CSSUnparsedValue(["100", "100"]);9 let paintInputs = new CSSUnparsedValue(["red"]);10 paint.paint(context, paintSize, paintInputs);11}12runTest();
Using AI Code Generation
1import {worklet} from 'wpt';2worklet(async () => {3 console.log('Hello from worklet');4});5import {worklet} from 'wpt';6worklet(async () => {7 console.log('Hello from worklet');8});9import {worklet} from 'wpt';10worklet(async () => {11 console.log('Hello from worklet');12});13import {worklet} from 'wpt';14worklet(async () => {15 console.log('Hello from worklet');16});17import {worklet} from 'wpt';18worklet(async () => {19 console.log('Hello from worklet');20});21import {worklet} from 'wpt';22worklet(async () => {23 console.log('Hello from worklet');24});25import {worklet} from 'wpt';26worklet(async () => {27 console.log('Hello from worklet');28});29import {worklet} from 'wpt';30worklet(async () => {31 console.log('Hello from worklet');32});33import {worklet} from 'wpt';34worklet(async () => {35 console.log('Hello from worklet');36});37import {worklet} from 'wpt';38worklet(async () => {39 console.log('Hello from worklet');40});41import {worklet} from 'wpt';42worklet(async () => {43 console.log('Hello from worklet');44});45import {worklet} from 'wpt';46worklet(async () => {47 console.log('Hello from worklet');48});
Using AI Code Generation
1importScripts("./worklet.js");2registerPaint('test', class {3 paint(ctx, geom, properties) {4 }5});6importScripts("./worklet.js");7registerPaint('worklet', class {8 paint(ctx, geom, properties) {9 }10});
Using AI Code Generation
1import { addEventListener } from 'worklet';2addEventListener('message', (e) => {3 e.source.postMessage('Hello World!');4});5 const worker = new Worklet('test.js');6 worker.onmessage = (e) => {7 console.log(e.data);8 };9 worker.postMessage('Hello World!');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!