Skip to content

Commit e99349c

Browse files
committed
Initial commit (as v1.0.0)
0 parents  commit e99349c

22 files changed

+10325
-0
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[*]
2+
charset = utf-8
3+
end_of_line = lf
4+
insert_final_newline = true
5+
indent_style = tab
6+
indent_size = 4
7+
8+
[{*.md,README,README.*,LICENSE,LICENSE.*}]
9+
indent_style = space
10+
11+
[{*.json,.bowerrc,.gitignore,.npmignore,.gitattributes,.editorconfig}]
12+
end_of_line = lf
13+
indent_style = space
14+
indent_size = 2

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.vs/
2+
/.vscode/
3+
/dist/
4+
/node_modules/
5+
.github
6+
npm-debug.log
7+
yarn-error.log

.npmignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/.settings/
2+
/.vs/
3+
/.vscode/
4+
/build/
5+
/examples/
6+
/src/
7+
/tools/
8+
/typings/
9+
.editorconfig
10+
.gitignore
11+
.gitmodules
12+
.gitattributes
13+
.npmignore
14+
tsconfig.json
15+
yarn.lock

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## v1.0.0
4+
5+
- Initial version (using [fluidsynth-emscripten v2.0.1-em](https://github.com/jet2jet/fluidsynth-emscripten/releases/tag/v2.0.1-em))

LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
fluid-js license
2+
3+
Copyright (C) 2018 jet
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without modification,
7+
are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice,
10+
this list of conditions and the following disclaimer.
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
3. The name of the author may not be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22+
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25+
OF SUCH DAMAGE.

README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
[![NPM version](https://badge.fury.io/js/fluid-js.svg)](https://www.npmjs.com/package/fluid-js)
2+
3+
fluid-js
4+
==========
5+
6+
fluid-js is a library that generates audio data (frames). fluid-js uses wasm version of FluidSynth.
7+
8+
## Install
9+
10+
```
11+
npm install --save fluid-js
12+
```
13+
14+
## Usage
15+
16+
### From main thread
17+
18+
Copies `dist/fluid.js` (or `dist/fluid.min.js`) and `externals/libfluidsynth-2.0.1.js` (libfluidsynth JS file) to your project, and writes `<script>` tags as following order:
19+
20+
```html
21+
<script src="libfluidsynth-2.0.1.js"></script>
22+
<script src="fluid.js"></script>
23+
```
24+
25+
When scripts are available, you can use APIs via `Fluid` namespace object.
26+
27+
```js
28+
// Prepare the AudioContext instance
29+
var context = new AudioContext();
30+
var synth = new Fluid.Synthesizer();
31+
synth.init(context.sampleRate);
32+
33+
// Create AudioNode (ScriptProcessorNode) to output audio data
34+
var node = synth.createAudioNode(context, 8192); // 8192 is the frame count of buffer
35+
node.connect(context.destination);
36+
37+
// Load your SoundFont data (sfontBuffer: ArrayBuffer)
38+
synth.loadSFont(sfontBuffer).then(function () {
39+
// Load your SMF file data (smfBuffer: ArrayBuffer)
40+
return synth.addSMFDataToPlayer(smfBuffer);
41+
}).then(function () {
42+
// Play the loaded SMF data
43+
return synth.playPlayer();
44+
}).then(function () {
45+
// Wait for finishing playing
46+
return synth.waitForPlayerStopped();
47+
}).then(function () {
48+
// Wait for all voices stopped
49+
return synth.waitForVoicesStopped();
50+
}).then(function () {
51+
// Releases the synthesizer
52+
synth.close();
53+
}, function (err) {
54+
console.log('Failed:', err);
55+
// Releases the synthesizer
56+
synth.close();
57+
});
58+
```
59+
60+
(Above example uses Web Audio API, but you can use `Synthesizer` without Web Audio, by using `render()` method.)
61+
62+
If you prefer to load fluid-js as an ES module, you can use `import` statement such as `import * as Fluid from 'fluid-js'`.
63+
64+
Notes:
65+
66+
* `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.
67+
* libfluidsynth JS file is not `import`-able and its license (LGPL v2.1) is differ from fluid-js's (BSD-3-Clause).
68+
69+
### With AudioWorklet
70+
71+
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:
72+
73+
```js
74+
var context = new AudioContext();
75+
context.audioWorklet.addModule('libfluidsynth-2.0.1.js')
76+
.then(function () {
77+
return context.audioWorklet.addModule('fluid.worklet.js');
78+
})
79+
.then(function () {
80+
// Create the synthesizer instance for AudioWorkletNode
81+
var synth = new Fluid.AudioWorkletNodeSynthesizer();
82+
synth.init(context.sampleRate);
83+
// You must create AudioWorkletNode before using other methods
84+
// (This is because the message port is not available until the
85+
// AudioWorkletNode is created)
86+
audioNode = synth.createAudioNode(context);
87+
audioNode.connect(context.destination); // or another node...
88+
// After node creation, you can use Synthesizer methods
89+
return synth.loadSFont(sfontBuffer).then(function () {
90+
return synth.addSMFDataToPlayer(smfBuffer);
91+
}).then(function () {
92+
return synth.playPlayer();
93+
}).then(function () {
94+
...
95+
});
96+
});
97+
```
98+
99+
## API
100+
101+
### Creation of Synthesizer instance
102+
103+
These classes implement the interface named `Fluid.ISynthesizer`.
104+
105+
* `Fluid.Synthesizer` (construct: `new Fluid.Synthesizer()`)
106+
* Creates the general synthesizer instance. No parameters are available.
107+
* `Fluid.AudioWorkletNodeSynthesizer` (construct: `new Fluid.AudioWorkletNodeSynthesizer()`)
108+
* Creates the synthesizer instance communicating AudioWorklet (see above). No parameters are available.
109+
110+
### `Fluid.ISynthesizer` methods
111+
112+
(Not documented yet. Please see `dist/lib/ISynthesizer.d.ts`.)
113+
114+
## License
115+
116+
fluid-js is licensed under [BSD 3-Clause License](./LICENSE) except for the files in `externals` directory.
117+
For licenses of the files in `externals` directory, please read [`externals/README.md`](./externals/README.md).

build/webpack.config.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
const webpack = require('webpack');
6+
7+
const packageJson = require('../package.json');
8+
9+
const LIBRARY_NAME = 'fluid-js';
10+
const LIBRARY_FILENAME = 'fluid';
11+
const LIBRARY_NAMESPACE = 'Fluid';
12+
const LIBRARY_VERSION = packageJson.version;
13+
const AUTHOR = packageJson.author;
14+
15+
const isMinified = process.env.NODE_ENV === 'minified';
16+
const suffix = isMinified ? '.min' : '';
17+
18+
const headerTextTemplate = fs.readFileSync(path.resolve(__dirname, '../src/banner/header.txt'), 'utf8');
19+
const preparedHeaderText = prependHeaderTextImpl(
20+
LIBRARY_NAME, AUTHOR, LIBRARY_VERSION
21+
);
22+
23+
const webpackConfBase = {
24+
mode: isMinified ? 'production' : 'development',
25+
devtool: 'source-map',
26+
module: {
27+
rules: [
28+
{
29+
test: /\.tsx?$/,
30+
use: [
31+
{
32+
loader: 'ts-project-loader',
33+
options: {
34+
tempBuildDir: isMinified ? void (0) : path.resolve(__dirname, '../dist/lib'),
35+
compilerOptions: {
36+
declaration: !isMinified
37+
}
38+
}
39+
}
40+
]
41+
}
42+
]
43+
},
44+
optimization: {
45+
concatenateModules: true,
46+
namedModules: false
47+
},
48+
plugins: [
49+
new webpack.BannerPlugin({
50+
banner: preparedHeaderText,
51+
raw: true
52+
})
53+
],
54+
resolve: {
55+
extensions: ['.tsx', '.ts', '.js']
56+
}
57+
};
58+
59+
module.exports = [
60+
Object.assign({
61+
entry: {
62+
[LIBRARY_FILENAME]: path.resolve(__dirname, '../src/main/index.ts')
63+
},
64+
output: {
65+
path: path.resolve(__dirname, '../dist'),
66+
filename: `[name]${suffix}.js`,
67+
libraryTarget: 'umd',
68+
library: {
69+
root: LIBRARY_NAMESPACE,
70+
amd: LIBRARY_NAMESPACE,
71+
commonjs: LIBRARY_NAME
72+
},
73+
globalObject: 'this'
74+
},
75+
}, webpackConfBase),
76+
Object.assign({
77+
entry: {
78+
[`${LIBRARY_FILENAME}.worklet`]: path.resolve(__dirname, '../src/main/workletEntry.ts')
79+
},
80+
output: {
81+
path: path.resolve(__dirname, '../dist'),
82+
filename: `[name]${suffix}.js`
83+
},
84+
}, webpackConfBase)
85+
];
86+
87+
/**
88+
* @param {number|string} num numeric data
89+
* @param {number} length minimum length
90+
* @return {string} converted string
91+
*/
92+
function toNumberStringWithZero(num, length) {
93+
num = num.toString();
94+
length -= num.length;
95+
if (length > 0)
96+
num = Array(length + 1).join('0') + num;
97+
return num;
98+
}
99+
100+
function prependHeaderTextImpl(name, author, version) {
101+
var date = new Date();
102+
var s;
103+
return headerTextTemplate
104+
.replace('[name]', name)
105+
.replace('[author]', author)
106+
.replace('[version]', version || '')
107+
.replace('[year4]', toNumberStringWithZero(date.getFullYear(), 4))
108+
.replace(
109+
'[date]',
110+
toNumberStringWithZero(date.getFullYear(), 4) + '-' +
111+
toNumberStringWithZero(date.getMonth() + 1, 2) + '-' +
112+
toNumberStringWithZero(date.getDate(), 2)
113+
);
114+
}

0 commit comments

Comments
 (0)