Seamless Looping of Audio

Get help with using AutoHotkey and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Seamless Looping of Audio

17 Nov 2013, 00:36

With Flash I could loop audio seamlessly. Is this at all possible with AHK? The following code repeats a wav file but there is a noticeable gap before it repeats:

Code: Select all

SetWorkingDir %A_ScriptDir%  	; Ensures a consistent starting directory.

f5::

LoopFile = %A_WorkingDir%\Loopy.wav
mciSendString("open " """" LoopFile """" " type mpegvideo Alias WAV01")
mciSendString("play WAV01 repeat")

return

esc:: exitapp

mciSendString(Command) {
  Return, DllCall("winmm.dll\mciSendString", Str,Command, Str,"", Int,0, Int,0)
}
Is there any way to get a seamless loop? Thanks
User avatar
ErrorOnLine2
Posts: 16
Joined: 31 Oct 2013, 18:54

Re: Seamless Looping of Audio

17 Nov 2013, 04:35

Maybe you can do something with Skan's code posted here.

http://www.autohotkey.com/board/topic/2 ... st-access/

I have a version of it, and if memory serves, it worked for me, but I don't have speakers hooked up to my 8 year old Gateway computer at the moment, so I can't retest it.
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Re: Seamless Looping of Audio

17 Nov 2013, 05:16

Thanks for the link, unfortunately I could get neither version of the code posted by Skan to do anything.
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Re: Seamless Looping of Audio

17 Nov 2013, 08:04

I've discovered that if I use SoundPlay I can get much better results:

Code: Select all

replay:
soundplay, Loopy.wav, wait
goto replay
However, in order to use this I have to put it in a separate ahk file and use run, otherwise the wait pauses my script. This means I have to hard code the sound file. Ideally I'd like to keep it in a function inside my main file. Not sure.
Guest

Re: Seamless Looping of Audio

17 Nov 2013, 09:27

There should be options to hardcoding: For example, changing and reading a plain text or ini file with the name of the sound file, or an "invisible" text control that you can read with the soundplaying script or even some more advanced interscript communication techniques.

P.s.: If you are ok to use an external dll-file, you could use the bass-dll (see "old" forum). It has much better sound playing abilities than 'soundfile', but it's more complicated. Perhaps, it'a a bit overkill, if you just want to loop a file... :geek:
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Re: Seamless Looping of Audio

17 Nov 2013, 16:08

Thanks for the suggestion about bass.dll. I have ended up using a text file but would be very interested to know more about advanced interscript communication techniques. Where might I find out about those?
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Re: Seamless Looping of Audio

17 Nov 2013, 17:25

Further investigation has revealed that the results obtained by using SoundPlay are less seamless than I thought and not good enough. I'll Start looking at Bass.dll but if anyone knows of an alternative I'd be grateful to hear about it. Thanks.
FanaticGuru
Posts: 1361
Joined: 30 Sep 2013, 22:25

Re: Seamless Looping of Audio

18 Nov 2013, 19:46

PuzzledGreatly wrote:Thanks for the link, unfortunately I could get neither version of the code posted by Skan to do anything.
It might have just been the errant color coding the other forum adds.

This Skan script works for me.

Code: Select all

#SingleInstance, Force
SetWorkingDir %A_ScriptDir%  

MySoundFile1 = %SystemRoot%\Media\tada.wav
MySoundFile2 = %SystemRoot%\Media\chimes.wav

FileRead, Sound1, *c %MySoundFile1%
FileRead, Sound2, *c %MySoundFile2%

PlaySound( Sound1 )
PlaySound( Sound2 )

Return

PlaySound( ByRef Sound ) {
 Return DllCall( "winmm.dll\PlaySound" ( A_IsUnicode ? "W" : "A" ), UInt,&Sound, UInt,0
               , UInt, ((SND_MEMORY:=0x4)|(SND_NODEFAULT:=0x2)) )
}


Don't know if it will help with making the loop any smoother but you can try it.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Re: Seamless Looping of Audio

18 Nov 2013, 22:49

Thanks, the code does work but unfortunately doesn't make the looping any smoother.
just me
Posts: 6551
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Seamless Looping of Audio

19 Nov 2013, 01:39

Code: Select all

#NoEnv
Sound := A_WinDir . "\Media\tada.wav"
SoundPlay, %Sound%
Return
; ======================================================================================================================
^!p:: ; start playing
   SoundLoop(Sound)
Return
^!s:: ; stop playing
   SoundLoop("")
Return
Esc:: ; ExitApp
   SoundLoop("")
ExitApp
; ======================================================================================================================
SoundLoop(File := "") {
   ; http://msdn.microsoft.com/en-us/library/dd743680(v=vs.85).aspx
   ; SND_ASYNC       0x00000001  /* play asynchronously */
   ; SND_NODEFAULT   0x00000002  /* silence (!default) if sound not found */
   ; SND_LOOP        0x00000008  /* loop the sound until next sndPlaySound */
   ; SND_NOWAIT      0x00002000  /* don't wait if the driver is busy */
   ; SND_FILENAME    0x00020000  /* name is file name */
   ; --------------- 0x0002200B
   Static AW := A_IsUnicode ? "W" : "A"
   Return DllCall("Winmm.dll\PlaySound" . AW, "Ptr", File = "" ? 0 : &File, "Ptr", 0, "UInt", 0x0002200B)
}
?
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Re: Seamless Looping of Audio

19 Nov 2013, 08:41

Brilliant! That works perfectly. Where did you get the information about the 0x numbers from and how can I learn about them? I've been trying out different ideas for several days now and kept drawing a blank. Thanks very much.
just me
Posts: 6551
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Seamless Looping of Audio

19 Nov 2013, 09:37

The numeric values of Windows constants can be found in the C/C++ header files included in the SDK's and/or the free 'express' editions of MS Visual Studio C++: :arrow: MSDN
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Re: Seamless Looping of Audio

20 Nov 2013, 09:58

Thanks for the link, now to try and make sense of the info.

Return to “Ask For Help”

Who is online

Users browsing this forum: boiler, Odlanir, pn4265 and 230 guests