Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Sound.ahk - Sound functions for use with AutoHotkey


  • This topic is locked This topic is locked
36 replies to this topic
williamsharkey
  • Members
  • 49 posts
  • Last active: Mar 14 2013 06:16 AM
  • Joined: 06 Oct 2007
Thank you Fincs

I will be using this in my Sequencer.

William

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
lets me fileselect and then crashes...
---------------------------
G:\music\3582-T~1\06-MC2~1.MP3
---------------------------
A problem occurred in initializing MCI.
---------------------------
OK   
---------------------------


Trubbleguy
  • Members
  • 122 posts
  • Last active: Jan 15 2017 10:50 AM
  • Joined: 20 Jan 2007
fincs, is there a way to load a second file in the same player without closing it first, i need the media window to stay sized and in the same spot every time i load a new video file, currently i have to close the file which closes the video window and when i open a new file it creates the video window in the middle of my desktop and then i have to move it where i want it and resize. this is driving me insane...

Azerty
  • Members
  • 72 posts
  • Last active: Jan 16 2009 10:08 AM
  • Joined: 19 Dec 2006
thks Fincs, nice work giving a will to do more with it :)

Winkie
  • Members
  • 10 posts
  • Last active:
  • Joined: 11 Apr 2006

Tohhmmss(milli){
min := Floor(milli / (1000 * 60))
hour := Floor(milli / (1000 * 3600))
sec := Floor(Floor(milli/1000) - (min * 60))
Return hour ":" min ":" sec
}


txt := Tohhmmss( "6500000" )
MsgBox, % txt

Result:
---------------------------
Milliseconds tester.ahk
---------------------------
1:108:20
---------------------------
OK   
---------------------------

This should be 1: 48: 20

If you change the function as below the result is ok:
Tohhmmss(milli){
	hour := Floor(milli / 3600000)
	min  := Mod(Floor(milli / 60000), 60)
	sec  := Mod(Floor( milli / 1000), 60)
	Return hour ":" min ":" sec
}

Winkie

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
fincs, the author of the functions in this popular post, appears to be a very infrequent visitor to this forum. His last post on this topic was back in July, 2007.

Although no changes were posted to this thread, fincs did make some significant changes to the function library in August, 2007 before abandoning (at least for now) the project. You can find the latest version of these functions in this inappropriately titled thread:

<!-- m -->http://www.autohotke...pic.php?t=22662<!-- m -->

Although not bug free, the new version does provide a few nice enhancements.

Winkie
  • Members
  • 10 posts
  • Last active:
  • Joined: 11 Apr 2006

fincs, the author of the functions in this popular post, appears to be a very infrequent visitor to this forum.


