From 03197b1ab992540b951fcbc6f841cfcd42a757f3 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 11 Jun 2022 16:42:33 +0200 Subject: Add kick sequencer --- src/view/sequencer.ts | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/view/sequencer.ts (limited to 'src/view/sequencer.ts') diff --git a/src/view/sequencer.ts b/src/view/sequencer.ts new file mode 100644 index 0000000..150f89b --- /dev/null +++ b/src/view/sequencer.ts @@ -0,0 +1,60 @@ +import h, { classNames } from 'lib/h' +import * as soundsLib from 'sounds' +import * as play from 'view/sequencer/play' +import * as addRemoveBeat from 'view/sequencer/addRemoveBeat' +import * as block from 'view/sequencer/block' + +export function view() { + let index = -1 + let blocks = [true] + let blocksNode = h('div', + { className: 'g-Sequencer__Blocks' }, + block.view({ + checked: true, + onCheck: checked => blocks[0] = checked + }) + ) + + let onNextStep = (sounds: soundsLib.Sounds) => { + let oldIndex = index + let newIndex = (index + 1) % blocks.length + index = newIndex + + let oldBlock = blocksNode.childNodes[oldIndex] as HTMLElement + if (oldBlock !== undefined) oldBlock.classList.remove('g-Sequencer__Block--Beat') + + let newBlock = blocksNode.childNodes[newIndex] as HTMLElement + newBlock.classList.add('g-Sequencer__Block--Beat') + + if (blocks[newIndex]) soundsLib.playKick(sounds) + } + + let sequencer = h('div', { className: 'g-Sequencer' }, + play.view({ + onNextStep, + onStop: () => { + let block = blocksNode.childNodes[index] as HTMLElement + block.classList.remove('g-Sequencer__Block--Beat') + index = -1 + } + }), + addRemoveBeat.view({ + initBeats: 1, + onRemove: index => { + let lastBlock = blocksNode.childNodes[index] + blocksNode.removeChild(lastBlock) + blocks.pop() + }, + onAdd: index => { + blocks.push(false) + blocksNode.appendChild(block.view({ + checked: false, + onCheck: checked => blocks[index] = checked + })) + } + }), + blocksNode + ) + + return sequencer +} -- cgit v1.2.3