FMOD Engine User Manual 2.03
Running FMOD as HTML5 on the web is made possible by having a web server host and deliver the JavaScript content to the client web browser. In a local development environment this would be hosted using a web server such as Apache (required for HTTPS / SharedArrayBuffer testing) or something like Simple Web Server.
FMOD is compiled using Emscripten 3.1.67.
FMOD supports the following minimum browser versions:
The standard (non SharedArrayBuffer/pthread) WASM build does not support threads for compatibility with older browsers. The developer must call System::update regularly to keep the audio running without skipping/stuttering.
If FMOD_OUTPUTTYPE_AUDIOWORKLET is being used then the FMOD software mixer will run asynchronously without the help of System::update. System::update would be important for streaming audio (i.e. files opened with System::createStream / FMOD_CREATESTREAM) only.
The SharedArrayBuffer/pthread WASM build supports background streaming, non blocking file loading and other non blocking behavior.
FMOD provides different binaries covering different runtime formats and compatibility.
If you are developing in HTML / Javascript your choices are:
If you are developing in C / C++ your choices are:
To include FMOD using JavaScript in a html file, refer to the following example in your HTML. Filename will vary based on what HTML technology or logging/reduced variant you might be using.
Core API Only:
<script type="text/javascript" src="fmod.js"></script>
Studio API:
<script type="text/javascript" src="fmodstudio.js"></script>
Select from below for your technology choice, and refer to the table for the folder the FMOD libraries are located in, and any accompanying files that must be copied in with them to your project folder.
Faster (approximately 30%) and uses half the memory of Javascript, but requires the web browser to support the WASM technology, and also a web server that understands the .wasm 'application/wasm' mime type.
Core API libraries - Located in /api/core/lib/wasm/.
| Core API file | Companion file (put in same folder) | Description |
|---|---|---|
| fmod.js | fmod.wasm | Release library for production code |
| fmodL.js | fmodL.wasm | Release library with logging output, use for debugging |
| fmod_reduced.js | fmod_reduced.wasm | Smaller release library for production code, with reduced features |
| fmodP.js | fmodP.wasm | SharedArrayBuffer / pthread version. Release library for production code |
| fmodPL.js | fmodPL.wasm | SharedArrayBuffer / pthread version. Release library with logging output, use for debugging |
| fmodP_reduced.js | fmodP_reduced.wasm | SharedArrayBuffer / pthread version. Smaller release library for production code, with reduced features |
Studio API libraries - Located in /api/studio/lib/wasm/. The Studio API files already contain the Core API, so you do not need to include the Core API files as well.
| Studio API file | Companion file (put in same folder) | Description |
|---|---|---|
| fmodstudio.js | fmodstudio.wasm | Release library for production code. Includes fmod_reduced.js |
| fmodstudioL.js | fmodstudioL.wasm | Release library with logging output, use for debugging |
| fmodstudioP.js | fmodstudioP.wasm | SharedArrayBuffer / pthread version. Release library for production code. Includes fmodP_reduced.js |
| fmodstudioPL.js | fmodstudioPL.wasm | SharedArrayBuffer / pthread version. Release library with logging output, use for debugging |
For using Emscripten with your own C/C++ code to convert to JavaScript. W32 is an intermediate format.
See Using FMOD with C/C++ for instructions on which compiler / linker flags to use.
Core API libraries - Located in /api/core/lib/w32/
| Core API file | Description |
|---|---|
| fmod.a | Release binary for production code |
| fmodL.a | Release binary with logging enabled for development |
| fmod_reduced.a | Smaller release binary with reduced features, for production code. |
| fmodP.a | SharedArrayBuffer / pthread version. Release binary for production code |
| fmodPL.a | SharedArrayBuffer / pthread version. Release binary with logging enabled for development |
| fmodP_reduced.a | SharedArrayBuffer / pthread version. Smaller release binary with reduced features, for production code. |
Studio API libraries - Located in /api/studio/lib/w32/. The Studio API files already contain the Core API, so you do not need to include the Core API files as well.
| Studio API file | Description |
|---|---|
| fmodstudio.a | Release binary for production code. Contains fmod_reduced.a |
| fmodstudioL.a | Release binary with logging enabled for development. Contains fmodL.a |
| fmodstudioP.a | SharedArrayBuffer / pthread version. Release binary for production code. Contains fmodP_reduced.a |
| fmodstudioPL.a | SharedArrayBuffer / pthread version. Release binary with logging enabled for development. Contains fmodPL.a |
Note: The fmodstudio versions of the library include the fmod_reduced version of the core, so accessing things like .ogg/.mp3 (see reduced ) through Studio::System::getCoreSystem are not supported. Use the fmodstudioL version to get full access to all features.
Older Javascript compiled. Slower and uses more memory, but best for compatibility.
Core API libraries - Located in /api/core/lib/js/
| Core API file | Companion file (put in same folder) | Description |
|---|---|---|
| fmod.js | fmod.js.mem | Release library for production code |
| fmodL.js | fmodL.js.mem | Release library with logging output, use for debugging |
| fmod_reduced.js | fmod_reduced.js.mem | Smaller release library for production code, with reduced features |
Studio API libraries - Located in /api/studio/lib/js/. The Studio API files already contain the Core API, so you do not need to include the Core API files as well.
| Studio API file | Companion file (put in same folder) | Description |
|---|---|---|
| fmodstudio.js | fmodstudio.js.mem | Release library for production code. Contains fmod_reduced.js |
| fmodstudioL.js | fmodstudioL.js.mem | Release library with logging output, use for debugging. Contains fmodL.js |
If you have a C/C++ program that uses FMOD and are converting it to JavaScript using Emscripten, you will need to link the relevant FMOD binaries as per the Emscripten Documentation, and ensure the following flags are added in your Emscripten compilation arguments:
-s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s EXPORTED_RUNTIME_METHODS=ccall,cwrap,setValue,getValue
Add these flags on top of the standard flags above:
-pthread -s PTHREAD_POOL_SIZE=5
Before any FMOD API calls, emit some Javascript that sets Module.window with your own window handle.
EM_ASM({
console.log("Setting FMOD module window handle");
Module.window = window;
});
To set up a JavaScript application and the 'FMOD object', declare some members of the object that can be used internally by FMOD.
var FMOD = {}; // FMOD global object which must be declared to enable 'main' and 'preRun' and then call the constructor function.
FMOD.window = window; // Optional. Specify main window handle from a different context if needed.
FMOD.preRun = prerun; // Optional. Will be called before FMOD runs, but after the Emscripten runtime has initialized
FMOD.onRuntimeInitialized = main; // Called when the Emscripten runtime has initialized
FMOD.INITIAL_MEMORY = 64*1024*1024; // FMOD Heap defaults to 16mb which is enough for this demo, but set it differently here for demonstration (64mb)
FMODModule(FMOD); // Requires being called to initialize the 'FMOD' object via the FMOD constructor.
function prerun()
{
// Call FMOD file preloading functions here to mount local files. Otherwise load custom data from memory or use own file system.
}
FMOD defaults to 16mb of memory to load sounds with and create FMOD objects with. Use the following global before calling the main FMOD constructor object, to control how much memory to use.
For WASM support, FMOD uses ALLOW_MEMORY_GROWTH, which will allow memory to grow dynamically from INITIAL_MEMORY up to a maximum of 2GB.
To use some of the mounting functionality provided for file access, specify 'preRun' to ensure it is executed before main().
Javascript parameters are passed by value, therefore when you pass one to a function, it makes the concept of a 'getter' function difficult.
The variable's value cannot be changed by the function from the caller's perspective, but it can add a new member to the variable, which is the mechanism FMOD always uses when 'getting' data from a function.
In C the variable can be altered as it would be passed by reference (using pointers).
In FMOD for JavaScript, the variable you pass in gets a new member called val which contains the new data.
i.e.
var outval = {}; // generic variable to reuse and be passed to FMOD functions.
var name; // to store name of sound.
sound.getName(outval);
name = outval.val; // 'val' contains the data. Pass it to the variable we want to keep.
console.log(name);
All FMOD functions that produce data in a variable after calling a function return data this way.
When using a structure to pass information to FMOD, a helper/constuctor function must be called to create the structure before using it/filling in its members, so that FMOD can understand what is being passed to it.
If these constructor functions are not used, the function it is being passed to will probably result in a 'binding' error (in the browser debug/log console/window).
var guid = FMOD.GUID();
var info = FMOD.STUDIO_BANK_INFO();
FMOD lets you load data from the host in a few different ways, depending on your setup.
FMOD exposes an emscripten mechanism to mount/pre-load files in the 'prerun()' function, that is described above in the Application Setup section of this document.
Call FS_createPreloadedFile to register your files so that FMOD can use filenames in file related functions. See Emscripten docs for more on this function.
For example the playsound example
// Will be called before FMOD runs, but after the Emscripten runtime has initialized
// Call FMOD file preloading functions here to mount local files. Otherwise load custom data from memory or use own file system.
function prerun()
{
var fileUrl = "/public/js/";
var fileName;
var folderName = "/";
var canRead = true;
var canWrite = false;
fileName = [
"dog.wav",
"lion.wav",
"wave.mp3"
];
for (var count = 0; count < fileName.length; count++)
{
FMOD.FS_createPreloadedFile(folderName, fileName[count], fileUrl + fileName[count], canRead, canWrite);
}
}
Then later in your app you can simply reference a file by path/filename.
result = gSystem.createSound("/lion.wav", FMOD.LOOP_OFF, null, outval);
CHECK_RESULT(result);
If you have raw data in memory that you want to pass to FMOD , you can. Warning though, javascript passes data by value, so the memory usage will temporarily double for the sound data when it is passed to fmod.
The load_from_memory has an example of this
var chars = new Uint8Array(e.target.result);
var outval = {};
var result;
var exinfo = FMOD.CREATESOUNDEXINFO();
exinfo.length = chars.length;
result = gSystem.createStream(chars.buffer, FMOD.LOOP_OFF | FMOD.OPENMEMORY, exinfo, outval);
CHECK_RESULT(result);
If you have a file system you can use file callbacks to load data into FMOD. See System::setFileSystem, or Studio::System::loadBankCustom
In load_bank example, there is a demonstration of this
var info = new FMOD.STUDIO_BANK_INFO();
info.opencallback = customFileOpen;
info.closecallback = customFileClose;
info.readcallback = customFileRead;
info.seekcallback = customFileSeek;
info.userdata = filename;
result = gSystem.loadBankCustom(info, FMOD.STUDIO_LOAD_BANK_NONBLOCKING, outval);
CHECK_RESULT(result);
In this case the filename is passed as userdata to let the callback get information for what file to load.
Here is the customFileOpen callback getting the filename and opening a file handle.
The file is opened in this case with a special FMOD provided file API. Replace this with your own API.
function customFileOpen(name, filesize, handle, userdata)
{
var filesize_outval = {};
var handle_outval = {}
// We pass the filename into our callbacks via userdata in the custom info struct
var filename = userdata;
var result = FMOD.file_open(gSystemLowLevel, filename, filesize_outval, handle_outval)
if (result == FMOD.OK)
{
filesize.val = filesize_outval.val;
handle.val = handle_outval.val;
}
return result;
}
To read data in the callback, into the buffer that FMOD has provided, FMOD has used a special technique where it passes the memory address, instead of a javascript buffer.
This means to write data to FMOD's buffer you must use FMOD.setValue using the buffer address, writing the data in a loop, a byte at a time. This could be optimized somewhat by using i32 to write 4 bytes at a time.
function customFileRead(handle, buffer, sizebytes, bytesread, userdata)
{
var bytesread_outval = {};
var buffer_outval = {};
// Read from the file into a new buffer. This part can be swapped for your own file system.
var result = FMOD.file_read(handle, buffer_outval, sizebytes, bytesread_outval) // read produces a new array with data.
if (result == FMOD.OK)
{
bytesread.val = bytesread_outval.val;
}
// Copy the new buffer contents into the buffer that is passed into the callback. 'buffer' is a memory address, so we can only write to it with FMOD.setValue
for (count = 0; count < bytesread.val; count++)
{
FMOD.setValue(buffer + count, buffer_outval.val[count], 'i8'); // See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#accessing-memory for docs on setValue.
}
return result;
}
FMOD comes with some functions to aid with reading file data and writing to memory buffers.
Here is the list of functions that are provided.
By default FMOD mixes at 48000hz. If the browser is not running at that rate, it will introduce a resampler to convert the rate, which consumes CPU time and adds a DSP block worth of latency.
This can be solved by querying the hardware's rate before System::init, and calling System::setSoftwareFormat to the same rate as the output. Here is an example (from the PlaySound example):
var outval = {};
result = gSystem.getDriverInfo(0, null, null, outval, null, null);
CHECK_RESULT(result);
result = gSystem.setSoftwareFormat(outval.val, FMOD.SPEAKERMODE_DEFAULT, 0)
CHECK_RESULT(result);
Some devices cannot handle the default buffer size of 4 blocks of 1024 samples (4096 samples) without stuttering, so to avoid this set the buffer size in your application to 2 blocks of 2048.
Here is an example
result = gSystem.setDSPBufferSize(2048, 2);
CHECK_RESULT(result);
If a non pthread FMOD library is being used and FMOD_OUTPUTTYPE_WEBAUDIO is being used (see Compatibility section of documentation), make sure System::update is being called frequently enough for mixing / streaming to be processed from the main thread.
The Core API libraries provide _reduced library file variations which can be used optionally.
The _reduced library is a smaller subset of features. This provides a smaller file size footprint for distribution purposes.
fmodstudio.js/fmodstudio.a/fmodstudio.wasm and their 'P' (SharedArrayBuffer/pthread) versions include the reduced library by default. To get the full feature set use the logging version of each file instead. This enables logging but you can turn it off with Debug_Initialize.
The following file formats are removed in favour of the .FSB file format only. .FSB support includes vorbis, FADPCM and pcm audio formats:
The following effects / mixer code is removed:
The following items are not supported:
The following callbacks remain unimplemented at this point, so they will not work: