Skip to content

Commit ca663df

Browse files
monomachine...
1 parent 82850c1 commit ca663df

File tree

4 files changed

+111
-41
lines changed

4 files changed

+111
-41
lines changed

src/Midinette.Elektron/Elektron.MachineDrum.fs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,24 @@ type MachineDrum(inPort: IMidiInput<int>, outPort: IMidiOutput<int>, getSysexNow
980980
#if FABLE_COMPILER
981981
failwithf "TODO FABLE"
982982
#else
983-
Midi.Sysex.helpGetSysex maxMessage timeout (fun sysex -> sysex.[0..5] = Sysex.mdHeader && sysex.[6] = request.ResponseMessageId) (request.BuildResponse >> Option.get) inPort
983+
let getSysexAsync =
984+
Midi.Sysex.helpGetSysex
985+
maxMessage
986+
timeout
987+
(fun sysex ->
988+
sysex.[0..5] = Sysex.mdHeader
989+
&& sysex.[6] = request.ResponseMessageId
990+
)
991+
(request.BuildResponse >> Option.get)
992+
inPort
993+
async {
994+
let! sysx = getSysexAsync
995+
match sysx with
996+
| Choice1Of2 sysx -> return sysx
997+
| Choice2Of2 exn ->
998+
printfn "oops"
999+
return None
1000+
}
9841001
#endif
9851002

9861003
let performSysExRequest (requestMessage: MachineDrumSysexRequests) =

src/Midinette.Elektron/Elektron.MonoMachine.fs

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -212,26 +212,32 @@ type GlobalSettings = {
212212
baseFrequency : int
213213
}
214214
with
215-
static member FromSysex (bytes: byte array) =
216-
let globalData = (getMonoMachineDataSliceFromSysexMessage bytes) |> Seq.toArray |> Elektron.Platform.byteToData
217-
218-
{
219-
midiChannelAutoTrack = globalData.[0x00]
220-
midiChannel = globalData.[0x01]
221-
midiChannelMultiTrig = globalData.[0x02]
222-
midiChannelMultiMap = globalData.[0x03]
223-
midiMachineChannels = [||]//globalData.[0x12..0x1]
224-
225-
// external sync
226-
ccDestinationsPerChannel = [||]//[|globalData.[0x18..0x1]|]
227-
programChangeIn = globalData.[0xfd] = 1uy
228-
velocityCurve = globalData.[0xff] |> VelocityCurve.FromByte
229-
fixedVelocity = globalData.[0x100]
230-
knobSpeed = globalData.[0x101]
231-
baseFrequency = 0 //globalData.[0x104..] |> Elektron.fourBytesToBigEndianInt
232-
}
215+
static member FromSysex (sysexData: byte array) =
216+
if areMonoMachineCheckSumAndLengthValid sysexData then
217+
let globalData = (getMonoMachineDataSliceFromSysexMessage sysexData) |> Seq.toArray |> Elektron.Platform.byteToData
218+
if globalData.Length < 0x101 then None
219+
else
220+
Some
221+
{
222+
midiChannelAutoTrack = globalData.[0x00]
223+
midiChannel = globalData.[0x01]
224+
midiChannelMultiTrig = globalData.[0x02]
225+
midiChannelMultiMap = globalData.[0x03]
226+
midiMachineChannels = [||]//globalData.[0x12..0x1]
227+
228+
// external sync
229+
ccDestinationsPerChannel = [||]//[|globalData.[0x18..0x1]|]
230+
programChangeIn = globalData.[0xfd] = 1uy
231+
velocityCurve = globalData.[0xff] |> VelocityCurve.FromByte
232+
fixedVelocity = globalData.[0x100]
233+
knobSpeed = globalData.[0x101]
234+
baseFrequency = 0 //globalData.[0x104..] |> Elektron.fourBytesToBigEndianInt
235+
}
236+
else
237+
failwithf "can't parse globalsettings from sysex %A" sysexData
238+
233239
type MonoMachineSysexResponse =
234-
| GlobalSettings of byte array
240+
| GlobalSettings of GlobalSettings
235241
| Kit of MonoMachineKit
236242
| Pattern of byte array
237243
| Song of byte array
@@ -279,11 +285,11 @@ with
279285
| QueryStatus _ -> Some 0x72uy
280286
member x.BuildResponse data =
281287
match x with
282-
| DumpGlobalSettings _ -> GlobalSettings data
283-
| DumpKit _ -> Kit(MonoMachineKit data)
284-
| DumpPattern _ -> Pattern data
285-
| DumpSong _ -> Song data
286-
| QueryStatus _ -> StatusResponse((MonoMachineStatusType.FromByte data.[monoMachineHeader.Length + 1]), data.[monoMachineHeader.Length + 2])
288+
| DumpGlobalSettings _ -> GlobalSettings.FromSysex data |> Option.map GlobalSettings
289+
| DumpKit _ -> Some <| Kit(MonoMachineKit data)
290+
| DumpPattern _ -> Some <| Pattern data
291+
| DumpSong _ -> Some <| Song data
292+
| QueryStatus _ -> Some <| StatusResponse((MonoMachineStatusType.FromByte data.[monoMachineHeader.Length + 1]), data.[monoMachineHeader.Length + 2])
287293
| AssignMachine(_) -> failwithf "not implemented"
288294
//| _ -> None
289295

@@ -295,9 +301,31 @@ type MonoMachineTrig =
295301

296302
type MonoMachine(inPort: IMidiInput<_>, outPort: IMidiOutput<_>) =
297303
let helpGetMonomachineSysex maxMessage (timeout: TimeSpan) (request: MonoMachineSysexRequests) (inPort: IMidiInput<_>) =
298-
Midi.Sysex.helpGetSysex maxMessage timeout (fun sysex ->
299-
sysex.[0..5] = Helpers.monoMachineHeader && Some sysex.[6] = request.ResponseMessageId) request.BuildResponse inPort
300-
304+
#if FABLE_COMPILER
305+
failwithf "TODO FABLE"
306+
#else
307+
let getSysexAsync =
308+
Midi.Sysex.helpGetSysex
309+
maxMessage
310+
timeout
311+
(fun sysex ->
312+
sysex.[0..5] = Helpers.monoMachineHeader
313+
&& Some sysex.[6] = request.ResponseMessageId
314+
)
315+
request.BuildResponse
316+
inPort
317+
async {
318+
let! sysx = getSysexAsync
319+
match sysx with
320+
| Choice1Of2(Some(sysx)) -> return sysx
321+
| Choice1Of2 (_) ->
322+
printfn "oops1"
323+
return None
324+
| Choice2Of2 exn ->
325+
printfn "oops2"
326+
return None
327+
}
328+
#endif
301329
let performSysExRequest (requestMessage: MonoMachineSysexRequests) =
302330
match requestMessage.ResponseMessageId with
303331
| Some id ->
@@ -581,7 +609,7 @@ type TimestampedMessage<'t> = {
581609

582610

583611
type MonoMachineEventListener(getNow: unit -> int, mm: MonoMachine) =
584-
let settings = mm.CurrentGlobalSettings |> Option.map GlobalSettings.FromSysex
612+
let settings = mm.CurrentGlobalSettings
585613
//let settings = { GlobalSettings.midiBaseChannel = 0uy }
586614
let midiIn = mm.MidiOutPort
587615
let midiRealtimeState = Midi.Registers.MidiRealtimeState()

src/Midinette.Elektron/Elektron.Platform.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ let checkSum (data: byte seq) =
9393
sum &&& 0b11111111111111
9494
// FABLE TODO
9595

96-
let areMachineDrumCheckSumAndLengthValid data = true
97-
(*
96+
let areMachineDrumCheckSumAndLengthValid data =
97+
9898
let checkSum =
9999
getMachineDrumDataSliceFromSysexMessage data
100100
|> checkSum
@@ -103,9 +103,9 @@ let areMachineDrumCheckSumAndLengthValid data = true
103103
let expectedLength = int (getLengthFromSysexMessage data)
104104
printfn "%i %i %i %i" length expectedLength checkSum expectedCheckSum
105105
length = expectedLength && checkSum = expectedCheckSum
106-
*)
107-
let areMonoMachineCheckSumAndLengthValid data = true
108-
(*
106+
107+
let areMonoMachineCheckSumAndLengthValid data =
108+
109109
let checkSum =
110110
getMonoMachineDataSliceFromSysexMessage data
111111
|> checkSum
@@ -114,7 +114,7 @@ let areMonoMachineCheckSumAndLengthValid data = true
114114
let expectedLength = int (getLengthFromSysexMessage data)
115115
printfn "%i %i %i %i" length expectedLength checkSum expectedCheckSum
116116
length = expectedLength && checkSum = expectedCheckSum
117-
*)
117+
118118
module SilverMachines =
119119
[<RequireQualifiedAccess>]
120120
type PatternBank =

src/Midinette/Midi.Sysex.fs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Sysex =
44
open System
55
open System.Diagnostics
66
open Midi
7+
open System.Net
8+
79
let sysexDeviceInquiry =
810
[|
911
0xf0uy
@@ -32,6 +34,7 @@ module Sysex =
3234
subscription.Dispose()
3335
return response
3436
}
37+
|> Async.Catch
3538

3639
let getSysexMessagesFromBytes data =
3740
let getSlice s l (a: _ array) = Array.init l (fun i -> a.[s + i])
@@ -42,9 +45,13 @@ module Sysex =
4245
elif data.[i] = 0xf7uy then yield getSlice beginIndex (i - beginIndex + 1) data
4346
|]
4447

