Sign in to follow this  
Followers 0

Sound UDF

61 posts in this topic

Posted (edited)

I have made a Sound UDF because AutoIt's built in SoundPlay isn't enough for me. It uses MCI.

UDF:

;===============================================================================
;
; Function Name:   _SoundOpen
; Description::    Opens a sound file for use with other _Sound functions
; Parameter(s):    $hFile - The sound file, $sAlias[optianal] - a name such as sound1,
;       if you do not specify one it is randomly generated
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): string(the sound id) - Success, 0 - Failure
;       @extended <> 0 - open failed, @error = 2 - File doesn't exist,
;       @error = 3 - alias contains whitespace
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundOpen($hFile, $sAlias = "")
 ;Declare variables
 Local $sSnd_id, $iCurrentPos, $iRet
 ;check for file
 If Not FileExists($hFile) Then Return SetError(2, 0, 0)
 ;search for whitespace by character
 For $iCurrentPos = 1 To StringLen($sAlias)
  If StringIsSpace(StringMid($sAlias, $iCurrentPos, 1)) Then Return SetError(3, 0, 0)
 Next
 ;create random alias if one is not supplied
 If $sAlias = "" Then
  $sSnd_id = RandomStr(10)
 Else
  $sSnd_id = $sAlias
 EndIf
 ;open file
 $iRet = mciSendString("open " & FileGetShortName($hFile) & " alias " & $sSnd_id)
 Return SetError(0, $iRet, $sSnd_id)
EndFunc   ;==>_SoundOpen

;===============================================================================
;
; Function Name:   _SoundClose
; Description::    Closes a sound
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 and @error = 1 - Failure
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundClose($sSnd_id)
 If mciSendString("close " & $sSnd_id) = 0 Then
  Return 1
 Else
  Return SetError(1, 0, 0)
 EndIf
EndFunc   ;==>_SoundClose

;===============================================================================
;
; Function Name:   _SoundPlay
; Description::    Plays a sound from the current position (beginning is the default)
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
;       $fWait - If set to 1 the script will wait for the sound to finish before continuing
;       - If set to 0 the script will continue while the sound is playing
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 - Failure
;       @error = 2 - $fWait is invalid, @error = 1 - play failed
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundPlay($sSnd_id, $fWait = 0)
 ;Declare variables
 Local $iRet
 ;validate $fWait
 If $fWait <> 0 And $fWait <> 1 Then Return SetError(2, 0, 0)
 ;if sound has finished, seek to start
 If _SoundPos($sSnd_id, 2) = _SoundLength($sSnd_id, 2) Then mciSendString("seek " & $sSnd_id & " to start")
 ;If $fWait = 1 then pass wait to mci
 If $fWait = 1 Then
  $iRet = mciSendString("play " & $sSnd_id & " wait")
 Else
  $iRet = mciSendString("play " & $sSnd_id)
 EndIf
 ;return
 If $iRet = 0 Then
  Return 1
 Else
  Return SetError(1, 0, 0)
 EndIf
EndFunc   ;==>_SoundPlay

;===============================================================================
;
; Function Name: _SoundStop
; Description::    Stops the sound
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundStop($sSnd_id)
 ;Declare variables
 Local $iRet, $iRet2
 ;seek to start
 $iRet = mciSendString("seek " & $sSnd_id & " to start")
 ;stop
 $iRet2 = mciSendString("stop " & $sSnd_id)
 ;return
 If $iRet = 0 And $iRet2 = 0 Then
  Return 1
 Else
  Return SetError(1, 0, 0)
 EndIf
EndFunc   ;==>_SoundStop

;===============================================================================
;
; Function Name:   _SoundPause
; Description::    Pauses the sound
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundPause($sSnd_id)
 ;Declare variables
 Local $iRet
 ;pause sound
 $iRet = mciSendString("pause " & $sSnd_id)
 ;return
 If $iRet = 0 Then
  Return 1
 Else
  Return SetError(1, 0, 0)
 EndIf
EndFunc   ;==>_SoundPause

;===============================================================================
;
; Function Name:   _SoundResume
; Description::    Resumes the sound after being paused
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundResume($sSnd_id)
 ;Declare variables
 Local $iRet
 ;resume sound
 $iRet = mciSendString("resume " & $sSnd_id)
 ;return
 If $iRet = 0 Then
  Return 1
 Else
  Return SetError(1, 0, 0)
 EndIf
EndFunc   ;==>_SoundResume

