Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/patrickroberts/synth-js
JavaScript MIDI-to-WAV synthesizer
https://github.com/patrickroberts/synth-js
command-line-tool javascript midi-parser node-module wav-files
Last synced: about 1 month ago
JSON representation
JavaScript MIDI-to-WAV synthesizer
- Host: GitHub
- URL: https://github.com/patrickroberts/synth-js
- Owner: patrickroberts
- License: mit
- Created: 2017-05-04T07:39:05.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-01T10:36:22.000Z (almost 2 years ago)
- Last Synced: 2024-10-06T23:45:30.427Z (3 months ago)
- Topics: command-line-tool, javascript, midi-parser, node-module, wav-files
- Language: JavaScript
- Size: 89.8 KB
- Stars: 37
- Watchers: 5
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# synth-js
Command-line utility and Node module for generating raw audio data from MIDI files.
## Installation
### Command-line utility:
```bash
$ npm link synth-js
```### Node module:
```bash
$ npm install --save synth-js
```### JavaScript:
```html
```
After including the file from `dst/synth.min.js`, the global variable `synth` will be initialized.
## Usage
### Command-line utility:
```bash
# assuming song.mid in cwd
$ synth -i song
# now song.wav contains raw audio data for song.mid
```### Node module:
```js
const synth = require('synth-js');
const fs = require('fs');let midBuffer = fs.readFileSync('song.mid');
// convert midi buffer to wav buffer
let wavBuffer = synth.midiToWav(midiBuffer).toBuffer();fs.writeFileSync('song.wav', wavBuffer, {encoding: 'binary'});
```### JavaScript:
```html
#wav:after {
content: " " attr(download);
}#wav:not([href]) {
display: none;
}var audio;
var input = document.getElementById('midi');
var anchor = document.getElementById('wav');input.addEventListener('change', function change() {
// clean up previous song, if any
if (anchor.hasAttribute('href')) {
URL.revokeObjectURL(anchor.href);
anchor.removeAttribute('href');if (audio && !audio.paused) {
audio.pause();
}
}// check if file exists
if (input.files.length > 0) {
var reader = new FileReader();
var midName = input.files[0].name;
// replace file extension with .wav
var wavName = midName.replace(/\..+?$/, '.wav');// set callback for array buffer
reader.addEventListener('load', function load(event) {
// convert midi arraybuffer to wav blob
var wav = synth.midiToWav(event.target.result).toBlob();
// create a temporary URL to the wav file
var src = URL.createObjectURL(wav);audio = new Audio(src);
audio.play();anchor.setAttribute('href', src);
});// read the file as an array buffer
reader.readAsArrayBuffer(input.files[0]);// set the name of the wav file
anchor.setAttribute('download', wavName);
}
});```
See the demo [here][browser-demo].
## FAQ
### Where can I find documentation?
Currently, documentation only exists for the command-line utility.
To access it, use `man`:```bash
$ man synth
```For Node or JavaScript, refer to the `src/` directory for accessible APIs:
* `synth.WAV()`
* `synth.MIDIStream()`
* `synth.midiToWav()`## License
Available under the MIT License
[browser-demo]: https://jsfiddle.net/patrob10114/o5r1adyz/show/