public void ConstrainNotesToMeasureBoundaries() { foreach (var note in this.Notes) { //get start and end boundary points for this note MidiTime startOfBar = new MidiTime(this.File, note.AveragePosition.Measures, 1, 0, this.TimeSignature); MidiTime endOfBar = new MidiTime(this.File, note.AveragePosition.Measures + 1, 1, 0, this.TimeSignature); if (note.StartPosition.GetAsTotalTicks() < startOfBar.GetAsTotalTicks()) { //note is mostly in current bar but the note starts just before the bar start. //Move the note start to the to start of the measure note.StartPosition = new MidiTime(startOfBar); note.CalculateEndPosition(); } else if (note.EndPosition.GetAsTotalTicks() > endOfBar.GetAsTotalTicks()) { ///note is mostly in current bar but the note ends just after the bar end. ///Shorten the length of the note. ///This will only rarely happen because at this point the note lengths are 1 and so arent long enough to span two measures note.Length = endOfBar.GetAsTotalTicks() - note.StartPosition.GetAsTotalTicks(); note.CalculateEndPosition(); } } }
/// <summary> /// the position of the region boundaries are calculated on the fly by using the current time /// signature and pulses per QN /// </summary> /// <param name="position"></param> /// <param name="pulsesPerQuarterNote"></param> /// <param name="timeSig"></param> /// <returns></returns> public Region GetRegion(MidiTime position, uint pulsesPerQuarterNote, TimeSignatureEvent timeSig) { uint posTicks = position.GetAsTotalTicks(pulsesPerQuarterNote, timeSig); foreach (Region r in this.Regions) { if (posTicks >= r.Start.GetAsTotalTicks(pulsesPerQuarterNote, timeSig) && posTicks < r.End.GetAsTotalTicks(pulsesPerQuarterNote, timeSig)) { return(r); } } return(null); }