Skip to content

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jazz-soft committed Jul 16, 2020
1 parent d7c0970 commit c423554
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 73 deletions.
162 changes: 93 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,43 @@ or get the full development version and minified scripts from [**GitHub**](https

##### Plain HTML

<script src="JZZ.js"></script>
<script src="JZZ.midi.SMF.js"></script>
//...
```html
<script src="JZZ.js"></script>
<script src="JZZ.midi.SMF.js"></script>
//...
```

##### CDN (jsdelivr)

<script src="https://cdn.jsdelivr.net/npm/jzz"></script>
<script src="https://cdn.jsdelivr.net/npm/jzz-midi-smf"></script>
//...
```html
<script src="https://cdn.jsdelivr.net/npm/jzz"></script>
<script src="https://cdn.jsdelivr.net/npm/jzz-midi-smf"></script>
//...
```

##### CDN (unpkg)

<script src="https://unpkg.com/jzz"></script>
<script src="https://unpkg.com/jzz-midi-smf"></script>
//...
```html
<script src="https://unpkg.com/jzz"></script>
<script src="https://unpkg.com/jzz-midi-smf"></script>
//...
```

##### CommonJS (Browserify and Node.js command line applications)

var JZZ = require('jzz');
require('jzz-midi-smf')(JZZ);
//...
```js
var JZZ = require('jzz');
require('jzz-midi-smf')(JZZ);
//...
```

##### AMD

require(['JZZ', 'JZZ.midi.SMF'], function(JZZ, dummy) {
// ...
});
```js
require(['JZZ', 'JZZ.midi.SMF'], function(JZZ, dummy) {
// ...
});
```

## MIDI files
Supported file formats: `.mid`, `.kar`, `.rmi`
Expand All @@ -61,83 +71,97 @@ Please check the [**API Reference**](https://jazz-soft.net/doc/JZZ/midifile.html

##### Playing MIDI file

var midiout = JZZ().openMidiOut();
var data = require('fs').readFileSync('file.mid', 'binary');
var smf = new JZZ.MIDI.SMF(data);
var player = smf.player();
player.connect(midiout);
player.play();
//...
player.speed(0.5); // play twice slower
```js
var midiout = JZZ().openMidiOut();
var data = require('fs').readFileSync('file.mid', 'binary');
var smf = new JZZ.MIDI.SMF(data);
var player = smf.player();
player.connect(midiout);
player.play();
//...
player.speed(0.5); // play twice slower
```

##### Viewing the contents of MIDI file

console.log(smf.toString());
```js
console.log(smf.toString());
```

##### Creating MIDI file from scratch

var smf = new JZZ.MIDI.SMF(0, 96); // type 0, 96 ticks per quarter note
var trk = new JZZ.MIDI.SMF.MTrk();
smf.push(trk);
// add contents:
trk.add(0, JZZ.MIDI.smfSeqName('This is a sequence name'))
.add(0, JZZ.MIDI.smfBPM(90)) // tempo 90 bpm
.add(96, JZZ.MIDI.noteOn(0, 'C6', 127))
.add(96, JZZ.MIDI.noteOn(0, 'Eb6', 127))
.add(96, JZZ.MIDI.noteOn(0, 'G6', 127))
.add(192, JZZ.MIDI.noteOff(0, 'C6'))
.add(192, JZZ.MIDI.noteOff(0, 'Eb6'))
.add(192, JZZ.MIDI.noteOff(0, 'G6'))
.add(288, JZZ.MIDI.smfEndOfTrack());
// or an alternative way:
trk.smfSeqName('This is a sequence name').smfBPM(90).tick(96)
.noteOn(0, 'C6', 127).noteOn(0, 'Eb6', 127).noteOn(0, 'G6', 127)
.tick(96).noteOff(0, 'C6').noteOff(0, 'Eb6').noteOff(0, 'G6')
.tick(96).smfEndOfTrack();
// or even shorter:
trk.smfSeqName('This is a sequence name').smfBPM(90).tick(96)
.ch(0).note('C6', 127, 96).note('Eb6', 127, 96).note('G6', 127, 96)
.tick(192).smfEndOfTrack();
```js
var smf = new JZZ.MIDI.SMF(0, 96); // type 0, 96 ticks per quarter note
var trk = new JZZ.MIDI.SMF.MTrk();
smf.push(trk);
// add contents:
trk.add(0, JZZ.MIDI.smfSeqName('This is a sequence name'))
.add(0, JZZ.MIDI.smfBPM(90)) // tempo 90 bpm
.add(96, JZZ.MIDI.noteOn(0, 'C6', 127))
.add(96, JZZ.MIDI.noteOn(0, 'Eb6', 127))
.add(96, JZZ.MIDI.noteOn(0, 'G6', 127))
.add(192, JZZ.MIDI.noteOff(0, 'C6'))
.add(192, JZZ.MIDI.noteOff(0, 'Eb6'))
.add(192, JZZ.MIDI.noteOff(0, 'G6'))
.add(288, JZZ.MIDI.smfEndOfTrack());
// or an alternative way:
trk.smfSeqName('This is a sequence name').smfBPM(90).tick(96)
.noteOn(0, 'C6', 127).noteOn(0, 'Eb6', 127).noteOn(0, 'G6', 127)
.tick(96).noteOff(0, 'C6').noteOff(0, 'Eb6').noteOff(0, 'G6')
.tick(96).smfEndOfTrack();
// or even shorter:
trk.smfSeqName('This is a sequence name').smfBPM(90).tick(96)
.ch(0).note('C6', 127, 96).note('Eb6', 127, 96).note('G6', 127, 96)
.tick(192).smfEndOfTrack();
```

##### Exporting MIDI file data as JSON or any custom format

One easy thing to remember: `SMF` is an `Array` of `Track`s and `Track` is an `Array` of MIDI events:

for (var i = 0; i < smf.length; i++) {
for (var j = 0; j < smf[i].length; j++) {
console.log('track:', i, 'tick:', smf[i][j].tt, smf[i][j].toString());
// or do whatever else with the message
}
}
```js
for (var i = 0; i < smf.length; i++) {
for (var j = 0; j < smf[i].length; j++) {
console.log('track:', i, 'tick:', smf[i][j].tt, smf[i][j].toString());
// or do whatever else with the message
}
}
```

##### Transposing MIDI file

for (var i = 0; i < smf.length; i++) {
if (smf[i] instanceof JZZ.MIDI.SMF.MTrk) {
for (var j = 0; j < smf[i].length; j++) {
var note = smf[i][j].getNote();
if (typeof note != 'undefined') {
if (smf[i][j].getChannel() != 9) { // skip the percussion channel
smf[i][j].setNote(note + 12); // transpose one octave up
}
}
```js
for (var i = 0; i < smf.length; i++) {
if (smf[i] instanceof JZZ.MIDI.SMF.MTrk) {
for (var j = 0; j < smf[i].length; j++) {
var note = smf[i][j].getNote();
if (typeof note != 'undefined') {
if (smf[i][j].getChannel() != 9) { // skip the percussion channel
smf[i][j].setNote(note + 12); // transpose one octave up
}
}
}
}
}
```

##### Getting the info

var player = smf.player();
var dump = smf.dump();
console.log('Type:', player.type());
console.log('Number of tracks:', player.tracks());
console.log('Size:', dump.length, 'bytes');
console.log('Duration:', player.duration(), 'ticks');
console.log('Total time:', player.durationMS(), 'milliseconds');
```js
var player = smf.player();
var dump = smf.dump();
console.log('Type:', player.type());
console.log('Number of tracks:', player.tracks());
console.log('Size:', dump.length, 'bytes');
console.log('Duration:', player.duration(), 'ticks');
console.log('Total time:', player.durationMS(), 'milliseconds');
```

##### Saving MIDI file

require('fs').writeFileSync('out.mid', smf.dump(), 'binary');
```js
require('fs').writeFileSync('out.mid', smf.dump(), 'binary');
```

## Live DEMOs (source code included)

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
},
"devDependencies": {
"coveralls": "^3.1.0",
"eslint": "^7.2.0",
"grunt": "^1.1.0",
"eslint": "^7.4.0",
"grunt": "^1.2.1",
"grunt-contrib-jshint": "^2.1.0",
"grunt-contrib-uglify": "^4.0.1",
"jzz-gui-player": "^1.4.0",
"jzz-synth-tiny": "^1.1.6",
"jzz-gui-player": "^1.4.3",
"jzz-synth-tiny": "^1.1.7",
"mocha": "^8.0.1",
"nyc": "^15.1.0"
},
Expand Down

0 comments on commit c423554

Please sign in to comment.