PDA

Click to See Complete Forum and Search --> : how can you play sounds w/out slowing speed?


seanm
Mar 10th, 2000, 10:14 AM
While trying to find a job in the field I've been trying to keep up on my VB by programming a game, and I have run into a small problem...

I want to be able to have the game play sounds at certain points. Sometimes there will be multiple sounds playing. From the Q&A I've seen that can be taken care of with the Sync/Async switch.

However, running that code seems to stop all the other processes, which isn't exactly what I want.

Is it because I'm running on a system which is too slow, or is there another way to get multiple, event-driven sounds playing at the same time as the code is running?

Thanks.

KENNNY
Mar 11th, 2000, 03:13 AM
what method of playing the sounds are u using.. MCI, Directsound...?
directsound lets you play lots at the same time; i'd recommend it.

seanm
Mar 11th, 2000, 03:23 AM
At the moment I playing with the playsound api, and I'm trying to download the dx7 128 meg file, although my isp shuts me down if i let my machine idle for more than a few minutes, even while downloading, and as a result I'm going to be coddling my system for the next 8 hours. I'd love to say that directsound is the answer for me, but if this puppy craps out one more time, ... I'd rather run the game minus sound, its just not that vital to my happiness.

Mar 11th, 2000, 03:46 AM
You do not need to download the 128Mb package, you could download only the DirectX SDK docs, headers and library files, it's about 7Mb and you'll just be fine. But if you want the samples, you'll have to download the huge package.

go there if you want to download it.

http://www.microsoft.com/downloads/release.asp?ReleaseID=12471

drewski
Mar 11th, 2000, 05:06 AM
Hey could you download that 128MB package using a program that will let you continue you download later? Maybe something like gozilla? I've heard that sometime this won't work for certain file is this one of those cases?

Just a thought,
Drew

Mar 11th, 2000, 05:46 AM
When it's not working, it's because the server doesn't support resuming, Microsoft servers support it. You can use GetRight or Gozilla to download it and if a problem happen just resume your download.

Kayan

KENNNY
Mar 11th, 2000, 06:31 AM
yes, use Getright (it works best for me), but anyway, as Kayan said, you don't really need the samples to learn DX7: just the 5mb docs :)

pardede
Mar 12th, 2000, 01:07 AM
if you're using netscape, they also have a smart downloader called Smart Download (obviously). Check out netscape's site to find it.

drewski
Mar 12th, 2000, 10:19 AM
I've heard of gozilla but not getright or smart download. What is better? and where can i get them?

Happy coding,
Drew

SYAlam
Mar 22nd, 2000, 01:28 AM
You can always use the MMControl:

MMControl1.FileName = "File.extension"
MMControl1.Command = "Open"
MMControl1.Command = "Play"

or you can write a module:

----------------------------------------------
' Functions and constants used to play sounds.
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

' Constant used with sndPlaySound function
Global Const SND_ASYNC = &H1
----------------------------------------------
Now to play sounds:
rc = sndPlaySound(App.Path & "\blah.wav", SND_ASYNC)

ah, but theres more..with DirectSound you can control at least 7 channels, and to do this:

Option Explicit
'------------------------------------------------------------
' WAVEMIX.BAS
' This module contains declarations for all the functions
' in the WaveMix DLL, and provides some higher-level Basic
' functions to make using WaveMix simpler.
'------------------------------------------------------------
Global hWaveMix As Long
Global lpWaveMix() As Long
Global WaveHandle As Long
Global WAVMIX_Quiet As Integer

Global Const WAVEMIX_MAXCHANNELS = 8

Type tChannelInfo
Loops As Long
WaveFile As String
End Type


Type WAVEMIXINFO
wSize As Integer
bVersionMajor As String * 1
bVersionMinor As String * 1
szDate(12) As String
dwFormats As Long
End Type

Type MIXCONFIG
wSize As Integer
dwFlagsLo As Integer
dwFlagsHi As Integer
wChannels As Integer
wSamplingRate As Integer
End Type

