Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

How to play a changing sound based on input value.

Hi there,

I have a probing application that accepts input from a USB device. It requires have a sound effect to indicting the input value. The input number would be 1 to 200. The application is developed in VB.Net. The sound should be from low to high ( pitch?) corresponding to the input value from low to high. I would appreciate any help or any sample code.
  • 3
  • 3
  • 2
1 Solution
Please re-explain what you are trying to do with input/output for sound.
I think they need some sort of method like this:

Sub PlaySound(ByVal Frequency As Integer, ByVal Duration As Single)
    PlayPitchedSound(Frequency, Duration)
End Sub


I've no idea how to implement playing a specific sound at a set frequency/pitch in VB.NET... used to know it in QBasic though :P

... so im interested in knowing the answer to this too
I should say that the PlayPitchedSound should be replace with some API call im guessing... im just writing out some naff pseudo code to maybe give you some idea of what I think they're after (if that makes sense)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

xiaominghngAuthor Commented:
 Below is my code to play the sounds. I split a 4 seconds wav file (which has different pitches from low to high) to 200 wav files. I play each wav file according to the num. But it sounds awful. It is not very smooth. I want to make it sound like the 4 second wav file.

    Public Const SND_ASYNC = &H1 ' play asynchronously
    Public Const SND_LOOP = &H8 ' loop the sound until next sndPlaySound
    Public Const SND_NOSTOP = &H10 ' don't stop any currently playing sound
    Public Const SND_NOWAIT = &H2000 ' don't wait if the driver is busy

    Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer

 PlaySound(Path & "Meter Wave" & CType(num, String) & ".wav", 0, SND_ASYNC Or SND_LOOP)

I would have thought there would be some API somewhere which works like the sound command (i forget the actual name) that was in QBasic... you could give a frequency in Hz... and the duration, and it would play that "note" for that set duration.

In QBasic, that gave you a continuous fluid note rather than a "joint" note... and obviously gave you more choice over the notes.

Hopefully someone knows of a similar API or command that can be brought into VB.NET
I think you will have to make(record) a wave that will have the sounds that you need. then call playsound to read only that small part of the wave file. I do that with a number grid program to read numbers.
xiaominghngAuthor Commented:

Can you tell me how to read only small part of a wave file in VB .NET? Thanks

Public Class frmPlayApart
    Inherits System.Windows.Forms.Form
    Private Declare Function mciGetErrorString Lib "MMSYSTEM.DLL" (ByVal errornum As Integer, ByVal ErrorString As String, ByVal errorsize As Short) As Short
    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
    Private voicefile As String = "c:\waves\elvis.wav"  '<---Wave file used for sound

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents rbn1 As System.Windows.Forms.RadioButton
    Friend WithEvents rbn2 As System.Windows.Forms.RadioButton
    Friend WithEvents rbn3 As System.Windows.Forms.RadioButton
    Friend WithEvents rbn4 As System.Windows.Forms.RadioButton
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.rbn4 = New System.Windows.Forms.RadioButton
        Me.rbn3 = New System.Windows.Forms.RadioButton
        Me.rbn2 = New System.Windows.Forms.RadioButton
        Me.rbn1 = New System.Windows.Forms.RadioButton
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.GroupBox1.Location = New System.Drawing.Point(24, 20)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(184, 160)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "GroupBox1"
        Me.rbn4.Location = New System.Drawing.Point(12, 108)
        Me.rbn4.Name = "rbn4"
        Me.rbn4.Size = New System.Drawing.Size(116, 24)
        Me.rbn4.TabIndex = 3
        Me.rbn4.Text = "Play 700 to 1000"
        Me.rbn3.Location = New System.Drawing.Point(12, 80)
        Me.rbn3.Name = "rbn3"
        Me.rbn3.TabIndex = 2
        Me.rbn3.Text = "Play 400 to 600"
        Me.rbn2.Location = New System.Drawing.Point(12, 52)
        Me.rbn2.Name = "rbn2"
        Me.rbn2.TabIndex = 1
        Me.rbn2.Text = "Play 100 to 200"
        Me.rbn1.Location = New System.Drawing.Point(12, 20)
        Me.rbn1.Name = "rbn1"
        Me.rbn1.TabIndex = 0
        Me.rbn1.Text = "Play 0 to 100"
        Me.Button1.Location = New System.Drawing.Point(32, 204)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 4
        Me.Button1.Text = "Start"
        Me.Button2.Location = New System.Drawing.Point(136, 204)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 4
        Me.Button2.Text = "End"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(252, 273)
        Me.Name = "Form2"
        Me.Text = "Form2"

    End Sub

#End Region
    Private Function ReadWaveArea(ByVal digit As Short) As Short
        Dim x As Integer

        Select Case digit
            Case 0 : x = mciSendString("play voice from 0 to 100 wait", CStr(0), 0, 0)
            Case 1 : x = mciSendString("play voice from 100 to 200 wait", CStr(0), 0, 0)
            Case 2 : x = mciSendString("play voice from 400 to 600 wait", CStr(0), 0, 0)
            Case 3 : x = mciSendString("play voice from 700 to 1000 wait", CStr(0), 0, 0)
        End Select
    End Function
    Private Sub InitializeWave()
        Dim sCmd As String
        Dim x, r As Integer
        Dim StrLen As Short
        Dim ErrorString As String

        FileOpen(1, voicefile, OpenMode.Binary)
        On Error GoTo 0
        sCmd = "open " & voicefile & " type waveaudio alias voice"
        x = mciSendString(sCmd, CStr(0), 0, 0)
        If x <> 0 Then
            StrLen = 255
            r = mciGetErrorString(x, ErrorString, StrLen)
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim res As Short
        Dim x As Integer

        If rbn1.Checked = True Then
            res = ReadWaveArea(0)
        ElseIf rbn2.Checked = True Then
            res = ReadWaveArea(1)
        ElseIf rbn3.Checked = True Then
            res = ReadWaveArea(2)
        ElseIf rbn4.Checked = True Then
            res = ReadWaveArea(3)
        End If
        x = mciSendString("close voice", CStr(0), 0, 0)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    End Sub
End Class

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now