Skip to content

Commit 12c46f1

Browse files
committed
Add ability to set drum volume and tempo
1 parent 0886028 commit 12c46f1

File tree

2 files changed

+28
-61
lines changed

2 files changed

+28
-61
lines changed

components/DrumController.js

Lines changed: 0 additions & 57 deletions
This file was deleted.

components/Main.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import drumMachine from './DrumMachine'
66
import visualizer from './Visualizer'
77
import '../styles/application.scss'
88

9-
const DRUM_VOL = 1.3
109
const INITIAL_TEMPO = 180
1110

1211
const viz = visualizer()
1312
const drumDefaults = {
1413
context,
1514
wave: 'sine',
16-
gainVal: DRUM_VOL,
15+
gainVal: 1.0,
1716
viz
1817
}
1918

@@ -315,6 +314,15 @@ var model = (function (state) {
315314
if (typeof data.stopped !== 'undefined') {
316315
this.stopped = data.stopped
317316
}
317+
if (typeof data.drumVolume !== 'undefined') {
318+
this.drums = this.drums.map(drum => ({
319+
...drum,
320+
instance: {
321+
...drum.instance,
322+
gainVal: data.drumVolume
323+
}
324+
}))
325+
}
318326
this.state.render(model)
319327
}
320328
}
@@ -350,6 +358,9 @@ var actions = {
350358
selectDrumBeat: function (data = {}) {
351359
model.present({ selectedDrumBeat: data.beat })
352360
},
361+
setDrumVolume: function (data = {}) {
362+
model.present({ drumVolume: data.volume })
363+
},
353364
stopDrum: function (data = {}) {
354365
model.present({ stopped: true })
355366
},
@@ -359,14 +370,14 @@ var actions = {
359370
}
360371
let drumBeatInterval
361372

362-
const startDrum = function () {
373+
const startDrum = function (tempo = INITIAL_TEMPO) {
363374
drumBeatInterval = setInterval(function () {
364375
actions.tick({ beat: model.beat })
365376
model.drums.forEach(drum => actions.hitDrumIfSelected({
366377
beat: model.beat,
367378
drum
368379
}))
369-
}, (60 * 1000) / INITIAL_TEMPO)
380+
}, (60 * 1000) / tempo)
370381
}
371382

372383
startDrum()
@@ -390,4 +401,17 @@ const mouseClick = function (e) {
390401
}
391402
}
392403

404+
const handleChange = function (e) {
405+
e.preventDefault()
406+
if (e.target.id === 'drum-volume') {
407+
actions.setDrumVolume({ volume: e.target.value / 100 })
408+
}
409+
if (e.target.id === 'tempo') {
410+
clearInterval(drumBeatInterval)
411+
startDrum(parseInt(e.target.value))
412+
}
413+
}
414+
393415
document.addEventListener('click', mouseClick, false)
416+
417+
document.addEventListener('change', handleChange)

0 commit comments

Comments
 (0)