xxxxxxxxxx
197
music_vae = new mm.MusicVAE('https://storage.googleapis.com/magentadata/js/checkpoints/music_vae/drums_2bar_lokl_small');
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 = 200;
vae_temperature = 2;
let drum;
let drumSpeed = 50;
let drumSpeedStep = 10;
//noiseSound
let noise, env, analyzer;
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
//createNoise
createCanvas(710, 200);
noise = new p5.Noise(); // other types include 'brown' and 'pink'
noise.start();
// multiply noise volume by 0
// (keep it quiet until we're ready to make noise!)
noise.amp(0);
env = new p5.Env();
// set attackTime, decayTime, sustainRatio, releaseTime
env.setADSR(0.001, 0.1, 0.2, 0.1);
// set attackLevel, releaseLevel
env.setRange(1, 0);
// p5.Amplitude will analyze all sound in the sketch
// unless the setInput() method is used to specify an input.
analyzer = new p5.Amplitude();
}
function draw() {
background(220);
//if(vaePlayer.stop()){
if (!(vaePlayer.isPlaying())) {
//playVAE();
music_vae
.sample(1, vae_temperature)
.then((samples) => {
vaePlayer.start(drum,drumSpeed)
});
}
//
// get volume reading from the p5.Amplitude analyzer
let level = analyzer.getLevel();
// use level to draw a green rectangle
let levelHeight = map(level, 0, 0.4, 0, height);
fill(100, 250, 100);
rect(0, height, width, -levelHeight);
}
function mousePressed() {
env.play(noise);
}
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(){
//playVAE();
//drumSpeed = drumSpeed - drumSpeedStep;
//if(drumSpeed <= 30 || drumSpeed >= 400){
// drumSpeedStep = - drumSpeedStep;
//}
music_vae
.sample(1, vae_temperature)
.then((samples) => {
drum = samples[0];
//download();
})
}
function playVAE() {
if (vaePlayer.isPlaying()) {
vaePlayer.stop();
return;
}
music_vae
.sample(1, vae_temperature)
.then((samples) => {
drum = samples[0];
})
/*
music_vae
.sample(1, vae_temperature)
.then((samples) => {
vaePlayer.start(drum,drumSpeed)
});*/
}
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);
});
}
function download() {
if (!drum) {
alert('You must generate a trio before you can download it!');
} else {
saveAs(new File([mm.sequenceProtoToMidi(drum)], 'drum.mid'));
}
}