Member 11287123 Ответов: 2

Как я могу использовать различные инструменты и громкость для winmm.dll в VB.NET


Я запрограммировал его vb.net чтобы играть музыкальные ноты из кода. Он использует winmm.dll но я хочу иметь возможность выбирать разные инструменты и менять громкость.
Вот это самое vb.net код:
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports VB = Microsoft.VisualBasic
Public Class Form1
    Protected Declare Auto Function midiOutOpen Lib "winmm.dll" (ByRef lphMidiOut As IntPtr, udeviceId As Integer, dwCallback As Integer, dwInstance As Integer, dwFlags As Integer) As Integer
    Protected Declare Auto Function midiOutClose Lib "winmm.dll" (hMidiOut As IntPtr) As Integer
    Protected Declare Auto Function midiOutShortMsg Lib "winmm.dll" (hMidiOut As IntPtr, dwMsg As Integer) As Integer
    Protected Const MMSYSERR_NOERROR As Integer = 0
    Public Const CALLBACK_FUNCTION As Integer = &H30000
    Public Const NOTES_ON = &H90
    Public Const MIDI_MAPPER = (-1)
    Dim y As Integer, x As Integer
    Dim highint As Integer, MidiMessage As Integer, lowint As Integer
    Dim Note1Play(200) As Integer, Length1_Note(200) As Single
    Dim Note2Play(200) As Integer, Length2_Note(200) As Single
    Dim Note3Play(200) As Integer, Length3_Note(200) As Single
    Dim Note4Play(200) As Integer, Length4_Note(200) As Single
    Dim midiOpenError As Long
    Dim midiMessageOut As Long
    Dim midiData2 As Integer
    Dim hMidiOut As IntPtr
    Dim NUMBEROFNOTES As Integer
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Static start As Single
        midiOpenError = midiOutOpen(hMidiOut, MIDI_MAPPER, 0, 0, 0)
        midiMessageOut = NOTES_ON
        midiData2 = 127

        Happy_Birthday()
        'Silent_Night
        'Star_Wars

        For y = 1 To NUMBEROFNOTES

            lowint = (Note1Play(y) * 256) + &H90
            PlayVoice1()
            lowint = (Note2Play(y) * 256) + &H94
            PlayVoice1()
            lowint = (Note3Play(y) * 256) + &H96
            PlayVoice1()
            lowint = (Note4Play(y) * 256) + &H98
            PlayVoice1()
            start = VB.Timer()
            Do While VB.Timer() < start + Length1_Note(y) - 0.2
                System.Windows.Forms.Application.DoEvents()
            Loop
        Next y
        Application.Exit()
    End Sub

    Private Sub PlayVoice1()
        highint = (midiData2 * 256) * 256
        MidiMessage = lowint + highint
        x = midiOutShortMsg(hMidiOut, MidiMessage)
    End Sub

    Private Sub Happy_Birthday()
        NUMBEROFNOTES = 26
        'DDEDGF#        DDEDAF#        DDD^ BGFECCBGAG
        'B4 = 71:        A4s = 70:        A4 = 69:        G4s = 68:        G4 = 67:
        'F4s = 66:        F4 = 65:        E4 = 64:        D4s = 63:        D4 = 62
        ' C4s = 61:        C4 = 60

 '******************** Data for Happy Birthday VOICE ONE **************************************
        For y = 1 To 12                 'G   G   A   G  >C  <B   G   G   A   G   D   C
            Note1Play(y) = Choose(y, 55, 55, 57, 55, 60, 59, 55, 55, 57, 55, 62, 60)
            Length1_Note(y) = Choose(y, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82)
        Next y
        For y = 13 To 24                 'G   G  >G   E   C   C   B   A   F   F   E  C
         Note1Play(y) = Choose(y - 12, 55, 55, 67, 64, 60, 60, 59, 57, 65, 65, 64, 60)
         Length1_Note(y) = Choose(y - 12, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91)
        Next y
        For y = 25 To 26               '  D    C
            Note1Play(y) = Choose(y - 24, 62, 60)
            Length1_Note(y) = Choose(y - 24, 0.91, 1.82)
        Next y
        '******************** Data for Happy Birthday VOICE Two **************************************

        For y = 1 To 12              'F   F   E   E   E   F   F   F   F   F   B  G
            Note2Play(y) = Choose(y, 53, 53, 52, 52, 52, 53, 53, 53, 53, 53, 59, 55)
            Length2_Note(y) = Choose(y, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82)
        Next y
        For y = 13 To 24             'E   E  >C   C  <G  <G   F   F  >C   C   C  <G
         Note2Play(y) = Choose(y - 12, 52, 52, 60, 60, 55, 55, 53, 53, 60, 60, 60, 55)
         Length2_Note(y) = Choose(y - 12, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91)
        Next y
        For y = 25 To 26                '  B   G
            Note2Play(y) = Choose(y - 24, 59, 55)
            Length2_Note(y) = Choose(y - 24, 0.91, 1.82)
        Next y
    ' '******************** Data for Happy Birthday VOICE Three ************************************

        For y = 1 To 12              'B   B  >C   C   E   D  <B   B   B   B   F  E
            Note3Play(y) = Choose(y, 59, 59, 60, 60, 64, 62, 59, 59, 59, 59, 65, 64)
            Length3_Note(y) = Choose(y, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82)
        Next y
        For y = 13 To 24                 'C   C   E   G   E   E   C   C   A   A   G  E
         Note3Play(y) = Choose(y - 12, 60, 60, 64, 67, 64, 64, 60, 60, 69, 69, 67, 64)
         Length3_Note(y) = Choose(y - 12, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91)
        Next y
        For y = 25 To 26
            Note3Play(y) = Choose(y - 24, 65, 64)
            Length3_Note(y) = Choose(y - 24, 0.91, 1.82)
        Next y
        '******************** Data for Happy Birthday VOICE Four *************************************
        For y = 1 To 12              'G   G   G   G   G   G   G   G   G   G   G  C
            Note4Play(y) = Choose(y, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 60)
            Length4_Note(y) = Choose(y, 0.455, 0.455, 0.91, 0.91, 0.91, 0.182, 0.455, 0.455, 0.91, 0.91, 0.91, 0.182)
        Next y
        For y = 13 To 24               'G   G   G   C   C   C   F   F   F   F   G  G
         Note4Play(y) = Choose(y - 12, 55, 55, 55, 60, 60, 60, 53, 53, 53, 53, 55, 55)
         Length4_Note(y) = Choose(y - 12, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91)
        Next y
        For y = 25 To 26                '  G   C
            Note4Play(y) = Choose(y - 24, 55, 60)
            Length4_Note(y) = Choose(y - 24, 0.91, 1.82)
        Next y
    End Sub


