cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/@magenta/music@1.1.1"></script>
  </head>

  <body><center><div id='content'>
    <h1>Multitrack Interpolation</h1>
    <div id='status'>Loading model (25 MB)...</div>
    <div id='settings'>
      <div id='style1'>
        <h2>Style 1</h2>
        <button id='sample1' disabled>Random</button><br><br>
        <label id='importLabel1'>
          <input type='file' id='import1' accept='audio/midi' disabled>
          Import MIDI
        </label>
      </div>
      <div id='style2'>
        <h2>Style 2</h2>
        <button id='sample2' disabled>Random</button><br><br>
        <label id='importLabel2'>
          <input type='file' id='import2' accept='audio/midi' disabled>
          Import MIDI
        </label>
      </div>
      <br><br>
      <div id='position'>
        <button id='play' disabled>Play</button><br><br>
        <input id='alpha' type='range' min='0' max='15' value='0' disabled>
      </div>
    </div>
    <br><br><br>
    <button id='playInterp' disabled>Play the Whole Thing</button><br><br>
    <button id='download' disabled>Save as MIDI</button><br>
  </div>
  <p id='fineprint'>Uses samples from <a href='https://www.polyphone-soundfonts.com/en/files/27-instrument-sets/256-sgm-v2-01'>SGM</a> with modifications by <a href="https://sites.google.com/site/soundfonts4u/">John Nebauer</a>.  May work poorly on mobile.
    </center></body>
</html>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Roboto);

