using System;
using System.IO;
using System.Text;
namespace NAudio.Midi
{
///
/// Represents a MIDI control change event
///
public class ControlChangeEvent : MidiEvent
{
private MidiController controller;
private byte controllerValue;
///
/// Reads a control change event from a MIDI stream
///
/// Binary reader on the MIDI stream
public ControlChangeEvent(BinaryReader br)
{
byte c = br.ReadByte();
controllerValue = br.ReadByte();
if((c & 0x80) != 0)
{
// TODO: might be a follow-on
throw new InvalidDataException("Invalid controller");
}
controller = (MidiController) c;
if((controllerValue & 0x80) != 0)
{
throw new InvalidDataException(String.Format("Invalid controllerValue {0} for controller {1}, Pos 0x{2:X}", controllerValue, controller, br.BaseStream.Position));
}
}
///
/// Creates a control change event
///
/// Time
/// MIDI Channel Number
/// The MIDI Controller
/// Controller value
public ControlChangeEvent(long absoluteTime, int channel, MidiController controller, int controllerValue)
: base(absoluteTime,channel,MidiCommandCode.ControlChange)
{
this.Controller = controller;
this.ControllerValue = controllerValue;
}
///
/// Describes this control change event
///
/// A string describing this event
public override string ToString()
{
return String.Format("{0} Controller {1} Value {2}",
base.ToString(),
this.controller,
this.controllerValue);
}
///
///
///
public override int GetAsShortMessage()
{
byte c = (byte)controller;
return base.GetAsShortMessage() + (c << 8) + (controllerValue << 16);
}
///
/// 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((byte)controller);
writer.Write((byte)controllerValue);
}
///
/// The controller number
///
public MidiController Controller
{
get
{
return controller;
}
set
{
if ((int) value < 0 || (int) value > 127)
{
throw new ArgumentOutOfRangeException("value", "Controller number must be in the range 0-127");
}
controller = value;
}
}
///
/// The controller value
///
public int ControllerValue
{
get
{
return controllerValue;
}
set
{
if (value < 0 || value > 127)
{
throw new ArgumentOutOfRangeException("value", "Controller Value must be in the range 0-127");
}
controllerValue = (byte) value;
}
}
}
}