Skip to content

Commit

Permalink
FIX: MIDI port using old path (test script included)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Jun 5, 2023
1 parent fd780c5 commit 67e519a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/mezz/sys-ports.reb
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ init-schemes: func [
parse spec/ref [
thru #":" 0 2 slash
opt "device:"
copy inp *parse-url/digits
opt [#"/" copy out *parse-url/digits]
copy inp url-parser/digit
opt [#"/" copy out url-parser/digit]
end
]
if inp [ spec/device-in: to integer! inp]
Expand Down
1 change: 1 addition & 0 deletions src/os/win32/dev-midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#endif

#ifdef INCLUDE_MIDI_DEVICE
//#define DEBUG_MIDI

// NOTE: this will be useful for higher level launchpad schemes:
// https://github.com/FMMT666/launchpad.py/blob/master/launchpad_py/launchpad.py
Expand Down
99 changes: 99 additions & 0 deletions src/tests/test-midi.r3
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
Rebol [
Title: "Test MIDI port"
Date: 5-Jun-2023
Author: "Oldes"
File: %test-midi.r3
Version: 1.0.0
Requires: 3.11.0
]

midi: query midi://
unless object? midi [ print as-purple "No MIDI available!" quit]

print [as-yellow "Input devices: " length? midi/devices-in]
foreach dev midi/devices-in [ print [tab mold dev] ]
print [as-yellow "Output devices:" length? midi/devices-out]
foreach dev midi/devices-out [ print [tab mold dev] ]


*MIDI_message: enum [
note-off: 2#{0000 1000}
note-on: 2#{0000 1001}
poly-aftertouch: 2#{0000 1010}
control-change: 2#{0000 1011}
program-change: 2#{0000 1100}
channel-preasure: 2#{0000 1101}
pitch_bend-change: 2#{0000 1110}
] "MIDI Voice message"

*MIDI_message_short: enum [
Off: 2#{0000 1000}
On: 2#{0000 1001}
PoPr: 2#{0000 1010}
Par: 2#{0000 1011}
PrCh: 2#{0000 1100}
ChPr: 2#{0000 1101}
Pb: 2#{0000 1110}
] "MIDI Voice message (short variant)"


process-midi: function [data [binary!]][
bin: binary data
while [(length? bin/buffer) >= 8][
binary/read bin [
status: UB 4
channel: UB 4
byte-1: SI8
byte-2: SI8
byte-3: SI8
time: UI32LE
]
either all [
byte-3 = 0
][
op: *MIDI_message_short/name status
prin ajoin [time #" " op " ch=" channel #" "]
print ajoin switch/default op [
Par [["c=" byte-1 " v=" byte-2]]
Pb ChPr [reduce ["v=" byte-1 + (byte-2 << 7)]]
PrCh [["p=" byte-1]]
][
["n=" byte-1 " v=" byte-2]
]
][ print "should not happen!" ]
]
]

close-midi: does [
try [close midi-out]
try [close midi-inp]
wait 0
]

midi-inp: open midi:1
midi-out: open [scheme: 'midi device-out: 1]

midi-out/awake:
midi-inp/awake: function [event [event!]][
switch event/type [
read [ process-midi read event/port ]
open [ print ["MIDI port opened!" event/port/spec/ref] ]
close [ print ["MIDI port closed!" event/port/spec/ref] ]
]
true
]

;; Play some random modern piano music ;-)
;; MIDI input should be printed in the console.
loop 50 [
write midi-out rejoin [
#{90} random 127 50 + random 77 0
#{90} random 127 50 + random 77 0
#{90} random 127 50 + random 77 0
]
wait 0.5 + random 0.5
]

close-midi

if system/options/script [ask "DONE"]

0 comments on commit 67e519a

Please sign in to comment.