# -*- coding: utf-8 -*-
"""Ejemplo 16: Reverse playback (reproduccion inversa)."""

import sys
import time
from basskit.dlls.bass import bass
from basskit.dlls.bass_fx import bass_fx
from basskit.constantes.bass import BASS_UNICODE, BASS_STREAM_DECODE, BASS_SAMPLE_FLOAT
from basskit.constantes.bass_fx import (
	BASS_FX_FREESOURCE, BASS_ATTRIB_REVERSE_DIR,
	BASS_FX_RVS_REVERSE, BASS_FX_RVS_FORWARD,
)


def main():
	print("=== Reverse Playback ===\n")
	if len(sys.argv) < 2:
		print("Uso: python 16_reverse.py <archivo_audio>")
		return

	bass.BASS_Init(-1, 44100, 0, 0, None)
	ruta = sys.argv[1].encode("utf-16-le") + b"\x00\x00"

	# NOTA: Para reverse necesitamos BASS_FX_ReverseCreate
	decode = bass.BASS_StreamCreateFile(0, ruta, 0, 0, BASS_STREAM_DECODE | BASS_UNICODE | BASS_SAMPLE_FLOAT)
	if decode == 0:
		print(f"Error: {bass.BASS_ErrorGetCode()}")
		bass.BASS_Free()
		return

	stream = bass_fx.BASS_FX_ReverseCreate(decode, 2.0, BASS_FX_FREESOURCE)
	if stream == 0:
		print(f"Error reverse: {bass.BASS_ErrorGetCode()}")
		bass.BASS_Free()
		return

	bass.BASS_ChannelPlay(stream, 0)
	print(f"Reproduciendo: {sys.argv[1]}")

	# Hacia adelante
	bass.BASS_ChannelSetAttribute(stream, BASS_ATTRIB_REVERSE_DIR, float(BASS_FX_RVS_FORWARD))
	print("\n[1/3] Hacia adelante - 4 seg")
	time.sleep(4)

	# Hacia atras
	bass.BASS_ChannelSetAttribute(stream, BASS_ATTRIB_REVERSE_DIR, float(BASS_FX_RVS_REVERSE))
	print("[2/3] Hacia ATRAS (reverse) - 4 seg")
	time.sleep(4)

	# Volver a adelante
	bass.BASS_ChannelSetAttribute(stream, BASS_ATTRIB_REVERSE_DIR, float(BASS_FX_RVS_FORWARD))
	print("[3/3] De vuelta hacia adelante - 3 seg")
	time.sleep(3)

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


if __name__ == "__main__":
	main()
