Sugestions or Comments about my Sound Tutorial - Page 5-VBForums
Page 5 of 5 FirstFirst ... 2345
Results 161 to 172 of 172

Thread: Sugestions or Comments about my Sound Tutorial

  1. #161
    Fanatic Member
    Join Date
    Mar 2009

    Re: Sugestions or Comments about my Sound Tutorial

    the following code gets the sound at 150 miliseconds intervals and it is still not good enough
    I'd totaly use MS speech reco but 3 minuses :
    when correcting spelling mistakes I can't use some letters (M, S)
    it uses win 7 perhapse it can be worked in xp ?
    it doesn't work with image capture from a webcam

    vb Code:
    1. [CODE]Imports System
    2. Imports System.Collections
    3. Imports System.ComponentModel
    4. Imports System.Drawing
    5. Imports System.Windows.Forms
    6. Imports Microsoft.DirectX.DirectSound
    7. Imports System.Threading
    8. Imports System.Collections.Specialized
    9. Public Class Sound_Card_Form
    10. Private Sub StartButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FindButton.Click
    11. 'Dim MyVU As New VolumeMeter
    12. 'MyVU.Start()
    13. Start()
    14. End Sub
    15. Private Sub FindButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FindButton.Click
    16. 'Dim MyVU As New VolumeMeter
    17. 'MyVU.FindDevices()
    18. FindDevices()
    19. End Sub
    21. ' Public Class VolumeMeter
    22. 'Inherits System.Windows.Forms.UserControl
    23. 'Public Delegate Sub VolumeChangedEventHandler(ByVal vcea As VolumeChangedEventArgs)
    24. 'Public Event VolumeChanged As VolumeChangedEventHandler
    25. Private Const SAMPLES As Integer = 8
    26. Private Shared SAMPLE_FORMAT_ARRAY As Integer() = {SAMPLES, 2, 1}
    27. Public Shared audioDevices As CaptureDevicesCollection
    28. Private Shared m_deviceNames As StringCollection
    29. Private deviceName As String = "(none)"
    30. Private deviceIndex As Integer = -1
    31. Private buffer As Microsoft.DirectX.DirectSound.CaptureBuffer
    32. Private liveVolumeThread As System.Threading.Thread
    33. Private m_sampleDelay As Integer = 100
    34. Private m_frameDelay As Integer = 10
    35. Private m_autoStart As Boolean = True
    36. 'Private components As System.ComponentModel.Container = Nothing
    37. Public Sub FindDevices()
    38. Dim audioDevices As New CaptureDevicesCollection
    39. Dim x As Integer = 0
    40. While x < audioDevices.Count
    41. ComboBox1.Items.Add(audioDevices.Item(x).Description)
    42. x = x + 1
    43. End While
    44. ComboBox1.SelectedIndex = 0
    45. End Sub
    46. Public Sub Start()
    47. [Stop]()
    48. Dim audioDevices As New CaptureDevicesCollection
    49. deviceIndex = ComboBox1.SelectedIndex
    50. If deviceIndex <> -1 Then
    51. ' initialize the capture buffer and start the animation thread
    52. Dim cap As New Capture(audioDevices(deviceIndex).DriverGuid)
    53. Dim desc As New CaptureBufferDescription()
    54. Dim wf As New WaveFormat()
    55. wf.BitsPerSample = 16
    56. wf.SamplesPerSecond = 44100
    57. wf.Channels = 2
    58. wf.BlockAlign = CShort(wf.Channels * wf.BitsPerSample / 8)
    59. wf.AverageBytesPerSecond = wf.BlockAlign * wf.SamplesPerSecond
    60. wf.FormatTag = WaveFormatTag.Pcm
    61. desc.Format = wf
    62. desc.BufferBytes = SAMPLES * wf.BlockAlign
    63. buffer = New Microsoft.DirectX.DirectSound.CaptureBuffer(desc, cap)
    64. buffer.Start(True)
    65. ' Start a seperate thread to read the buffer and update the progress bars
    66. liveVolumeThread = New Thread(AddressOf updateProgress) 'Thread starts at updateProgress
    67. Control.CheckForIllegalCrossThreadCalls = False ' This is needed otherwise the form will not update
    68. liveVolumeThread.Priority = ThreadPriority.Lowest ' Thread works in the background
    69. liveVolumeThread.Start()
    70. End If
    71. End Sub
    72. Public Sub [Stop]()
    73. If liveVolumeThread IsNot Nothing Then
    74. liveVolumeThread.Abort()
    75. liveVolumeThread.Join()
    76. liveVolumeThread = Nothing
    77. End If
    78. If buffer IsNot Nothing Then
    79. If buffer.Capturing Then
    80. buffer.[Stop]()
    81. End If
    82. buffer.Dispose()
    83. buffer = Nothing
    84. End If
    85. End Sub
    87. Public Sub updateProgress()
    88. While True
    89. Dim tempFrameDelay As Integer = m_frameDelay
    90. Dim tempSampleDelay As Integer = m_sampleDelay
    91. Dim samples__1 As Array = buffer.Read(0, GetType(Int16), LockFlag.FromWriteCursor, SAMPLE_FORMAT_ARRAY)
    92. ' for each channel, determine the step size necessary for each iteration
    93. Dim leftGoal As Integer = 0
    94. Dim rightGoal As Integer = 0
    95. ' Sum the 8 samples
    96. For i As Integer = 0 To SAMPLES - 1
    97. leftGoal += CType(samples__1.GetValue(i, 0, 0), Int16)
    98. rightGoal += CType(samples__1.GetValue(i, 1, 0), Int16)
    99. Next
    100. ' Calculate the average of the 8 samples
    101. leftGoal = CInt(Math.Abs(leftGoal \ SAMPLES))
    102. rightGoal = CInt(Math.Abs(rightGoal \ SAMPLES))
    103. Dim range1 As Double = leftGoal - ProgressBar1.Value ' calculates the difference between new and the current progress bar value
    104. Dim range2 As Double = rightGoal - ProgressBar2.Value
    105. ' Assign the exact current value to the progress bar
    106. Dim exactValue1 As Double = ProgressBar1.Value
    107. Dim exactValue2 As Double = ProgressBar2.Value
    108. Dim stepSize1 As Double = range1 / tempSampleDelay * tempFrameDelay
    109. ' Limit the value range to positive values
    110. If Math.Abs(stepSize1) < 0.01 Then
    111. stepSize1 = Math.Sign(range1) * 0.01
    112. End If
    113. Dim absStepSize1 As Double = Math.Abs(stepSize1)
    114. Dim stepSize2 As Double = range2 / tempSampleDelay * tempFrameDelay
    115. If Math.Abs(stepSize2) < 0.01 Then
    116. stepSize2 = Math.Sign(range2) * 0.01
    117. End If
    118. Dim absStepSize2 As Double = Math.Abs(stepSize2)
    119. ' increment/decrement the bars' values until both equal their desired goals,
    120. ' sleeping between iterations
    121. If (ProgressBar1.Value = leftGoal) AndAlso (ProgressBar2.Value = rightGoal) Then
    122. Thread.Sleep(tempSampleDelay)
    123. Else
    124. Do
    125. If ProgressBar1.Value <> leftGoal Then
    126. If absStepSize1 < Math.Abs(leftGoal - ProgressBar1.Value) Then
    127. exactValue1 += stepSize1
    128. ProgressBar1.Value = CInt(Math.Truncate(Math.Round(exactValue1)))
    129. 'This is the real value
    130. 'decibels = 20 * Log10(ProgressBar1.Value/ 32768.0)
    131. Else
    132. ProgressBar1.Value = leftGoal
    133. End If
    134. End If
    135. If ProgressBar2.Value <> rightGoal Then
    136. If absStepSize2 < Math.Abs(rightGoal - ProgressBar2.Value) Then
    137. exactValue2 += stepSize2
    138. ProgressBar2.Value = CInt(Math.Truncate(Math.Round(exactValue2)))
    139. Else
    140. ProgressBar2.Value = rightGoal
    141. End If
    142. End If
    143. Thread.Sleep(tempFrameDelay)
    144. Loop While (ProgressBar1.Value <> leftGoal) OrElse (ProgressBar2.Value <> rightGoal)
    145. End If
    146. End While
    147. End Sub
    149. End Class[/CODE]
    Last edited by moti barski; Aug 23rd, 2011 at 03:11 PM.
    time to amp up the beef

  2. #162
    Hyperactive Member
    Join Date
    Oct 2008

    Re: Sugestions or Comments about my Sound Tutorial

    I read your tutorial at but it is lacking something important, something that most people making video games etc will need to do, and that is playing sounds generated on the fly WITHOUT first storing them in a WAV file. For example this code here generates an array of a sine wave.
    dim mysound(44099) as integer
    for n = 0 to 44099
    next n
    This generates an array of 16bit signed integers that holds a sine wave that if played back at 44100 samples per second will last for 1 second and the tone will be at a frequency of 1000hz.

    I want to then use that array and feed it into a command that will cause it to be played through the sound card at a rate of 44100 samples per second. Your tutorial did NOT cover such, and instead focused ONLY on how to play WAV files, which is actually trivial to do with the Windows Media Player ActiveX control.

    I'm looking for a tutorial for how to feed raw sound data in an array to my sound card, tell my sound card length of the array, the sample rate, bit depth, and number of channels that the sound is supposed to be played at, and the result is I get the desired sound being instantly played as intended, NO INTERMEDIATE STEPS (like saving WAV files) needed.

    Now can I do this in DirectX? Or maybe a Windows API call, or maybe some kind of DLL or OCX file? Any help would be nice, but your tuturoial didn't help me with my endevour. As soon as I saw it talking about WAV files, I knew it wasn't going to help me.

  3. #163

    Thread Starter
    Join Date
    Feb 2002
    Canada, Toronto

    Re: Sugestions or Comments about my Sound Tutorial

    Quote Originally Posted by Ben321 View Post
    ...but your tuturoial didn't help me with my endevour. As soon as I saw it talking about WAV files, I knew it wasn't going to help me.
    It took me this long to reply because of that statement alone...

    The point of the tutorial was to make people understand how it works.

    I did not say anywhere in the article that you have to use wave files, that is just an example. If you understand how to process the sound, then you should be able to modify the examples I have in the article for your needs.

    There is a sample Split Buffer Play in post #4, that should help you for what you need, but I guess you did not get that far in the tutorial before you gave up on it...

    I doesn't look like your trying too hard. Try to figure things out on your own. How do you think I figured out everything? there was no tutorial to help me!

    I can't make a tutorial for every single possible need, and I will not spoon feed you (or anyone).

  4. #164
    Junior Member
    Join Date
    Jun 2012

    Re: Sugestions or Comments about my Sound Tutorial

    Can we play wave file at different frequencies, I meant to say that i have wave file of 240 HZ and i want to play it as 360 HZ,without modifying original file or creating new modified file of the 360 HZ,
    I know i can do this by generating the sine wave and then changing its parameters, but i want to develop educational program on music and timber of sound is very important. so that i can record Base note and then play it as different frequency. Is it possible?? i have put this in the thread in this forum but one person gave me idea by creating new modified file with change in sampling rate and bit rate and then play it. It is OK but the problem is it is not working under the FOR LOOP.
    Can we have only one wave file and then play it as different frequency?

  5. #165

    Thread Starter
    Join Date
    Feb 2002
    Canada, Toronto

    Re: Sugestions or Comments about my Sound Tutorial

    In my sound tutorial, take a look at post #7. Use the function ConvertWave16ReSample to resample your buffer.

    But you have to calculate in Sample Rate rather than frequency. You are not saying what is the sample rate of your wave file, I'll assume it is 22050Hz (so that I can make an example for you)

    So, let's say your sound is 22050Hz sample rate, and in it you have a sound frequency of 240Hz.

    To get a frequency of 360Hz, then you have to do some math: 240 / 360 = 0.666666

    So, if your sample rate is 22050Hz then you will have to resample it at 22050 * 0.666666 = 14700 Hz

    Now convert your buffer like this:
    ResultBuffer = ConvertWave16ReSample(Your_Buffer, 22050, 14700, False)
    (I also assume your wave file is Mono)

    Of you can do the calculations when you call the function, for example:
    ResultBuffer = ConvertWave16ReSample(Your_Buffer, 22050, 22050 * (240 / 360), False)

    This will result in compressing the sound to a lower sample rate, but when you play it at original sample rate (22050Hz), the sound (frequency) will play faster, at 360Hz.
    Last edited by CVMichael; Jun 21st, 2012 at 09:22 AM.

  6. #166
    Junior Member
    Join Date
    Jun 2012

    Re: Sugestions or Comments about my Sound Tutorial

    I Did the same. i first copy the code for the playing the file with DirectX7 and then put a command button to play it. Then as per your instructions i Copied the function if the converwave16Resample function and passed the arguments to this function like this

    resultbuffer = ConvertWave16ReSample(DSSecBuffer, 22500, 14700, False)

    which gives me the error to type mismatch:array or user defined type should be used for DSSecBuffer

    Can you help me?

  7. #167
    Junior Member
    Join Date
    Jun 2012

    Re: Sugestions or Comments about my Sound Tutorial

    Here is the total code running

    Private DX As New DirectX7
    Private DSEnum As DirectSoundEnum
    Private DIS As DirectSound

    Private DSSecBuffer As DirectSoundBuffer

    Private Sub Form_Load()

    ' get enumeration object
    Set DSEnum = DX.GetDSEnum

    ' select the first sound device, and create the Direct Sound object
    Set DIS = DX.DirectSoundCreate(DSEnum.GetGuid(1))

    ' Set the Cooperative Level to normal
    DIS.SetCooperativeLevel Me.hWnd, DSSCL_NORMAL

    ' load the wave file, and create the buffer for it
    Set DSSecBuffer = DIS.CreateSoundBufferFromFile("C:\WINDOWS\Media\tada.wav", BuffDesc, WaveFormat)

    resultbuffer = ConvertWave16ReSample(DSSecBuffer, 22500, 14700, False)

    resultbuffer.Play DSBPLAY_DEFAULT

    End Sub

    Public Function ConvertWave16ReSample(Buff() As Integer, ByVal FromSample As Long, ByVal ToSample As Long, ByVal Stereo As Boolean) As Integer()
    Dim K As Long, Lx As Long, RX As Long
    Dim Ret() As Integer, Per As Double, NewSize As Long

    If Not Stereo Then
    NewSize = Fix((UBound(Buff) + 1) * ToSample / FromSample + 0.5)
    ReDim Ret(NewSize - 1)

    For K = 0 To UBound(Ret) - 1
    Per = K / UBound(Ret)

    Lx = Fix(UBound(Buff) * Per)

    Ret(K) = FindYForX(UBound(Buff) * Per, Lx, Buff(Lx), Lx + 1, Buff(Lx + 1))
    Next K

    Ret(UBound(Ret)) = Buff(UBound(Buff))
    NewSize = Fix((UBound(Buff) + 1) * ToSample / FromSample + 0.5)
    NewSize = NewSize - (NewSize Mod 2)
    ReDim Ret(NewSize - 1)

    For K = 0 To UBound(Ret) Step 2
    Per = K / (UBound(Ret) + 2)

    ' Left channel
    Lx = Fix(UBound(Buff) * Per / 2#) * 2
    Ret(K + 0) = FindYForX(UBound(Buff) * Per, Lx, Buff(Lx), Lx + 2, Buff(Lx + 2))

    ' Right channel
    RX = Lx + 1
    Ret(K + 1) = FindYForX(UBound(Buff) * Per + 1, RX, Buff(RX), RX + 2, Buff(RX + 2))
    Next K

    Ret(UBound(Ret) - 1) = Buff(UBound(Buff) - 1)
    Ret(UBound(Ret)) = Buff(UBound(Buff))
    End If

    ConvertWave16ReSample = Ret
    End Function
    Last edited by jignesh142; Jun 23rd, 2012 at 06:12 AM.

  8. #168
    Junior Member
    Join Date
    Jun 2012

    Re: Sugestions or Comments about my Sound Tutorial

    I request CVMichael to help me as i have limited knowledge about this. I think you can only help me to complete my educational software on music.

  9. #169

    Thread Starter
    Join Date
    Feb 2002
    Canada, Toronto

    Re: Sugestions or Comments about my Sound Tutorial

    After this long, you still did not figure out that you cannot pass an object to a function that expects an array?

    If you cannot fallow my tutorial, and if you cannot figure out something so simple, that means you need a LOT of help, and I don't have time for that. I gave you enough clues, you just have to try harder, or put the project asside until you know more programming.

  10. #170
    Junior Member
    Join Date
    Jun 2012

    Re: Sugestions or Comments about my Sound Tutorial

    It is true that i have very little knowledge of the VB as i am not professional programmer.I don't expect the explanations of the each term or logic in the reply as your time is very Important, what i need your help is to modify the above code with the correct code, if you can do so then it will be great help for me and i believe that it will not spend your much time.

  11. #171
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Bristol, UK

    Re: Sugestions or Comments about my Sound Tutorial

    If you want somebody to write code for you and make sure it works, then that can easily take lots of time (explaining a few things to you generally takes much less effort).

    While somebody may be able to do it faster than you, it doesn't mean it will only take them a few seconds - it wouldn't be surprising if it took at least an hour.

    It is unfair to ask a complete stranger to do that scale of favour for you, especially as there is a very slim chance that you will do anything for them in return, and you are very likely to expect the same kind of "help" again in future. In addition to that, a large percentage of people who ask for help on the forum can be helped in about a minute or two, so doing something for you would mean ignoring lots of other people who want help too.

    If you want the work done for you, it is unreasonable to expect it for free (there are several sites where you can hire people for that kind of thing). If you want it for free, you are almost certainly going to have to do almost everything yourself, and only get help in terms of relatively quick answers (post #165 above is better than you should expect).

  12. #172
    Junior Member
    Join Date
    Jun 2012

    Re: Sugestions or Comments about my Sound Tutorial

    Finally i have made my software regarding the music.

    It is better to generate the different frequency sounds then to change the frequency of the particular sound. It is easy to generate the sine wave sound with the different frequencies.

    Thank you to all to help me , Specially CVMichel...

Page 5 of 5 FirstFirst ... 2345

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width

Survey posted by VBForums.