@@ -27,11 +27,15 @@ class MidiService extends ServiceBundle<MidiInputServiceConfig, MidiInputService
2727 devices . push ( device ) ;
2828 }
2929 } ) ;
30+
3031 if ( devices . length === 0 ) {
31- return error ( "No device matched the configured pattern! " ) ;
32+ return error ( "No device matched the configured pattern. " ) ;
3233 }
33- if ( devices . length > 1 ) {
34- return error ( "The configured pattern is ambiguous!" ) ;
34+
35+ // If we have a device with the exact same name we prioritize it and use that device.
36+ // If we have no exact match an ambiguous pattern is not allowed.
37+ if ( devices . length > 1 && ! devices . includes ( config . device ) ) {
38+ return error ( "The configured pattern is ambiguous." ) ;
3539 }
3640 }
3741
@@ -48,11 +52,18 @@ class MidiService extends ServiceBundle<MidiInputServiceConfig, MidiInputService
4852 logger . info ( `Checking device name "${ config . device } ".` ) ;
4953
5054 let deviceName : string | null = null ;
51- easymidi . getInputs ( ) . forEach ( ( device ) => {
52- if ( device . includes ( config . device ) && deviceName === null ) {
53- deviceName = device ;
54- }
55- } ) ;
55+ const allDevices = easymidi . getInputs ( ) ;
56+ if ( allDevices . includes ( config . device ) ) {
57+ // If we have a device with the correct name we use that device.
58+ deviceName = config . device ;
59+ } else {
60+ // Otherwise we find a device which contains the pattern.
61+ easymidi . getOutputs ( ) . forEach ( ( device ) => {
62+ if ( device . includes ( config . device ) && deviceName === null ) {
63+ deviceName = device ;
64+ }
65+ } ) ;
66+ }
5667
5768 logger . info ( `Connecting to MIDI input device ${ deviceName } .` ) ;
5869 if ( deviceName !== null ) {
0 commit comments