;===============================================================================
;
; Function Name:   _SoundLength
; Description::    Returns the length of the sound in the format hh:mm:ss
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file,
;       $iMode = 1 - hh:mm:ss, $iMode = 2 - milliseconds
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): Length of the sound - Success, 0 and @error = 1 - $iMode is invalid
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundLength($sSnd_id, $iMode = 1)
 ;Declare variables
 Local $iSnd_len_ms, $iSnd_len_min, $iSnd_len_hour, $iSnd_len_sec, $sSnd_len_format
 ;validate $iMode
 If $iMode <> 1 And $iMode <> 2 Then Return SetError(1, 0, 0)
 ;tell mci to use time in milliseconds
 mciSendString("set time format miliseconds")
 ;recieve length of sound
 $iSnd_len_ms = mciSendString("status " & $sSnd_id & " length")
 ;assign modified data to variables
 $iSnd_len_min = Int($iSnd_len_ms/60000)
 $iSnd_len_hour = Int($iSnd_len_min/60)
 $iSnd_len_sec = Int(Int($iSnd_len_ms/1000)-($iSnd_len_min*60))
 ;assign formatted data to $sSnd_len_format
 $sSnd_len_format = StringFormat("%02i:%02i:%02i", $iSnd_len_hour, $iSnd_len_min, $iSnd_len_sec)
 ;return correct variable
 If $iMode = 1 Then Return $sSnd_len_format
 If $iMode = 2 Then Return $iSnd_len_ms
EndFunc   ;==>_SoundLength

;===============================================================================
;
; Function Name:   _SoundSeek
; Description::    Seeks the sound to a specified time
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen (must NOT be a file), $iHour, $iMin, $iSec
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundSeek($sSnd_id, $iHour, $iMin, $iSec)
 ;Declare variables
 Local $iMs = 0
 ;prepare mci to recieve time in milliseconds
 mciSendString("set time format miliseconds")
 ;modify the $iHour, $iMin and $iSec parameters to be in milliseconds
 ;and add to $iMs
 $iMs += $iSec*1000
 $iMs += $iMin*60*1000
 $iMs += $iHour*60*60*1000
 ; seek sound to time ($iMs)
 $iRet = mciSendString("seek " & $sSnd_id & " to " & $iMs)
 ;return
 If $iRet = 0 Then
  Return 1
 Else
  Return SetError(1, 0, 0)
 EndIf
EndFunc   ;==>_SoundSeek

;===============================================================================
;
; Function Name:   _SoundStatus
; Description::    All devices can return the "not ready", "paused", "playing", and "stopped" values.
;       Some devices can return the additional "open", "parked", "recording", and "seeking" values.(MSDN)
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): Sound Status
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundStatus($sSnd_id)
 ;return status
 Return mciSendString("status " & $sSnd_id & " mode")
EndFunc   ;==>_SoundStatus

;===============================================================================
;
; Function Name:   _SoundPos
; Description::    Returns the current position of the song
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file,
;       $iMode = 1 - hh:mm:ss, $iMode = 2 - milliseconds
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): Current Position - Success, @error = 1 - $iMode is invalid
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundPos($sSnd_id, $iMode = 1)
 ;Declare variables
 Local $iSnd_pos_ms, $iSnd_pos_min, $iSnd_pos_hour, $iSnd_pos_sec, $sSnd_pos_format
 ;validate $iMode
 If $iMode <> 1 And $iMode <> 2 Then Return SetError(1, 0, 0)
 ;tell mci to use time in milliseconds
 mciSendString("set time format miliseconds")
 ;receive position of sound
 $iSnd_pos_ms = mciSendString("status " & $sSnd_id & " position")
 ;modify data and assign to variables
 $iSnd_pos_min = Int($iSnd_pos_ms/60000)
 $iSnd_pos_hour = Int($iSnd_pos_min/60)
 $iSnd_pos_sec = Int(Int($iSnd_pos_ms/1000)-($iSnd_pos_min*60))
 ;assign formatted data to $sSnd_pos_format
 $sSnd_pos_format = StringFormat("%02i:%02i:%02i",$iSnd_pos_hour, $iSnd_pos_min, $iSnd_pos_sec)
 ;return correct variable
 If $iMode = 1 Then Return $sSnd_pos_format
 If $iMode = 2 Then Return $iSnd_pos_ms
EndFunc   ;==>_SoundPos

;internal functions
Func mciSendString($string)
 Local $iRet
 $iRet = DllCall("winmm.dll", "int", "mciSendStringA", "str", $string, "str", "", "int", 65534, "hwnd", 0)
 If Not @error Then Return $iRet[2]
EndFunc   ;==>mciSendString

Func RandomStr($len)
 Local $string
 For $iCurrentPos = 1 To $len
  $string &= Chr(Random(97, 122, 1))
 Next
 Return $string
EndFunc   ;==>RandomStroÝ÷ ØLZ^jëh×6 



$sound = _SoundOpen("C:\test2.mp3", "test")

If @error = 2 Then
 MsgBox(0, "Error", "The file does not exist")
 Exit
ElseIf @error = 3 Then
 MsgBox(0, "Error", "The alias was invalid")
 Exit
EndIf

