Skip to content

Commit 6972faf

Browse files
committed
Auto-connect jack midi inputs
1 parent 53bf3a0 commit 6972faf

2 files changed

Lines changed: 45 additions & 16 deletions

File tree

AudioPlugSharpJack/AudioPlugSharpJack.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="JackSharpCore" Version="0.1.2" />
20+
<PackageReference Include="JackSharpCore" Version="0.1.6" />
2121
</ItemGroup>
2222

2323
<ItemGroup>
2424
<ProjectReference Include="..\AudioPlugSharp\AudioPlugSharp.csproj" />
2525
</ItemGroup>
2626

2727
<ItemGroup>
28-
<None Include="..\README.md" Pack="true" PackagePath="\"/>
28+
<None Include="..\README.md" Pack="true" PackagePath="\" />
2929
</ItemGroup>
3030

3131
</Project>

AudioPlugSharpJack/JackHost.cs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class JackHost<T> : IAudioHost where T : IAudioPlugin, IAudioPluginProces
1818

1919
string saveFolder;
2020
Processor jackProcessor;
21+
Controller jackController;
2122

2223
public JackHost(T plugin)
2324
{
@@ -89,14 +90,23 @@ public void Run()
8990
MaxAudioBufferSize = 512;
9091
BitsPerSample = EAudioBitsPerSample.Bits32;
9192

92-
jackProcessor = new(Plugin.PluginName, 1, 2, 1, 0, autoconnect: true);
93+
jackProcessor = new(Plugin.PluginName, Plugin.InputPorts.Length > 0 ? 1 : 0, 2, 1, 0, autoconnect: true);
9394

9495
if (!jackProcessor.Start())
9596
{
9697
Logger.Log("Unable to connect to Jack");
9798
}
9899
else
99100
{
101+
jackController = new(Plugin.PluginName + "Controller");
102+
jackController.PortChanged += JackController_PortChanged;
103+
104+
if (jackController.Start())
105+
{
106+
Logger.Log("Unable to start Jack controller");
107+
}
108+
109+
100110
SampleRate = jackProcessor.SampleRate;
101111

102112
Logger.Log("Jack sample rate: " + SampleRate);
@@ -132,6 +142,22 @@ public void Run()
132142
}
133143
}
134144

145+
private void JackController_PortChanged(object sender, JackSharp.Events.PortRegistrationEventArgs e)
146+
{
147+
// Auto-connect any physical midi ports
148+
if ((e.ChangeType == JackSharp.Events.ChangeType.New) && (e.Port.PortType == JackSharp.Ports.PortType.Midi) && (e.Port.IsPhysicalPort) && (e.Port.Direction == JackSharp.Ports.Direction.Out))
149+
{
150+
var midiIn = jackProcessor.MidiInPorts.FirstOrDefault();
151+
152+
if (midiIn != null)
153+
{
154+
Logger.Log("Connect midi port:" + e.Port.ClientName + ":" + e.Port.PortName);
155+
156+
jackController.Connect(e.Port.ClientName + ":" + e.Port.PortName, Plugin.PluginName + ":" + midiIn.Name);
157+
}
158+
}
159+
}
160+
135161
void Process(ProcessBuffer buffer)
136162
{
137163
CurrentAudioBufferSize = (uint)jackProcessor.BufferSize;
@@ -142,20 +168,23 @@ void Process(ProcessBuffer buffer)
142168
{
143169
byte[] midiData = midiEvent.MidiData;
144170

145-
int commandCode = (midiData[0] & 0xF0);
146-
int channel = (midiData[0] & 0x0F) + 1;
147-
148-
if (commandCode == 144)
149-
{
150-
Plugin.HandleNoteOn(channel, midiData[1], (float)midiData[2] / 127.0f, 0);
151-
}
152-
else if (commandCode == 128)
153-
{
154-
Plugin.HandleNoteOff(channel, midiData[1], (float)midiData[2] / 127.0f, 0);
155-
}
156-
else if (commandCode == 160)
171+
if (midiData.Length > 2)
157172
{
158-
Plugin.HandlePolyPressure(channel, midiData[1], (float)midiData[2] / 127.0f, 0);
173+
int commandCode = (midiData[0] & 0xF0);
174+
int channel = (midiData[0] & 0x0F) + 1;
175+
176+
if (commandCode == 144)
177+
{
178+
Plugin.HandleNoteOn(channel, midiData[1], (float)midiData[2] / 127.0f, 0);
179+
}
180+
else if (commandCode == 128)
181+
{
182+
Plugin.HandleNoteOff(channel, midiData[1], (float)midiData[2] / 127.0f, 0);
183+
}
184+
else if (commandCode == 160)
185+
{
186+
Plugin.HandlePolyPressure(channel, midiData[1], (float)midiData[2] / 127.0f, 0);
187+
}
159188
}
160189
}
161190
}

0 commit comments

Comments
 (0)