xxxxxxxxxx
119
music_vae = new mm.MusicVAE('https://storage.googleapis.com/magentadata/js/checkpoints/music_vae/mel_4bar_small_q2');
music_vae.initialize();
music_rnn = new mm.MusicRNN('https://storage.googleapis.com/magentadata/js/checkpoints/music_rnn/basic_rnn');
music_rnn.initialize();
rnn_steps = 20;
rnn_temperature = 2;
vae_steps = 20;
vae_temperature = 1.8;
function setup() {
createCanvas(400, 400);
//player = new mm.Player();
//player.start(ORIGINAL_TWINKLE_TWINKLE);
//player.stop();
vaePlayer = new mm.Player();//vae
rnnPlayer = new mm.Player();
//play();//rnn
playVAE();//vae
//playInterpolation();//vae
}
function draw() {
background(220);
}
ORIGINAL_TWINKLE_TWINKLE = {
notes: [
{pitch: 60, startTime: 0.0, endTime: 0.5},
{pitch: 60, startTime: 0.5, endTime: 1.0},
{pitch: 67, startTime: 1.0, endTime: 1.5},
{pitch: 67, startTime: 1.5, endTime: 2.0},
{pitch: 69, startTime: 2.0, endTime: 2.5},
{pitch: 69, startTime: 2.5, endTime: 3.0},
{pitch: 67, startTime: 3.0, endTime: 4.0},
{pitch: 65, startTime: 4.0, endTime: 4.5},
{pitch: 65, startTime: 4.5, endTime: 5.0},
{pitch: 64, startTime: 5.0, endTime: 5.5},
{pitch: 64, startTime: 5.5, endTime: 6.0},
{pitch: 62, startTime: 6.0, endTime: 6.5},
{pitch: 62, startTime: 6.5, endTime: 7.0},
{pitch: 60, startTime: 7.0, endTime: 8.0},
],
totalTime: 8
};
NEW_BEE = {
notes: [
{pitch: 67, startTime: 0.0, endTime: 0.5},
{pitch: 64, startTime: 0.5, endTime: 1.0},
{pitch: 64, startTime: 1.0, endTime: 2.0},
{pitch: 65, startTime: 2.0, endTime: 2.5},
{pitch: 62, startTime: 2.5, endTime: 3.0},
{pitch: 62, startTime: 3.0, endTime: 4.0},
{pitch: 60, startTime: 4.0, endTime: 4.5},
{pitch: 64, startTime: 4.5, endTime: 5.0},
{pitch: 67, startTime: 5.0, endTime: 5.5},
{pitch: 67, startTime: 5.5, endTime: 6.0},
{pitch: 60, startTime: 6.0, endTime: 8.0},
],
totalTime: 8
};
function play() {
if (rnnPlayer.isPlaying()) {
rnnPlayer.stop();
return;
}
// The model expects a quantized sequence, and ours was unquantized:
const qns = mm.sequences.quantizeNoteSequence(NEW_BEE, 4);
music_rnn
.continueSequence(qns, rnn_steps, rnn_temperature)
.then((sample) => rnnPlayer.start(sample));
}
function keyPressed(){
setup();
}
function playVAE() {
if (vaePlayer.isPlaying()) {
vaePlayer.stop();
return;
}
music_vae
.sample(1, vae_temperature)
.then((sample) => vaePlayer.start(sample[0]));
}
function playInterpolation() {
if (vaePlayer.isPlaying()) {
vaePlayer.stop();
return;
}
// Music VAE requires quantized melodies, so quantize them first.
const star = mm.sequences.quantizeNoteSequence(ORIGINAL_TWINKLE_TWINKLE, 4);
const bee = mm.sequences.quantizeNoteSequence(NEW_BEE, 4);
music_vae
.interpolate([star, bee], 4)
.then((sample) => {
const concatenated = mm.sequences.concatenate(sample);
vaePlayer.start(concatenated);
});
}