From c4e9058ee26841baae4c0fd51e081949747d5f48 Mon Sep 17 00:00:00 2001 From: Justin Kerk Date: Tue, 22 Jul 2014 04:51:52 +0000 Subject: [PATCH] [JSMESS] Add shim for Web Audio sound module [Katelyn Gadd, Justin Kerk] --- .gitattributes | 2 ++ src/osd/modules/sound/js_sound.c | 39 ++++++++++++++++++++++++++++++++ src/osd/modules/sound/js_sound.h | 31 +++++++++++++++++++++++++ src/osd/sdl/sdl.mak | 6 +++++ src/osd/sdl/sdlmain.c | 8 +++++++ 5 files changed, 86 insertions(+) create mode 100644 src/osd/modules/sound/js_sound.c create mode 100644 src/osd/modules/sound/js_sound.h diff --git a/.gitattributes b/.gitattributes index 0465c977fcb..105d9b950d0 100644 --- a/.gitattributes +++ b/.gitattributes @@ -9303,6 +9303,8 @@ src/osd/modules/debugger/qt/debugqtwindow.c svneol=native#text/plain src/osd/modules/debugger/qt/debugqtwindow.h svneol=native#text/plain src/osd/modules/sound/direct_sound.c svneol=native#text/plain src/osd/modules/sound/direct_sound.h svneol=native#text/plain +src/osd/modules/sound/js_sound.c svneol=native#text/plain +src/osd/modules/sound/js_sound.h svneol=native#text/plain src/osd/modules/sound/none.c svneol=native#text/plain src/osd/modules/sound/none.h svneol=native#text/plain src/osd/modules/sound/sdl_sound.c svneol=native#text/plain diff --git a/src/osd/modules/sound/js_sound.c b/src/osd/modules/sound/js_sound.c new file mode 100644 index 00000000000..a2bc6086343 --- /dev/null +++ b/src/osd/modules/sound/js_sound.c @@ -0,0 +1,39 @@ +// license:BSD-3-Clause +// copyright-holders:Miodrag Milanovic, Katelyn Gadd +/*************************************************************************** + + js_sound.c + + Shim for native JavaScript sound interface implementations (Emscripten only). + +*******************************************************************c********/ + + +#include "js_sound.h" +#include "emscripten.h" + +//------------------------------------------------- +// sound_js - constructor +//------------------------------------------------- +sound_js::sound_js(const osd_interface &osd) + : osd_sound_interface(osd) +{ +} + +void sound_js::update_audio_stream(const INT16 *buffer, int samples_this_frame) +{ + EM_ASM_ARGS({ + // Forward audio stream update on to JS backend implementation. + jsmess_update_audio_stream($0, $1); + }, (unsigned int)buffer, samples_this_frame); +} + +void sound_js::set_mastervolume(int attenuation) +{ + EM_ASM_ARGS({ + // Forward volume update on to JS backend implementation. + jsmess_set_mastervolume($0); + }, attenuation); +} + +const osd_sound_type OSD_SOUND_JS = &osd_sound_creator; diff --git a/src/osd/modules/sound/js_sound.h b/src/osd/modules/sound/js_sound.h new file mode 100644 index 00000000000..ad0785fe960 --- /dev/null +++ b/src/osd/modules/sound/js_sound.h @@ -0,0 +1,31 @@ +// license:BSD-3-Clause +// copyright-holders:Miodrag Milanovic, Katelyn Gadd +/*************************************************************************** + + js_sound.h + + Shim for native JavaScript sound interface implementations (Emscripten only). + +*******************************************************************c********/ + +#pragma once + +#ifndef __SOUND_JS_H__ +#define __SOUND_JS_H__ + +#include "osdepend.h" + +class sound_js : public osd_sound_interface +{ +public: + // construction/destruction + sound_js(const osd_interface &osd); + virtual ~sound_js() { } + + virtual void update_audio_stream(const INT16 *buffer, int samples_this_frame); + virtual void set_mastervolume(int attenuation); +}; + +extern const osd_sound_type OSD_SOUND_JS; + +#endif /* __SOUND_JS_H__ */ diff --git a/src/osd/sdl/sdl.mak b/src/osd/sdl/sdl.mak index 43ee7e4afce..3e67d0cbe6f 100644 --- a/src/osd/sdl/sdl.mak +++ b/src/osd/sdl/sdl.mak @@ -417,6 +417,12 @@ endif # add an ARCH define DEFS += -DSDLMAME_ARCH="$(ARCHOPTS)" -DSYNC_IMPLEMENTATION=$(SYNC_IMPLEMENTATION) +# Add JavaScript sound module for Emscripten compiles + +ifeq ($(TARGETOS),emscripten) +OSDOBJS += $(OSDOBJ)/modules/sound/js_sound.o +endif + #------------------------------------------------- # Generic defines and additions #------------------------------------------------- diff --git a/src/osd/sdl/sdlmain.c b/src/osd/sdl/sdlmain.c index 2cabef9012d..f4b52598c20 100644 --- a/src/osd/sdl/sdlmain.c +++ b/src/osd/sdl/sdlmain.c @@ -50,6 +50,9 @@ #include "osdsdl.h" #include "sdlos.h" #include "modules/sound/sdl_sound.h" +#if defined(SDLMAME_EMSCRIPTEN) +#include "modules/sound/js_sound.h" +#endif #if !defined(NO_DEBUGGER) #include "modules/debugger/debugqt.h" #endif @@ -540,7 +543,12 @@ void sdl_osd_interface::video_register() void sdl_osd_interface::sound_register() { sound_options_add("sdl", OSD_SOUND_SDL); +#if defined(SDLMAME_EMSCRIPTEN) + sound_options_add("js", OSD_SOUND_JS); + sound_options_add("auto", OSD_SOUND_JS); // making JS audio default one +#else sound_options_add("auto", OSD_SOUND_SDL); // making SDL audio default one +#endif } //============================================================