@huffduff/midi-writer-ts 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +24 -0
- package/.eslintignore +3 -0
- package/.eslintrc.js +18 -0
- package/.nvmrc +1 -0
- package/.travis.yml +3 -0
- package/LICENSE +21 -0
- package/README.md +200 -0
- package/browser/midiwriter.js +1367 -0
- package/build/index.cjs +1219 -0
- package/build/index.mjs +1217 -0
- package/build/types/abstract-event.d.ts +7 -0
- package/build/types/chunks/chunk.d.ts +5 -0
- package/build/types/chunks/header.d.ts +13 -0
- package/build/types/chunks/track.d.ts +139 -0
- package/build/types/constants.d.ts +16 -0
- package/build/types/main.d.ts +56 -0
- package/build/types/meta-events/copyright-event.d.ts +18 -0
- package/build/types/meta-events/cue-point-event.d.ts +18 -0
- package/build/types/meta-events/end-track-event.d.ts +16 -0
- package/build/types/meta-events/instrument-name-event.d.ts +18 -0
- package/build/types/meta-events/key-signature-event.d.ts +13 -0
- package/build/types/meta-events/lyric-event.d.ts +18 -0
- package/build/types/meta-events/marker-event.d.ts +18 -0
- package/build/types/meta-events/meta-event.d.ts +5 -0
- package/build/types/meta-events/tempo-event.d.ts +20 -0
- package/build/types/meta-events/text-event.d.ts +18 -0
- package/build/types/meta-events/time-signature-event.d.ts +13 -0
- package/build/types/meta-events/track-name-event.d.ts +18 -0
- package/build/types/midi-events/controller-change-event.d.ts +22 -0
- package/build/types/midi-events/midi-event.d.ts +7 -0
- package/build/types/midi-events/note-event.d.ts +31 -0
- package/build/types/midi-events/note-off-event.d.ts +36 -0
- package/build/types/midi-events/note-on-event.d.ts +36 -0
- package/build/types/midi-events/pitch-bend-event.d.ts +17 -0
- package/build/types/midi-events/program-change-event.d.ts +20 -0
- package/build/types/utils.d.ts +105 -0
- package/build/types/vexflow.d.ts +30 -0
- package/build/types/writer.d.ts +45 -0
- package/examples/chopin-prelude-e-minor.js +143 -0
- package/examples/hot-cross-buns.js +24 -0
- package/examples/mauro.giuliani-op.47-main-theme.js +136 -0
- package/examples/notes-by-start-tick.js +45 -0
- package/examples/zelda-main-theme.js +435 -0
- package/jsdoc.json +5 -0
- package/package.json +79 -0
- package/postinstall.js +1 -0
- package/rollup.config.js +22 -0
- package/runkit.js +18 -0
- package/test/main.js +339 -0
- package/test/vexflow.js +165 -0
- package/test/writer.js +59 -0
- package/tsconfig.json +13 -0
- package/typedoc.json +5 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# http://editorconfig.org
|
|
2
|
+
root = true
|
|
3
|
+
|
|
4
|
+
[*]
|
|
5
|
+
charset = utf-8
|
|
6
|
+
end_of_line = lf
|
|
7
|
+
indent_size = 4
|
|
8
|
+
indent_style = tab
|
|
9
|
+
insert_final_newline = true
|
|
10
|
+
trim_trailing_whitespace = true
|
|
11
|
+
|
|
12
|
+
[*.json]
|
|
13
|
+
insert_final_newline = false
|
|
14
|
+
indent_size = 2
|
|
15
|
+
indent_style = space
|
|
16
|
+
|
|
17
|
+
[*.md]
|
|
18
|
+
indent_size = 2
|
|
19
|
+
indent_style = space
|
|
20
|
+
trim_trailing_whitespace = false
|
|
21
|
+
|
|
22
|
+
[*.yml]
|
|
23
|
+
indent_size = 2
|
|
24
|
+
indent_style = space
|
package/.eslintignore
ADDED
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
"env": {
|
|
3
|
+
"browser": true,
|
|
4
|
+
"es6": true,
|
|
5
|
+
"node": true
|
|
6
|
+
},
|
|
7
|
+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
|
|
8
|
+
"parser": '@typescript-eslint/parser',
|
|
9
|
+
"plugins": ['@typescript-eslint'],
|
|
10
|
+
"globals": {
|
|
11
|
+
"Atomics": "readonly",
|
|
12
|
+
"SharedArrayBuffer": "readonly"
|
|
13
|
+
},
|
|
14
|
+
"parserOptions": {
|
|
15
|
+
"ecmaVersion": 2020,
|
|
16
|
+
"sourceType": "module"
|
|
17
|
+
}
|
|
18
|
+
};
|
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v18.14.2
|
package/.travis.yml
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Garrett Grimm
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
♬ MidiWriterJS
|
|
2
|
+
===============
|
|
3
|
+
[](https://www.npmjs.com/package/midi-writer-js)
|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
[](https://npm.runkit.com/midi-writer-js)
|
|
7
|
+
|
|
8
|
+
MidiWriterJS is a JavaScript library providing an API for generating expressive multi-track MIDI files.
|
|
9
|
+
|
|
10
|
+
Note that the `master` branch is in active development so if you're looking for a tried and true stable version please use the latest release.
|
|
11
|
+
|
|
12
|
+
[Source Documentation](https://grimmdude.com/MidiWriterJS/docs/)
|
|
13
|
+
|
|
14
|
+
Install
|
|
15
|
+
------------
|
|
16
|
+
```sh
|
|
17
|
+
npm install midi-writer-js
|
|
18
|
+
```
|
|
19
|
+
Getting Started
|
|
20
|
+
------------
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
import MidiWriter from 'midi-writer-js';
|
|
24
|
+
|
|
25
|
+
// Start with a new track
|
|
26
|
+
const track = new MidiWriter.Track();
|
|
27
|
+
|
|
28
|
+
// Define an instrument (optional):
|
|
29
|
+
track.addEvent(new MidiWriter.ProgramChangeEvent({instrument: 1}));
|
|
30
|
+
|
|
31
|
+
// Add some notes:
|
|
32
|
+
const note = new MidiWriter.NoteEvent({pitch: ['C4', 'D4', 'E4'], duration: '4'});
|
|
33
|
+
track.addEvent(note);
|
|
34
|
+
|
|
35
|
+
// Generate a data URI
|
|
36
|
+
const write = new MidiWriter.Writer(track);
|
|
37
|
+
console.log(write.dataUri());
|
|
38
|
+
```
|
|
39
|
+
Documentation
|
|
40
|
+
------------
|
|
41
|
+
|
|
42
|
+
### `MidiWriter.Track()`
|
|
43
|
+
|
|
44
|
+
- `addEvent({event}, mapFunction)`
|
|
45
|
+
- `setTempo(tempo)`
|
|
46
|
+
- `addText(text)`
|
|
47
|
+
- `addCopyright(text)`
|
|
48
|
+
- `addTrackName(text)`
|
|
49
|
+
- `addInstrumentName(text)`
|
|
50
|
+
- `addMarker(text)`
|
|
51
|
+
- `addCuePoint(text)`
|
|
52
|
+
- `addLyric(text)`
|
|
53
|
+
- `setTimeSignature(numerator, denominator)`
|
|
54
|
+
|
|
55
|
+
### `MidiWriter.NoteEvent({options})`
|
|
56
|
+
|
|
57
|
+
The MIDI spec defines that each note must have a `NoteOnEvent` and `NoteOffEvent` (or `NoteOnEvent` with zero velocity) event, marking the beginning and end of the sounding note. While it's possible to manually add these events to a track with `Track.addEvent()`, the `NoteEvent` provides a more intuitive interface for doing this with a single, "pseudo" event. Under the hood, the `NoteEvent` event generates the relevant `NoteOnEvent` and `NoteOffEvent` events.
|
|
58
|
+
|
|
59
|
+
Each MIDI event has a `delta` property, which is used to define the number of ticks to wait after the previous event. This can be challenging to calculate if you're not necessarily adding events in a serial fashion. Because of this, you can alternatively use the `tick` property to define the exact tick where the event should fall.
|
|
60
|
+
|
|
61
|
+
The `NoteEvent` supports these options:
|
|
62
|
+
|
|
63
|
+
<table>
|
|
64
|
+
<thead>
|
|
65
|
+
<tr>
|
|
66
|
+
<th>Name</th>
|
|
67
|
+
<th>Type</th>
|
|
68
|
+
<th>Default</th>
|
|
69
|
+
<th>Description</th>
|
|
70
|
+
</tr>
|
|
71
|
+
</thead>
|
|
72
|
+
<tbody>
|
|
73
|
+
<tr>
|
|
74
|
+
<td><b>pitch</b></td>
|
|
75
|
+
<td>string or array</td>
|
|
76
|
+
<td></td>
|
|
77
|
+
<td>Each pitch can be a string or valid MIDI note code. Format for string is <code>C#4</code>. Pro tip: You can use the output from <a href="https://github.com/danigb/tonal" target="_blank">tonal</a> functions to build scales, chords, intervals, etc. in this parameter.</td>
|
|
78
|
+
</tr>
|
|
79
|
+
<tr>
|
|
80
|
+
<td><b>duration</b></td>
|
|
81
|
+
<td>string or array</td>
|
|
82
|
+
<td></td>
|
|
83
|
+
<td>
|
|
84
|
+
How long the note should sound.
|
|
85
|
+
<ul>
|
|
86
|
+
<li><code>1</code> : whole</li>
|
|
87
|
+
<li><code>2</code> : half</li>
|
|
88
|
+
<li><code>d2</code> : dotted half</li>
|
|
89
|
+
<li><code>dd2</code> : double dotted half</li>
|
|
90
|
+
<li><code>4</code> : quarter</li>
|
|
91
|
+
<li><code>4t</code> : quarter triplet</li>
|
|
92
|
+
<li><code>d4</code> : dotted quarter</li>
|
|
93
|
+
<li><code>dd4</code> : double dotted quarter</li>
|
|
94
|
+
<li><code>8</code> : eighth</li>
|
|
95
|
+
<li><code>8t</code> : eighth triplet</li>
|
|
96
|
+
<li><code>d8</code> : dotted eighth</li>
|
|
97
|
+
<li><code>dd8</code> : double dotted eighth</li>
|
|
98
|
+
<li><code>16</code> : sixteenth</li>
|
|
99
|
+
<li><code>16t</code> : sixteenth triplet</li>
|
|
100
|
+
<li><code>32</code> : thirty-second</li>
|
|
101
|
+
<li><code>64</code> : sixty-fourth</li>
|
|
102
|
+
<li><code>Tn</code> : where n is an explicit number of ticks (T128 = 1 beat)</li>
|
|
103
|
+
</ul>
|
|
104
|
+
If an array of durations is passed then the sum of the durations will be used.
|
|
105
|
+
</td>
|
|
106
|
+
</tr>
|
|
107
|
+
<tr>
|
|
108
|
+
<td><b>wait</b></td>
|
|
109
|
+
<td>string or array</td>
|
|
110
|
+
<td><code>0</code></td>
|
|
111
|
+
<td>How long to wait before sounding note (rest). Takes same values as <b>duration</b>.</td>
|
|
112
|
+
</tr>
|
|
113
|
+
<tr>
|
|
114
|
+
<td><b>sequential</b></td>
|
|
115
|
+
<td>boolean</td>
|
|
116
|
+
<td><code>false</code></td>
|
|
117
|
+
<td>If true then array of pitches will be played sequentially as opposed to simulatanously.</td>
|
|
118
|
+
</tr>
|
|
119
|
+
<tr>
|
|
120
|
+
<td><b>velocity</b></td>
|
|
121
|
+
<td>number</td>
|
|
122
|
+
<td><code>50</code></td>
|
|
123
|
+
<td>How loud the note should sound, values 1-100.</td>
|
|
124
|
+
</tr>
|
|
125
|
+
<tr>
|
|
126
|
+
<td><b>repeat</b></td>
|
|
127
|
+
<td>number</td>
|
|
128
|
+
<td><code>1</code></td>
|
|
129
|
+
<td>How many times this event should be repeated.</td>
|
|
130
|
+
</tr>
|
|
131
|
+
<tr>
|
|
132
|
+
<td><b>channel</b></td>
|
|
133
|
+
<td>number</td>
|
|
134
|
+
<td><code>1</code></td>
|
|
135
|
+
<td>MIDI channel to use.</td>
|
|
136
|
+
</tr>
|
|
137
|
+
<tr>
|
|
138
|
+
<td><b>grace</b></td>
|
|
139
|
+
<td>string or array</td>
|
|
140
|
+
<td></td>
|
|
141
|
+
<td>Grace note to be applied to note event. Takes same value format as <code>pitch</code></td>
|
|
142
|
+
</tr>
|
|
143
|
+
<tr>
|
|
144
|
+
<td><b>tick</b></td>
|
|
145
|
+
<td>number</td>
|
|
146
|
+
<td></td>
|
|
147
|
+
<td>Specific tick where this event should be played. If this parameter is supplied then <code>wait</code> is disregarded if also supplied.</td>
|
|
148
|
+
</tr>
|
|
149
|
+
</tbody>
|
|
150
|
+
</table>
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
### `MidiWriter.Writer(tracks)`
|
|
154
|
+
The `Writer` class provides a few ways to output the file:
|
|
155
|
+
- `buildFile()` *Uint8Array*
|
|
156
|
+
- `base64()` *string*
|
|
157
|
+
- `dataUri()` *string*
|
|
158
|
+
- `stdout()` *file stream (cli)*
|
|
159
|
+
|
|
160
|
+
### Hot Cross Buns
|
|
161
|
+
Here's an example of how everyone's favorite song "Hot Cross Buns" could be written. Note use of the mapping function passed as the second argument of `addEvent()`. This can be used to apply specific properties to all events. With some
|
|
162
|
+
street smarts you could also use it for programmatic crescendos and other property 'animation'.
|
|
163
|
+
```javascript
|
|
164
|
+
import MidiWriter from 'midi-writer-js';
|
|
165
|
+
|
|
166
|
+
const track = new MidiWriter.Track();
|
|
167
|
+
|
|
168
|
+
track.addEvent([
|
|
169
|
+
new MidiWriter.NoteEvent({pitch: ['E4','D4'], duration: '4'}),
|
|
170
|
+
new MidiWriter.NoteEvent({pitch: ['C4'], duration: '2'}),
|
|
171
|
+
new MidiWriter.NoteEvent({pitch: ['E4','D4'], duration: '4'}),
|
|
172
|
+
new MidiWriter.NoteEvent({pitch: ['C4'], duration: '2'}),
|
|
173
|
+
new MidiWriter.NoteEvent({pitch: ['C4', 'C4', 'C4', 'C4', 'D4', 'D4', 'D4', 'D4'], duration: '8'}),
|
|
174
|
+
new MidiWriter.NoteEvent({pitch: ['E4','D4'], duration: '4'}),
|
|
175
|
+
new MidiWriter.NoteEvent({pitch: ['C4'], duration: '2'})
|
|
176
|
+
], function(event, index) {
|
|
177
|
+
return {sequential: true};
|
|
178
|
+
}
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
const write = new MidiWriter.Writer(track);
|
|
182
|
+
console.log(write.dataUri());
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### VexFlow Integration
|
|
186
|
+
MidiWriterJS can export MIDI from VexFlow voices, though this feature is still experimental. Current usage is to use `MidiWriter.VexFlow.trackFromVoice(voice)` to create a MidiWriterJS `Track` object:
|
|
187
|
+
```javascript
|
|
188
|
+
|
|
189
|
+
// ...VexFlow code defining notes
|
|
190
|
+
const voice = create_4_4_voice().addTickables(notes);
|
|
191
|
+
|
|
192
|
+
const vexWriter = new MidiWriter.VexFlow();
|
|
193
|
+
const track = vexWriter.trackFromVoice(voice);
|
|
194
|
+
const writer = new MidiWriter.Writer([track]);
|
|
195
|
+
console.log(writer.dataUri());
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
## Demos
|
|
200
|
+
* [Example with Magenta player](https://codepen.io/dirkk0/pen/rNZLXjZ) by Dirk Krause [@dirkk0](https://github.com/dirkk0)
|