@jballi: I am too!!! (at least in posting... Didn't post in two years :?: :!:)

I came to this post because I had a function in one of my scripts that had the same flaw. That function is based on one of the Winamp-scripts somewhere on this forum (I think). So I searched for milliseconds in the forum-search which led to here.

- Edit 1 -: BTW, in the post you linked to the milliseconds conversing function does seem to be buggy too. It's doen't handle values bigger than 60 hours correctly in my test.

- Edit 2 -: My final result for the function:
; funcFormatMilliSeconds ( '(Milli)Seconds as Integer', 'Input is in seconds? as Boolean' )
; -- by Winkie -- Last changed 01 May 2008
; Requires AHK 1.0.46+
; Int = 3600000 		Returns 1:00:00
; Int = 60000 		Returns 1:00
; Int = 12000		Returns 0:12
funcFormatMilliSeconds( Int, Bool = 0 ) {
	If Bool ; If input is in seconds convert into milliseconds
		Int *= 1000
	Hrs := Floor( Int / 3600000 ) ? Floor( Int / 3600000 ) ":" : ""
 	Mins := Mod( Floor( Int / 60000 ), 60) < 10 && Hrs
		? "0" Mod( Floor( Int / 60000 ), 60) : Mod( Floor( Int / 60000 ), 60)
	Secs := Mod( Floor( Int / 1000 ), 60) < 10 
		? "0" Mod( Floor( Int / 1000 ), 60) : Mod( Floor( Int / 1000 ), 60)
	Return Hrs Mins ":" Secs
}

Winkie

soggos
  • Members
  • 129 posts
  • Last active: Nov 30 2012 10:35 AM
  • Joined: 27 Mar 2008
How you got simple volum control on MCI ?

soggos
  • Members
  • 129 posts
  • Last active: Nov 30 2012 10:35 AM
  • Joined: 27 Mar 2008
Cause i have a console
i can make sample : define the begin and the end of the loop
it's good for live, but i would like to define the volume of this mci handle!
not define the master or wave just this mci
cause i want to drive many console for mixing
the code:


if %0% != 0
	{
	SelectedFileName = %1%
	}
ELSE
	SelectedFileName = 

	
	

gosub console



if %0% != 0
{

	h := Media_Open(SelectedFileName)

SplitPath, SelectedFileName, name, dir, ext, name_no_ext, drive

Media_ToHHMMSS(Media_Length(h), hh, mm, ss)
TEMPS_TOTAL = %hh%:%mm%:%ss%

len := Media_Length(h)
guicontrol ,,TEMPS_POSI2, %len%
guicontrol ,,TEMPS_POSI1, 0
guicontrol ,,NOMTITRE, %dir% %name_no_ext%
Media_Play(h)
}
SetTimer ,AFFICHE_TEMPS,100
return






; ------------------------------------------------------------------

CONSOLE:
Gui, Font, S8 Bold cGreen, Verdana
Gui, Add, Text, x6 y7 w320 h20 vNOMTITRE +Center, 
Gui, Add, Text, x326 y7 w60 h20 vTEMPS_PASSE +Center, 0
Gui, Add, Text, x116 y27 w80 h20 vTEMPS_PASSE2 +Right, 10
Gui, Font, S8 Bold cBlack, Verdana
Gui, Add, Text, x386 y7 w60 h20 , %TEMPS_TOTAL%
Gui, Font, S7 Norm cBlack, Verdana
Gui, Add, Button, x226 y67 w100 h20 gJOUER, &JOUER
Gui, Add, Button, x326 y67 w100 h20 gPAUSER, &PAUSE
Gui, Add, Button, x326 y47 w100 h20 gSTOPPER, &STOPPER
Gui, Add, Button, x36 y47 w80 h20 gBOUCLE1, &Deb Boucle
Gui, Add, Button, x36 y67 w80 h20 gBOUCLE2, &Faire Boucle
Gui, Add, Edit, x116 y47 w80 h20 vTEMPS_POSI1 +Right, 
Gui, Add, Edit, x116 y67 w80 h20 vTEMPS_POSI2 +Right, 
Gui, Add, Edit, x226 y47 w100 h20 vTEMPS_POSIN, 
Gui, Add, Progress, x11 y97 w520 h10 cBlue vMyProgress, 
Gui, Add, Slider, x6 y102 w530 h20 Range0-%len% Page1000 TickInterval500 NoTicks vMyProgress2 gALLER, 0
Gui, Add, Button, x196 y47 w20 h20 gRAZBOUCLE1, 0
Gui, Add, Button, x36 y87 w160 h10 gRAZBOUCLE2, fin
Gui, Add, Button, x6 y47 w30 h20 gALLER1, aller
Gui, Add, Button, x6 y67 w30 h20 gALLER2, aller
Gui, Add, Button, x16 y127 w250 h20 gALLERMOINS, -
Gui, Add, Button, x276 y127 w250 h20 gALLERPLUS, +
Gui, Add, Text, x306 y27 w120 h20 vSTATUSdeLECTURE +Center, %STATUSdeLECTURE%
Gui, Add, Checkbox, x226 y27 w80 h20 vFAIREBOUCLE, &BOUCLE

Gui, Add, Button, x226 y5 w80 h20 gVOLUMEmoins, &moins




Gui, Show,    h195 w562,, %SelectedFileName%
Return


; ------------------------------------------------------------------
AFFICHE_TEMPS:

	pos := Media_Position(h)
guicontrol ,,TEMPS_PASSE2, %pos%
    GuiControl,, TEMPS_POSIN, %pos%

combien := ( pos * 100 ) / len
    GuiControl,, MyProgress, %combien%
	
    ;GuiControl,, MyProgress2, %combien%

az := Media_Status(h)
guicontrol ,,STATUSdeLECTURE, %az%

guiControlGet ,FAIREBOUCLE
if FAIREBOUCLE = 1
	goto BOUCLER
return


; ------------------------------------------------------------------
BOUCLER:

	settimer , AFFICHE_TEMPS, off
guiControlGet ,TEMPS_POSI1
	Media_Seek(h, TEMPS_POSI1, False)
Media_Play(h)
	
loop	
{
guiControlGet ,FAIREBOUCLE
if FAIREBOUCLE = 0
{
	settimer , AFFICHE_TEMPS, on
break
}
guicontrol ,,STATUSdeLECTURE, en BOUCLE
guiControlGet ,TEMPS_POSI1
guiControlGet ,TEMPS_POSI2
	pos := Media_Position(h)
		If(pos >= TEMPS_POSI2)
	{
	Media_Seek(h, TEMPS_POSI1, False)
	}
else
{
guicontrol ,,TEMPS_PASSE2, %pos%
combien := ( pos * 100 ) / len
    GuiControl ,, MyProgress, %combien%
}	
}
	
	
return

; ------------------------------------------------------------------

ALLER:

guiControlGet ,MyProgress2
guicontrol ,,TEMPS_POSIN, %MyProgress2%

	Media_Seek(h, MyProgress2, False)
return

; ------------------------------------------------------------------
BOUCLE1:
pos := Media_Position(h)
guicontrol ,,TEMPS_POSI1, %pos%
return


; -----------------------------------------------------------------
BOUCLE2:
pos := Media_Position(h)
guicontrol ,,TEMPS_POSI2, %pos%
guiControl ,,FAIREBOUCLE,1
return


; ------------------------------------------------------------------
RAZBOUCLE1:
guicontrol ,,TEMPS_POSI1, 0
return
RAZBOUCLE2:
len := Media_Length(h)
guicontrol ,,TEMPS_POSI2, %len%
return



; ------------------------------------------------------------------
ALLER1:
guiControlGet ,TEMPS_POSI1
Media_Seek(h, TEMPS_POSI1, False)
return
ALLER2:
guiControlGet ,TEMPS_POSI2
Media_Seek(h, TEMPS_POSI2, False)
return


; ------------------------------------------------------------------
ALLERPLUS:
Media_Seek(h, 100, True)
	pos := Media_Position(h)
guicontrol ,,TEMPS_POSIN, %pos%
return

ALLERMOINS:
Media_Seek(h, -100, True)
	pos := Media_Position(h)
guicontrol ,,TEMPS_POSIN, %pos%
return






; ------------------------------------------------------------------
JOUER:
;guiControlGet ,TEMPS_POSIN
;guiControlGet ,MyProgress2 

;Media_Seek(h, TEMPS_POSIN, False)
Media_Play(h)
return

STOPPER:
Media_Stop(h)
return

PAUSER:
Media_Pause(h)
return

PAUSERR:
Media_Resume(h)
return





; =======================================================================================
; ======================================================================================
;	... . (drag&drop, rezise ...

	GuiDropFiles:  ; Support drag & drop.

	
compte =
	;	StringSplit, Array, A_GuiEvent, .

Loop, parse, A_GuiEvent, `n
{
    SelectedFileName = %A_LoopField%  ; Get the first file only (in case there's more than one).
    compte+=1
	;	break
}


if compte >1
{
FileList = %A_GuiEvent%
Sort, FileList
;Loop, parse, FileList, `n
    ;GuiControl ,, 
	;MsgBox File number %A_Index% is:`n%A_LoopField%.
	
GuiControl,, MyEdit1, %FileList%  ; Put the text into the control.
	;	Gosub LISTEdeFICHIERS
}


	else
	{
	
Loop, parse, A_GuiEvent, `n
	{
	
 ;Gui , Show, , >  %A_GuiEvent%
 ;CurrentFileName = %A_GuiEvent%
 
	Gosub NOUVEAU
    ;	SelectedFileName = %A_LoopField%  ; Get the first file only (in case there's more than one).
		break
	}
	

	

	;	GuiControl,, MyEdit1, %SelectedFileName%  ; Put the text into the control.
	

 

	}	


return

; ------------------------------------------------------------------
NOUVEAU:

;Media_Close(h)
gui destroy
;msgbox ,4096,, %SelectedFileName%


h :=Media_Open(SelectedFileName)

Media_Play(h)

 len := Media_Length(h)

Media_ToHHMMSS(Media_Length(h), hh, mm, ss)
TEMPS_TOTAL = %hh%:%mm%:%ss%
 gosub CONSOLE
 
SplitPath, SelectedFileName, name, dir, ext, name_no_ext, drive
guicontrol ,,NOMTITRE, %name_no_ext%
guicontrol ,,TEMPS_POSI2, %len%
guicontrol ,,TEMPS_POSI1, 0
guicontrol ,,MyProgress2, 0
guicontrol ,,TEMPS_TOTAL, %TEMPS_TOTAL%




return



F5::
reload
return

F7::
ExitApp 
return



GuiClose:
ExitApp




; ----------------------------------------------------------------------------------;;
;; AutoHotkey Version: 1.0.47.04
;; Language:       Anyone
;; Author:         Fincs <[email protected]>
;:
;; Script Function:
;;   Functions to handle multimedia files
;;
;: Title: Media.ahk - Multimedia functions to use with AutoHotkey

;
; Function: Media_Open
; Description:
;		Opens a multimedia file for use with other multimedia functions
; Syntax: Media_Open(File[, Alias=""])
; Parameters:
;		File - The multimedia file
;		Alias - (optional) A name such as media1, if you do not
;			specify one it is automatically generated
; Return Value:
;		The multimedia handle or a 0 to indicate failure
; Example:
;		file:mediaopenclose-example.ahk
;Media_Play(h)


;h := Media_Open("I:\2.WMV")




; -----------------------------------------------------------------------------------
Media_Open(File, Alias=""){
	Static SoundNumber = 0
	IfNotExist, %File%
	{
		ErrorLevel = -2
		Return 0
	}
	If Alias =
	{
		SoundNumber ++
		Alias = AutoHotkey%SoundNumber%
	}
	;Loop, %Param1%
	;	File_Short = %A_LoopFileShortPath%
	ErrorLevel := _mciExecute("open """ File """ alias " Alias)
	Return Alias
}

;
; Function: Media_Close
; Description:
;		Closes a multimedia file
; Syntax: Media_Close(MediaHandle)
; Parameters:
;		MediaHandle - Multimedia handle returned by [url=Media_Open.htm]Media_Open()[/url]
; Return Value:
;		Return value returned by MCI
; Example:
;		file:mediaopenclose-example.ahk
;

Media_Close(MediaHandle){
	Return _mciExecute("close " MediaHandle)
}

;
; Function: Media_Play
; Description:
;		Plays a multimedia file from the current position (beginning is the default)
; Syntax: Media_Play(MediaHandle[, Wait=0])
; Parameters:
;		MediaHandle - Multimedia handle returned by [url=Media_Open.htm]Media_Open()[/url]
;		Wait - (optional) If TRUE the script will wait for the multimedia file to finish before continuing.
;			Else the script will continue while the multimedia file is playing.
; Return Value:
;		Return value returned by MCI
; Example:
;		file:playonesecond-example.ahk
;

Media_Play(MediaHandle, Wait=0){
	If(Wait <> 0 AND Wait <> 1)
		Return -2
	If Wait = 1
		Return _mciExecute("play " MediaHandle " wait")
	Else
		Return _mciExecute("play " MediaHandle)
}

;
; Function: Media_Stop
; Description:
;		Stops the multimedia file
; Syntax: Media_Stop(MediaHandle)
; Parameters:
;		MediaHandle - Multimedia handle returned by [url=Media_Open.htm]Media_Open()[/url]
; Return Value:
;		Return value returned by MCI
; Example:
;		file:playonesecond-example.ahk
;

Media_Stop(MediaHandle){
	ret := _mciExecute("seek " MediaHandle " to start")
	ret := ret && _mciExecute("stop " MediaHandle)
	Return ret
}

;
; Function: Media_Pause
; Description:
;		Pauses the multimedia file
; Syntax: Media_Pause(MediaHandle)
; Parameters:
;		MediaHandle - Multimedia handle returned by [url=Media_Open.htm]Media_Open()[/url]
; Return Value:
;		Return value returned by MCI
; Example:
;		file:playonesecond-example.ahk
;

Media_Pause(MediaHandle){

	Return _mciExecute("pause " MediaHandle)
}

;
; Function: Media_Resume
; Description:
;		Resumes the multimedia file after being paused
; Syntax: Media_Resume(MediaHandle)
; Parameters:
;		MediaHandle - Multimedia handle returned by [url=Media_Open.htm]Media_Open()[/url]
; Return Value:
;		Return value returned by MCI
;

Media_Resume(MediaHandle){
	Return _mciExecute("resume " MediaHandle)
}


; MLONG MCIWndPlayReverse(
  ; hwnd  
; );
; MCI_SETAUDIO

;
; Function: Media_Length
; Description:
;		Returns the length of the multimedia file
; Syntax: Media_Length(MediaHandle)
; Parameters:
;		MediaHandle - Multimedia handle returned by [url=Media_Open.htm]Media_Open()[/url]
; Return Value:
;		Length of the multimedia file in milliseconds
; Example:
;		file:fileinfo-example.ahk
;

Media_Length(MediaHandle){
	rc := _mciSendString("set time format milliseconds", dummy)
	If !rc
		Return -rc
	_mciSendString("status " MediaHandle " length", ret)
	Return ret
}

;
; Function: Media_Seek
; Description:
;		Seeks the multimedia file to a specified time
; Syntax: Media_Seek(MediaHandle, Position[, Relative=0])
; Parameters:
;		MediaHandle - Multimedia handle returned by [url=Media_Open.htm]Media_Open()[/url]
;		Pos - Time to seek to in milliseconds
;		Relative - (Optional) If specified, the position specified is relative
;			to the current position
; Return Value:
;		Return value returned by MCI
; Example:
;		file:playnseek-example.ahk
;

Media_Seek(MediaHandle, Position, Relative=0){
	rc := _mciSendString("set time format milliseconds", dummy)
	_mciSendString("status " MediaHandle " mode", stat)
	If stat = playing
		_mciExecute("pause " MediaHandle)
	If !rc
		Return -rc
	_mciSendString("status " MediaHandle " position", cpos)
	_mciSendString("status " MediaHandle " length", length)
	If Relative = 1
		cpos += Position
	Else
		cpos := Position
	If cpos > %length%
		cpos = %length%
	If cpos < 0
		cpos = 0
	ret := _mciExecute("seek " MediaHandle " to " cpos)
	If stat = playing
		_mciExecute("play " MediaHandle)
	Return ret
}

 
;
; Function: Media_Status
; Description:
;		Returns the status of the multimedia file 
; Syntax: Media_Status(MediaHandle)
; Parameters:
;		MediaHandle - Multimedia handle returned by [url=Media_Open.htm]Media_Open()[/url]
; Return Value:
;		Current status of the multimedia file
; Remarks:
;		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)
;


Media_Status(MediaHandle){
	_mciSendString("status " MediaHandle " mode", ret)
	Return ret
}

;
; Function: Media_Position
; Description:
;		Returns the current position of the multimedia file
; Syntax: Media_Position(MediaHandle)
; Parameters:
;		MediaHandle - Multimedia handle returned by [url=Media_Open.htm]Media_Open()[/url]
; Return Value:
;		Position of the multimedia file in milliseconds
; Example:
;		file:mediaplayer-example.ahk
;

Media_Position(MediaHandle){
	rc := _mciSendString("set time format milliseconds", dummy)
	If !rc
		Return -rc
	_mciSendString("status " MediaHandle " position", ret)
	Return ret
}

;===============================================================================

;
; Function: Media_ToMilliseconds
; Description:
;		Converts the specified hour, minute and second into a valid milliseconds timestamp
; Syntax: Media_ToMilliseconds(Hour, Min, Sec)
; Parameters:
;		Hour, Min, Sec - Position to convert to milliseconds
; Return Value:
;		The specified position converted to milliseconds
;

Media_ToMilliseconds(Hour, Min, Sec){
	milli := Sec * 1000
	milli += (Min * 60) * 1000
	milli += (Hour * 3600) * 1000
	Return milli
}

;
; Function: Media_ToHHMMSS
; Description:
;		Converts the specified milliseconds timestamp to a (Hour, Min, Sec) timestamp
; Syntax: Media_ToHHMMSS(milliseconds, ByRef Hour, ByRef Min, ByRef Sec)
; Parameters:
;		milliseconds - Milliseconds timestamp to convert
;		(ByRef) Hour, Min, Sec - Output variables where store the converted timestamp
; Example:
;		file:fileinfo-example.ahk
;
Media_ToHHMMSS(milliseconds, ByRef Hour, ByRef Min, ByRef Sec){
	milliseconds //= 1000
	Sec := Mod(milliseconds, 60)
	milliseconds //= 60
	Min := Mod(milliseconds, 60)
	milliseconds //= 60
	Hour := Mod(milliseconds, 60)
}

_mciExecute(string){
	Return DllCall("winmm.dll\mciExecute", "str", string, "Cdecl Int")
}

_mciSendString(string, ByRef output, handle=0){
	VarSetCapacity(output, 36, 0)
	ret := DllCall("winmm.dll\mciSendStringA", "str", string, "str", output, "int", 36, "uint", 0, "Cdecl Int")
	If ErrorLevel
		Return -ErrorLevel
	Return ret
}


How-to define the volume of this mci ??
i have tested:




 Media_VOLUME(MediaHandle,iVol ){
 
_mciSendString("MCIWnd_SetVolume" MediaHandle ,10)
;or:
;_mciExecute("MCIWndSet_Volume" MediaHandle ivol)
;_mciSendCommand("MCIWnd_SetVolume" MediaHandle "ivol",ivol)
;_mciExecute("MCIWnd_SetVolume" MediaHandle "ivol" ivol)
;LONG MCIWndSetVolume(MediaHandle ,ivol) ;Parameters
; _MCIWndSetVolume(MediaHandle ,500)

    Return 
 }


VOLUMEmoins:                 ; with a button you call the function  Media_VOLUME(h,ivol)
ivol := ivol-100
 Media_VOLUME(h,ivol)
return


but nothing
:?:

Link Satonaka
  • Members
  • 7 posts
  • Last active: Aug 25 2014 03:44 PM
  • Joined: 19 Aug 2008
have the issues addressed on page one been addressed and posted somewhere else?

Awesome script, by the way. Without this I was going to have to make my program write and open many avisynth scripts, lol. I need to learn more about windows dlls...

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005

have the issues addressed on page one been addressed and posted somewhere else?

Awesome script, by the way. Without this I was going to have to make my program write and open many avisynth scripts, lol. I need to learn more about windows dlls...

Work on this post was abandoned a while back. I created a new project to continue the work started by fincs. See this post:
<!-- m -->http://www.autohotke...pic.php?t=35266<!-- m -->

I hope this helps.

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
I've enhanced the demo player a bit, some time ago. Maybe someone would find it useful. DOWNLOAD

(AHK 1.0.48.05 and Win98SE) forever | My scripts are here


fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
This project has been finally discontinued in favor of jballi's MCI.ahk (great work you did there!).
Could a moderator lock this topic...?

ALTHOUGH

Sound.ahk and Media.ahk can still be downloaded from my AutoHotkey.net account.

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
If you check the MCI thread, you'll find that I had problems with that library and your Sound lib proved to be better. So I think users should have the choice whether to use something that works for them or not. ;)

(AHK 1.0.48.05 and Win98SE) forever | My scripts are here


fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007

If you check the MCI thread, you'll find that I had problems with that library and your Sound lib proved to be better. So I think users should have the choice whether to use something that works for them or not. ;)


Hey, I've read that and that's why I left the files up ;)