
#include <Sound.au3>
#include <File.au3>
Opt("TrayAutoPause", 0)

;^=Ctrl oder Strg
;!=Alt
;#=Win
;+=Shift
HotKeySet("{ESC}", "F_Ende")
HotKeySet("^p","F_Pause")
HotKeySet("{Up}", "F_VolumeUp")
HotKeySet("{Down}", "F_VolumeDown")
HotKeySet("{Right}", "F_PosV")
HotKeySet("{Left}", "F_PosR")
HotKeySet("^!r", "F_TitelRepeat")
HotKeySet("^{F5}", "F_PlaylistEdit")

Global $pause = False, $Vol = 10, $Tim, $Line = 0, $snd, $Count = 0, $FrL, $Rep = False
Global $Path = @ScriptDir & "\SoundPlayList.txt"
$hUser = DllOpen("User32.dll")


;$snd = _SoundOpen("F:\Sound\Normale MP3\Diverses\Ike & Tina Turner - Nutbush City Limits.mp3")
;F_Sound()
If Not FileExists($Path) Then
	MsgBox(4112, "Fehler", "Playliste nicht gefunden" & @CRLF & "Es wird eine neue Playliste im Scriptordner erstellt")
	$Playlist = FileOpen($Path,10)
	FileClose($Playlist)
EndIf

$Playlist = FileOpen($Path,0)
;~ Read()
;~ _SoundPlay($snd)
;~ SoundSetWaveVolume($Vol)
$Tim = Timerinit()




While 1
	Sleep(500)
	If TimerDiff($Tim) > 1500 Then SplashOff()

	If Not $Rep And _SoundStatus($snd) = "stopped" Then
	$Line += 1

	_SoundClose($snd)
	F_Read()
	;MsgBox(4096, "Debug", $FrL)
	_SoundPlay($snd)
	SoundSetWaveVolume($Vol)
	Endif


If $Rep And _SoundStatus($snd) = "stopped" Then
	_SoundSeek($snd, 0, 0, 0)
	_SoundPlay($snd)
EndIf




WEnd


Func F_Read()
	$Count = 0
	Local $CountLines = _FileCountLines($Path)
	$FrL = FileReadLine($Playlist, $Line)
	If $FrL = "" And $Line = 1 Then
		MsgBox(4112, "Fehler", "Playliste Ist leer und muss noch befüllt werden!")
		F_PlaylistEdit()
		Return
	EndIf

	If $Line = $CountLines + 1 Then Exit MsgBox(4096, "Titel", "Playliste zu Ende gespielt - Exit")

While $Frl = ""
	If $Line = $CountLines + 1 Then Exit MsgBox(4096, "Titel", "Playliste zu Ende gespielt - Exit")
	$Line += 1
	$FrL = FileReadLine($Playlist, $Line)
Wend

	$snd =  _SoundOpen($FrL)
	;If @error = 1 Then MsgBox(4096, "Titel", $FrL & @CRLF & "MCI-Fehler beim Öffnen")
	If @error = 2 Then
		MsgBox(4096, "Titel", $FrL & @CRLF & "Datei nicht gefunden",3)
	ElseIf @extended <> 0 Then
		Local $iExtended = @extended ; Zuweisung, weil @extended nach DllStructCreate() geändert wird
		Local $stText = DllStructCreate("char[128]")
		DllCall("winmm.dll", "short", "mciGetErrorStringA", "str", $iExtended, "struct*", $stText, "int", 128)
		MsgBox(4112, "Fehler", "Öffnen fehlgeschlagen." & @CRLF & "Fehlernummer: " & $iExtended & @CRLF & "Fehlerbeschreibung: " & DllStructGetData($stText, 1) & @CRLF & "Beachte: Möglicherweise kann der Sound dennoch korrekt abgespielt werden.")
		MsgBox(4096, "Titel", "Folgende Datei nicht abspielbar" & @CRLF & "sollte aus Playliste entfernt werden" & @CRLF & $Frl)
	Else
		;MsgBox($MB_SYSTEMMODAL, "Erfolgsmeldung", "Die Datei wurde erfolgreich geöffnet.",3)
	EndIf


	;ClipPut($FrL)
	Sleep(500)
EndFunc


Func F_VolumeUp()
	$Vol += 1
	If $Vol > 99 Then $Vol = 100
	SoundSetWaveVolume($Vol)
	$spla = SplashTextOn("Volume", "Volume "&$Vol&" %", 350, 80, -1,-1, 0, "TimesNewRoman", 36, 700)
	$Tim = Timerinit()
