Can I get TrackChunks from NotesPlaybackStarted event? #174
-
Hi, I'm a compete beginner of drywetmidi! I can access to TrackChunk from MidiFile object, but cannot do it from NotesEventArgs. For example, what I'd like to do is as follows:
I'd be so glad if someone could help me! Many thanks!! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, First of all, you have a big problem in your code: Playback playback = midifile.GetPlayback(outputdevice);
playback.NotesPlaybackStarted += OnNotesStarted;
playback.Start();
} What do you think will happen when the program leaves the method? Yes, all local variables will be cleaned up by the garbage collector. And thus playback won't work (or will work some small time). You need to define Well, as for knowing of track chunk – there is no simple way. But there is a workaround. First, create this class: private sealed class TimedEventWithTrackChunkIndex : TimedEvent, IMetadata
{
public TimedEventWithTrackChunkIndex(MidiEvent midiEvent, long time, int trackChunkIndex)
: base(midiEvent, time)
{
Metadata = trackChunkIndex;
}
public object Metadata { get; set; }
public override TimedEvent Clone()
{
return new TimedEventWithTrackChunkIndex(Event, Time, Convert.ToInt32(Metadata));
}
} And create playback with this code: var timedEvents = midiFile
.GetTrackChunks()
.SelectMany((c, i) => c.GetTimedEvents().Select(e => new TimedEventWithTrackChunkIndex(e.Event, e.Time, i)))
.OrderBy(e => e.Time);
var tempoMap = midiFile.GetTempoMap();
playback = new Playback(timedEvents, tempoMap); And now you can get track chunk index in the ////////////////////////////////////////////////////////
// **** Want to display the track ID here too! ****
////////////////////////////////////////////////////////
var metadata = (IMetadata)note.GetTimedNoteOnEvent();
var trackChunkIndex = Convert.ToInt32(metadata.Metadata); |
Beta Was this translation helpful? Give feedback.
Hi,
First of all, you have a big problem in your code:
What do you think will happen when the program leaves the method? Yes, all local variables will be cleaned up by the garbage collector. And thus playback won't work (or will work some small time). You need to define
playback
field and usePlayback
via class field and not via local variable. The same problem withoutputDevice
.Well, as for knowing of track chunk – there is no simple way. But there is a workaround. First, create this class: