Sign in to follow this  
Followers 0
jennico

_SoundLoop and Stop Sound Loop example

3 posts in this topic

#1 ·  Posted (edited)

how to make sound loops and stop them again ?

here's my example script:

If StringInStr($cmdlineraw,"???") Then
    #NoTrayIcon
    $cmdlineraw=StringSplit(StringMid($cmdlineraw,StringInStr($cmdlineraw,"???")),":")
    _SoundLoop("c:\windows\media\"&$cmdlineraw[2]&".wav")
    While WinExists("My Sound Mashine Loop Example by jennico")
        Sleep(10)
    WEnd
    Exit
EndIf

Global Const $GUI_ENABLE=64
Global Const $GUI_DISABLE=128

Global $pid[2]

GUICreate("My Sound Mashine Loop Example by jennico")

$loop=GUICtrlCreateButton("Play 'Ding' in a loop",10,50,120,30)
$stop=GUICtrlCreateButton("Stop 'Ding' Loop",150,50,120,30)
    GUICtrlSetState(-1,$GUI_DISABLE)
$tloop=GUICtrlCreateButton("Play 'Tada' in a loop",10,100,120,30)
$tstop=GUICtrlCreateButton("Stop 'Tada' Loop",150,100,120,30)
    GUICtrlSetState(-1,$GUI_DISABLE)
$ding=GUICtrlCreateButton("Play 'Ding' only once",10,150,120,30)
$tada=GUICtrlCreateButton("Play 'Tada' only once",10,200,120,30)

SoundSetWaveVolume(100)
GUISetState()

Do
    $msg=GUIGetMsg()
    If $msg=$tada Then
        DllCall("winmm.dll","int","mciSendStringA","str","play "&@WindowsDir&"\Media\tada.wav","str","","int",65534,"hwnd",0); same as _SoundPlay()
        ToolTip('in this way you can mix sounds !')
    EndIf
    If $msg=$ding Then
        DllCall("winmm.dll","int","mciSendStringA","str","play "&@WindowsDir&"\Media\ding.wav","str","","int",65534,"hwnd",0); same as _SoundPlay()
        ToolTip('in this way you can mix sounds !')
    EndIf
    If $msg=$loop Then
        If @Compiled Then
            $pid[0]=Run(@ScriptFullPath&" ???:ding")
        Else
            $pid[0]=Run(@AutoItExe&' "'&@ScriptFullPath&'" ???:ding')
        EndIf
        GUICtrlSetState($stop,$GUI_ENABLE)
        GUICtrlSetState($loop,$GUI_DISABLE)
        ToolTip('ding, ding, ding...')
    EndIf
    If $msg=$stop Then
        ProcessClose($pid[0])
        GUICtrlSetState($stop,$GUI_DISABLE)
        GUICtrlSetState($loop,$GUI_ENABLE)
        ToolTip('Silence!')
    EndIf
    If $msg=$tloop Then
        If @Compiled Then
            $pid[1]=Run(@ScriptFullPath&" ???:tada")
        Else
            $pid[1]=Run(@AutoItExe&' "'&@ScriptFullPath&'" ???:tada')
        EndIf
        GUICtrlSetState($tstop,$GUI_ENABLE)
        GUICtrlSetState($tloop,$GUI_DISABLE)
        ToolTip('tada, tada, tada...')
    EndIf
    If $msg=$tstop Then
        ProcessClose($pid[1])
        GUICtrlSetState($tstop,$GUI_DISABLE)
        GUICtrlSetState($tloop,$GUI_ENABLE)
        ToolTip('Silence!')
    EndIf
Until $msg=-3


Func _SoundLoop($sSoundFile)
    Local Const $SND_ALIAS = 0x10000
    Local Const $SND_ALIAS_ID = 0x110000
    Local Const $SND_APPLICATION = 0x80
    Local Const $SND_ASYNC = 0x1
    Local Const $SND_FILENAME = 0x20000
    Local Const $SND_LOOP = 0x8
    Local Const $SND_MEMORY = 0x4
    Local Const $SND_NODEFAULT = 0x2
    Local Const $SND_NOSTOP = 0x10
    Local Const $SND_NOWAIT = 0x2000
    Local Const $SND_PURGE = 0x40
    Local Const $SND_RESOURCE = 0x40004
    Local Const $SND_SYNC = 0x0
    DllCall('winmm.dll', 'int', 'PlaySoundA', 'str', $sSoundFile, 'int', 0, 'int', BitOR($SND_ASYNC, $SND_FILENAME, $SND_LOOP))
EndFunc

could be very interesting for game scripting....

have fun

j.

edit: forgot to say: you can pass lots of parameters by commandline !

Edited by jennico

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Share this post


Link to post
Share on other sites



here's my example script:

Script works ok for .WAV files. Personally I was looking for a way to loop a MIDI file. After hours of trying to pass the repeat parameter to winmm.dll, I finally figured out another way, which is so simple! Create an infinite while loop (by using a numeric loop) and make the call. Surprisingly it handles these frequent calls very well, no errors.. just a nice repeat of your MIDI (probably works with other audio types too).

$MIDFile =  "C:\WINDOWS\Media\flourish.mid"
While 1
    DllCall('winmm.dll', 'int', 'mciExecute', 'str', "play " & $MIDFile)
WEnd

Share this post


Link to post
Share on other sites

$MIDFile =  "C:\WINDOWS\Media\flourish.mid"
While 1
    DllCall('winmm.dll', 'int', 'mciExecute', 'str', "play " & $MIDFile)
WEnd

Faire:

I too need a way to loop midi files. I've created a bunch of midi drum loops: some 1-bar intros, 4-bar phrases, and some endings, in various styles. Now I'd like to use my computer as a drum machine so I can play the drum tracks via midi along with my electric piano.

I'm envisioning of some kind of app that would allow me to select my "style" set and a tempo. Then I could tap "Z" to start playing the intro, "X" for the phrase variation 1, "C" for variation 2, "V" for an ending, etc. Each selection would wait until the previously selected loop was done. If I didn't select anything, the loop would repeat indefinitely.

I thought your looping idea might be a way to handle it, but there is about a half-second pause in between each loop in your example. Plus I'd need a way to queue up different files in order in real time. Any other ideas on how to play midis like this without a pause?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0