# -*- coding: utf-8 -*-
"""Ejemplo 17: Fade in/out con BASS_ChannelSlideAttribute."""

import sys
import time
from basskit.dlls.bass import bass
from basskit.constantes.bass import BASS_UNICODE, BASS_ATTRIB_VOL


def main():
	print("=== Fade In / Fade Out (SlideAttribute) ===\n")
	if len(sys.argv) < 2:
		print("Uso: python 17_fade_in_out.py <archivo_audio>")
		return

	bass.BASS_Init(-1, 44100, 0, 0, None)
	ruta = sys.argv[1].encode("utf-16-le") + b"\x00\x00"
	stream = bass.BASS_StreamCreateFile(0, ruta, 0, 0, BASS_UNICODE)
	if stream == 0:
		print(f"Error: {bass.BASS_ErrorGetCode()}")
		bass.BASS_Free()
		return

	# Empezar en silencio
	bass.BASS_ChannelSetAttribute(stream, BASS_ATTRIB_VOL, 0.0)
	bass.BASS_ChannelPlay(stream, 0)
	print(f"Reproduciendo: {sys.argv[1]}\n")

	# Fade In: 0% -> 100% en 3 segundos
	print("[1/3] Fade IN (0% -> 100%) en 3 segundos...")
	bass.BASS_ChannelSlideAttribute(stream, BASS_ATTRIB_VOL, 1.0, 3000)
	while bass.BASS_ChannelIsSliding(stream, BASS_ATTRIB_VOL):
		time.sleep(0.1)
	print("  Fade in completado.")

	# Reproducir normal 3 seg
	print("[2/3] Volumen al 100% - 3 seg")
	time.sleep(3)

	# Fade Out: 100% -> 0% en 4 segundos
	print("[3/3] Fade OUT (100% -> 0%) en 4 segundos...")
	bass.BASS_ChannelSlideAttribute(stream, BASS_ATTRIB_VOL, 0.0, 4000)
	while bass.BASS_ChannelIsSliding(stream, BASS_ATTRIB_VOL):
		time.sleep(0.1)
	print("  Fade out completado.")

	bass.BASS_StreamFree(stream)
	bass.BASS_Free()
	print("\nFin del ejemplo.")


if __name__ == "__main__":
	main()