Что я уже пробовал:

Я пробовал использовать код другого программиста для использования различных инструментов, но они никогда не работают, поэтому я пробую этот сайт.

2 Ответов

Рейтинг:
2

Member 11287123

Я нашел ответ так что можно использовать много различных инструментов в vb6

Private Declare Function midiOutShortMsg Lib "winmm.dll" (ByVal hMidiOut As Long, ByVal dwMsg As Long) As Long
Private Declare Function midiOutOpen Lib "winmm.dll" (lphMidiOut As Long, ByVal udeviceid As Long, ByVal dwCallback As Long, ByVal dwInstance As Long, ByVal dwFlags As Long) As Long
Private Declare Function midiOutClose Lib "winmm.dll" (ByVal hMidiOut As Long) As Long

Dim Note1Play(200)   As Long, Length1_Note(200)  As Single
Dim NUMBEROFNOTES As Integer
Dim ThisTime
Dim y As Integer
Dim handleMidiOut As Long
Dim ret As Integer
Dim Instrument As Integer

Private Sub Form_Load()
   
    OpenMidiOut (-1) 'device id For midi mapper
    Instrument = 14
    ret = midiOutShortMsg(handleMidiOut, (256 * Instrument) + 192)
   
    Happy_Birthday
    
   For y = 1 To NUMBEROFNOTES
      Call PlayNote(0, Note1Play(y), 127)
      ThisTime = Timer
      Do
      Loop While ThisTime + Length2_Note(y) * 0.7 > Timer
      Call StopNote(0, Note1Play(y), 127)
   Next y
      Call CloseMidiOut       
End
End Sub

Private Function OpenMidiOut(ByVal dev_id As Integer) As Integer
    Call CloseMidiOut
    ret = midiOutOpen(handleMidiOut, dev_id, 0, 0, 0)
    OpenMidiOut = (handleMidiOut <> 0)
End Function

Private Sub CloseMidiOut() ' close any opened midi device
    If handleMidiOut <> 0 Then
        ret = midiOutClose(handleMidiOut)
        handleMidiOut = 0
    End If
End Sub

Private Sub PlayNote(Ch As Integer, ByVal nn As Integer, vel As Integer)
    Call ExecuteMidiOutShortMsg(&H90 + Ch, nn, vel)
End Sub
Private Sub StopNote(Ch As Integer, ByVal nn As Integer, vel As Integer)
  Call ExecuteMidiOutShortMsg(&H80 + Ch, nn, vel)
End Sub

Private Sub ExecuteMidiOutShortMsg(b1 As Integer, b2 As Integer, b3 As Integer)
    ret = midiOutShortMsg(handleMidiOut, PackDword(0, b3, b2, b1))
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Call CloseMidiOut 'unloads the opened Midi Mapper
End Sub

Private Function PackDword(i1 As Integer, i2 As Integer, i3 As Integer, i4 As Integer) As Long
    PackDword = i2 * &H10000 + i3 * &H100 + i4
End Function

Private Sub Happy_Birthday()
NUMBEROFNOTES = 26
  'function "Choose" can only handle up to 12 variables at a time
  For y = 1 To 12                    'G   G   A   G  >C  <B   G   G   A   G   D   C
           Note1Play(y) = Choose(y, 55, 55, 57, 55, 60, 59, 55, 55, 57, 55, 62, 60)
           Length1_Note(y) = Choose(y, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82, 0.455, 0.455, 0.91, 0.91, 0.91, 1.82)
  Next y
  For y = 13 To 24                  'G   G  >G   E   C   C   B   A   F   F   E  C
      Note1Play(y) = Choose(y - 12, 55, 55, 67, 64, 60, 60, 59, 57, 65, 65, 64, 60)
      Length1_Note(y) = Choose(y - 12, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91, 0.455, 0.455, 0.91, 0.91)
  Next y
  For y = 25 To 26               '  D    C
      Note1Play(y) = Choose(y - 24, 62, 60)
      Length1_Note(y) = Choose(y - 24, 0.91, 1.82)
  Next y
End Sub


Рейтинг:
1

Dave Kreskowiak

МММ... В Windows MultiMedia нет никаких инструментов. MIDI предназначен для использования с реальными инструментами с MIDI-интерфейсами в них. Это для таких вещей, как воспроизведение MIDI-файлов на реальных инструментах. (MIDI) цифровой интерфейс музыкального инструмента.

Ни в Windows, ни в .NET Framework нет ничего, что поставляло бы звуки различных инструментов.

Для этого вам придется найти стороннюю библиотеку.


Dave Kreskowiak

Вам придется разместить свои вопросы на форуме под статьей, о которой вы говорите.

Member 11287123

Как мне удалить свой вопрос?