Alex Simmons
New Member
- Joined
- Dec 3, 2012
- Messages
- 17
I am attempting to precisely synchronise the playing of an audio wav file with the system clock. I need it to reliably start playing in sync with the clock every time, or at least with a predictable and consistent delay. At present the start delay is not consistent, sometimes it's right on cue, while sometimes it has a delay of ~1 second.
It's for a sporting count down timer, hence why having it play on cue is important to me.
The wav file I created (TTstart2.wav) is 10.800 seconds long and provides a 800ms beep at 10 seconds to go, then 250ms beeps at 5, 4, 3, 2, and 1 seconds to go, and then a long 800ms higher frequency beep at zero seconds.
I'm using Excel 2010 VBA.
I've no problem in getting the wav file to play, I just used the PlayWAV code I found online and included below.
The problem is getting it to play right on cue every time.
It does so most of the time, but no matter what way I make the call to play the wav file, occasionally it has a start delay. I've tried all sort of timing loops (my actual code has countdown timers displayed).
It happens once in about every 4 or 5 times it's called to play and I don't understand why or how to fix it, or what an alternative approach might be to sync an audio file with the system clock. Perhaps Excel VBA can't actually manage such things with such timing precision.
This is some code I have to demonstrate the problem (my code where it occurs is embedded in other more complex stopwatch timer arrangement so I replicated the problem in a more simple example).
The problem is that no matter what method I use to call a sub to play the wav file (be it Application.OnTime, or various timing loops, timer delays subs etc), about every 4th or 5th time the wav file is delayed by about a second. All other times it plays right on cue, but for some reason there is an occasional delay and I can't work out why.
Any suggestions?
Thanks!
It's for a sporting count down timer, hence why having it play on cue is important to me.
The wav file I created (TTstart2.wav) is 10.800 seconds long and provides a 800ms beep at 10 seconds to go, then 250ms beeps at 5, 4, 3, 2, and 1 seconds to go, and then a long 800ms higher frequency beep at zero seconds.
I'm using Excel 2010 VBA.
I've no problem in getting the wav file to play, I just used the PlayWAV code I found online and included below.
The problem is getting it to play right on cue every time.
It does so most of the time, but no matter what way I make the call to play the wav file, occasionally it has a start delay. I've tried all sort of timing loops (my actual code has countdown timers displayed).
It happens once in about every 4 or 5 times it's called to play and I don't understand why or how to fix it, or what an alternative approach might be to sync an audio file with the system clock. Perhaps Excel VBA can't actually manage such things with such timing precision.
This is some code I have to demonstrate the problem (my code where it occurs is embedded in other more complex stopwatch timer arrangement so I replicated the problem in a more simple example).
Code:
Option Explicit
Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
'----------------------------------------
Sub PlayWAV()
Dim WAVFile As String
WAVFile = "TTstart2.wav"
' TTstart2.wav is my own audio wav file
WAVFile = ThisWorkbook.Path & "\" & WAVFile
' I've put the wav file in same folder as the workbook
Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
' this plays the wav file in asynchronous mode meaning it does not stop other code
End Sub
'----------------------------------------
Sub repeatplayaudio()
Dim audiostart As Range
Dim i As Integer
Dim H, M, S, playtime As String
' loop through the start times listed in chronological order in column A1:A8
' before running this sub:
' in my sheet I put a start time in cell A1 which is just into the future
' and then have 15 second gaps between each subsequent cell
' e.g. if it's currently 9:09:00AM in cell A1 I put 9:10:00AM
' then cell A2 = A1 + 15/24/3600
' and copy down
For i = 1 To 8
' choose next cell in column A
Set audiostart = Range("A" & i)
' wait until start time arrives
Do While audiostart.Value > Now
Loop
' once start time has arrived then play the audio file
' set the hour, minute and second into a string for the OnTime function
H = Hour(audiostart.Value)
M = Minute(audiostart.Value)
S = Second(audiostart.Value)
playtime = H & ":" & M & ":" & S
Application.OnTime TimeValue(playtime), "PlayWAV"
Next i
End Sub
The problem is that no matter what method I use to call a sub to play the wav file (be it Application.OnTime, or various timing loops, timer delays subs etc), about every 4th or 5th time the wav file is delayed by about a second. All other times it plays right on cue, but for some reason there is an occasional delay and I can't work out why.
Any suggestions?
Thanks!