using System;
using System.IO;
namespace NAudio.Midi
{
///
/// Represents a MIDI Channel AfterTouch Event.
///
public class ChannelAfterTouchEvent : MidiEvent
{
private byte afterTouchPressure;
///
/// Creates a new ChannelAfterTouchEvent from raw MIDI data
///
/// A binary reader
public ChannelAfterTouchEvent(BinaryReader br)
{
afterTouchPressure = br.ReadByte();
if ((afterTouchPressure & 0x80) != 0)
{
// TODO: might be a follow-on
throw new FormatException("Invalid afterTouchPressure");
}
}
///
/// Creates a new Channel After-Touch Event
///
/// Absolute time
/// Channel
/// After-touch pressure
public ChannelAfterTouchEvent(long absoluteTime, int channel, int afterTouchPressure)
: base(absoluteTime, channel, MidiCommandCode.ChannelAfterTouch)
{
AfterTouchPressure = afterTouchPressure;
}
///
/// 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(afterTouchPressure);
}
///
/// The aftertouch pressure value
///
public int AfterTouchPressure
{
get { return afterTouchPressure; }
set
{
if (value < 0 || value > 127)
{
throw new ArgumentOutOfRangeException("value", "After touch pressure must be in the range 0-127");
}
afterTouchPressure = (byte) value;
}
}
///
///
///
public override int GetAsShortMessage()
{
return base.GetAsShortMessage() + (afterTouchPressure << 8);
}
///
/// Describes this channel after-touch event
///
public override string ToString()
{
return $"{base.ToString()} {afterTouchPressure}";
}
}
}