grimmdude / MidiWriterJS

♬ A JavaScript library which provides an API for programmatically generating and creating expressive multi-track MIDI files and JSON.
MIT License
547 stars 58 forks source link

One Octave Too Low #85

Closed 360disrupt closed 2 years ago

360disrupt commented 2 years ago

Version:

"midi-writer-js": "^2.0.1",

Issue:

Everything is pitched down by 1 Octave.

Code:

        // Start with a new track
        let track = new MidiWriter.Track();
        track.addTrackName(trackName);

        notes = ['C0', 'C1', 'C2'];
        // Add some notes:
        notes.forEach(pitch => {
            // @ts-ignore
            let midiInfo = new MidiWriter.NoteEvent({pitch, duration: '16', velocity: 100});
            console.log('midiInfo', midiInfo); // everything is logged correctly
            track.addEvent(midiInfo);
        });

        // Generate a data URI
        let write = new MidiWriter.Writer(track);
        return write.dataUri();
   After downloading the mid file and copying into Ableton, I get:

image

grimmdude commented 2 years ago

Hi @360disrupt,

I believe this depends on what Ableton considers "middle C"; sounds like it's considering it to be C3. Looking at GarageBand I see it defines it as C3 as well.

The MIDI spec defines note 60 as middle C, and the note-to-number mapping being used in this library considers C4 to be middle C. Hence C4 is mapped to 60.

I was always taught that C4 was middle C, but searching around online does not a yield a clear consensus. Perhaps it would be useful to add an option in this library to define middle C?

-Garrett

grimmdude commented 2 years ago

Hey @360disrupt,

Just released a change in 2.1.0 that should let change middle C like this:


track.addEvent(new MidiWriter.NoteEvent({pitch: 'E4', duration: '4'});

const write = new MidiWriter.Writer(track, {middleC: 'C3'});

// Can also set after writer instantiation
write.setOption('middleC', 'C3');

Let me know if you have any issues.