@@ -6,14 +6,13 @@ import drumMachine from './DrumMachine'
6
6
import visualizer from './Visualizer'
7
7
import '../styles/application.scss'
8
8
9
- const DRUM_VOL = 1.3
10
9
const INITIAL_TEMPO = 180
11
10
12
11
const viz = visualizer ( )
13
12
const drumDefaults = {
14
13
context,
15
14
wave : 'sine' ,
16
- gainVal : DRUM_VOL ,
15
+ gainVal : 1.0 ,
17
16
viz
18
17
}
19
18
@@ -315,6 +314,15 @@ var model = (function (state) {
315
314
if ( typeof data . stopped !== 'undefined' ) {
316
315
this . stopped = data . stopped
317
316
}
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
+ }
318
326
this . state . render ( model )
319
327
}
320
328
}
@@ -350,6 +358,9 @@ var actions = {
350
358
selectDrumBeat : function ( data = { } ) {
351
359
model . present ( { selectedDrumBeat : data . beat } )
352
360
} ,
361
+ setDrumVolume : function ( data = { } ) {
362
+ model . present ( { drumVolume : data . volume } )
363
+ } ,
353
364
stopDrum : function ( data = { } ) {
354
365
model . present ( { stopped : true } )
355
366
} ,
@@ -359,14 +370,14 @@ var actions = {
359
370
}
360
371
let drumBeatInterval
361
372
362
- const startDrum = function ( ) {
373
+ const startDrum = function ( tempo = INITIAL_TEMPO ) {
363
374
drumBeatInterval = setInterval ( function ( ) {
364
375
actions . tick ( { beat : model . beat } )
365
376
model . drums . forEach ( drum => actions . hitDrumIfSelected ( {
366
377
beat : model . beat ,
367
378
drum
368
379
} ) )
369
- } , ( 60 * 1000 ) / INITIAL_TEMPO )
380
+ } , ( 60 * 1000 ) / tempo )
370
381
}
371
382
372
383
startDrum ( )
@@ -390,4 +401,17 @@ const mouseClick = function (e) {
390
401
}
391
402
}
392
403
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
+
393
415
document . addEventListener ( 'click' , mouseClick , false )
416
+
417
+ document . addEventListener ( 'change' , handleChange )
0 commit comments