-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtp-midi-store.html
96 lines (83 loc) · 2.4 KB
/
tp-midi-store.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<link rel="import" href="../bower_components/polymer/polymer.html">
<!--
`tp-midi-store`
@demo demo/index.html
-->
<dom-module id="tp-midi-store">
<template>
</template>
<script>
Polymer({
is: 'tp-midi-store',
properties: {
/* Specifies time resolution for each beat. Cannot be chaned dynamically now.*/
resolution: {
type: Number,
value: 24,
},
},
_starttime: 0,
// FIXME: We may want to consider a de facto JSON based format.
_data: {
resolution: 24,
messages: [],
},
// FIXME: Should consider channel.
_hangingNotes: [],
data: function () {
return this._data.messages;
},
ready: function () {
},
/**
* Appends MIDI messages at time.
* @param {MidiTime} time
* @param {sequence<octet>} message
*/
append: function (time, message) {
var status = message[0] & 0xf0;
var channel = message[0] & 0x0f;
// Note on with velocity 0 should be handled as a note off.
if (status == 0x90 && message[2] == 0)
status = 0x80;
var data = {};
switch (status) {
case 0x80:
// FIXME: Merging note-on/off could be optional.
if (!this._hangingNotes[message[1]] ||
this._hangingNotes[message[1]].length == 0) {
console.error('unexpected note off received.');
break;
}
// FIXME: Should consider channels.
data = this._hangingNotes[message[1]].shift();
data.offAt = {
measure: time.measure,
beat: time.beat,
tick: time.tick
};
return data;
case 0x90:
data.type = 'note';
data.number = message[1];
data.velocity = message[2];
data.onAt = {
measure: time.measure,
beat: time.beat,
tick: time.tick
};
data.offAt = data.onAt;
this._data.messages.push(data);
if (!this._hangingNotes[data.number])
this._hangingNotes[data.number] = [];
this._hangingNotes[data.number].push(data);
break;
default:
// do not support it.
break;
}
return undefined;
},
});
</script>
</dom-module>