Private Type MIXPLAYPARAMS
wSize As Integer
hMixSessionLo As Integer
hMixSessionHi As Integer
iChannelLo As Integer
iChannelHi As Integer
lpMixWaveLo As Integer
lpMixWaveHi As Integer
hWndNotifyLo As Integer
hWndNotifyHi As Integer
dwFlagsLo As Integer
dwFlagsHi As Integer
wLoops As Integer
End Type

Declare Function WaveMixInit Lib "WAVMIX32.DLL" () As Long
Declare Function WaveMixConfigureInit Lib "WAVMIX32.DLL" (lpConfig As MIXCONFIG) As Long
Declare Function WaveMixActivate Lib "WAVMIX32.DLL" (ByVal hMixSession As Long, ByVal fActivate As Integer) As Long
Declare Function WaveMixOpenWave Lib "WAVMIX32.DLL" (ByVal hMixSession As Long, szWaveFilename As Any, ByVal hInst As Long, ByVal dwFlags As Long) As Long
Declare Function WaveMixOpenChannel Lib "WAVMIX32.DLL" (ByVal hMixSession As Long, ByVal iChannel As Long, ByVal dwFlags As Long) As Long
Declare Function WaveMixPlay Lib "WAVMIX32.DLL" (lpMixPlayParams As Any) As Integer
Declare Function WaveMixFlushChannel Lib "WAVMIX32.DLL" (ByVal hMixSession As Long, ByVal iChannel As Integer, ByVal dwFlags As Long) As Integer
Declare Function WaveMixCloseChannel Lib "WAVMIX32.DLL" (ByVal hMixSession As Long, ByVal iChannel As Integer, ByVal dwFlags As Long) As Integer
Declare Function WaveMixFreeWave Lib "WAVMIX32.DLL" (ByVal hMixSession As Long, ByVal lpMixWave As Long) As Integer
Declare Function WaveMixCloseSession Lib "WAVMIX32.DLL" (ByVal hMixSession As Long) As Integer
Declare Sub WaveMixPump Lib "WAVMIX32.DLL" ()
Declare Function WaveMixGetInfo Lib "WAVMIX32.DLL" (lpWaveMixInfo As WAVEMIXINFO) As Integer

Private Function HiWord(ByVal l As Long) As Integer
l = l \ &H10000

HiWord = Val("&H" & Hex$(l))
End Function

Private Function LoWord(ByVal l As Long) As Integer
l = l And &HFFFF&

LoWord = Val("&H" & Hex$(l))
End Function


Function WAVMIX_AddFile(FileName As String) As Integer
'------------------------------------------------------------
' Open a wave file and assign it to the next available
' channel.
'------------------------------------------------------------
Dim wRtn As Long

WAVMIX_AddFile = False
If WAVMIX_Quiet Then Exit Function
If WaveHandle + 1 = WAVEMIX_MAXCHANNELS Then Exit Function

ReDim Preserve lpWaveMix(WaveHandle)
lpWaveMix(WaveHandle) = WaveMixOpenWave(hWaveMix, ByVal FileName, 0, 0)
wRtn = WaveMixOpenChannel(hWaveMix, WaveHandle, 0)
WAVMIX_AddFile = WaveHandle
WaveHandle = WaveHandle + 1
End Function

Sub WAVMIX_SetFile(FileName As String, AChannel As Long)
'------------------------------------------------------------
' Assign a new wave file, FileName, to the specified channel,
' AChannel. If this channel is currently assigned another
' wave file, stop playing the channel and free the active
' wave file.
'------------------------------------------------------------
Dim wRtn As Long

If WAVMIX_Quiet Then Exit Sub

If AChannel > UBound(lpWaveMix) Then
ReDim Preserve lpWaveMix(AChannel)
WaveHandle = AChannel
End If

' If another wave is currently assigned to this
' channel, free it.
If lpWaveMix(AChannel) <> 0 Then
WAVMIX_StopChannel AChannel
wRtn = WaveMixFreeWave(hWaveMix, lpWaveMix(AChannel))
End If

' Open the new wave and assign it to this channel.
lpWaveMix(AChannel) = WaveMixOpenWave(hWaveMix, ByVal FileName, 0, 0)
wRtn = WaveMixOpenChannel(hWaveMix, AChannel, 0)
End Sub


