Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have programmed in vb.net to play musical notes from code. It uses winmm.dll but I want to be able to select different instruments and to change the volume.
Here is the vb.net code:
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


What I have tried:

I tried using other programmer's code to use different instruments but they never seem to work so I am trying this site.
Posted
Updated 13-Jul-18 7:21am

Ummm... Windows MultiMedia doesn't have any instruments in it. MIDI is for use with REAL instruments with MIDI interfaces in them. This is for stuff like playing MIDI files on real instruments. (MIDI) Musical Instrument Digital Interface.

Neither Windows, nor the .NET Framework has anything that will supply the sounds of various instruments.

You're going to have to find a 3rd party library for that.
 
Share this answer
 
Comments
Dave Kreskowiak 12-Jul-18 16:56pm    
You're going to have to post your questions in the forum below the article you're talking about.
Member 11287123 12-Jul-18 17:08pm    
How do I delete my question?
I found the answer so it is possible to use many different instruments in 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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900