Skip to content

Commit

Permalink
FEAT: allowed specifying MIDI devices using its names
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Jun 7, 2023
1 parent 828473f commit 0401ce5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
20 changes: 19 additions & 1 deletion src/mezz/sys-ports.reb
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ init-schemes: func [
spec: system/standard/port-spec-midi
init: func [port /local spec inp out] [
spec: port/spec
if url? spec/ref [
either url? spec/ref [
parse spec/ref [
thru #":" 0 2 slash
opt "device:"
Expand All @@ -497,12 +497,30 @@ init-schemes: func [
]
if inp [ spec/device-in: to integer! inp]
if out [ spec/device-out: to integer! out]
][
;; Lookup device IDs using full names (or wildcards)
all [
any-string? inp: select spec 'device-in
spec/device-in: find inp query/mode midi:// 'devices-in
]
all [
any-string? out: select spec 'device-out
spec/device-out: find out query/mode midi:// 'devices-out
]
]
; make port/spec to be only with midi related keys
set port/spec: copy system/standard/port-spec-midi spec
;protect/words port/spec ; protect spec object keys of modification
true
]
find: func[device [any-string!] devices [block!]][
forall devices [
if lib/find/match/any devices/1 device [
return index? devices
]
]
none
]
]

make-scheme [
Expand Down
41 changes: 25 additions & 16 deletions src/tests/test-midi.r3
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,32 @@ close-midi: does [
wait 0
]

midi-inp: open midi:1
midi-out: open [scheme: 'midi device-out: 1]
either port? midi-out: try [
open [
scheme: 'midi
device-out: "Microsoft GS Wavetable" ;; note that the name is not complete, but still accepted
]
][
;; Play some random modern piano music ;-)
loop 50 [
write midi-out rejoin [
;; NOTE VOLUME
#{90} random 127 50 + random 77 0
#{90} random 127 50 + random 77 0
#{90} random 127 50 + random 77 0
]
;; Random time between notes :)
wait 0.5 + random 0.5
]
try [close midi-out]
wait 0
][
;; No SW synth...
print as-purple "Not playing the great piano music, because no SW synth found!"
]

midi-out/awake:
print as-yellow "You have 10 seconds to try your (first) MIDI device input!"
midi-inp: open midi:1
midi-inp/awake: function [event [event!]][
switch event/type [
read [ process-midi read event/port ]
Expand All @@ -78,21 +100,8 @@ midi-inp/awake: function [event [event!]][
]
true
]

wait 10
close-midi
halt
;; 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 0401ce5

Please sign in to comment.