body {text-align:center; font-family:Roboto; font-size:12px; margin-top:20px; background-color:white}
h1 {font-size:24px}
h2 {font-size:16px}
input {text-align:center; font-size:14px; width:300px}
#status {color:black}
#content {width:500px; padding:10px 20px; background-color:lightgray; border:1px solid black; border-radius:20px}
#style1 {float:left}
#style2 {float:right}
#position {float:center}
input[type='file'] {display:none}
label {font-size:14px; border:1px solid #ccc; padding:6px; display:inline-block; border-radius:5px; background-color:white; color:gray}
button {font-size:14px; border:1px solid #ccc; padding:6px; display:inline-block; border-radius:5px; background-color:white; color:black}
button:disabled {color:gray}
button:focus {outline:0}
#fineprint {font-size:12px; width:500px}
            
          
!
            
              const QPM = 120;
const STEPS_PER_QUARTER = 24;
const Z_DIM = 256;
const HUMANIZE_SECONDS = 0.01;
const MAX_PAN = 0.2;

const MIN_DRUM = 35;
const MAX_DRUM = 81;

const tf = mm.tf;

// Set up Multitrack MusicVAE.
const model = new mm.MusicVAE('https://storage.googleapis.com/magentadata/js/checkpoints/music_vae/multitrack_fb256');

// Set up effects chain.
const globalCompressor = new mm.Player.tone.MultibandCompressor();
const globalReverb = new mm.Player.tone.Freeverb(0.25);
const globalLimiter = new mm.Player.tone.Limiter();

globalCompressor.connect(globalReverb);
globalReverb.connect(globalLimiter);
globalLimiter.connect(mm.Player.tone.Master);

// Set up per-program effects.
const programMap = new Map();
for (let i=0; i<128; i++) {
  const programCompressor = new mm.Player.tone.Compressor();
  const pan = 2 * MAX_PAN * Math.random() - MAX_PAN;
  const programPanner = new mm.Player.tone.Panner(pan);  
  programMap.set(i, programCompressor);
  programCompressor.connect(programPanner);
  programPanner.connect(globalCompressor);
}

// Set up per-drum effects.
const drumMap = new Map();
for (let i=MIN_DRUM; i<=MAX_DRUM; i++) {
  const drumCompressor = new mm.Player.tone.Compressor();
  const pan = 2 * MAX_PAN * Math.random() - MAX_PAN;
  const drumPanner = new mm.Player.tone.Panner(pan);
  drumMap.set(i, drumCompressor);
  drumCompressor.connect(drumPanner);  
  drumPanner.connect(globalCompressor);
}

// Set up SoundFont player.
const player = new mm.SoundFontPlayer('https://storage.googleapis.com/download.magenta.tensorflow.org/soundfonts_js/sgm_plus', globalCompressor, programMap, drumMap);

// Get UI elements.
const statusDiv = document.getElementById('status');
const playButton = document.getElementById('play');
const sampleButton1 = document.getElementById('sample1');
const sampleButton2 = document.getElementById('sample2');
const importButton1 = document.getElementById('import1');
const importButton2 = document.getElementById('import2');
const alphaSlider = document.getElementById('alpha');
const playInterpButton = document.getElementById('playInterp');
const saveButton = document.getElementById('download');

const numSteps = +alphaSlider.max + 1;

// Declare style / sequence variables.
var z1, z2;
var seqs;
var interpSeq;

var playing = false;

// Sample a latent vector.
function generateSample(doneCallback) {
  const z = tf.randomNormal([1, Z_DIM]);
  z.data().then(zArray => {
    z.dispose();
    doneCallback(zArray);
  });
}

// Randomly adjust note times.
function humanize(s) {
  const seq = mm.sequences.clone(s);
  seq.notes.forEach((note) => {
    let offset = HUMANIZE_SECONDS * (Math.random() - 0.5);
    if (seq.notes.startTime + offset < 0) {
      offset = -seq.notes.startTime;
    }
    if (seq.notes.endTime > seq.totalTime) {
      offset = seq.totalTime - seq.notes.endTime;
    }
    seq.notes.startTime += offset;
    seq.notes.endTime += offset;
  });
  return seq;
}

// Construct spherical linear interpolation tensor.
function slerp(z1, z2, n) {
  const norm1 = tf.norm(z1);
  const norm2 = tf.norm(z2);
  const omega = tf.acos(tf.matMul(tf.div(z1, norm1),
                                  tf.div(z2, norm2),
                                  false, true));
  const sinOmega = tf.sin(omega);
  const t1 = tf.linspace(1, 0, n);
  const t2 = tf.linspace(0, 1, n);
  const alpha1 = tf.div(tf.sin(tf.mul(t1, omega)), sinOmega).as2D(n, 1);
  const alpha2 = tf.div(tf.sin(tf.mul(t2, omega)), sinOmega).as2D(n, 1);
  const z = tf.add(tf.mul(alpha1, z1), tf.mul(alpha2, z2));
  return z;
}

// Concatenate multiple NoteSequence objects.
function concatenateSequences(seqs) {
  const seq = mm.sequences.clone(seqs[0]);
  let numSteps = seqs[0].totalQuantizedSteps;
  for (let i=1; i<seqs.length; i++) {
    const s = mm.sequences.clone(seqs[i]);
    s.notes.forEach(note => {
      note.quantizedStartStep += numSteps;
      note.quantizedEndStep += numSteps;
      seq.notes.push(note);
    });
    numSteps += s.totalQuantizedSteps;
  }
  seq.totalQuantizedSteps = numSteps;
  return seq;
}

// Interpolate the two styles.
function interpolateSamples(doneCallback) {
  const z1Tensor = tf.tensor2d(z1, [1, Z_DIM]);
  const z2Tensor = tf.tensor2d(z2, [1, Z_DIM]);
  const zInterp = slerp(z1Tensor, z2Tensor, numSteps);
  
  model.decode(zInterp, undefined, undefined, STEPS_PER_QUARTER)
    .then(sequences => {
      seqs = sequences;
      const seq = concatenateSequences(seqs);
      const mergedSeq = mm.sequences.mergeInstruments(seq);
      interpSeq = mm.sequences.unquantizeSequence(mergedSeq);
    
      interpSeq.ticksPerQuarter = STEPS_PER_QUARTER;
    
      setLoadingState();
      player.loadSamples(interpSeq)
        .then(doneCallback);
    });
}

// Encode a MIDI file.
function encodeMIDIFile(file, doneCallback, failCallback) {
  var reader = new FileReader();
  reader.onerror = e => {
    failCallback('Unable to read MIDI file.');
  }
  reader.onload = e => {
    var seq;
    try {
      seq = mm.midiToSequenceProto(reader.result);
    } catch(e) {
      failCallback('Unable to parse MIDI file.');
      return;
    }

    var quantizedSeq;
    try {
      quantizedSeq = mm.sequences.quantizeNoteSequence(seq, STEPS_PER_QUARTER);
    } catch(e) {
      failCallback('Unable to quantize MIDI file, possibly due to tempo or time signature changes.');
      return;
    }

    const quartersPerBar = 4 *  quantizedSeq.timeSignatures[0].numerator / quantizedSeq.timeSignatures[0].denominator;
    if (quartersPerBar !== 4) {
      failCallback('Time signatures other than 4/4 not supported.');
      return;
    }
    
    if (quantizedSeq.totalQuantizedSteps > 4 * STEPS_PER_QUARTER) {
      failCallback('Imported MIDI file must be a single bar.');
      return;
    }
    
    model.encode([quantizedSeq])
      .then(z => {
        z.data().then(zArray => {
          z.dispose();
          doneCallback(zArray);
        });
      });
  }
  
  reader.readAsBinaryString(file);
}

// Set UI state to updating styles.
function setUpdatingState() {
  statusDiv.innerText = 'Updating styles...';
  statusDiv.style.color = 'black';
  playButton.disabled = true;
  alphaSlider.disabled = true;
  sampleButton1.disabled = true;
  sampleButton2.disabled = true;
  import1.disabled = true;
  import2.disabled = true;
  importLabel1.style.color = 'gray';
  importLabel2.style.color = 'gray';
  playInterpButton.disabled = true;
  saveButton.disabled = true;
}

// Set UI state to updating instruments.
function setLoadingState() {
  statusDiv.innerText = 'Loading samples...';
  statusDiv.style.color = 'black';
  playButton.disabled = true;
  alphaSlider.disabled = true;
  sampleButton1.disabled = true;
  sampleButton2.disabled = true;
  import1.disabled = true;
  import2.disabled = true;
  importLabel1.style.color = 'gray';
  importLabel2.style.color = 'gray';
  playInterpButton.disabled = true;
  saveButton.disabled = true;
}

// Set UI state to playing.
function setStoppedState() {
  statusDiv.innerText = 'Ready to play!';
  statusDiv.style.color = 'black';
  playButton.innerText = 'Play';
  playButton.disabled = false;
  alphaSlider.disabled = false;
  sampleButton1.disabled = false;
  sampleButton2.disabled = false;
  import1.disabled = false;
  import2.disabled = false;
  importLabel1.style.color = 'black';
  importLabel2.style.color = 'black';
  playInterpButton.innerText = 'Play the Whole Thing';
  playInterpButton.disabled = false;
  saveButton.disabled = false;
}

// Set UI state to playing.
function setPlayingState() {
  statusDiv.innerText = 'Move the slider to interpolate styles.';
  statusDiv.style.color = 'black';
  playButton.innerText = 'Stop';
  playButton.disabled = false;
  alphaSlider.disabled = false;
  sampleButton1.disabled = false;
  sampleButton2.disabled = false;
  import1.disabled = false;
  import2.disabled = false;
  importLabel1.style.color = 'black';
  importLabel2.style.color = 'black';
  playInterpButton.disabled = true;
  saveButton.disabled = false;
}
// Set UI state to playing the whole interpolation.
function setPlayingInterpState() {
  statusDiv.innerText = 'Playing...';
  statusDiv.style.color = 'black';
  playButton.disabled = true;
  alphaSlider.disabled = true;
  sampleButton1.disabled = true;
  sampleButton2.disabled = true;
  import1.disabled = true;
  import2.disabled = true;
  importLabel1.style.color = 'gray';
  importLabel2.style.color = 'gray';
  playInterpButton.innerText = 'Stop Playing';
  playInterpButton.disabled = false;
  saveButton.disabled = true;
}

// Play the interpolated sequence for the current slider position.
function playIdx(idx, doneCallback) {
  const unquantizedSeq = mm.sequences.unquantizeSequence(seqs[idx]);
  player.start(humanize(unquantizedSeq))
    .then(doneCallback);
}

// Play the current position until stopped.
function play() {
  const idx = alphaSlider.value;
  playIdx(idx, play);
}

// Play the interpolated sequence.
function playInterp(idx, doneCallback) {
  if (idx === numSteps) {
    doneCallback();
  } else {
    alphaSlider.value = idx;
    playIdx(idx, () => playInterp(idx + 1, doneCallback));
  }
}

// Toggle playing the entire interpolated sequence.
function togglePlayingInterp() {
  mm.Player.tone.context.resume();
  
  if (playing) {
    playing = false;
    setStoppedState();
    player.stop();
  } else {
    playing = true;
    setPlayingInterpState();
    playInterp(0, setStoppedState);
  }
}

// Update the start style.
function updateSample1() {
  playing = false;
  setUpdatingState();
  player.stop();
  setTimeout(() => {
    generateSample(z => {
      z1 = z;
      interpolateSamples(setStoppedState);
    });
  }, 0);
}

// Update the end style.
function updateSample2() {
  playing = false;
  setUpdatingState();
  player.stop();
  setTimeout(() => {
    generateSample(z => {
      z2 = z;
      interpolateSamples(setStoppedState);
    });
  }, 0);
}

// Import the start style from MIDI.
function importSample1() {
  if (import1.value.length === 0) {
    return;
  }
  
  playing = false;
  setUpdatingState();
  player.stop();
  filename = import1.files[0];
  setTimeout(() => {
    encodeMIDIFile(filename, z => {
      z1 = z;
      interpolateSamples(setStoppedState);
    }, (errorMessage) => {
      setStoppedState();
      statusDiv.innerText = errorMessage;
      statusDiv.style.color = 'red';
    });
  }, 0);
}

// Import the end style from MIDI.
function importSample2() {
  if (import2.value.length === 0) {
    return;
  }
  
  playing = false;
  setUpdatingState();
  player.stop();
  filename = import2.files[0];
  setTimeout(() => {
    encodeMIDIFile(filename, z => {
      z2 = z;
      interpolateSamples(setStoppedState);
    }, (errorMessage) => {
      setStoppedState();
      statusDiv.innerText = errorMessage;
      statusDiv.style.color = 'red';
    });
  }, 0);
}

// Save sequence as MIDI.
function saveSequence() {
  const midi = mm.sequenceProtoToMidi(interpSeq);
  const file = new Blob([midi], {type: 'audio/midi'});
    
  if (window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(file, 'interp.mid');
  } else { // Others
    const a = document.createElement('a');
    const url = URL.createObjectURL(file);
    a.href = url;
    a.download = 'interp.mid';
    document.body.appendChild(a);
    a.click();
    setTimeout(() => {
      document.body.removeChild(a);
      window.URL.revokeObjectURL(url);  
    }, 0); 
  }
}

// Start or stop playing the sequence at the current slider position.
function togglePlaying() {
  mm.Player.tone.context.resume();
  
  if (playing) {
    playing = false;
    setStoppedState();
    player.stop();
  } else {
    playing = true;
    setPlayingState();
    play();
  }
}

sampleButton1.onclick = updateSample1;
sampleButton2.onclick = updateSample2;
playButton.onclick = togglePlaying;
playInterpButton.onclick = togglePlayingInterp;
saveButton.onclick = saveSequence;

import1.onchange = importSample1;
import2.onchange = importSample2;

model.initialize()
  .then(() => {
    setUpdatingState();
    setTimeout(() => {
      generateSample(z => {
        z1 = z;
        generateSample(z => {
          z2 = z;
          interpolateSamples(setStoppedState);
        });
      });
    }, 0);
  });

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console