Skip to content

Commit 0401ce5

Browse files
committed
FEAT: allowed specifying MIDI devices using its names
1 parent 828473f commit 0401ce5

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

src/mezz/sys-ports.reb

+19-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ init-schemes: func [
487487
spec: system/standard/port-spec-midi
488488
init: func [port /local spec inp out] [
489489
spec: port/spec
490-
if url? spec/ref [
490+
either url? spec/ref [
491491
parse spec/ref [
492492
thru #":" 0 2 slash
493493
opt "device:"
@@ -497,12 +497,30 @@ init-schemes: func [
497497
]
498498
if inp [ spec/device-in: to integer! inp]
499499
if out [ spec/device-out: to integer! out]
500+
][
501+
;; Lookup device IDs using full names (or wildcards)
502+
all [
503+
any-string? inp: select spec 'device-in
504+
spec/device-in: find inp query/mode midi:// 'devices-in
505+
]
506+
all [
507+
any-string? out: select spec 'device-out
508+
spec/device-out: find out query/mode midi:// 'devices-out
509+
]
500510
]
501511
; make port/spec to be only with midi related keys
502512
set port/spec: copy system/standard/port-spec-midi spec
503513
;protect/words port/spec ; protect spec object keys of modification
504514
true
505515
]
516+
find: func[device [any-string!] devices [block!]][
517+
forall devices [
518+
if lib/find/match/any devices/1 device [
519+
return index? devices
520+
]
521+
]
522+
none
523+
]
506524
]
507525

508526
make-scheme [

src/tests/test-midi.r3

+25-16
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,32 @@ close-midi: does [
6666
wait 0
6767
]
6868

69-
midi-inp: open midi:1
70-
midi-out: open [scheme: 'midi device-out: 1]
69+
either port? midi-out: try [
70+
open [
71+
scheme: 'midi
72+
device-out: "Microsoft GS Wavetable" ;; note that the name is not complete, but still accepted
73+
]
74+
][
75+
;; Play some random modern piano music ;-)
76+
loop 50 [
77+
write midi-out rejoin [
78+
;; NOTE VOLUME
79+
#{90} random 127 50 + random 77 0
80+
#{90} random 127 50 + random 77 0
81+
#{90} random 127 50 + random 77 0
82+
]
83+
;; Random time between notes :)
84+
wait 0.5 + random 0.5
85+
]
86+
try [close midi-out]
87+
wait 0
88+
][
89+
;; No SW synth...
90+
print as-purple "Not playing the great piano music, because no SW synth found!"
91+
]
7192

72-
midi-out/awake:
93+
print as-yellow "You have 10 seconds to try your (first) MIDI device input!"
94+
midi-inp: open midi:1
7395
midi-inp/awake: function [event [event!]][
7496
switch event/type [
7597
read [ process-midi read event/port ]
@@ -78,21 +100,8 @@ midi-inp/awake: function [event [event!]][
78100
]
79101
true
80102
]
81-
82103
wait 10
83104
close-midi
84-
halt
85-
;; Play some random modern piano music ;-)
86-
;; MIDI input should be printed in the console.
87-
loop 50 [
88-
write midi-out rejoin [
89-
#{90} random 127 50 + random 77 0
90-
#{90} random 127 50 + random 77 0
91-
#{90} random 127 50 + random 77 0
92-
]
93-
wait 0.5 + random 0.5
94-
]
95105

96-
close-midi
97106

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

0 commit comments

Comments
 (0)