-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.js
48 lines (42 loc) · 1.32 KB
/
transport.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { store } from './reduxStore'
import { delay } from './utilsMelody'
const { isQuantizedSequence, unquantizeSequence } = core.sequences
// set velocity to something sensible if it's not specified
const defaultVelocity = velocity => n => ({ velocity, ...n })
const gateEndTime = pc => n => ({
...n,
endTime: n.startTime + pc * (n.endTime - n.startTime)
})
const regulariseMelody = ({ velocity }) => melody => ({
...melody,
notes: melody.notes
.map(defaultVelocity(velocity))
// .map(gateEndTime(0.95))
})
// const p = ()=>window.player
const p = () => midiPlayer
export const playDrumNow = (pitch = 50) => {
player.isPlaying() && player.stop()
player.start({ totalTime: 0, notes: [{ pitch, isDrum: true }] })
}
export const startPlayer = ({
melody,
meme,
tempo = store?.getState()?.tempo || 120,
velocity = 75
// regulariseVelocity=true,
}) => {
if (meme) { melody = store.getState().memes?.[meme]?.src }
// const uMelody = isQuantizedSequence(melody) ?
// unquantizeSequence(melody,tempo) : melody
// console.log("startPlayer", melody,uMelody)
stopPlayer()
return delay(10)().then(() =>
p().start(
regulariseMelody({ velocity })(melody),
isQuantizedSequence(melody) ? tempo : undefined
) // returns promise
)
}
export const stopPlayer = () =>
p().isPlaying() && p().stop()