Skip to content

Commit 260a123

Browse files
bunch of fixes and simplification
1 parent 1024d31 commit 260a123

File tree

13 files changed

+340
-145
lines changed

13 files changed

+340
-145
lines changed

scripts/test.fsx

Lines changed: 143 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
12
#load "../.paket/load/net472/fsnative.fsx"
23
#load "../.paket/load/net472/RtMidi.Core.fsx"
34
#r @"C:\dev\src\gitlab.com\gauthier\Midinette\build\Debug\AnyCPU\netstandard2.0\Midinette.dll"
5+
#r @"C:\dev\src\gitlab.com\gauthier\Midinette\build\Debug\AnyCPU\netstandard2.0\Midinette.Elektron.dll"
46
#r @"C:\dev\src\gitlab.com\gauthier\Midinette\build\Debug\AnyCPU\netstandard2.0\Midinette.Platform.RtMidi.dll"
7+
#load @"C:\dev\src\gitlab.com\gauthier\ElektronControl\ToolLib\PrettyPrint.fs"
8+
open Elektron.MachineDrum
9+
open Midi
510
open RtMidi.Core.Devices
11+
open RtMidi.Core.Devices.Nrpn
12+
open Elektron
13+
open Elektron.MonoMachine
14+
open System
615

716
open fsnative
817
open fsnative.Internals
@@ -24,27 +33,149 @@ match library with
2433

2534
open Midinette.Platform
2635
let platform = Midinette.Platform.RtMidi.RtMidiMidinettePlatformImpl()
27-
let iplatform = platform :> IMidiPlatform<IDeviceInfo, Midi.MidiEvent<int64>, int64, Midi.MidiMessage>
36+
let iplatform = platform :> IMidiPlatform<int64>
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
//////////////////// MD
58+
let mdOut = (iplatform.InputDevices |> Array.filter (fun d -> d.Name = "machinedrum-out ")).[0] |> iplatform.GetMidiInput |> Option.get
59+
let mdIn = (iplatform.OutputDevices |> Array.filter (fun d -> d.Name = "machinedrum-in ")).[0] |> iplatform.GetMidiOutput |> Option.get
60+
61+
mdOut.ChannelMessageReceived.Add(fun m -> printfn "[%20i] channel message: %A" m.Timestamp m.Message)
62+
mdOut.SystemMessageReceived.Add(fun m -> printfn "[%20i] system message: %A" m.Timestamp m.Message)
63+
mdOut.RealtimeMessageReceived.Add(fun m -> printfn "[%20i] realtime message: %A" m.Timestamp m.Message)
64+
mdOut.SysexReceived.Add(fun bytes -> printfn "sysex message: %A" bytes)
65+
66+
mdOut.Open (1024*1024)
67+
mdIn.Open (1024*1024) 0
68+
69+
let md = new MachineDrum<_>(mdOut, mdIn, (fun () -> platform.Now))
70+
71+
72+
Elektron.MachineDrum.mdDetection
73+
(fun () -> platform.Now)
74+
[|mdOut|]
75+
[|mdIn|]
76+
None
77+
(fun md ->
78+
printfn "inPort: %s" md.MidiInPort.DeviceInfo.Name
79+
printfn "outPort: %s" md.MidiOutPort.DeviceInfo.Name
80+
)
81+
82+
83+
let mdListener = new MachineDrumEventListener<_>(md, (fun () -> platform.Now))
84+
85+
86+
87+
88+
mdListener.Event.Add(fun e -> printfn "[%20i] MD EVENT: %A" e.Timestamp e.Message)
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
//////////////////// MONOMACHINE
100+
let monoOut = (iplatform.InputDevices |> Array.filter (fun d -> d.Name = "monomachine-out ")).[0] |> iplatform.GetMidiInput |> Option.get
101+
let monoIn = (iplatform.OutputDevices |> Array.filter (fun d -> d.Name = "monomachine-in ")).[0] |> iplatform.GetMidiOutput |> Option.get
102+
103+
104+
105+
106+
107+
108+
monoOut.ChannelMessageReceived.Add(fun m -> printfn "[%20i] channel message: %A" m.Timestamp m.Message)
109+
monoOut.SystemMessageReceived.Add(fun m -> printfn "[%20i] system message: %A" m.Timestamp m.Message)
110+
monoOut.RealtimeMessageReceived.Add(fun m -> printfn "[%20i] realtime message: %A" m.Timestamp m.Message)
111+
monoOut.SysexReceived.Add(fun bytes -> printfn "sysex message: %A" bytes)
112+
monoOut.Open (1024*1024)
113+
monoIn.Open (1024*1024) 0
114+
let getNow () = platform.Now
115+
let monomachine = new MonoMachine.MonoMachine<_,_>(monoOut, monoIn, getNow)
116+
let evtl = new MonoMachineEventListener<_>(getNow, monomachine)
117+
evtl.Event.Add(fun e ->
118+
match e.Message with
119+
| Sysex data -> printfn "[%20i] mnm:" e.Timestamp; PrettyPrint.printBytes data
120+
| _ -> printfn "[%20i] mnm: %A" e.Timestamp e.Message
121+
)
122+
123+
monomachine.CurrentGlobalSettings
124+
monomachine.DumpPattern 0uy
125+
let (Some(Kit(kit))) = monomachine.DumpKit 16uy
126+
let (Some(Pattern(pattern))) = monomachine.DumpPattern 0uy
127+
pattern
128+
|> Elektron.Platform.dataToByte
129+
kit.unpacked.[0xb..0xb+6]
130+
131+
132+
133+
134+
135+
monomachine.MidiInPort.WriteMessage 0L (Midi.MidiMessage.NoteOn 0x00uy 0x64uy 100uy)
136+
monomachine.MidiInPort.WriteMessage 0L (Midi.MidiMessage.NoteOff 0x00uy 0x64uy 100uy)
137+
monomachine.DumpPattern 0uy
138+
let globalsysex =
139+
"""
140+
f0 00 20 3c 03 00 50 03 01 00 12 08 00 02 06 07
141+
02 01 54 03 02 03 01 05 00 09 00 0a 0b 0c 0d 0e
142+
01 02 00 07 0a 01 02 07 0a 01 00 02 07 0a 01 02
143+
07 0a 00 01 02 07 0a 01 02 07 21 0a 06 01 00 64
144+
40 03 00 00 0b 17 23 2f 3b 47 0b 53 5f 6b 17 77
145+
40 7f 52 20 10 41 00 01 02 00 20 7f 05 00 11 30
146+
0e 30 00 5b f7
147+
""".Split([|' ';'\n'|], StringSplitOptions.RemoveEmptyEntries)
148+
|> Array.map (fun s -> Convert.ToByte(s, 16))
149+
150+
monomachine.DumpKit 0uy
151+
let (MonoMachineSysexResponse.Kit(kit) ) = MonoMachineSysexResponse.BuildResponse globalsysex
152+
kit.unpacked.Length
153+
154+
let gdata =
155+
ArraySegment(globalsysex, 0xa, (Array.length globalsysex) - 15)
156+
|> Seq.toArray
157+
|> Elektron.Platform.dataToByte
158+
// |> PrettyPrint.printBytes
159+
gdata.Length
160+
PrettyPrint.printBytes (Elektron.Platform.dataToByte a)
161+
PrettyPrint.printBytes globalsysex
162+
163+
28164

