Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / May 2007



Tip: Looking for answers? Try searching our database.

Play wave audio from an array buffer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Harvey Triana - 28 May 2007 20:14 GMT
Hi-

Does anybody know where I can find information on how to play wave audio
from an array buffer!?

Thanks,
<Harvey Triana />
Schmidt - 28 May 2007 20:40 GMT
> Hi-
>
> Does anybody know where I can find information on how to play wave audio
> from an array buffer!?

Easiest way (with good performance) is with DirectSound.
It enables you, to play multiple Sound-Buffers simultaneously.

Look at this example:
(needs 'DirectX8 for Visual Basic Type Library' in Project-References)

'***Into a Form
Option Explicit

Private Type Stereo: L As Integer: R As Integer: End Type

Private dx As DirectX8, DS As DirectSound8, Buffers As New Collection
Private Const Tones& = 25

Private Sub Form_Load()
Dim Freq#, i&
 AutoRedraw = True
 On Error Resume Next
 Set dx = New DirectX8: Set DS = dx.DirectSoundCreate("")
 If Err Then MsgBox "Err opening DSound": Unload Me: Exit Sub
 DS.SetCooperativeLevel hWnd, DSSCL_PRIORITY
 Freq = 440 ' standard pitch a
 For i = 0 To Tones - 1 'prepare DXSoundBuffer-Objects
   SoundBufferAdd "K" & i, Freq
   Freq = Freq * 2 ^ (1 / 12)
 Next i
End Sub

'if mousedown and mouseup on different tone-ranges, the sound
'continues until mouseup on the tone-range again
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x!, Y!)
 SoundBufferPlay "K" & x \ (ScaleWidth / Tones), -2000
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x!, Y!)
 SoundBufferStop "K" & x \ (ScaleWidth / Tones)
End Sub

Private Sub SoundBufferAdd(ByVal Key$, ByVal Freq#)
Dim DsDesc As DSBUFFERDESC, Buf(4800) As Stereo, TwoPI#, x%, Y%, LY%
Dim DsBuffer As DirectSoundSecondaryBuffer8
 SoundBufferRemove Key
 If Freq < 10 Then Freq = 10 Else If Freq > 20000 Then Freq = 20000
 With DsDesc.fxFormat
   .nFormatTag = WAVE_FORMAT_PCM: .nChannels = 2 'Stereo
   .nBitsPerSample = 16: .lSamplesPerSec = 48000
   .nBlockAlign = .nChannels * .nBitsPerSample \ 8
   .lAvgBytesPerSec = .nBlockAlign * .lSamplesPerSec

   TwoPI = 8 * Atn(1): Freq = TwoPI / .lSamplesPerSec * Freq
   For x = 0 To UBound(Buf) 'Create sinus wave inside a Tmp-Buffer
     Y = 32767 * Sin(x * Freq)
     If LY < 0 And Y >= 0 Then Exit For
     LY = Y: Buf(x).L = Y: Buf(x).R = Y
   Next x

   DsDesc.lBufferBytes = x * .nBlockAlign
   DsDesc.lFlags = DSBCAPS_CTRLVOLUME Or DSBCAPS_STATIC
   Set DsBuffer = DS.CreateSoundBuffer(DsDesc)
   DsBuffer.WriteBuffer 0, x * .nBlockAlign, Buf(0).L, DSBLOCK_DEFAULT
   Buffers.Add DsBuffer, Key
 End With
End Sub

'Volume between -10000 (max attenuation) and 0 (no attenuation)
Private Sub SoundBufferPlay(ByVal Key$, Optional ByVal Volume&)
Dim DsBuffer As DirectSoundSecondaryBuffer8
 On Error Resume Next
 Set DsBuffer = Buffers(Key)
 If Err Then Err.Clear: Exit Sub 'Key doesn't exist
 DsBuffer.SetVolume Volume
 If Err Then Caption = Err.Description
 DsBuffer.Play DSBPLAY_LOOPING
End Sub

Private Sub SoundBufferStop(ByVal Key$)
Dim DsBuffer As DirectSoundSecondaryBuffer8
 On Error Resume Next
 Set DsBuffer = Buffers(Key)
 If Err Then Err.Clear: Exit Sub 'Key doesn't exist
 DsBuffer.Stop: DsBuffer.SetCurrentPosition 0
End Sub

Private Sub SoundBufferRemove(ByVal Key$)
 On Error Resume Next
 Buffers.Remove Key
 If Err Then Err.Clear
End Sub

Private Sub Form_Resize()
Dim i&, x#, dx#
 Cls
 dx = ScaleWidth / Tones
 For i = 1 To Tones
   x = x + dx: Line (x, 0)-(x, ScaleHeight)
 Next i
 CurrentY = 0: CurrentX = 60: Print "a"
 CurrentY = 0: CurrentX = ScaleWidth \ 2 - 60: Print "a'"
 CurrentY = 0: CurrentX = ScaleWidth - 160: Print "a''"
End Sub

Olaf
Vinchenzo vinç - 28 May 2007 21:35 GMT
> Hi-
>
> Does anybody know where I can find information on how to play wave audio
> from an array buffer!?

   Hello Harvey!,
   did you tried with the 'PlaySound' API function and its SND_MEMORY flag?:
   http://msdn2.microsoft.com/en-us/library/ms712879.aspx

success = PlaySound(theArray(LBound(theArray)), 0&, SND_MEMORY)

       ...you may want to play it asynchronously:
success = PlaySound(theArray(LBound(theArray)), 0&, SND_MEMORY Or SND_ASYNC)

'--------
Private Const SND_MEMORY As Long = &H4
Private Const SND_ASYNC As Long = &H1

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" ( _
 pszSound As Any, _
 ByVal hMod As Long, _
 ByVal fdwSound As Long) As Long
