From 03c4d4e3a79c600447bd2399375d565a288eba8a Mon Sep 17 00:00:00 2001 From: Daniel Brooks Date: Wed, 3 Aug 2016 17:56:36 -0700 Subject: [PATCH] remove webaudio buffer copying for MAME; MAME is now doing this itself --- loader.js | 219 ------------------------------------------------------ 1 file changed, 219 deletions(-) diff --git a/loader.js b/loader.js index e5ca599..bdeb06b 100644 --- a/loader.js +++ b/loader.js @@ -454,7 +454,6 @@ var Module = null; config.emulator_arguments = build_mame_arguments(config.muted, config.mame_driver, config.nativeResolution, config.sample_rate, config.peripheral, config.extra_mame_args); - config.needs_jsmame_webaudio = true; return config; } MAMELoader.__proto__ = BaseLoader; @@ -884,8 +883,6 @@ var Module = null; blockSomeKeys(); setupFullScreen(); disableRightClickContextMenu(canvas); - if (game_data.needs_jsmame_webaudio) - setup_jsmame_webaudio(); // Emscripten doesn't use the proper prefixed functions for fullscreen requests, // so let's map the prefixed versions to the correct function. @@ -1355,222 +1352,6 @@ var Module = null; return Array.prototype.slice.call(xml.childNodes); } - function setup_jsmame_webaudio() { - // jsmame web audio backend v0.3 - // katelyn gadd - kg at luminance dot org ; @antumbral on twitter - //taisel working on it atm - - var jsmame_web_audio = (function () { - - var context = null; - var gain_node = null; - var eventNode = null; - var sampleScale = 32766; - var inputBuffer = new Float32Array(44100); - var bufferSize = 44100; - var start = 0; - var rear = 0; - var watchDogDateLast = null; - var watchDogTimerEvent = null; - - function lazy_init () { - //Make - if (context) { - //Return if already created: - return; - } - if (typeof AudioContext != "undefined") { - //Standard context creation: - context = new AudioContext(); - } - else if (typeof webkitAudioContext != "undefined") { - //Older webkit context creation: - context = new webkitAudioContext(); - } - else { - //API not found! - return; - } - //Generate a volume control node: - gain_node = context.createGain(); - //Set initial volume to 1: - gain_node.gain.value = 1.0; - //Connect volume node to output: - gain_node.connect(context.destination); - //Initialize the streaming event: - init_event(); - }; - - function init_event() { - //Generate a streaming node point: - if (typeof context.createScriptProcessor == "function") { - //Current standard compliant way: - eventNode = context.createScriptProcessor(4096, 0, 2); - } - else { - //Deprecated way: - eventNode = context.createJavaScriptNode(4096, 0, 2); - } - //Make our tick function the audio callback function: - eventNode.onaudioprocess = tick; - //Connect stream to volume control node: - eventNode.connect(gain_node); - //WORKAROUND FOR FIREFOX BUG: - initializeWatchDogForFirefoxBug(); - }; - - function initializeWatchDogForFirefoxBug() { - //TODO: decide if we want to user agent sniff firefox here, - //since Google Chrome doesn't need this: - watchDogDateLast = (new Date()).getTime(); - if (watchDogTimerEvent === null) { - watchDogTimerEvent = setInterval(function () { - var timeDiff = (new Date()).getTime() - watchDogDateLast; - if (timeDiff > 500) { - disconnect_old_event(); - init_event(); - } - }, 500); - } - }; - - function disconnect_old_event() { - //Disconnect from audio graph: - eventNode.disconnect(); - //IIRC there was a firefox bug that did not GC this event when nulling the node itself: - eventNode.onaudioprocess = null; - //Null the glitched/unused node: - eventNode = null; - }; - - function set_mastervolume ( - // even though it's 'attenuation' the value is negative, so... - attenuation_in_decibels - ) { - lazy_init(); - if (!context) return; - - // http://stackoverflow.com/questions/22604500/web-audio-api-working-with-decibels - // seemingly incorrect/broken. figures. welcome to Web Audio - // var gain_web_audio = 1.0 - Math.pow(10, 10 / attenuation_in_decibels); - - // HACK: Max attenuation in MAME appears to be 32. - // Hit ' then left/right arrow to test. - // FIXME: This is linear instead of log10 scale. - var gain_web_audio = 1.0 + (+attenuation_in_decibels / +32); - if (gain_web_audio < +0) - gain_web_audio = +0; - else if (gain_web_audio > +1) - gain_web_audio = +1; - - gain_node.gain.value = gain_web_audio; - }; - - function update_audio_stream ( - pBuffer, // pointer into emscripten heap. int16 samples - samples_this_frame // int. number of samples at pBuffer address. - ) { - lazy_init(); - if (!context) return; - - for ( - var i = 0, - l = samples_this_frame | 0; - i < l; - i++ - ) { - var offset = - // divide by sizeof(INT16) since pBuffer is offset - // in bytes - ((pBuffer / 2) | 0) + - ((i * 2) | 0); - - var left_sample = HEAP16[offset]; - var right_sample = HEAP16[(offset + 1) | 0]; - - // normalize from signed int16 to signed float - var left_sample_float = left_sample / sampleScale; - var right_sample_float = right_sample / sampleScale; - - inputBuffer[rear++] = left_sample_float; - inputBuffer[rear++] = right_sample_float; - if (rear == bufferSize) { - rear = 0; - } - if (start == rear) { - start += 2; - if (start == bufferSize) { - start = 0; - } - } - } - }; - function tick (event) { - //Find all output channels: - for (var bufferCount = 0, buffers = []; bufferCount < 2; ++bufferCount) { - buffers[bufferCount] = event.outputBuffer.getChannelData(bufferCount); - } - //Copy samples from the input buffer to the Web Audio API: - for (var index = 0; index < 4096 && start != rear; ++index) { - buffers[0][index] = inputBuffer[start++]; - buffers[1][index] = inputBuffer[start++]; - if (start == bufferSize) { - start = 0; - } - } - //Pad with silence if we're underrunning: - while (index < 4096) { - buffers[0][index] = 0; - buffers[1][index++] = 0; - } - //Deep inside the bowels of vendors bugs, - //we're using watchdog for a firefox bug, - //where the user agent decides to stop firing events - //if the user agent lags out due to system load. - //Don't even ask.... - watchDogDateLast = (new Date()).getTime(); - } - - function get_context() { - return context; - }; - - function sample_count() { - //TODO get someone to call this from the emulator, - //so the emulator can do proper audio buffering by - //knowing how many samples are left: - if (!context) { - //Use impossible value as an error code: - return -1; - } - var count = rear - start; - if (start > rear) { - count += bufferSize; - } - return count; - } - - return { - set_mastervolume: set_mastervolume, - update_audio_stream: update_audio_stream, - get_context: get_context, - sample_count: sample_count - }; - - })(); - - // depreciated; just for backwards compatibility - window.jsmess_set_mastervolume = jsmame_web_audio.set_mastervolume; - window.jsmess_update_audio_stream = jsmame_web_audio.update_audio_stream; - window.jsmess_sample_count = jsmame_web_audio.sample_count; - window.jsmess_web_audio = jsmame_web_audio; - - window.jsmame_set_mastervolume = jsmame_web_audio.set_mastervolume; - window.jsmame_update_audio_stream = jsmame_web_audio.update_audio_stream; - window.jsmame_sample_count = jsmame_web_audio.sample_count; - window.jsmame_web_audio = jsmame_web_audio; - } - window.IALoader = IALoader; window.DosBoxLoader = DosBoxLoader; window.JSMESSLoader = MAMELoader; // depreciated; just for backwards compatibility