Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Pitch Bend message support #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Assets/MidiJack/MidiDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class ChannelState
// (X-1 represents velocity)
public float[] _noteArray;

//Pitch bend values. Range -1 to 1
public float _pitchBendValue;

// Knob number to knob value mapping
public Dictionary<int, float> _knobMap;

Expand Down Expand Up @@ -99,17 +102,26 @@ public float GetKnob(MidiChannel channel, int knobNumber, float defaultValue)
return defaultValue;
}

public float GetPitchBend(MidiChannel channel)
{
UpdateIfNeeded();
var cs = _channelArray[(int)channel];
return cs._pitchBendValue;
}

#endregion

#region Event Delegates

public delegate void NoteOnDelegate(MidiChannel channel, int note, float velocity);
public delegate void NoteOffDelegate(MidiChannel channel, int note);
public delegate void KnobDelegate(MidiChannel channel, int knobNumber, float knobValue);
public delegate void PitchBendDelegate(MidiChannel channel, float bendValue);

public NoteOnDelegate noteOnDelegate { get; set; }
public NoteOffDelegate noteOffDelegate { get; set; }
public KnobDelegate knobDelegate { get; set; }
public PitchBendDelegate pitchBendDelegate { get; set; }

#endregion

Expand Down Expand Up @@ -235,6 +247,16 @@ void Update()
noteOffDelegate((MidiChannel)channelNumber, message.data1);
}

//Pitch Bend message?
if(statusCode == 14)
{
var bend = 1.0f / 127 * message.data2 * 2 - 1; //Map to range between -1 and 1 with center at 0
_channelArray[channelNumber]._pitchBendValue = bend;
if (pitchBendDelegate != null)
pitchBendDelegate((MidiChannel)channelNumber, bend);
//Debug.Log("Pitch bend: " + bend.ToString("0.000"));
}

// CC message?
if (statusCode == 0xb)
{
Expand Down
5 changes: 5 additions & 0 deletions Assets/MidiJack/MidiMaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public static MidiDriver.KnobDelegate knobDelegate {
set { MidiDriver.Instance.knobDelegate = value; }
}

public static MidiDriver.PitchBendDelegate pitchBendDelegate {
get { return MidiDriver.Instance.pitchBendDelegate; }
set { MidiDriver.Instance.pitchBendDelegate = value; }
}

// Returns the key state (on: velocity, off: zero).
public static float GetKey(MidiChannel channel, int noteNumber)
{
Expand Down
Binary file modified MidiJack.unitypackage
Binary file not shown.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,16 @@ mixed status of all active channels.

Returns the list of active controllers.

- MidiMaster.GetPitchBendValue(channel)

Returns the current pitch bend value. The value ranges from -1.0 to 1.0 with 0 for no bend.

There are also delegates for the each type of MIDI event.

- MidiMaster.noteOnDelegate (channel, noteNumber, velocity)
- MidiMaster.noteOffDelegate (channel, noteNumber)
- MidiMaster.knobDelegate (channel, knobNumber, konbValue)
- MidiMaster.pitchBendDelegate (channel, bendValue)

MIDI Monitor Window
-------------------
Expand Down