using System;
using System.IO;
using System.Text;
namespace NAudio.Midi
{
///
/// Represents a MIDI time signature event
///
public class TimeSignatureEvent : MetaEvent
{
private byte numerator;
private byte denominator;
private byte ticksInMetronomeClick;
private byte no32ndNotesInQuarterNote;
///
/// Reads a new time signature event from a MIDI stream
///
/// The MIDI stream
/// The data length
public TimeSignatureEvent(BinaryReader br,int length)
{
if(length != 4)
{
throw new FormatException(String.Format("Invalid time signature length: Got {0}, expected 4", length));
}
numerator = br.ReadByte();
denominator = br.ReadByte(); //2=quarter, 3=eigth etc
ticksInMetronomeClick = br.ReadByte();
no32ndNotesInQuarterNote = br.ReadByte();
}
///
/// Creates a new TimeSignatureEvent
///
/// Time at which to create this event
/// Numerator
/// Denominator
/// Ticks in Metronome Click
/// No of 32nd Notes in Quarter Click
public TimeSignatureEvent(long absoluteTime, int numerator, int denominator, int ticksInMetronomeClick, int no32ndNotesInQuarterNote)
:
base(MetaEventType.TimeSignature, 4, absoluteTime)
{
this.numerator = (byte)numerator;
this.denominator = (byte)denominator;
this.ticksInMetronomeClick = (byte)ticksInMetronomeClick;
this.no32ndNotesInQuarterNote = (byte)no32ndNotesInQuarterNote;
}
///
/// Creates a deep clone of this MIDI event.
///
public override MidiEvent Clone() => (TimeSignatureEvent)MemberwiseClone();
///
/// Numerator (number of beats in a bar)
///
public int Numerator
{
get { return numerator; }
}
///
/// Denominator (Beat unit),
/// 1 means 2, 2 means 4 (crochet), 3 means 8 (quaver), 4 means 16 and 5 means 32
///
public int Denominator
{
get { return denominator; }
}
///
/// Ticks in a metronome click
///
public int TicksInMetronomeClick
{
get { return ticksInMetronomeClick; }
}
///
/// Number of 32nd notes in a quarter note
///
public int No32ndNotesInQuarterNote
{
get { return no32ndNotesInQuarterNote; }
}
///
/// The time signature
///
public string TimeSignature
{
get
{
string den = String.Format("Unknown ({0})",denominator);
switch(denominator)
{
case 1:
den = "2";
break;
case 2:
den = "4";
break;
case 3:
den = "8";
break;
case 4:
den = "16";
break;
case 5:
den = "32";
break;
}
return String.Format("{0}/{1}",numerator,den);
}
}
///
/// Describes this time signature event
///
/// A string describing this event
public override string ToString()
{
return String.Format("{0} {1} TicksInClick:{2} 32ndsInQuarterNote:{3}",
base.ToString(),TimeSignature,ticksInMetronomeClick,no32ndNotesInQuarterNote);
}
///
/// Calls base class export first, then exports the data
/// specific to this event
/// MidiEvent.Export
///
public override void Export(ref long absoluteTime, BinaryWriter writer)
{
base.Export(ref absoluteTime, writer);
writer.Write(numerator);
writer.Write(denominator);
writer.Write(ticksInMetronomeClick);
writer.Write(no32ndNotesInQuarterNote);
}
}
}