Skip to content

Commit

Permalink
Merge pull request #18 from ho0ber/xtm
Browse files Browse the repository at this point in the history
XTM first pass
  • Loading branch information
ho0ber authored Jan 22, 2019
2 parents ef3fe23 + 76baab2 commit a23da2f
Show file tree
Hide file tree
Showing 9 changed files with 497 additions and 77 deletions.
33 changes: 26 additions & 7 deletions NK2Tray/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class Button
public int channel;
public MidiOut midiOut;

public Button(ref MidiOut midiOutRef, ButtonType butType, int cont, bool initialState)
public Button(ref MidiOut midiOutRef, ButtonType butType, int cont, bool initialState, MidiCommandCode code=MidiCommandCode.ControlChange)
{
commandCode = MidiCommandCode.ControlChange;
commandCode = code;
channel = 1;
buttonType = butType;
controller = cont;
Expand All @@ -32,20 +32,39 @@ public Button(ref MidiOut midiOutRef, ButtonType butType, int cont, bool initial

public void SetLight(bool state)
{
midiOut.Send(new ControlChangeEvent(0, channel, (MidiController)(controller), state ? 127 : 0).GetAsShortMessage());
if (commandCode == MidiCommandCode.ControlChange)
midiOut.Send(new ControlChangeEvent(0, channel, (MidiController)(controller), state ? 127 : 0).GetAsShortMessage());
else if (commandCode == MidiCommandCode.NoteOn)
midiOut.Send(new NoteOnEvent(0, 1, controller, state ? 127 : 0, 0).GetAsShortMessage());
}

public bool HandleEvent(MidiInMessageEventArgs e)
{
if (e.MidiEvent.CommandCode != commandCode)
return false;

ControlChangeEvent me = (ControlChangeEvent)e.MidiEvent;
int c;

if (me.Channel != channel || me.ControllerValue != 127) // Only on correct channel and button-down (127)
return false;
if (commandCode == MidiCommandCode.ControlChange)
{
var me = (ControlChangeEvent)e.MidiEvent;

if (me.Channel != channel || me.ControllerValue != 127) // Only on correct channel and button-down (127)
return false;

int c = (int)me.Controller;
c = (int)me.Controller;
}
else if (commandCode == MidiCommandCode.NoteOn)
{
var me = (NoteEvent)e.MidiEvent;

if (me.Channel != channel || me.Velocity != 127) // Only on correct channel and button-down (127)
return false;

c = me.NoteNumber;
}
else
return false;

if (c == controller)
{
Expand Down
176 changes: 143 additions & 33 deletions NK2Tray/Fader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,77 @@

namespace NK2Tray
{
public class Fader
public class FaderDef
{
public MidiCommandCode commandCode;
public bool delta;
public float range;
public int channel;
public bool selectPresent;
public bool mutePresent;
public bool recordPresent;
public int faderOffset;
public int selectOffset;
public int muteOffset;
public int recordOffset;
public MidiCommandCode faderCode;
public MidiCommandCode selectCode;
public MidiCommandCode muteCode;
public MidiCommandCode recordCode;

public FaderDef(bool _delta, float _range, int _channel,
bool _selectPresent, bool _mutePresent, bool _recordPresent,
int _faderOffset, int _selectOffset, int _muteOffset, int _recordOffset,
MidiCommandCode _faderCode, MidiCommandCode _selectCode, MidiCommandCode _muteCode, MidiCommandCode _recordCode)
{
delta = _delta;
range = _range;
channel = _channel;
selectPresent = _selectPresent;
mutePresent = _mutePresent;
recordPresent = _recordPresent;
faderOffset = _faderOffset;
selectOffset = _selectOffset;
muteOffset = _muteOffset;
recordOffset = _recordOffset;
faderCode = _faderCode;
selectCode = _selectCode;
muteCode = _muteCode;
recordCode = _recordCode;
}
}

public class Fader
{
public int faderNumber;
public int inputController;
public int selectController;
public int muteController;
public int recordController;
public FaderDef faderDef;
public MixerSession assignment;
public bool assigned;
public MidiOut midiOut;
public MidiDevice parent;
public string identifier;

public Fader(MidiDevice midiDevice, int faderNum, int inputOffst, int selectOffset, int muteOffset, int recordOffset)
public Fader(MidiDevice midiDevice, int faderNum)
{
parent = midiDevice;
midiOut = midiDevice.midiOut;
commandCode = MidiCommandCode.ControlChange;
channel = 1;
faderNumber = faderNum;
inputController = faderNum + inputOffst;
selectController = faderNum + selectOffset;
muteController = faderNum + muteOffset;
recordController = faderNum + recordOffset;
faderDef = parent.DefaultFaderDef;
}

public Fader(MidiDevice midiDevice, int faderNum, FaderDef _faderDef)
{
parent = midiDevice;
midiOut = midiDevice.midiOut;
faderNumber = faderNum;
faderDef = _faderDef;
}

private int inputController => faderNumber + faderDef.faderOffset;
private int selectController => faderNumber + faderDef.selectOffset;
private int muteController => faderNumber + faderDef.muteOffset;
private int recordController => faderNumber + faderDef.recordOffset;


public void ResetLights()
{
SetSelectLight(false);
Expand All @@ -45,6 +88,8 @@ public void Assign(MixerSession mixerSession)
identifier = mixerSession.sessionIdentifier;
SetSelectLight(true);
SetRecordLight(false);
if (faderDef.delta)
parent.SetVolumeIndicator(faderNumber, mixerSession.GetVolume());
}

public void AssignInactive(string ident)
Expand All @@ -61,80 +106,145 @@ public void Unassign()
assignment = null;
SetSelectLight(false);
identifier = "";
if (faderDef.delta)
parent.SetVolumeIndicator(faderNumber, -1);
}

public void SetSelectLight(bool state)
{
midiOut.Send(new ControlChangeEvent(0, 1, (MidiController)(selectController), state ? 127 : 0).GetAsShortMessage());
parent.SetLight(selectController, state);
}

public void SetMuteLight(bool state)
{
midiOut.Send(new ControlChangeEvent(0, 1, (MidiController)(muteController), state ? 127 : 0).GetAsShortMessage());
parent.SetLight(muteController, state);
}

public void SetRecordLight( bool state)
{
midiOut.Send(new ControlChangeEvent(0, 1, (MidiController)(recordController), state ? 127 : 0).GetAsShortMessage());
parent.SetLight(recordController, state);
}

public bool HandleEvent(MidiInMessageEventArgs e)
public bool Match(int faderNumber, MidiEvent midiEvent, MidiCommandCode code, int offset)
{
if (e.MidiEvent.CommandCode != commandCode)
if (midiEvent.Channel != faderDef.channel)
return false;
if (midiEvent.CommandCode != code)
return false;
if (code == MidiCommandCode.ControlChange)
{
var me = (ControlChangeEvent)midiEvent;
if ((int)me.Controller == faderNumber + offset)
return true;
}
else if (code == MidiCommandCode.NoteOn)
{
var me = (NoteEvent)midiEvent;
if (me.NoteNumber == faderNumber + offset)
return true;
}
else if (code == MidiCommandCode.PitchWheelChange)
{
return true;
}

ControlChangeEvent me = (ControlChangeEvent)e.MidiEvent;
return false;
}

if (me.Channel != channel)
return false;
public int GetValue(MidiEvent midiEvent)
{
if (midiEvent.CommandCode == MidiCommandCode.ControlChange)
{
var me = (ControlChangeEvent)midiEvent;
return me.ControllerValue;
}
else if (midiEvent.CommandCode == MidiCommandCode.NoteOn)
{
var me = (NoteEvent)midiEvent;
return me.Velocity;
}
else if (midiEvent.CommandCode == MidiCommandCode.PitchWheelChange)
{
var me = (PitchWheelChangeEvent)midiEvent;
return me.Pitch;
}

int controller = (int)me.Controller;
return 0;
}

if (controller == inputController)
public bool HandleEvent(MidiInMessageEventArgs e)
{
// Fader match
if (assigned && Match(faderNumber, e.MidiEvent, faderDef.faderCode, faderDef.faderOffset))
{
if (assigned)
if (faderDef.delta)
{
assignment.SetVolume(me.ControllerValue / 127f);
if (assignment.IsDead())
SetRecordLight(true);
float curVol;
var val = GetValue(e.MidiEvent);
if (val > faderDef.range / 2)
curVol = assignment.ChangeVolume((faderDef.range - val) / faderDef.range);
else
curVol = assignment.ChangeVolume(val / faderDef.range);
parent.SetVolumeIndicator(faderNumber, curVol);
}
else
{
assignment.SetVolume(GetValue(e.MidiEvent) / faderDef.range);
}

if (assignment.IsDead())
SetRecordLight(true);

return true;
}
else if (controller == selectController)

// Select match
if (Match(faderNumber, e.MidiEvent, faderDef.selectCode, faderDef.selectOffset))
{
if (me.ControllerValue != 127) // Only on button-down
if (GetValue(e.MidiEvent) != 127) // Only on button-down
return true;

Console.WriteLine($@"Attempting to assign current window to fader {faderNumber}");
if (assigned)
{
Unassign();
parent.SaveAssignments();
}
else
{
var pid = WindowTools.GetForegroundPID();
var mixerSession = parent.audioDevice.FindMixerSessions(pid);
if (mixerSession != null)
{
Assign(mixerSession);
parent.SaveAssignments();
}
else
Console.WriteLine($@"MixerSession not found for pid {pid}");
}
return true;
}
else if (controller == muteController)

// Mute match
if (assigned && Match(faderNumber, e.MidiEvent, faderDef.muteCode, faderDef.muteOffset))
{
if (me.ControllerValue != 127) // Only on button-down
if (GetValue(e.MidiEvent) != 127) // Only on button-down
return true;

SetMuteLight(assignment.ToggleMute());
if (assignment.IsDead())
SetRecordLight(true);
return true;
}
else if (controller == recordController)

// Record match
if (assigned && Match(faderNumber, e.MidiEvent, faderDef.recordCode, faderDef.recordOffset))
{
SetRecordLight(assignment.IsDead());
return true;
}
return false;

}
}
}
}
Loading

0 comments on commit a23da2f

Please sign in to comment.