Skip to content

Commit 8adbe40

Browse files
unmidinette of portmidi, with a new Midinette.Platform.PortMidi project
1 parent 2313ecd commit 8adbe40

15 files changed

+535
-484
lines changed

Midinette.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Midinette.Elektron", "src\M
99
EndProject
1010
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Midinette.Fable", "src\Midinette.Fable\src\Midinette.Fable.fsproj", "{7478DC1B-308E-4921-9654-77DC9B4A61F4}"
1111
EndProject
12+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Midinette.Platform.PortMidi", "src\Midinette.Platform.PortMidi\Midinette.Platform.PortMidi.fsproj", "{B7D84F4A-6664-4D07-8689-317CABE09A2F}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{7478DC1B-308E-4921-9654-77DC9B4A61F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{7478DC1B-308E-4921-9654-77DC9B4A61F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{7478DC1B-308E-4921-9654-77DC9B4A61F4}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{B7D84F4A-6664-4D07-8689-317CABE09A2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{B7D84F4A-6664-4D07-8689-317CABE09A2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{B7D84F4A-6664-4D07-8689-317CABE09A2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{B7D84F4A-6664-4D07-8689-317CABE09A2F}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

paket.dependencies

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
source https://nuget.org/api/v2
2+
source ../portmidisharp/build/Debug/AnyCPU
3+
24
storage: none
35
redirects: force
46
generate_load_scripts: on
@@ -9,7 +11,7 @@ nuget Fable.Core
911
nuget Fable.Import.Browser
1012
nuget Fable.Import.WebMIDI
1113
nuget FSharp.Core = 4.2.3
12-
14+
nuget PortMidiSharp
1315
group Build
1416
source https://nuget.org/api/v2
1517
framework >= net461

paket.lock

Lines changed: 314 additions & 312 deletions
Large diffs are not rendered by default.

src/Midinette.Elektron/Elektron.MachineDrum.fs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open System.Threading
77
open System
88
open System.Diagnostics
99
open Midi
10+
open Midinette.Platform
1011

1112
[<RequireQualifiedAccess>]
1213
type LFOType =
@@ -975,13 +976,14 @@ type MidiOutputData =
975976
| Message of MidiMessage
976977
| Sysex of bytes: byte array
977978

978-
type MachineDrum(inPort: IMidiInput<int>, outPort: IMidiOutput<int>, getSysexNowTimestamp: unit -> int) =
979+
type MachineDrum<'timestamp,'midievent>(inPort: IMidiInput<'midievent>, outPort: IMidiOutput<'timestamp, _>, getSysexNowTimestamp) =
979980
let helpGetMDSysex maxMessage (timeout: TimeSpan) (request: MachineDrumSysexRequests) inPort : Async<MachineDrumSysexResponses option> =
980981
#if FABLE_COMPILER
981982
failwithf "TODO FABLE"
982983
#else
983984
let getSysexAsync =
984-
Midi.Sysex.helpGetSysex
985+
986+
Midinette.Sysex.Sysex.helpGetSysex
985987
maxMessage
986988
timeout
987989
(fun sysex ->
@@ -1066,10 +1068,11 @@ type MachineDrum(inPort: IMidiInput<int>, outPort: IMidiOutput<int>, getSysexNow
10661068
| Some globals ->
10671069
let now = getNow ()
10681070
for timestamp, mdEvent in mdEvents do
1071+
let timestamp = timestamp // + now /// TDODODODODODO??
10691072
x.EventToMidiMessages mdEvent globals
10701073
|> Array.iter (
10711074
function
1072-
| MidiOutputData.Message message -> outPort.WriteMessage (timestamp + now) message
1075+
| MidiOutputData.Message message -> outPort.WriteMessage timestamp message
10731076
| MidiOutputData.Sysex sysex -> outPort.WriteSysex now sysex
10741077
)
10751078
| None -> ()
@@ -1133,12 +1136,12 @@ module MachineDrumEventParser =
11331136
elif cc <= 95uy then 2uy
11341137
else 3uy
11351138

1136-
type TimestampedMessage<'t> = {
1137-
Timestamp : int
1139+
type TimestampedMessage<'t,'timestamp> = {
1140+
Timestamp : 'timestamp
11381141
Message: 't
11391142
}
11401143

1141-
type MachineDrumEventListener(md: MachineDrum, getNow : unit -> int) =
1144+
type MachineDrumEventListener(md: MachineDrum<_,_>, getNow) =
11421145
let mutable mdGlobalSettings = md.CurrentGlobalSettings
11431146
let midiIn = md.MidiOutPort
11441147
//let mutable lastKit = {Timestamp = 0; Message = None }
@@ -1247,7 +1250,7 @@ type MachineDrumEventListener(md: MachineDrum, getNow : unit -> int) =
12471250

12481251
[<CLIEvent>] member x.Event = event.Publish
12491252

1250-
let mdDetection getTimestamp inputs outputs onSysex withMachineDrum =
1253+
let mdDetection (getTimestamp) (inputs: IMidiInput<_> array) (outputs: IMidiOutput<_,_> array) onSysex withMachineDrum =
12511254
let queryMessage = QueryStatus(GlobalSlot)
12521255
let onSysex =
12531256
match onSysex with
@@ -1258,17 +1261,18 @@ let mdDetection getTimestamp inputs outputs onSysex withMachineDrum =
12581261
| Some(MachineDrumSysexResponses.GlobalSettingsResponse globals) -> true
12591262
| _ -> false
12601263
)
1264+
1265+
Midinette.Sysex.Sysex.deviceInquiry inputs outputs
1266+
onSysex
1267+
(fun midiOut ->
1268+
midiOut.WriteSysex (getTimestamp()) (QueryStatus(GlobalSlot).Sysex)
1269+
)
1270+
(fun midiIn midiOut ->
1271+
let md = MachineDrum(midiIn, midiOut, getTimestamp)
1272+
withMachineDrum md
1273+
{ new System.IDisposable with member x.Dispose () = () }
1274+
)
12611275

1262-
Sysex.deviceInquiry inputs outputs
1263-
onSysex
1264-
(fun midiOut ->
1265-
midiOut.WriteSysex 0 (QueryStatus(GlobalSlot).Sysex)
1266-
)
1267-
(fun midiIn midiOut ->
1268-
let md = MachineDrum(midiIn, midiOut, getTimestamp)
1269-
withMachineDrum md
1270-
{ new System.IDisposable with member x.Dispose () = () }
1271-
)
12721276

12731277

12741278

src/Midinette.Elektron/Elektron.MonoMachine.fs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Elektron.MonoMachine
22

33
open System
44
open Midi
5+
open Midinette.Platform
56
open Elektron.Platform
67
open Elektron.Platform.SilverMachines
78
type midi7 = byte
@@ -314,13 +315,13 @@ type MonoMachineTrig =
314315
| LFO = 0b0100uy
315316

316317

317-
type MonoMachine(inPort: IMidiInput<_>, outPort: IMidiOutput<_>) =
318+
type MonoMachine<'timestamp,'midievent>(inPort: IMidiInput<'midievent>, outPort: IMidiOutput<'timestamp,_>, nowTimestamp) =
318319
let helpGetMonomachineSysex maxMessage (timeout: TimeSpan) (request: MonoMachineSysexRequests) (inPort: IMidiInput<_>) =
319320
#if FABLE_COMPILER
320321
failwithf "TODO FABLE"
321322
#else
322323
let getSysexAsync =
323-
Midi.Sysex.helpGetSysex
324+
Midinette.Sysex.Sysex.helpGetSysex
324325
maxMessage
325326
timeout
326327
(fun sysex ->
@@ -347,24 +348,24 @@ type MonoMachine(inPort: IMidiInput<_>, outPort: IMidiOutput<_>) =
347348
let task =
348349
helpGetMonomachineSysex 5 (TimeSpan.FromMilliseconds(5000.)) requestMessage inPort
349350
|> Async.StartAsTask
350-
requestMessage.Sysex |> outPort.WriteSysex 0
351+
requestMessage.Sysex |> outPort.WriteSysex (nowTimestamp ())
351352
task.Result
352353
| None ->
353-
requestMessage.Sysex |> outPort.WriteSysex 0
354+
requestMessage.Sysex |> outPort.WriteSysex (nowTimestamp ())
354355
None
355356

356357
member x.MidiOutPort = inPort
357358
member x.MidiInPort = outPort
358359

359360
member x.ChangeLevel track level =
360361
Midi.Nrpn.makeNRPN2 (00uy + track) 127uy level
361-
|> outPort.WriteMessages 0
362+
|> outPort.WriteMessages (nowTimestamp ())
362363
member x.ChangePitch track pitch =
363364
Midi.Nrpn.makeNRPN1 (112uy + track) pitch
364-
|> outPort.WriteMessages 0
365+
|> outPort.WriteMessages (nowTimestamp ())
365366
member x.SendTrigs track (trigs: MonoMachineTrig) =
366367
Midi.Nrpn.makeNRPN1 127uy ((track <<< 4) + (byte trigs))
367-
|> outPort.WriteMessages 0
368+
|> outPort.WriteMessages (nowTimestamp ())
368369
member x.DumpGlobal id =
369370
performSysExRequest (DumpGlobalSettings id)
370371
member x.DumpKit kit =
@@ -374,7 +375,7 @@ type MonoMachine(inPort: IMidiInput<_>, outPort: IMidiOutput<_>) =
374375
member x.QueryStatus statusType =
375376
performSysExRequest (QueryStatus statusType)
376377
member x.AssignMachine track machine parameterAssignMode =
377-
(AssignMachine (track, machine, parameterAssignMode)).Sysex |> outPort.WriteSysex 0
378+
(AssignMachine (track, machine, parameterAssignMode)).Sysex |> outPort.WriteSysex (nowTimestamp ())
378379

379380
member x.Dump dumpRequest =
380381
performSysExRequest dumpRequest
@@ -637,7 +638,7 @@ type TimestampedMessage<'t> = {
637638
}
638639

639640
open Midi.MessageMatching
640-
type MonoMachineEventListener(getNow: unit -> int, mm: MonoMachine) =
641+
type MonoMachineEventListener(getNow: unit -> int, mm: MonoMachine<_,_>) =
641642
let settings = mm.CurrentGlobalSettings
642643
//let settings = { GlobalSettings.midiBaseChannel = 0uy }
643644
let midiIn = mm.MidiOutPort
@@ -659,7 +660,7 @@ type MonoMachineEventListener(getNow: unit -> int, mm: MonoMachine) =
659660
|> Array.map (fun t -> t, Midi.Registers.MidiChannelState<_>())
660661
|> dict
661662

662-
let makeMessage timestamp m = { Timestamp = timestamp; Message = m}
663+
//let makeMessage timestamp m = { Timestamp = timestamp; Message = m}
663664

664665
let onChannelMessage (midiEvent: MidiEvent<_>) =
665666
match settings with
@@ -708,7 +709,7 @@ type MonoMachineEventListener(getNow: unit -> int, mm: MonoMachine) =
708709
let onSysexMessage sysex =
709710
Sysex sysex
710711

711-
let realtimeListener = midiIn.RealtimeMessageReceived.Subscribe(fun m ->
712+
let realtimeListener = midiIn.RealtimeMessageReceived.Subscribe(fun (m: MidiEvent<_>) ->
712713
let oldStarted = midiRealtimeState.started
713714
midiRealtimeState.UpdateWithEvent m.Message
714715
if oldStarted <> midiRealtimeState.started then

src/Midinette.Fable/src/Midinette.Fable.fs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Midinette.Fable
22
open Midi
3+
open Midinette.Platform
34
module WM = Fable.Import.WebMIDI
45

56
type DeviceInfo(port: WM.IMIDIPort) =
67
member x.Port = port
7-
interface Midi.IDeviceInfo with
8+
interface IDeviceInfo with
89
member x.DeviceId = port.Id |> int
910
member x.Name = sprintf "id:%20A name:%20A manufacturer:%A" port.Id port.Name.Value port.Manufacturer
1011

@@ -32,13 +33,13 @@ type WebMIDIInputPortWrapper(webMidiInput: WM.IMIDIInput, onStateChange, onMidiI
3233
let realtimeMessageReceived = new Event<_>()
3334

3435
do
35-
let midiIn = this :> Midi.IMidiInput<double>
36+
let midiIn = this :> IMidiInput<MidiEvent<double>>
3637

3738
webMidiInput.set_OnMidiMessage (fun mm ->
3839
let midiMessage = Midi.MidiMessage.Encode mm.Data.[0] mm.Data.[1] mm.Data.[2]
3940

4041
if midiMessage.IsChannelMessage then
41-
channelMessageReceived.Trigger(MidiEvent(midiMessage, mm.ReceivedTime))
42+
channelMessageReceived.Trigger(MidiEvent<_>(midiMessage, mm.ReceivedTime))
4243
match onMidiIn with
4344
| Some onMidiIn -> onMidiIn mm
4445
| _ -> ()
@@ -50,7 +51,7 @@ type WebMIDIInputPortWrapper(webMidiInput: WM.IMIDIInput, onStateChange, onMidiI
5051
| _ -> ()
5152
)
5253

53-
interface Midi.IMidiInput<double> with
54+
interface IMidiInput<MidiEvent<double>> with
5455
member x.DeviceInfo = DeviceInfo(webMidiInput) :> _
5556
member x.Close () = webMidiInput.Close () |> ignore
5657
member x.Open bufferSize = webMidiInput.Open() |> ignore
@@ -63,35 +64,21 @@ type WebMIDIInputPortWrapper(webMidiInput: WM.IMIDIInput, onStateChange, onMidiI
6364

6465
type WebMIDIOutputPortWrapper(webMidiOutput: WM.IMIDIOutput, device, onStateChange) as this =
6566
do
66-
let midiOut = this :> Midi.IMidiOutput<double>
67+
let midiOut = this :> IMidiOutput<double, MidiMessage>
6768

6869
webMidiOutput.set_OnStateChange (fun sc ->
6970
match onStateChange with
7071
| Some onStateChange -> onStateChange sc
7172
| _ -> ()
7273
)
7374

74-
interface IMidiOutput<double> with
75+
interface IMidiOutput<double, MidiMessage> with
7576
member x.Open bufferSize latency = ()
7677
member x.Close () = ()
7778
member x.WriteMessage timestamp message =
7879
webMidiOutput.SendAt (float timestamp) [|message.Status; message.Data1; message.Data2|]
7980
member x.WriteMessages timestamp messages =
80-
messages |> Array.iter (((x :> IMidiOutput<_>).WriteMessage )timestamp)
81+
messages |> Array.iter (((x :> IMidiOutput<_,_>).WriteMessage )timestamp)
8182
member x.WriteSysex timestamp data =
8283
webMidiOutput.SendAt (float timestamp) data
8384
member x.DeviceInfo = (DeviceInfo device) :> _
84-
85-
86-
(*
87-
open Fable.Core
88-
89-
(************************************************)
90-
(* You should remove the next lines and *)
91-
(* start writing your library in this file *)
92-
(************************************************)
93-
94-
[<Emit("$0 + $1")>]
95-
let add (x: int) (y: int) = jsNative
96-
97-
*)

src/Midinette.Fable/src/Midinette.Fable.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Version>0.0.0</Version>
44
<TargetFrameworks>netstandard2.0</TargetFrameworks>
@@ -10,6 +10,7 @@
1010
</ItemGroup>
1111
<ItemGroup>
1212
<Content Include="*.fsproj; *.fs" PackagePath="fable\" />
13+
<Content Include="paket.references" />
1314
</ItemGroup>
1415
<ItemGroup>
1516
<ProjectReference Include="..\..\Midinette\Midinette.fsproj" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
5+
<OutputPath>..\..\build\$(Configuration)\$(Platform)</OutputPath>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Compile Include="PortMidiMidinettePlatformImpl.fs" />
9+
<Content Include="paket.references" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\..\portmidisharp\PortMidiSharp\PortMidiSharp.fsproj" />
13+
<ProjectReference Include="..\Midinette\Midinette.fsproj" />
14+
</ItemGroup>
15+
<Import Project="..\..\.paket\Paket.Restore.targets" />
16+
</Project>

0 commit comments

Comments
 (0)