xxxxxxxxxx
367
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 = 1;
let drum;
let drumSpeed = 75;
let drumSpeedStep = 10;
//Tone.js
let loopBeat;
let bassSynth;
let cymbalSynth;
let amSynth;
let fmSynth;
let pluckSynth;
let counter;
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
//Tone.js
counter = 0;
bassSynth = new Tone.MembraneSynth().toMaster();
pluckSynth = new Tone.PluckSynth().toMaster();
amSynth = new Tone.AMSynth({
harmonicity: 1.4, //5/1, //3/1
detune: 0,
oscillator: {
type: "sine" //carrier signal
},
envelope: {
attack: 0.0001,
decay: 0.01,
sustain: 1,
release: 0.5
},
modulation: {
type: "square"
},
modulationEnvelope: {
attack: 0.005,
decay: 0,
sustain: 1,
release: 0.5
}
}).toMaster();
fmSynth = new Tone.FMSynth({
harmonicity: 1.02,
modulationIndex: 10,
detune: 0,
oscillator: {
type: "sine"
},
envelope: {
attack: 0.01,
decay: 0.01,
sustain: 1,
release: 0.5
},
modulation: {
type: "square"
},
modulationEnvelope: {
attack: 0.005,
decay: 0,
sustain: 1,
release: 0.5
}
}).toMaster();
cymbalSynth = new Tone.MetalSynth({
frequency: 250,
envelope: {
attack: 0.001,
decay: 0.1,
release: 0.01
},
harmonicity: 3.1,
modulationIndex: 16,
resonance: 8000,
octaves: 0.5
}
).toMaster();
loopBeat = new Tone.Loop(song, '16n').start();;
Tone.Transport.start(0).bpm.value = 75;
}
function draw() {
background(220);
//if(vaePlayer.stop()){
if (!(vaePlayer.isPlaying())) {
//playVAE();
music_vae
.sample(1, vae_temperature)
.then((samples) => {
vaePlayer.start(drum, drumSpeed)
});
}
}
function song(time) {
if (counter % 4 == 0) {
bassSynth.triggerAttackRelease('F#1', '8n', time, 1);
}
if (counter == 0) {
amSynth.triggerAttackRelease('A1', '16n', time, 1);
}
if (counter == 10) {
amSynth.triggerAttackRelease('Bb1', '16n', time, 1);
}
if (counter % 4 !== 1) {
if (counter == 3 || counter == 12) {
cymbalSynth.envelope.decay = 0.5;
} else {
cymbalSynth.envelope.decay = 0.01;
}
cymbalSynth.triggerAttackRelease('32n', time, 0.3);
}
counter = (counter + 1) % 16;
}
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'));
}
}