nfroidure / midifile

A MIDI file parser/writer using ArrayBuffers
http://karaoke.insertafter.com
MIT License
199 stars 30 forks source link

Reading MIDI track labels as String #22

Open jochemstoel opened 7 years ago

jochemstoel commented 7 years ago

Thank you for creating NPM package midifile. I read the 'documentation' and had a quick browse through the code but did not find an answer there.

How do I read the names of separate tracks of the MIDI file as a String? By names I mean the optional label assigned to the track by the creator of the file. For example drums or acoustic guitar one or vocals.

Thanks in advance.

nfroidure commented 7 years ago

@jochemstoel I don't think it is doable out of the box right now. Any PR allowing to do so would be merged.

ritz078 commented 5 years ago

Here is how I read that. Currently, the event with a subtype of EVENT_META_TRACK_NAME has the data property. Just need to convert it to a string.

import MidiEvents from "midievents";
import UTF8 from "utf-8";
import MidiFile from "midifile";

const midi = new MidiFile(this.arrayBuffer);
const events = midi.getEvents();
events.forEach(event => {
 if (event.subtype === MidiEvents.EVENT_META_TRACK_NAME) {
    const text = UTF8.getStringFromBytes(event.data, 0, event.length, true);
        this.song.header.name.push(text);
  }
})