0

I'm trying to find the interval size (i.e. how many semitones between a pair of notes) for each pair of notes in a midi file. For example, a C major scale midi file would return {2,2,1,2,2,2,1}.

I downloaded DryWetMIDI and imported it as an asset into Unity, messing around with the code. However, I'm having trouble understanding the library.

One possible solution might be to use the CompareTo(Note) method each time a new note is played to compare it to the previous note played, but I'm not sure how to code that.

I have not yet tried other libraries.

nuance
  • 3
  • 1

1 Answers1

0

I'm the author of DryWetMIDI. I'm not sure what you exactly want, but maybe this code will help you:

var lastNotesByChannels = new Dictionary<FourBitNumber, Note>();

foreach (var note in midiFile.GetNotes())
{
    if (lastNotesByChannels.TryGetValue(note.Channel, out var lastNote))
    {
        var interval = note.NoteNumber - lastNote.NoteNumber;
        // do something with interval
    }

    lastNotesByChannels[note.Channel] = note;
}

Here we handle intervals between adjacent notes by each channel. Please adapt this code to your task.

Please note that the library has comprehensive documentation, please use it: https://melanchall.github.io/drywetmidi.

Maxim
  • 1,995
  • 1
  • 19
  • 24