EndFunc

Func F_VolumeDown()
	$Vol -= 1
	If $Vol < 1 Then $Vol = 0
	SoundSetWaveVolume($Vol)
	SplashTextOn("Volume", "Volume "&$Vol&" %", 350, 80, -1,-1, 0, "TimesNewRoman", 36, 700)
	$Tim = Timerinit()
EndFunc


Func F_PosV()
HotKeySet("{Right}")
$Count = Int(_SoundPos($snd,2)/1000)
$CountMax = _SoundLength($snd,2)/1000
	;If $Count - 30 >= _SoundPos($snd,2)/1000 Then
	If $Count + 30 <= $CountMax Then
		$Count += 30
	_SoundSeek($snd,0,0,$Count)
	_SoundPlay($snd)
	Else
	$Count += 2
	If $Count > $CountMax Then $Count = Int($CountMax - 2)
	_SoundSeek($snd,0,0,$Count)
	_SoundPlay($snd)
	Endif
While _IsPressed("27", $hUser)
	Sleep(20)
WEnd
HotKeySet("{Right}", "F_PosV")
	SplashTextOn("Playposition", "Sekunde " & $Count, 350, 80, -1,-1, 0, "TimesNewRoman", 28, 700)
	$Tim = Timerinit()
EndFunc


Func F_PosR()
HotKeySet("{Left}")
$Count = Int(_SoundPos($snd,2)/1000)
$CountMax = _SoundLength($snd,2)/1000
	;If $Count - 30 >= _SoundPos($snd,2)/1000 Then
	If $Count >= 30 Then
		$Count -= 30
	_SoundSeek($snd,0,0,$Count)
	_SoundPlay($snd)
	Else
	$Count -= 2
	If $Count < 0 Then $Count = 0
	_SoundSeek($snd,0,0,$Count)
	_SoundPlay($snd)
	Endif
While _IsPressed("25", $hUser)
	Sleep(20)
WEnd
HotKeySet("{Left}", "F_PosR")
	SplashTextOn("Playposition", "Sekunde " & $Count, 350, 80, -1,-1, 0, "TimesNewRoman", 28, 700)
	$Tim = Timerinit()
EndFunc


Func F_TitelRepeat()
	HotKeySet("^!r");Shift Alt w
	$Rep = Not $Rep

While _IsPressed("52", $hUser)
	Sleep(20)
WEnd
	HotKeySet("^!r", "F_TitelRepeat")
	SplashTextOn("Repeat",  $Rep ? "Repeat - Ein":"Repeat - Aus", 350, 80, -1,-1, 0, "TimesNewRoman", 28, 700)
	$Tim = Timerinit()
EndFunc


Func F_Pause()
	If Not $pause Then
		_SoundPause($snd)
	Else
		_SoundResume($snd)
	EndIf
	$pause = Not $pause
EndFunc   ;==>PAUSE


Func F_PlaylistEdit()
	FileClose($Playlist)
	$Playlist = FileOpen(@ScriptDir & "\SoundPlayList.txt",1)
	$FOD = FileOpenDialog("Lieder-Suchen", @HomePath & "\", "Songs (*.wav;*.mp3)", 4)
	;MsgBox(4096, "Titel", $FOD)
	If StringInStr($FOD,"|") Then
		Local $aSongs = StringSplit($FOD, "|",2)
		For $i = 1 to Ubound($aSongs)-1
		$SongEinzeln = $aSongs[0]&"\"&$aSongs[$i]
		FileWriteLine($Playlist,$SongEinzeln)
		Next
		$FOD = Null
	Else
	FileWriteLine($Playlist, $FOD)
	EndIf

	FileClose($Playlist)
	$Playlist = FileOpen(@ScriptDir & "\SoundPlayList.txt",0)


EndFunc

Func F_Ende()
	_SoundClose($snd)
	FileClose($Playlist)
	DllClose($hUser)
	Exit
EndFunc   ;==>ende



Func _IsPressed($sHexKey, $vDLL = 'user32.dll')
	Local $a_R = DllCall($vDLL, "short", "GetAsyncKeyState", "int", '0x' & $sHexKey)
	If @error Then Return SetError(@error, @extended, False)
	Return BitAND($a_R[0], 0x8000) <> 0
EndFunc   ;==>_IsPressed


