Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

VB6 - Simple DirectSound interface class to play little sound effects

$
0
0
Here's yet another class to play little sound effects on VB6 apps.
Note that I'm not a programmer, never studied anything, so it's not very professional, but it's simple and very easy to use.

The code allows to load several little wav files from disk or directly from the resources inside the compiled exe or dll, and play them with a single line of code like this:

Code:

Sounds.PlaySound "Explosion1"
Initialize DirectSound with:
Code:

Sounds.IniciarDS(hWnd)
Credits to CVMichael and his awesome tutorial: http://www.vbforums.com/showthread.p...Sound-Tutorial

Download: Attachment 126011

Look for my TicTacToe post to see it working.

Code:

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsDS"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit

Private Sonidos As New Collection          ' Almacena todos los sonidos
Private DX As New DirectX8              ' Un directx
Private DSEnum As DirectSoundEnum8      ' Lista con los dispositivos de sonido, por eso el tipo DirectSoundEnum8
Private DIS As DirectSound8            ' Una vez que enumero, agarro uno.
Private BuffDesc As DSBUFFERDESC        ' Almacena la descripción para el búfer
Dim DSSecBuffer As DirectSoundSecondaryBuffer8  ' Buffer que almacena los datos de audio

Private Sub Class_Initialize()
    'nada por aquí
End Sub

Private Sub Class_Terminate()
    Set Sonidos = Nothing
    Set DIS = Nothing
    Set DSEnum = Nothing
End Sub

Public Function IniciarDS(hWnd As Long) As Integer

    ' 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 hWnd, DSSCL_NORMAL
   
    ' allow frequency changes and volume changes
    BuffDesc.lFlags = DSBCAPS_CTRLVOLUME 'Or DSBCAPS_CTRLFREQUENCY

End Function

Public Sub LoadSoundFromFile(file As String, Optional sSndKey As String)

    Set DSSecBuffer = DIS.CreateSoundBufferFromFile(file, BuffDesc)
   
    If Len(sSndKey) = 0 Then
        Sonidos.Add DSSecBuffer
    Else
        Sonidos.Add DSSecBuffer, sSndKey
    End If

    Set DSSecBuffer = Nothing
   
End Sub
   
Public Sub LoadSoundFromResource(resName As String, Optional sSndKey As String)
    ' Los recursos deben ser del tipo "WAV"
    ' Si no se especifica sSndKey se usa el nombre del recurso como clave
   
    Set DSSecBuffer = DIS.CreateSoundBufferFromResource(vbNullString, resName, BuffDesc)
   
    If Len(sSndKey) = 0 Then
        Sonidos.Add DSSecBuffer, resName
    Else
        Sonidos.Add DSSecBuffer, sSndKey
    End If

    Set DSSecBuffer = Nothing
   
End Sub

Public Sub SetVolume(sSndKey As Variant, vol As Byte)
    ' volume is from 0 to -10,000 (where 0 is the lowdest, and -10,000 is silence)
    ' Pero yo lo uso con un número entre 0 y 255 para que sea más fácil
   
    If vol < 0 Then vol = 0
    If vol > 255 Then vol = 255
    Set DSSecBuffer = Sonidos(sSndKey)
    DSSecBuffer.SetVolume 10000 * (vol / 255 - 1)
    Set DSSecBuffer = Nothing
End Sub

Public Sub PlaySound(sSndKey As Variant)
    Set DSSecBuffer = Sonidos(sSndKey)
    DSSecBuffer.Play DSBPLAY_DEFAULT
    Set DSSecBuffer = Nothing
End Sub


Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>