Skip to content
Advertisement

How to convert a magenta.js note sequence to a midi file?

I am trying to convert a note sequence in Magenta.js to a midi file and get an url so users can download it and use that url in my midi-player/visualizer.

// Create a magenta note sequence
generateMelody(sendedNotes, 0.7, document.getElementById('bpm').value).then((magentaSequence) => {

    // Create midi out of magenteSequence
    const magentaMidi = core.sequenceProtoToMidi(magentaSequence);

    // Convert byte array to file
    const magentaFile = new Blob([magentaMidi], { type: 'audio/midi' })

    // Get url of the file
    const magentaURL = URL.createObjectURL(magentaFile);

    // Create midi elements and populate the template
    const magentaContent = melodyTemplate({'id': 2, 'src': magentaURL});

    // Add new item to results
    document.querySelector('#results').innerHTML += magentaContent;

But I get this error:

Uncaught (in promise) Error: The sequence you are using with the visualizer does not have a totalTime field set, so the visualizer can't be horizontally sized correctly.

Advertisement

Answer

I solved it by adding:

magentaSequence.notes.forEach(n => n.velocity = bpm);

It seems that the notes need a velocity for this code to work. I found the details in this link https://github.com/magenta/magenta-js/issues/462

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement