html
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
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1 align="center" id="Hz"></h1>
<input type="range" class="ADSR" id="envRel" min="1" max="15" value="5" style="left:50px">
<input type="range" class="ADSR" id="envAtt" min="0" max="9" value="0">
<p style="position: absolute; margin: 0px; top: 185px; left: 65px">Att</p>
<p style="position: absolute; margin: 0px; top: 185px; left: 105px">Rel</p>
<div class="piano-roll" onTouchStart="playTone(event)">
<button class="note" id="-9">C
</button>
<button class=halfnote id="-8" ></button>
<button class="note" id="-7" >D</button>
<button class=halfnote id="-6" style="left:56px"></button>
<button class="note" id="-5">E</button>
<button class="note" id="-4">F</button>
<button class=halfnote id="-3" style="left:125px"></button>
<button class="note" id="-2">G</button>
<button class=halfnote id="-1" style="left:160px"></button>
<button class="note" id="0" >A</button>
<button class=halfnote id="1" style="left:195px"></button>
<button class="note" id="2">B</button>
<button class="note" id="3">C</button>
</div>
Enter to Rename, Shift+Enter to Preview
css
css
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
.piano-roll {
position: absolute;
width: 96vw;
height: 30vh;
background: none;
top: 68vh;
left: 12vw;
display: inline-flex;
min-width: 100px;
overflow: hidden;
}
.note {
height: 100%;
width: 10%;
background: white;
display: flex;
border-radius: 5px;
}
.halfnote {
position: absolute;
top: 0px;
left: 22px;
height: 40%;
width: 7%;
background: black;
border-radius: 5px;
Enter to Rename, Shift+Enter to Preview
js
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
var ctx = new AudioContext();
function playTone(e) {
var osc = ctx.createOscillator();
var gain = ctx.createGain();
var attackSlider = document.getElementById("envAtt");
var releaseSlider = document.getElementById("envRel");
var now = ctx.currentTime;
var attack = attackSlider.value/10;
var release = releaseSlider.value/10;
const diff = 1.0594630943593;
var frequency = (440 * Math.pow(diff,e.target.id)).toFixed(2);
osc.connect(gain);
gain.connect(ctx.destination);
osc.type = "square";
osc.frequency.value = frequency;
osc.start(0);
gain.gain.cancelScheduledValues(now);
// Anchor beginning of ramp at current value.
gain.gain.value = 0; gain.gain.setValueAtTime(gain.gain.value, now);
/*to set attack*/
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run