45-
type DetectedDevice<'timestamp> = DetectedDevice of responseData: byte array * deviceOutput: IMidiInput<'timestamp> * deviceInput: IMidiOutput<'timestamp>
48+
type DetectedDevice<'timestamp> =
49+
50+
| DetectedDevice of responseData: byte array * deviceOutput: IMidiInput<'timestamp> * deviceInput: IMidiOutput<'timestamp>
51+
| Error of exn * deviceOutput: IMidiInput<'timestamp> * deviceInput: IMidiOutput<'timestamp>
4652

4753
let deviceInquiry (inputPorts: IMidiInput<_> array) (outputPorts: IMidiOutput<_> array) sysexMatcher doWithOutput withInputAndOutput =
54+
// maybe buggy
4855
let detectedPairs = [|
4956
let sysexInputTimeout = System.TimeSpan.FromSeconds 5.0
5057
for o in outputPorts do
@@ -54,11 +61,29 @@ module Sysex =
5461
use scope = withInputAndOutput i o
5562
let buildResponse sysexData = DetectedDevice(sysexData, i, o)
5663
let response = helpGetSysex 10 sysexInputTimeout sysexMatcher buildResponse i
57-
yield response
64+
yield i,o,response
5865
|]
5966
doWithOutput o
60-
yield (responses |> Async.Parallel |> Async.RunSynchronously)
67+
yield
68+
(responses
69+
|> Array.map (fun (_,_,response) -> response)
70+
|> Async.Parallel
71+
|> Async.RunSynchronously
72+
|> Array.zip (responses |> Array.map (fun (i,o,_) ->(i,o)))
73+
)
6174
|]
62-
detectedPairs
63-
|> Array.map (Array.choose id)
64-
|> Array.collect id
75+
detectedPairs
76+
|> Array.map (
77+
fun items ->
78+
items
79+
|> Array.map (
80+
fun ((i,o), result) ->
81+
82+
match result with
83+
| Choice1Of2(data) -> data
84+
| Choice2Of2 exn -> Some (Error(exn, i, o))
85+
)
86+
87+
)
88+
//|> Array.map (Array.choose id)
89+
//|> Array.collect id

0 commit comments

Comments
 (0)