Search code examples
javascripttensorflowmiditensorflow.jsmagenta

Uncaught (in promise) Error: Number of splits must evenly divide the axis


Summary

  1. Context
  2. The problem
  3. What did I try to fix this bug?
  4. How to reproduce this bug (+ required data)?
  5. My questions
  6. Sources

Context

I would want to generate a new sequence of notes of a MIDI file thanks to MusicRNN chord_pitches_improv.

The problem

My call to music_rnn.continueSequence triggers the following error:

Uncaught (in promise) Error: Number of splits must evenly divide the axis. at C (music:22) at split_ (music:22) at Module.split (music:22) at t.sampleRnn (music:83) at music:83 at music:22 at t.scopedRun (music:22) at t.tidy (music:22) at Module.$e (music:22) at t. (music:83)

What did I try to fix this bug?

Nothing because I don't even understand the error. Note: I have MuseScore. So If something must be changed in the MIDI file, I can do it.

How to reproduce this bug (+ required data)?

  1. Download the following MIDI file: https://a.uguu.se/kP1iJ4JVeTrD_b.mid and rename it "b.mid"

  2. Create a directory in which you store the MIDI file

  3. In this same directory, create a file titled index.html: copy/paste in this HTML file the code provided below (section "Sources")

  4. Open your browser and open this HTML file: my script will automatically be executed, and the error will be triggered.

My questions

What does this error mean? How could I fix it? Should I change something in the MIDI file (if yes: what? How? Can I use MuseScore to do it?)?

Sources

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Music</title>
    <script src="https://cdn.jsdelivr.net/npm/@magenta/music"></script>
    <script src="https://unpkg.com/@tonejs/midi"></script>
</head>
<body>

<script>
    loadMidi = async function() {
        const sequence = {'notes': []}
        const midi = await Midi.fromUrl("b.mid")
        midi.tracks.forEach(track => {
          const notes = track.notes
          notes.forEach(note => {
            if(note.midi < 48) {
                note.midi = 48
            }
            if(note.midi > 83) {
                note.midi = 83
            }

            sequence['notes'].push(
                {
                    pitch: note.midi,
                    velocity: note.velocity * 100,
                    startTime: note.time,
                    endTime: note.time + note.duration
                }
            )
          })
        })

         try {
            const quantizedSequence = mm.sequences.quantizeNoteSequence(sequence, 1)
            const improvCheckpoint = 'https://storage.googleapis.com/magentadata/js/checkpoints/music_rnn/chord_pitches_improv'
            const music_rnn = new mm.MusicRNN(improvCheckpoint)
            const prepareMusic = async() => {
                await music_rnn.initialize()
                var improvisedMelody = await music_rnn.continueSequence(quantizedSequence, 60, 1.1, ['Bm', 'Bbm', 'Gb7', 'F7', 'Ab', 'Ab7', 'G7', 'Gb7', 'F7', 'Bb7', 'Eb7', 'AM7'])
                improvisedMelody.notes.forEach(n => n.velocity = 100)
                var midi_bytes_array = mm.sequenceProtoToMidi(improvisedMelody)
                saveByteArray("generated_music.midi", midi_bytes_array);
            }

            prepareMusic()

          } catch (error) {
            console.error(error)
        }

       }
       loadMidi();

     function saveByteArray(reportName, byte) {
            var blob = new Blob([byte], {type: "audio/midi"});
            var link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            var fileName = reportName;
            link.download = fileName;
            link.click();
        };
    </script>

</body>
</html>

Solution

  • I got same error when I was trying to quantize my note sequence which was already in quantized format. It is worthy to check your note sequence format.