'---------

Signature

   Greetings
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
( ! ) Preceding answers in Google:
   http://groups.google.com/group/microsoft.public.vb.winapi
( i ) Temperance in the forum:
   http://www.microsoft.com/communities/conduct/default.mspx
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Harvey Triana - 28 May 2007 22:09 GMT
Works!:
Call PlaySoundArray(Ding(1), 0&, SND_MEMORY)

' load array
Private Ding() As Byte
'
Private Sub GetDing()
   Dim rf As Long
    rf = FreeFile
   Open <FileName> For Binary As rf
   ReDim Ding(1 To LOF(rf))
   Get #rf, , Ding()
   Close rf
End Sub

Thanks,
<Harvey Triana />

"Harvey Triana" <harveytriana@hotmail.com> escribió en el mensaje
news:e6EOh0VoHHA.4032@TK2MSFTNGP02.phx.gbl...
> Hi-
>
> Does anybody know where I can find information on how to play wave audio
> from an array buffer!?

   Hello Harvey!,
   did you tried with the 'PlaySound' API function and its SND_MEMORY
flag?:
   http://msdn2.microsoft.com/en-us/library/ms712879.aspx

success = PlaySound(theArray(LBound(theArray)), 0&, SND_MEMORY)

       ...you may want to play it asynchronously:
success = PlaySound(theArray(LBound(theArray)), 0&, SND_MEMORY Or SND_ASYNC)

'--------
Private Const SND_MEMORY As Long = &H4
Private Const SND_ASYNC As Long = &H1

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" ( _
 pszSound As Any, _
 ByVal hMod As Long, _
 ByVal fdwSound As Long) As Long
'---------

Signature

   Greetings
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
( ! ) Preceding answers in Google:
   http://groups.google.com/group/microsoft.public.vb.winapi
( i ) Temperance in the forum:
   http://www.microsoft.com/communities/conduct/default.mspx
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Harvey Triana - 28 May 2007 21:48 GMT
Thanks to all.

> Hi-
>
[quoted text clipped - 3 lines]
> Thanks,
> <Harvey Triana />
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.