vb.netwinapimidimcimcisendstring

Opening shareable Midi synth device


this is my first post here, so I hope I won't screw it up.

I am creating a twink of guitar hero as a console app in vb.net (homework) and I have ran into some problems. I am using CarlsMidiTools to pass parsed note values to synth. But I also want to play accompaniment music at the background. Problem is, that I can only get working one of these at the same time, probably because I am unable to get my synth (standard MS GS wavetable) to get into shareable mode. I also have secondary synth and if I pass notes to primary one and accompaniment music to the secondary I can get it working. But it is wrong solution and requires downloading secondary synth for potential user.

I have found this line using "shareable" which is used in code for opening cd drives but it doesn't work for sequencer I guess.

mciSendString("open cdaudio alias cd wait shareable", 0, 0, 0) 

TL;DR / My question is - how do I get my default synth into shareable mode, so it can listen to carlsMidiTools lib commands as well as to mciSendString play option?

Module Module1
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

Dim Inst As New CarlsMidiTools.Instrument
Sub Main()
    'open midi
    mciSendString("open ..\..\..\..\The_Unforgiven\guitar.mid type sequencer alias midi", 0, 0, 0)
    'Play the midi
    mciSendString("play midi", 0, 0, 0)
    'set synth for CarlsMidiTools
    Inst.OutputDeviceName = "Microsoft GS Wavetable Synth"
    Inst.OutputChannel = 2
    Inst.ChangePatchGM("Violin")
    Inst.Volume = 127
    Inst.NoteDuration = 0
    Inst.Open()
    Inst.PlayNote(70, 127)
    'Incredible awful loop to simulate gameloop in real app and pause the program
    For i As Integer = 0 To 2
        i = i - 1
    Next
End Sub
End Module

This code throws up "Error opening MIDI port - device in use" because I tried to open it again for CarlsMidiTools and then it starts playing the guitar.mid but "Inst.PlayNote(70, 127)" won't get the chance to say a word.


Solution

  • I am not sure about the standard MS GS wavetable but for real MIDI device, this is not possible on Windows.

    The MIDI drivers is not multi-client, it can not be opened by:

    If you are in the second case the solution is to share the device handle between the objects that are needing it.