forked from tambien/Piano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.js
139 lines (117 loc) · 3.17 KB
/
Demo.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {Piano} from './src/Piano'
import Tone, { Buffer, Master, Part, Transport } from 'tone'
Tone.context.lookAhead = 0
import MidiConvert from 'midiconvert'
import WebMidi from 'webmidi'
import events from 'events'
var piano = new Piano([21, 108], 5).toMaster()
piano.load().then(()=>{
//make the button active on load
let button = document.querySelector('button')
button.classList.add('active')
document.querySelector('#loading').remove()
})
/**
* LOADING BAR
*/
Buffer.on('progress', (prog) => {
document.querySelector('#loading #fill').style.width = (prog * 100).toString() + '%'
})
/**
* MIDI FILE
*/
MidiConvert.load('clair_de_lune.mid').then((midi) => {
function playNote(time, event){
piano.keyDown(event.note, event.velocity, time).keyUp(event.note, time + event.duration)
}
Transport.bpm.value = midi.bpm
Transport.timeSignature = midi.timeSignature
//schedule the pedal
let sustain = new Part((time, event) => {
if (event.value){
piano.pedalDown(time)
} else {
piano.pedalUp(time)
}
}, midi.tracks[0].controlChanges[64]).start(0)
let noteOffEvents = new Part((time, event) => {
piano.keyUp(event.midi, time, event.velocity)
}, midi.tracks[0].noteOffs).start(0)
let noteOnEvents = new Part((time, event) => {
piano.keyDown(event.midi, time, event.velocity)
}, midi.tracks[0].notes).start(0)
})
/**
* VOLUME CONTROLS
*/
window.addEventListener('DOMContentLoaded', () => {
let inputs = document.querySelectorAll('input')
for (let input of inputs){
input.addEventListener('input', (e) => {
piano.setVolume(e.target.name, e.target.value)
})
}
let button = document.querySelector('button')
button.addEventListener('click', (e) => {
if (Transport.state === 'started'){
Transport.pause()
button.textContent = 'START'
} else {
Transport.start()
button.textContent = 'PAUSE'
}
})
})
class Midi extends events.EventEmitter{
constructor(){
super()
this._isEnabled = false
WebMidi.enable((err) => {
if (!err){
this._isEnabled = true
if (WebMidi.inputs){
WebMidi.inputs.forEach((input) => this._bindInput(input))
}
WebMidi.addListener('connected', (device) => {
if (device.input){
this._bindInput(device.input)
}
})
}
})
}
_bindInput(inputDevice){
if (this._isEnabled){
WebMidi.addListener('disconnected', (device) => {
if (device.input){
device.input.removeListener('noteOn')
device.input.removeListener('noteOff')
}
})
inputDevice.addListener('noteon', 'all', (event) => {
this.emit('keyDown', event.note.number, event.velocity)
})
inputDevice.addListener('noteoff', 'all', (event) => {
this.emit('keyUp', event.note.number, event.velocity)
})
inputDevice.addListener('controlchange', "all", (event) => {
if (event.controller.name === 'holdpedal'){
this.emit(event.value ? 'pedalDown' : 'pedalUp')
}
})
}
}
}
const midi = new Midi()
midi.on('keyDown', (note, velocity) => {
piano.keyDown(note, Tone.now(), velocity)
})
midi.on('keyUp', (note, velocity) => {
piano.keyUp(note, Tone.now(), velocity)
})
midi.on('pedalDown', () => {
piano.pedalDown()
})
midi.on('pedalUp', () => {
piano.pedalUp()
})