Giter Club home page Giter Club logo

Comments (7)

tambien avatar tambien commented on September 10, 2024

Seems like you might be encoding just the track output and not the entire midi file. make sure you're calling encode on the Midi object and not on one of the track objects.

from midi.

yipcma avatar yipcma commented on September 10, 2024

@tambien Thanks for the quick reply. I just checked and I'm indeed calling it on the midi object that has the following structure...

t {header: Object, tracks: Array(1)}
header
:
Object
tracks
:
Array(1)
0
:
t
length
:
1
__proto__
:
Array(0)
bpm
:
(...)
duration
:
(...)
startTime
:
(...)
timeSignature
:
(...)
__proto__
:
Object

from midi.

tambien avatar tambien commented on September 10, 2024

could you share some of the code?

from midi.

yipcma avatar yipcma commented on September 10, 2024

sure, lemme make a minimal gist.

from midi.

yipcma avatar yipcma commented on September 10, 2024

problem solved. There is no issue with MidiConvert, just me not properly handling the binary string. AOK.

from midi.

tambien avatar tambien commented on September 10, 2024

If you could, share your solution, or submit a PR if some documentation is missing. I'm bet other people might encounter this problem

from midi.

kn0ll avatar kn0ll commented on September 10, 2024

ran into the same issue last night. the issue was that the value returned from Midi.encode() is a binary string, not a byte array.

(the jsmidgen code for this was especially confusing. says it returns a byte array but very clearly returns a string? am i missing something?)

in any case, and this is where it gets kind of interesting, fs.writeFileSync handles the binary string just fine. but the Blob constructor... barely handles it fine, sometimes.

const midi = MidiConvert.create();
const encoded = midi.encode();

const blob = new Blob([bytes], {type: "audio/midi; charset=binary"});

the above will actually work fine, somehow. the Blob can instantiate, and the blob can be parsed correctly when written to a file. so a binary string works just fine for Blob so long as your MIDI doesn't have any tracks.

const midi = MidiConvert.create();
midi.track();

const encoded = midi.encode();

const blob = new Blob([bytes], {type: "audio/midi; charset=binary"});

but, as soon as we add a track, as shown above, is where the blob starts to fail. the blob instantiates correctly. but if you try to write it to a file then load it, you get Uncaught Unexpected chunk - expected MTrk, got MTr.

the solution is to convert the binary string to a typed array before passing it to the blob constructor, like so:

const midi = MidiConvert.create();
midi.track();

const encoded = midi.encode();

const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
  bytes[i] = binaryString.charCodeAt(i);
}

const blob = new Blob([bytes], {type: "audio/midi; charset=binary"});

cheers m8s... hope this helps someone...

from midi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.