melanchall / drywetmidi

.NET library to read, write, process MIDI files and to work with MIDI devices
https://melanchall.github.io/drywetmidi
MIT License
540 stars 75 forks source link

Question: Additional Note Attributes ? #175

Closed JohnTruety closed 2 years ago

JohnTruety commented 2 years ago

Is there any way to extend the Note class with additional attributes (i.e. Tracknumber) ? Thank you for your help.

melanchall commented 2 years ago

First of all, please describe your scenario. How do you get notes? midiFile.GetNotes()?

JohnTruety commented 2 years ago

How do you get notes? midiFile.GetNotes()? Yes

I need it in a sequencer application only for internal use. It should not be written to a midi file.

melanchall commented 2 years ago

Well, there is no simple way but workaround exists. First, create this class:

private sealed class TimedEventWithTrackChunkIndex : TimedEvent
{
    public TimedEventWithTrackChunkIndex(MidiEvent midiEvent, long time, int trackChunkIndex)
        : base(midiEvent, time)
    {
        TrackChunkIndex = trackChunkIndex;
    }

    public int TrackChunkIndex { get; }

    public override TimedEvent Clone()
    {
        return new TimedEventWithTrackChunkIndex(Event, Time, TrackChunkIndex);
    }
}

And now you nedd to collect notes in this way:

var notes = midiFile
    .GetTrackChunks()
    .SelectMany((c, i) => c
        .GetTimedEvents()
        .Select(e => new TimedEventWithTrackChunkIndex(e.Event, e.Time, i))
        .GetObjects(ObjectType.Note))
    .Cast<Note>()
    .OrderBy(n => n.Time)
    .ToArray();

To get track chunk index from a note, use this:

var trackChunkIndex = ((TimedEventWithTrackChunkIndex)note.GetTimedNoteOnEvent()).TrackChunkIndex;
JohnTruety commented 2 years ago

Fantastic. Many many thanks. And lot of thanks for your great library!!

melanchall commented 2 years ago

Thanks for using the library :)

Also since you're not the first person who asked me to solve the same task, I'll try to implement an easier way for that. So please don't close the issue.

melanchall commented 2 years ago

:rocket: 6.1.0 version is released now!

Now the second code snippet can be simplified to this:

var notes = midiFile
    .GetNotes(new NoteDetectionSettings
    {
        TimedEventDetectionSettings = new TimedEventDetectionSettings
        {
            Constructor = d => new TimedEventWithTrackChunkIndex(d.Event, d.Time, d.EventsCollectionIndex)
        }
    });

Thanks for using the library!

JohnTruety commented 2 years ago

Great J

Thanks for your help!

Von: Maxim Dobroselsky @. Gesendet: Montag, 16. Mai 2022 13:33 An: melanchall/drywetmidi @.> Cc: JohnTruety @.>; Author @.> Betreff: Re: [melanchall/drywetmidi] Question: Additional Note Attributes ? (Issue #175)

🚀 6.1.0 version is released now!

Now the second code snippet can be simplified to this:

var notes = midiFile

.GetNotes(new NoteDetectionSettings

{

    TimedEventDetectionSettings = new TimedEventDetectionSettings

    {

        Constructor = d => new TimedEventWithTrackChunkIndex(d.Event, d.Time, d.EventsCollectionIndex)

    }

});

Thanks for using the library!

— Reply to this email directly, view it on GitHub https://github.com/melanchall/drywetmidi/issues/175#issuecomment-1127557852 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ARKSCK4UWOCYWCR2UHRA2MLVKIW7FANCNFSM5UOBSFKA . You are receiving this because you authored the thread.Message ID: @.***>