vb.netaudiosystem-sounds

Is there a way to play notes in VB.net that isn't console.bleep?


I am creating a program for interval recognition and I would really like the sounds outputted to be slightly... nicer than the ones outputted by console.bleep - preferably a piano sound or something like that. This is what I have done with console.bleep so you can get an idea of what I need to do:

Imports System.Threading
Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            IntervalClass.PlayInterval(11, 5)
      End Sub
End Class
Class IntervalClass

      Protected Shared Sub Play(interval() As Note)
            Dim n As Note
            For Each n In interval
                  If n.NoteTone = Tone.REST Then
                        Thread.Sleep(CInt(n.NoteDuration))
                  Else
                        Console.Beep(CInt(n.NoteTone), CInt(n.NoteDuration))
                  End If
            Next n
      End Sub

      Public Shared Sub PlayInterval(ByRef StartingNote As Integer, ByRef EndingNote As Integer)
            Dim Notes() As String = {Tone.C3, Tone.Cs3, Tone.D3, Tone.Ds3, Tone.E3, Tone.F3, Tone.Fs3, Tone.G3, Tone.Gs3, Tone.A3, Tone.As3, Tone.B3, Tone.C4, Tone.Cs4, Tone.D4, Tone.Ds4, Tone.E4, Tone.F4, Tone.Fs4, Tone.G4, Tone.Gs4, Tone.A4, Tone.As4, Tone.B4, Tone.C5, Tone.Cs5, Tone.D5, Tone.Ds5, Tone.E5, Tone.F5, Tone.Fs5, Tone.G5, Tone.Gs5, Tone.A5, Tone.As5, Tone.B5, Tone.C6}
            Dim PlayInterval As Note() = {
            New Note(Notes(StartingNote), Duration.HALF), New Note(Notes(EndingNote), Duration.HALF)}
            Play(PlayInterval)
      End Sub

      Protected Enum Tone
            REST = 0
            C3 = 130.81
            Cs3 = 138.59
            D3 = 146.83
            Ds3 = 155.56
            E3 = 164.81
            F3 = 174.61
            Fs3 = 185
            G3 = 196
            Gs3 = 207.65
            A3 = 220
            As3 = 233.08
            B3 = 246.94
            C4 = 261.63
            Cs4 = 277.18
            D4 = 293.66
            Ds4 = 311.13
            E4 = 329.63
            F4 = 349.23
            Fs4 = 369.99
            G4 = 392
            Gs4 = 415.3
            A4 = 440
            As4 = 466.16
            B4 = 493.88
            C5 = 523.25
            Cs5 = 554.37
            D5 = 587.33
            Ds5 = 622.25
            E5 = 659.25
            F5 = 698.46
            Fs5 = 739.99
            G5 = 783.99
            Gs5 = 830.61
            A5 = 880
            As5 = 932.33
            B5 = 987.77
            C6 = 1046.5
      End Enum

      Protected Enum Duration
            WHOLE = 1600
            HALF = WHOLE / 2
            QUARTER = HALF / 2
            EIGHTH = QUARTER / 2
            SIXTEENTH = EIGHTH / 2
      End Enum
      Protected Structure Note
            Private toneVal As Tone
            Private durVal As Duration

            Public Sub New(frequency As Tone, time As Duration)
                  toneVal = frequency
                  durVal = time
            End Sub

            Public ReadOnly Property NoteTone() As Tone
                  Get
                        Return toneVal
                  End Get
            End Property

            Public ReadOnly Property NoteDuration() As Duration
                  Get
                        Return durVal
                  End Get
            End Property
      End Structure
End Class

Is there any way to get nicer sounds whether with console.bleep or with some other method? (preferably I would also be able to put them in an array as the indexes will be needed to call the right notes) Thanks


Solution

  • If you want to use pre-loaded sounds, then you can leverage the SystemSound class: https://learn.microsoft.com/en-us/dotnet/api/system.media.systemsound

    As far as I'm aware you have access to:

    1. Asterisk
    2. Beep
    3. Exclamation
    4. Hand
    5. Question

    And example would be:

    Dim sounds = {SystemSounds.Asterisk, SystemSounds.Beep, SystemSounds.Exclamation, SystemSounds.Hand, SystemSounds.Question}
    sounds(0).Play() ' play asterisk sound
    

    If you wanted to play a custom file, then you could leverage the SoundPlayer class: https://learn.microsoft.com/en-us/dotnet/api/system.media.soundplayer

    An example would be:

    Dim sounds = {New SoundPlayer("file1.wav"), New SoundPlayer("file2.wav"), New SoundPlayer("file3.wav")}
    sounds(0).Play() ' play file1.wav