Sub WAVMIX_Close()
'------------------------------------------------------------
' Stop playing all channels and free all wave files, then
' close down this WaveMix session.
'------------------------------------------------------------
Dim wRtn As Long
Dim i As Integer, rc As Integer

If WAVMIX_Quiet Then Exit Sub

If (hWaveMix <> 0) Then
For i = 0 To UBound(lpWaveMix)
If lpWaveMix(i) <> 0 Then
WAVMIX_StopChannel CLng(i)
rc = WaveMixFreeWave(hWaveMix, lpWaveMix(i))
End If
Next
wRtn = WaveMixCloseSession(hWaveMix)
hWaveMix = 0
End If
End Sub

Function WAVMIX_InitMixer() As Integer
'------------------------------------------------------------
' Initialize and activate the WaveMix DLL.
'------------------------------------------------------------
Dim wRtn As Long
Dim config As MIXCONFIG

If WAVMIX_Quiet Then Exit Function

WaveHandle = 0
ReDim lpWaveMix(0)
ChDir App.Path

config.wSize = Len(config)
config.dwFlagsHi = 1
config.dwFlagsLo = 0
'Allow stereo sound
config.wChannels = 2
hWaveMix = WaveMixConfigureInit(config)
wRtn = WaveMixActivate(hWaveMix, True)

If (wRtn <> 0) Then
WAVMIX_InitMixer = False
Call WaveMixCloseSession(hWaveMix)
hWaveMix = 0
Else
WAVMIX_InitMixer = True
End If
End Function

Sub WAVMIX_StopChannel(ByVal ChannelNum As Long)
'------------------------------------------------------------
' Stop playing the specified channel.
'------------------------------------------------------------
Dim rc As Integer

If WAVMIX_Quiet Then Exit Sub
If (hWaveMix = 0) Then Exit Sub

rc = WaveMixFlushChannel(hWaveMix, ChannelNum, 0)
End Sub

Sub WAVMIX_Activate(Activate As Long)
'------------------------------------------------------------
' Activate the WaveMix DLL.
'------------------------------------------------------------
Dim rc As Integer

If WAVMIX_Quiet Then Exit Sub
If (hWaveMix = 0) Then Exit Sub

rc = WaveMixActivate(hWaveMix, Activate)
End Sub

Sub WAVMIX_PlayChannel(ChannelNum As Long, LoopWave As Long)
'------------------------------------------------------------
' Play a specified channel, and indicate whether the sound
' should be looped.
'------------------------------------------------------------
Dim params As MIXPLAYPARAMS
Dim wRtn As Long

If WAVMIX_Quiet Then Exit Sub
If ChannelNum > UBound(lpWaveMix) Then Exit Sub
If (hWaveMix = 0) Then Exit Sub

params.wSize = Len(params)
params.hMixSessionLo = LoWord(hWaveMix)
params.hMixSessionHi = HiWord(hWaveMix)
params.iChannelLo = LoWord(ChannelNum)
params.iChannelHi = HiWord(ChannelNum)
params.lpMixWaveLo = LoWord(lpWaveMix(ChannelNum))
params.lpMixWaveHi = HiWord(lpWaveMix(ChannelNum))
params.hWndNotifyLo = 0
params.hWndNotifyHi = 0
params.dwFlagsHi = 5
params.dwFlagsLo = 0
params.wLoops = LoopWave
wRtn = WaveMixPlay(params)
End Sub

Theres the module for it. Youll need WaveMix.dll i can send it if you ask. To actually play the channel:

Dim AChannel As Long

AChannel = Index
If btnPlay(Index).Caption = "Play" Then
If channel(Index).Loops Then btnPlay(Index).Caption = "Stop"
WAVMIX_PlayChannel AChannel, channel(Index).Loops
Else
btnPlay(Index).Caption = "Play"
WAVMIX_StopChannel AChannel
End If


Wah lah. There we go. If you have any problems, e-mail me the errors and I'll fix it. I'm sure this works, because I use this code all the time.