PlaySound Plays Short of Full Wav file

Just starting out? Need help? Post your questions and find answers here.
Randy Walker
Addict
Addict
Posts: 1064
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

PlaySound Plays Short of Full Wav file

Post by Randy Walker »

When I run this code I get a short grunt instead of full enunciation of the word "Copied". If I try it again immediately it will play the full wav file. If I wait 10 seconds and try again, it just gives me another grunt. What am I missing?

Code: Select all

 InitSound()           ; Initialize Sound system
 ;UseOGGSoundDecoder()  ; Use ogg files

 ; Loads a sound from a file
 LoadSound(0,"C:\XferTemp\Copied.wav")
 ; Play sound
 If IsSound(0)
   PlaySound(0,0,100)
 EndIf
 Delay(1000)
 FreeSound(0) ; The sound is freed
 End
This is my wav file: https://www.hostize.com/v/DOkNyNmb7z
For some strange reason this site appends ".wav" onto the end of my wav file so you would need to adjust for that, along with whatever folder path you save it to.
NOTE: I used the free "PDFX.exe" from the window store to read the word "Copied" while recording it using the free "WavePad Audio Editor".
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PlaySound Plays Short of Full Wav file

Post by infratec »

Played here always immediately and complete: Win10 x64 PB 6.21 x86 asm backend.

Code: Select all

InitSound()

If LoadSound(0,"Copied.wav")
  PlaySound(0,0,100)
  Delay(1000)
  FreeSound(0) ; The sound is freed
EndIf
pjay
Enthusiast
Enthusiast
Posts: 253
Joined: Thu Mar 30, 2006 11:14 am

Re: PlaySound Plays Short of Full Wav file

Post by pjay »

I had similar problems a couple of years ago, but for me the issue manifested itself as playback latency (& program stalling) rather than clipping.

If no sounds are played for ~15 seconds then I would get up to 500ms latency / stall on my PC.

Code: Select all

; Playsound() latency bug - usually seen after ~15 seconds of sound inactivity. Win x64 pb6.11
InitSound() : OpenConsole()
LoadSound(0, #PB_Compiler_Home + "examples\Sources - Advanced\Waponez II\Data\Lazer.wav")

Countdown_Set = 5 : Countdown_Increase = 5 : Countdown = Countdown_Set
Print("Playing after 5...")
Repeat
  Delay(1000) : Countdown - 1 : Print(Str(Countdown) + ", ")
  If Countdown <= 0
    soundTime = ElapsedMilliseconds() : 
    PlaySound(0)
    soundTime = ElapsedMilliseconds() - soundTime
    PrintN("") : PrintN("After a wait of " + Str(Countdown_Set) + " seconds, PlaySound() had latency of: "+Str(soundTime) + " ms.")
    If soundTime > 150 : PrintN("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^") : Input() : End : EndIf
    Countdown_Set + Countdown_Increase : If Countdown_Set > 20 : Countdown_Set = 20 : EndIf
    Print("Playing again after delay of " + Countdown_Set + "... ") 
    Countdown = Countdown_Set
  EndIf
ForEver

I worked around it by silently playing a sound every few seconds.
Randy Walker
Addict
Addict
Posts: 1064
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: PlaySound Plays Short of Full Wav file

Post by Randy Walker »

infratec wrote: Mon Sep 08, 2025 8:59 pm Played here always immediately and complete: Win10 x64 PB 6.21 x86 asm backend.

Code: Select all

InitSound()

If LoadSound(0,"Copied.wav")
  PlaySound(0,0,100)
  Delay(1000)
  FreeSound(0) ; The sound is freed
EndIf
I was testing in PBv6.21 also but Windows 11 so maybe a difference there. After applying a small modification based on Pjay's comment I got an acceptable result:

Code: Select all

InitSound()

If LoadSound(0,"C:\XferTemp\Copied.wav")
  PlaySound(0,0,0) ;Added this line
  Delay(1000) ;And added this line
  PlaySound(0,0,100)
  Delay(1000)
  FreeSound(0) ; The sound is freed
EndIf
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Post Reply