How to play a changing sound based on input value.

Posted on 2005-03-29
Medium Priority
Last Modified: 2011-09-20
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.
Question by:xiaominghng
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
LVL 27

Expert Comment

ID: 13654226
Please re-explain what you are trying to do with input/output for sound.
LVL 12

Expert Comment

ID: 13654257
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
LVL 12

Expert Comment

ID: 13654263
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)
Industry Leaders: 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!


Author Comment

ID: 13655641
 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)

LVL 12

Expert Comment

ID: 13655682
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
LVL 27

Expert Comment

ID: 13656115
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.

Author Comment

ID: 13692054

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

Accepted Solution

planocz earned 1500 total points
ID: 13697481

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question