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.