using System;
using System.IO;
using System.Text;
namespace NAudio.Midi
{
///
/// Represents a MIDI text event
///
public class TextEvent : MetaEvent
{
private byte[] data;
///
/// Reads a new text event from a MIDI stream
///
/// The MIDI stream
/// The data length
public TextEvent(BinaryReader br,int length)
{
data = br.ReadBytes(length);
}
///
/// Creates a new TextEvent
///
/// The text in this type
/// MetaEvent type (must be one that is
/// associated with text data)
/// Absolute time of this event
public TextEvent(string text, MetaEventType metaEventType, long absoluteTime)
: base(metaEventType, text.Length, absoluteTime)
{
Text = text;
}
///
/// Creates a deep clone of this MIDI event.
///
public override MidiEvent Clone() => (TextEvent)MemberwiseClone();
///
/// The contents of this text event
///
public string Text
{
get
{
Encoding byteEncoding = NAudio.Utils.ByteEncoding.Instance;
return byteEncoding.GetString(data);
}
set
{
Encoding byteEncoding = NAudio.Utils.ByteEncoding.Instance;
data = byteEncoding.GetBytes(value);
metaDataLength = data.Length;
}
}
///
/// The raw contents of this text event
///
public byte[] Data
{
get
{
return data;
}
set
{
data = value;
metaDataLength = data.Length;
}
}
///
/// Describes this MIDI text event
///
/// A string describing this event
public override string ToString()
{
return String.Format("{0} {1}",base.ToString(),Text);
}
///
/// 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(data);
}
}
}