Skip to content

soundworks | plugin platform init

npm version

soundworks plugin to handle initialization of browser client features that require a user interaction such as resuming audio context, etc.

Tutorial: https://soundworks.dev/tutorials/plugin-platform-init.html

Table of Contents

Installation

sh
npm install @soundworks/plugin-platform-init --save

Usage

Server

js
// src/server/index.js
import { Server } from '@soundworks/core/server.js';
import pluginPlatformInit from '@soundworks/plugin-platform-init/server.js';

const server = new Server(config);
// 
server.pluginManager.register('platform-init', pluginPlatformInit);

Client

js
// src/clients/**/index.js
import { Client } from '@soundworks/core/client.js';
import pluginPlatformInit from '@soundworks/plugin-platform-init/client.js';

const client = new Client(config);
const audioContext = new AudioContext();
// pass the audio context to the plugin
client.pluginManager.register('platform-init', pluginPlatformInit, { audioContext });

await client.start();

console.log(audioContext.state === 'running');

Available features

By default, the @soundworks/plugin-platform-init provide a way to resume audio context (as shown above) but also to access microphone, camera streams, and motion sensors throught the @ircam/devicemotion package.

sh
npm install --save @ircam/devicemotion
js
// src/clients/**/index.js
import { Client } from '@soundworks/core/client.js';
import pluginPlatformInit from '@soundworks/plugin-platform-init/client.js';
import devicemotion from '@ircam/devicemotion';

const client = new Client(config);

client.pluginManager.register('platform-init', pluginPlatformInit, { 
  microphone: true,
  camera: true,
  devicemotion,
});

await client.start();

const platformInit = await client.pluginManager.get('platform-init');

const micStream = platformInit.get('microphone');
const cameraStream = platformInit.get('camera');
devicemotion.addEventListener(e => console.log(e));

Note that these additional features require a https connection.

You can also add any arbitraty logic by passing a function to the onCheck and onActivate options:

js
let onCheckCalled = false;
let onActivateCalled = false;

client.pluginManager.register('platform-init', pluginPlatformInit, {
  onCheck: (plugin) => {
    onCheckCalled = true;
    return Promise.resolve();
  },
  onActivate: (plugin) => {
    onActivateCalled = true;
    return Promise.resolve();
  }
});

API

Classes

PluginPlatformInitClient

Client-side representation of the soundworks' platform init plugin.

PluginPlatformInitServer

Client-side representation of the soundworks' platform init plugin.

PluginPlatformInitClient

Client-side representation of the soundworks' platform init plugin.

Kind: global class

new PluginPlatformInitClient()

The constructor should never be called manually. The plugin will be instantiated by soundworks when registered in the pluginManager

Available options:

  • audioContext {AudioContext} - instance audio context to be resumed aliases: ['webaudio', 'audio-context', 'audioContext']
  • devicemotion {DeviceMotion} - @ircam/devicemotion module. aliases: ['devicemotion', 'device-motion']
  • micro {Boolean} - create a microphone stream with all feature (i.e. echoCancellation, noiseReduction, autoGainControl) set to false.
    • aliases: ['mic', 'micro']
    • todo: implement deviceId
  • video {Boolean} - create a camera stream
    • todo: implement deviceId
  • onCheck {Function} - function executed when the plugin is started to check for example if the feature is available. The provided function should return a Promise.
  • onActive {Function} - function executed on the user gesture to init a feature. The provided function should return a Promise.

Example

js
client.pluginManager.register('platform-init', pluginPlatformInit, { audioContext });

pluginPlatformInitClient.onUserGesture()

Method to be executed by the application on the first user gesture. Calling this method several times will result in a no-op after the first call.

By default, this method is automatically called by the soundworks launcher, you should not have to call it manually in most cases.

Kind: instance method of PluginPlatformInitClient
Example

js
myView.addEventListener((e) => {
  platformPlugin.onUserGesture(e);
});

pluginPlatformInitClient.get(featureId)

Returns the poayload associated to a given feature.

Kind: instance method of PluginPlatformInitClient

ParamTypeDescription
featureIdStringId of the feature as given when the plugin was registered

PluginPlatformInitServer

Client-side representation of the soundworks' platform init plugin.

Kind: global class

Credits

https://soundworks.dev/credits.html

License

BSD-3-Clause