MsgBox(0, "Length", "The sound has a length of " & _Soundlength($sound))
_SoundPlay($sound)
Sleep(2000)
MsgBox(0, "Pause", "The sound will now be paused for 2 seconds")
_SoundPause($sound)
Sleep(2000)

MsgBox(0, "Resume", "The sound will be resumed")
_SoundResume($sound)
Sleep(2000)
MsgBox(0, "Seek", "We will seek the song to 1 minute 48 seconds and display the position with _SoundPos")
_SoundSeek($sound, 0, 0, 35)
MsgBox(0, "Position", "The current position is " & _SoundPos($sound) & @CRLF & "We need to play the sound after using _SoundSeek")
_SoundPlay($sound)
MsgBox(0, "Status", "The status of the sound is '" & _SoundStatus($sound) & "'" & @CRLF & "The song will be stopped in 5 seconds")
Sleep(5000)
_SoundStop($sound)
MsgBox(0, "Stop", "The song will be played to show the effect of _SoundStop")
_SoundPlay($sound)
Sleep(3000)
_SoundClose($sound)
MsgBox(0, "Bye", "Goodbye")
Exit

No Longer Any Attachment; Download Latest Beta

Update: Added support for the MustDeclareVars option

Update2: Added repeat option to _SoundPlay

Update3: Commented all functions

Update4: Removed Repeat option for technical reasons

This UDF now comes with AutoIt Beta 3.2.1.2+

Edited by RazerM

Share this post


Link to post
Share on other sites



Posted

nice going razer :whistle:

Share this post


Link to post
Share on other sites

Posted

Thanks, One thing i noted when making this is that _SoundSeek only works if an alias is used for the sound file. Any ideas?

Share this post


Link to post
Share on other sites

Posted

why can i only play the sound once

even if i use then _soundopen again

the same sound do not play

??

do i something wrong? :whistle:

i want to play a sound en check when the sound has stopped then play it again

a sort of loop function

Recards jpam

Share this post


Link to post
Share on other sites

Posted

Download the new UDF(updated)

you only need to open a sound once

#include <Sound.au3>
$sound = _SoundOpen("C:\test2.mp3", "loop")
While 1
    _SoundPlay($sound, 1)
WEnd

Share this post


Link to post
Share on other sites

Posted

Nice RazerM... :whistle:

This will be incredibly useful for a media player I'm working on.

Share this post


Link to post
Share on other sites

Posted

Yeh that's what i thought. Good for media players

Share this post


Link to post
Share on other sites

Posted

#include <Sound.au3>

$sound = _SoundOpen("C:\test2.mp3", "loop")

_SoundPlay($sound, 0)

While 1

If _SoundPos($sound, 2) = _SoundLength($sound, 2) Then _SoundPlay($sound)

;do stuff

WEnd

very nice ideas

Share this post


Link to post
Share on other sites

Posted

@RazerM

thanks RazerM

i try it again with your example

very usefull for my new game

jpam

Share this post


Link to post
Share on other sites

Posted

Thank you a lot for sharing these functions !

Is it possible to record from the microphone ??? It would be so good !

Share this post


Link to post
Share on other sites

Posted

Would this be a good way to implement sound into games? (Playing multiple sounds at once. Fast)

Share this post


Link to post
Share on other sites

Posted

@Manadar

Yes, more than one sound can be played at once.

@Olish

This isn't a UDF for mci commands, however some mci commands are used. I have no intention to implement recording.

Share this post


Link to post
Share on other sites

Posted

I have updated the udf, put FileGetShortName() in _SoundOpen for compatibility with paths that have spaces.

Share this post


Link to post
Share on other sites

Posted

This isn't a UDF for mci commands, however some mci commands are used. I have no intention to implement recording.

Is it possible to use MCI Commands ? Which DLL may I use to implement recording myself ?

Thank you for your help.

Share this post


Link to post
Share on other sites

Posted (edited)

In the udf is the function mciSendString

Func mciSendString($string)
    Local $ret
    $ret = DllCall("winmm.dll","int","mciSendString","str",$string,"str","","int",65534,"hwnd",0)
    If Not @error Then Return $ret[2]
EndFunc   ;==>mciSendString oÝ÷ Úêå

Edited by RazerM

Share this post


Link to post
Share on other sites

Posted

your sound udf works super !!!

thanks RazerM for this very usefull udf

jpam

Share this post


Link to post
Share on other sites

Posted

:whistle: It is PERFECT!!!!!!! Thanks God, something for MP3 Player!

@eynstyne: Media Player aleardy done: by me

i542

Share this post


Link to post
Share on other sites

Posted

Nice job. :whistle: These should be useful for a script i have wanted to write. :)

Share this post


Link to post
Share on other sites

Posted

Thanks for the feedback

Share this post


Link to post
Share on other sites

Posted

I have updated the first page with improvements.

You can now set a sound to loop automatically by setting the 2nd parameter in _SoundPlay to 1

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

  • Recently Browsing   0 members

    No registered users viewing this page.