29-
let monoOut =
30-
(iplatform.InputDevices |> Array.filter (fun d -> d.Name = "monomachine-out ")).[0]
31-
|> iplatform.GetMidiInput
32165

33-
monoOut.Value.ChannelMessageReceived.Add(fun m -> printfn "[%20i] channel message: %A" m.Timestamp m.Message)
34-
monoOut.Value.SystemMessageReceived.Add(fun m -> printfn "[%20i] system message: %A" m.Timestamp m.Message)
35-
monoOut.Value.RealtimeMessageReceived.Add(fun m -> printfn "[%20i] realtime message: %A" m.Timestamp m.Message)
36-
monoOut.Value.SysexReceived.Add(fun bytes -> printfn "sysex message: %A" bytes)
37-
monoOut.Value.Open (1024*1024)
38166
let mdm = global.RtMidi.Core.MidiDeviceManager.Default
39167
mdm.GetAvailableMidiApis()
40168

41-
let rtmin = (mdm.InputDevices |> Seq.find (fun d -> d.Name = "monomachine-out ")).CreateDevice()
169+
let rtmin = (mdm.InputDevices |> Seq.find (fun d -> d.Name = "machinedrum-out ")).CreateDevice()
42170

43-
rtmin.InputDevice.Message.Add(fun bytes -> printfn "inputmessage %A" bytes)
44-
rtmin.add_Nrpn (NrpnMessageHandler(fun a n -> printfn "[%20i] nrpn %A %i %i" platform.Now n.Channel n.Parameter n.Value))
171+
rtmin.InputDevice.Message.Add(fun bytes -> printfn "==========="; PrettyPrint.printBytes bytes)
172+
//rtmin.add_Nrpn (NrpnMessageHandler(fun a n -> printfn "[%20i] nrpn %A %i %i" platform.Now n.Channel n.Parameter n.Value))
173+
45174
rtmin.Open()
46175
mdm.OutputDevices
47176

