mirror of
https://github.com/wgreenberg/musicbox.git
synced 2024-11-22 19:40:25 +01:00
Add play/pause button and compressor
This commit is contained in:
parent
ae3346c73a
commit
04c255d9db
2 changed files with 21 additions and 3 deletions
|
@ -43,4 +43,5 @@ p {
|
||||||
letters are louder, non-letters are silent.</p>
|
letters are louder, non-letters are silent.</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<button id="play">Play/Stop</button>
|
||||||
</html>
|
</html>
|
||||||
|
|
23
musicbox.js
23
musicbox.js
|
@ -8,7 +8,7 @@ async function loadBuffer(ctx, path) {
|
||||||
function createBufferSource(ctx, buffer) {
|
function createBufferSource(ctx, buffer) {
|
||||||
let source = ctx.createBufferSource();
|
let source = ctx.createBufferSource();
|
||||||
source.buffer = buffer;
|
source.buffer = buffer;
|
||||||
source.connect(ctx.destination);
|
source.connect(ctx.compressor);
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,6 +158,15 @@ function crossProduct(a, b) {
|
||||||
window.addEventListener('load', async () => {
|
window.addEventListener('load', async () => {
|
||||||
loadHash();
|
loadHash();
|
||||||
const ctx = new AudioContext();
|
const ctx = new AudioContext();
|
||||||
|
const compressor = new DynamicsCompressorNode(ctx, {
|
||||||
|
threshold: -50,
|
||||||
|
knee: 40,
|
||||||
|
ratio: 12,
|
||||||
|
attack: 0,
|
||||||
|
release: 0.25,
|
||||||
|
});
|
||||||
|
compressor.connect(ctx.destination);
|
||||||
|
ctx.compressor = compressor;
|
||||||
const piano = new Piano('CDEFGAB');
|
const piano = new Piano('CDEFGAB');
|
||||||
const beats = new Beats();
|
const beats = new Beats();
|
||||||
await piano.init(ctx);
|
await piano.init(ctx);
|
||||||
|
@ -166,10 +175,18 @@ window.addEventListener('load', async () => {
|
||||||
const pianoSequencer = setupSequencer(piano, 'piano');
|
const pianoSequencer = setupSequencer(piano, 'piano');
|
||||||
const beatsSequencer = setupSequencer(beats, 'beats');
|
const beatsSequencer = setupSequencer(beats, 'beats');
|
||||||
|
|
||||||
|
let stopped = true;
|
||||||
|
|
||||||
|
document.getElementById('play').addEventListener('click', () => {
|
||||||
|
stopped = !stopped;
|
||||||
|
});
|
||||||
|
|
||||||
const bpm = 240;
|
const bpm = 240;
|
||||||
const msPerBeat = (1 / bpm) * 60 * 1000;
|
const msPerBeat = (1 / bpm) * 60 * 1000;
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
pianoSequencer.step();
|
if (!stopped) {
|
||||||
beatsSequencer.step();
|
pianoSequencer.step();
|
||||||
|
beatsSequencer.step();
|
||||||
|
}
|
||||||
}, msPerBeat);
|
}, msPerBeat);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue