
Research
/Security News
Contagious Interview Campaign Escalates With 67 Malicious npm Packages and New Malware Loader
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
fluid-js is a library that generates audio data (frames). fluid-js uses wasm version of FluidSynth.
npm install --save fluid-js
Copies dist/fluid.js
(or dist/fluid.min.js
) and externals/libfluidsynth-2.0.2.js
(libfluidsynth JS file) to your project, and writes <script>
tags as following order:
<script src="libfluidsynth-2.0.2.js"></script>
<script src="fluid.js"></script>
When scripts are available, you can use APIs via Fluid
namespace object.
// Prepare the AudioContext instance
var context = new AudioContext();
var synth = new Fluid.Synthesizer();
synth.init(context.sampleRate);
// Create AudioNode (ScriptProcessorNode) to output audio data
var node = synth.createAudioNode(context, 8192); // 8192 is the frame count of buffer
node.connect(context.destination);
// Load your SoundFont data (sfontBuffer: ArrayBuffer)
synth.loadSFont(sfontBuffer).then(function () {
// Load your SMF file data (smfBuffer: ArrayBuffer)
return synth.addSMFDataToPlayer(smfBuffer);
}).then(function () {
// Play the loaded SMF data
return synth.playPlayer();
}).then(function () {
// Wait for finishing playing
return synth.waitForPlayerStopped();
}).then(function () {
// Wait for all voices stopped
return synth.waitForVoicesStopped();
}).then(function () {
// Releases the synthesizer
synth.close();
}, function (err) {
console.log('Failed:', err);
// Releases the synthesizer
synth.close();
});
(Above example uses Web Audio API, but you can use Synthesizer
without Web Audio, by using render()
method.)
If you prefer to load fluid-js as an ES module, you can use import
statement such as import * as Fluid from 'fluid-js'
.
Notes:
fluid.js
intends the ES2015-supported environment. If you need to run the script without errors on non-ES2015 environment such as IE11 (to notify 'unsupported'), you should load those scripts dynamically, or use transpiler such as babel.Fluid.waitForReady
.import
-able and its license (LGPL v2.1) is differ from fluid-js's (BSD-3-Clause).fluid-js supports AudioWorklet process via dist/fluid.worklet.js
(or dist/fluid.worklet.min.js
). You can load fluid-js on the AudioWorklet as the following code:
var context = new AudioContext();
context.audioWorklet.addModule('libfluidsynth-2.0.2.js')
.then(function () {
return context.audioWorklet.addModule('fluid.worklet.js');
})
.then(function () {
// Create the synthesizer instance for AudioWorkletNode
var synth = new Fluid.AudioWorkletNodeSynthesizer();
synth.init(context.sampleRate);
// You must create AudioWorkletNode before using other methods
// (This is because the message port is not available until the
// AudioWorkletNode is created)
audioNode = synth.createAudioNode(context);
audioNode.connect(context.destination); // or another node...
// After node creation, you can use Synthesizer methods
return synth.loadSFont(sfontBuffer).then(function () {
return synth.addSMFDataToPlayer(smfBuffer);
}).then(function () {
return synth.playPlayer();
}).then(function () {
...
});
});
fluid-js and libfluidsynth can be executed on a Web Worker. Executing on a Web Worker prevents from blocking main thread while rendering.
To use fluid-js on a Web Worker, simply call importScripts
as followings:
self.importScripts('libfluidsynth-2.0.2.js');
self.importScripts('fluid.js');
(You can also load fluid-js as an ES Module from the Web Worker.)
Note that since the Web Audio is not supported on the Web Worker, the APIs/methods related to the Web Audio will not work. If you want to use both Web Worker and AudioWorklet, you should implement AudioWorkletProcessor manually as followings:
MessageChannel
and sending its port instances to Web Worker and AudioWorklet, they can communicate each other directly.process
methodThese classes implement the interface named Fluid.ISynthesizer
.
Fluid.Synthesizer
(construct: new Fluid.Synthesizer()
)
Fluid.AudioWorkletNodeSynthesizer
(construct: new Fluid.AudioWorkletNodeSynthesizer()
)
createAudioNode
method to use other instance methods.The Sequencer
instance is created only via following methods:
Fluid.Synthesizer.createSequencer
(static method)
Fluid.ISequencer
instance. The instance can be used with Fluid.Synthesizer
instances.Fluid.AudioWorkletNodeSynthesizer.prototype.createSequencer
(instance method)
Fluid.ISequencer
instance. The instance can be used with Fluid.AudioWorkletNodeSynthesizer
instances which handled createSequencer
calls.NOTE: libfluidsynth-2.0.2.js
(or above) is necessary to use this feature.
From v1.2.0, you can hook MIDI events posted by player. For Fluid.Synthesizer
instance, use hookPlayerMIDIEvents
method as followings:
syn.hookPlayerMIDIEvents(function (s, type, event) {
// hook '0xC0' event (Program Change event)
if (type === 0xC0) {
// if the 'program' value is 0, use another SoundFont
if (event.getProgram() === 0) {
syn.midiProgramSelect(event.getChannel(), secondSFont, 0, 0);
return true;
}
}
// return false to use default processings for other events
return false;
});
For Fluid.AudioWorkletNodeSynthesizer
instance, use hookPlayerMIDIEventsByName
as followings:
// We must add method to AudioWorkletGlobalScope to pass to another module.
AudioWorkletGlobalScope.myHookPlayerEvents = function (s, type, event, data) {
if (type === 0xC0) {
if (event.getProgram() === 0) {
// 'secondSFont' will be passed from 'hookPlayerMIDIEventsByName'
s.midiProgramSelect(event.getChannel(), data.secondSFont, 0, 0);
return true;
}
}
return false;
};
// before use this, 'worklet.js' above must be loaded as AudioWorklet completely, and
// syn.createAudioNode must be called to activate worklet.
// The first parameter is the method name added to 'AudioWorkletGlobalScope'.
// The second parameter will be passed to the worklet.
syn.hookPlayerMIDIEventsByName('myHookPlayerEvents', { secondSFont: secondSFont });
The sequencer also supports 'user-defined client' to handle event data.
Synthesizer.createSequencer
, use Synthesizer.registerSequencerClient
static method.
Synthesizer.sendEventNow
static method to event data processed by the synthesizer or another clients.createSequencer
of AudioWorkletNodeSynthesizer
, use registerSequencerClientByName
instance method.
hookPlayerMIDIEventsByName
's callback.Synthesizer.sendEventNow
in the worklet. Synthesizer
constructor is available via AudioWorkletGlobalScope.Fluid.Synthesizer
.Fluid.rewriteEventData
(AudioWorkletGlobalScope.Fluid.rewriteEventData
for worklet).Fluid
methodswaitForReady
Can be used to wait for the synthesizer engine's ready.
Return: Promise
object (resolves when the synthesizer engine (libfluidsynth) is ready)
Fluid.ISynthesizer
methods(Not documented yet. Please see dist/lib/ISynthesizer.d.ts
.)
fluid-js is licensed under BSD 3-Clause License except for the files in externals
directory.
For licenses of the files in externals
directory, please read externals/README.md
.
v1.2.0
FAQs
Wrapper JS library of fluidsynth for using with Web Audio or etc.
The npm package fluid-js receives a total of 6 weekly downloads. As such, fluid-js popularity was classified as not popular.
We found that fluid-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.