177+
rtmin.SetNrpnMode NrpnMode.On
178+
48179

49180

50181
let e : RtMidi.Core.Unmanaged.Devices.RtMidiInputDevice = null

src/Midinette.Elektron/Elektron.MachineDrum.fs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ open System
88
open System.Diagnostics
99
open Midi
1010
open Midinette.Platform
11-
11+
(*
1212
[<RequireQualifiedAccess>]
1313
type LFOType =
1414
| Free
@@ -26,6 +26,22 @@ with
2626
| Free -> 0uy
2727
| Trig -> 1uy
2828
| Hold -> 2uy
29+
*)
30+
[<Struct>]
31+
type LFOType = private SDULFOType of byte
32+
with
33+
static member Free = SDULFOType 0uy
34+
static member Trig = SDULFOType 1uy
35+
static member Hold = SDULFOType 2uy
36+
37+
static member FromByte b =
38+
match b with
39+
| 0uy -> LFOType.Free
40+
| 1uy -> LFOType.Trig
41+
| 2uy -> LFOType.Hold
42+
| i -> failwithf "invalid lfo type %i" i
43+
44+
static member ToByte = function | SDULFOType b -> b
2945

3046

3147
module MachineSpecs =
@@ -823,6 +839,7 @@ type MDGlobalSettings(bytes: byte array) =
823839
|> function | 127uy -> None | v -> Some v
824840
*)
825841
type MachineDrumSysexMessageId =
842+
| Global = 0x50uy
826843
| Kit = 0x52uy
827844
| Pattern = 0x67uy
828845
| Song = 0x69uy
@@ -976,7 +993,7 @@ type MidiOutputData =
976993
| Message of MidiMessage
977994
| Sysex of bytes: byte array
978995

979-
type MachineDrum<'timestamp,'midievent>(inPort: IMidiInput<'midievent>, outPort: IMidiOutput<'timestamp, _>, getSysexNowTimestamp) =
996+
type MachineDrum<'timestamp>(inPort: IMidiInput<'timestamp>, outPort: IMidiOutput<'timestamp>, getSysexNowTimestamp) =
980997
let helpGetMDSysex maxMessage (timeout: TimeSpan) (request: MachineDrumSysexRequests) inPort : Async<MachineDrumSysexResponses option> =
981998
#if FABLE_COMPILER
982999
failwithf "TODO FABLE"
@@ -1141,7 +1158,7 @@ type TimestampedMessage<'t,'timestamp> = {
11411158
Message: 't
11421159
}
11431160

1144-
type MachineDrumEventListener(md: MachineDrum<_,_>, getNow) =
1161+
type MachineDrumEventListener<'timestamp>(md: MachineDrum<'timestamp>, getNow) =
11451162
let mutable mdGlobalSettings = md.CurrentGlobalSettings
11461163
let midiIn = md.MidiOutPort
11471164
//let mutable lastKit = {Timestamp = 0; Message = None }
@@ -1250,15 +1267,16 @@ type MachineDrumEventListener(md: MachineDrum<_,_>, getNow) =
12501267

12511268
[<CLIEvent>] member x.Event = event.Publish
12521269

1253-
let mdDetection (getTimestamp) (inputs: IMidiInput<_> array) (outputs: IMidiOutput<_,_> array) onSysex withMachineDrum =
1270+
let mdDetection (getTimestamp) (inputs: IMidiInput<_> array) (outputs: IMidiOutput<_> array) onSysex withMachineDrum =
12541271
let queryMessage = QueryStatus(GlobalSlot)
12551272
let onSysex =
12561273
match onSysex with
12571274
| Some onSysex -> onSysex
12581275
| _ ->
12591276
(fun sysex ->
12601277
match MachineDrumSysexResponses.BuildResponse sysex with
1261-
| Some(MachineDrumSysexResponses.GlobalSettingsResponse globals) -> true
1278+
//| Some(MachineDrumSysexResponses.GlobalSettingsResponseglobals) -> true
1279+
| Some(MachineDrumSysexResponses.StatusResponse(_)) -> true
12621280
| _ -> false
12631281
)
12641282

0 commit comments

Comments
 (0)