-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtualKeyboard.js
More file actions
89 lines (74 loc) · 2.37 KB
/
virtualKeyboard.js
File metadata and controls
89 lines (74 loc) · 2.37 KB
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
import React from "react";
import MIDIInput from "./midiControlledSynth"
import "./synthesizer.css";
let midiInput = new MIDIInput();
const VirtualKeyboard = () => {
let keys = [
{ note: "C", frequency: 261.6 },
{ note: "C#", accidental: true, frequency: 277.2 },
{ note: "D", frequency: 293.7 },
{ note: "D#", accidental: true, frequency: 311.1 },
{ note: "E", frequency: 329.6},
{ note: "F", frequency: 349.2},
{ note: "F#", accidental: true, frequency: 370},
{ note: "G", frequency: 392},
{ note: "G#", accidental: true, frequency: 415.3},
{ note: "A", frequency: 440},
{ note: "A#", accidental: true, frequency: 466.2},
{ note: "B", frequency: 493.9}
];
let keyboardPressed = false;
document.onkeydown = (e) => {
if(!keyboardPressed) {
keyboardPressed = true;
let buttonCodes = ["KeyA","KeyW","KeyS","KeyE","KeyD","KeyF","KeyT","KeyG","KeyY","KeyH","KeyU","KeyJ"];
let buttonCodeIndex = buttonCodes.indexOf(e.code);
if(buttonCodeIndex !== -1) {
let selectedKey = keys[buttonCodeIndex];
keyPressed(selectedKey.frequency);
}
}
}
document.onkeyup = (e) => {
keyboardPressed = false;
keyReleased();
}
let keyPressed = (frequency) => {
midiInput.playSound(frequency);
}
let keyReleased = () => {
midiInput.stopSound();
}
let createVirtualKeyboard = () => {
let keyPosition = 450;
return (
<div>
<div id="virtual-keyboard">
<div id="oscillator-controls">
Type:
<select id="oscillator-type" onChange={(event) => midiInput.setOscillatorType(event.target.value)}>
<option value="sine">Sine</option>
<option value="triangle">Triangle</option>
<option value="square">Square</option>
<option value="sawtooth">Sawtooth</option>
</select>
Release:
<input type="range" min="0.1" max="2" step="0.1" id="release-slider" onInput={(event) => midiInput.setReleaseTime(event.target.value)} />
</div>
{keys.map((key, index) => {
keyPosition += (index === 5 ? 50 : 25);
return (
<div onMouseDown={() => keyPressed(key.frequency)} onMouseUp={keyReleased}
onMouseLeave={keyReleased} key={index}
className={"key" + (key.accidental ? " accidental" : "")}
style={{ left: keyPosition + "px"}}>
</div>
)
})}
</div>
</div>
)
}
return createVirtualKeyboard();
}
export default VirtualKeyboard;