-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpolate.js
51 lines (48 loc) · 1.62 KB
/
interpolate.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
49
50
51
import { createAsyncThunk } from '@reduxjs/toolkit'
import { removeNonJson, forceQuantized, trimToBars } from './utilsMelody'
import { actions } from './reduxStore'
import { BASIC_2 } from './melodies'
const { trim } = core.sequences
// import {melodies} from './melodies'
// https://magenta.github.io/magenta-js/music/classes/_music_vae_model_.musicvae.html#interpolate
/* interpolate(
inputSequences: INoteSequence[],
numInterps: number | number[],
temperature?: number,
controlArgs?: MusicVAEControlArgs
): Promise<INoteSequence[]>
*/
// see https://redux-toolkit.js.org/api/createAsyncThunk
export const interpolateMelodies = createAsyncThunk(
'meme/interpolate',
async ({
sources = ['a', 'c', 'b', 'd'],
stepsPerQuarter = 8,
count = 8,
temperature = 1
}, { dispatch, getState }) => {
const { memes, interpolate } = getState()
const inputSequences = sources.map(s => memes[s])
.map(m => m?.src || BASIC_2) // default in case one missing
.map(forceQuantized({ stepsPerQuarter }))
.map(removeNonJson)
// console.log("inputSequences",inputSequences)
const newMelodies = await model.interpolate(
inputSequences,
count,
temperature
)
// console.log("new melodies",newMelodies)
return newMelodies
.map(trimToBars(2)) // 4 beats, two bars - remove dangling beats
.map(removeNonJson)
}, {}
)
export const doInterpolation = (dispatch) => {
console.log('interpolating')
dispatch(actions.isInterpolating(true))
dispatch(interpolateMelodies({
sources: ['a', 'c', 'b', 'd']
}))
}
// const {dispatch,getState} = thunkAPI