Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ManageTempoMap fails on empty MidiFile #321

Open
FireFox2000000 opened this issue Feb 9, 2025 · 1 comment
Open

ManageTempoMap fails on empty MidiFile #321

FireFox2000000 opened this issue Feb 9, 2025 · 1 comment
Labels
bug Bug in the library
Milestone

Comments

@FireFox2000000
Copy link

Hello, currently I am trying to generate a tempo map that starts at certain tempo map values, and then after a certain amount of ticks change to a new tempo and time signature. Using the ManageTempoMap api this work fine, except in the cases where the initial tempo and time signature are standard values (120bpm @ 4/4), which results in an exception being thrown.

---
System.ArgumentException : Collection of EventsCollection is empty.
Parameter name: eventsCollections
---
at Melanchall.DryWetMidi.Common.ThrowIfArgument.IsEmptyCollection[T] (System.String parameterName, System.Collections.Generic.IEnumerable`1[T] collection, System.String message) [0x00008] in <6afb839a39da48d6bfc3946fb9a5d8af>:0 
  at Melanchall.DryWetMidi.Interaction.TempoMapManager..ctor (Melanchall.DryWetMidi.Core.TimeDivision timeDivision, System.Collections.Generic.IEnumerable`1[T] eventsCollections) [0x0001c] in <6afb839a39da48d6bfc3946fb9a5d8af>:0 
  at Melanchall.DryWetMidi.Interaction.TempoMapManagingUtilities.ManageTempoMap (System.Collections.Generic.IEnumerable`1[T] eventsCollections, Melanchall.DryWetMidi.Core.TimeDivision timeDivision) [0x00016] in <6afb839a39da48d6bfc3946fb9a5d8af>:0 
  at Melanchall.DryWetMidi.Interaction.TempoMapManagingUtilities.ManageTempoMap (System.Collections.Generic.IEnumerable`1[T] trackChunks, Melanchall.DryWetMidi.Core.TimeDivision timeDivision) [0x0003b] in <6afb839a39da48d6bfc3946fb9a5d8af>:0 
  at Melanchall.DryWetMidi.Interaction.TempoMapManagingUtilities.ManageTempoMap (Melanchall.DryWetMidi.Core.MidiFile file) [0x00017] in <6afb839a39da48d6bfc3946fb9a5d8af>:0 

This can be reproduced in the following ways-

Melanchall.DryWetMidi.Core.MidiFile midiFile = new();
var timeDivision = new Melanchall.DryWetMidi.Core.TicksPerQuarterNoteTimeDivision(192);
midiFile.TimeDivision = timeDivision;
midiFile.ReplaceTempoMap(TempoMap.Create(timeDivision, Tempo.FromBeatsPerMinute(120)));
using (var tempoMapManager = midiFile.ManageTempoMap())
{ ... }

or via

Melanchall.DryWetMidi.Core.MidiFile midiFile = new();
var timeDivision = new Melanchall.DryWetMidi.Core.TicksPerQuarterNoteTimeDivision(192);
midiFile.TimeDivision = timeDivision;
midiFile.ReplaceTempoMap(TempoMap.Create(timeDivision, new TimeSignature(4, 4)));
using (var tempoMapManager = midiFile.ManageTempoMap())
{ ... }

This does not occur if the tempo is not 120 (i.e. 121 or 119 are fine) or the time signature is not standard (i.e does not throw on anything other than 4/4).
midiFile.ReplaceTempoMap(TempoMap.Create(timeDivision, Tempo.FromBeatsPerMinute(121)));
midiFile.ReplaceTempoMap(TempoMap.Create(timeDivision, new TimeSignature(6, 4)));

@melanchall melanchall changed the title Exception using Melanchall.DryWetMidi.Interaction.TempoMapManagingUtilities.ManageTempoMap when initial tempo or time signature are default values ManageTempoMap fails on empty MidiFile Feb 9, 2025
@melanchall melanchall added the bug Bug in the library label Feb 9, 2025
@melanchall melanchall added this to the 8.0.0 milestone Feb 9, 2025
@github-project-automation github-project-automation bot moved this to To do in DryWetMIDI Feb 9, 2025
@melanchall melanchall moved this from To do to In progress in DryWetMIDI Feb 9, 2025
@melanchall
Copy link
Owner

Hi @FireFox2000000,

Thanks for the issue. The problem is not with tempo/time signature values. The root cause is calling ManageTempoMap on empty MidiFile. I'll fix the bug in the upcoming release.

Right now a workaround is constructing a MidiFile with one empty track chunk:

Melanchall.DryWetMidi.Core.MidiFile midiFile = new (new TrackChunk());

// ...

Thank you again for the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug in the library
Projects
Status: In progress
Development

No branches or pull requests

2 participants