# -*- coding: utf-8 -*-
"""Ejemplo 12: TODOS los efectos DX8 (9 efectos con parametros)."""

import sys
import time
import ctypes
from basskit.dlls.bass import bass
from basskit.constantes.bass import (
	BASS_UNICODE, BASS_FX_DX8_CHORUS, BASS_FX_DX8_COMPRESSOR,
	BASS_FX_DX8_DISTORTION, BASS_FX_DX8_ECHO, BASS_FX_DX8_FLANGER,
	BASS_FX_DX8_GARGLE, BASS_FX_DX8_I3DL2REVERB, BASS_FX_DX8_PARAMEQ,
	BASS_FX_DX8_REVERB,
)
from basskit.estructuras.bass import (
	BASS_DX8_CHORUS, BASS_DX8_COMPRESSOR, BASS_DX8_DISTORTION,
	BASS_DX8_ECHO, BASS_DX8_FLANGER, BASS_DX8_GARGLE,
	BASS_DX8_I3DL2REVERB, BASS_DX8_PARAMEQ, BASS_DX8_REVERB,
)

EFECTOS = [
	("Chorus", BASS_FX_DX8_CHORUS, BASS_DX8_CHORUS,
	 {"fWetDryMix": 60, "fDepth": 40, "fFeedback": 40, "fFrequency": 2, "lWaveform": 1, "fDelay": 16, "lPhase": 3}),
	("Compressor", BASS_FX_DX8_COMPRESSOR, BASS_DX8_COMPRESSOR,
	 {"fGain": 5, "fAttack": 10, "fRelease": 200, "fThreshold": -20, "fRatio": 4, "fPredelay": 4}),
	("Distortion", BASS_FX_DX8_DISTORTION, BASS_DX8_DISTORTION,
	 {"fGain": -10, "fEdge": 60, "fPostEQCenterFrequency": 2400, "fPostEQBandwidth": 2400, "fPreLowpassCutoff": 4000}),
	("Echo", BASS_FX_DX8_ECHO, BASS_DX8_ECHO,
	 {"fWetDryMix": 50, "fFeedback": 50, "fLeftDelay": 300, "fRightDelay": 300, "lPanDelay": 0}),
	("Flanger", BASS_FX_DX8_FLANGER, BASS_DX8_FLANGER,
	 {"fWetDryMix": 60, "fDepth": 80, "fFeedback": -50, "fFrequency": 0.5, "lWaveform": 1, "fDelay": 2, "lPhase": 2}),
	("Gargle", BASS_FX_DX8_GARGLE, BASS_DX8_GARGLE,
	 {"dwRateHz": 50, "dwWaveShape": 0}),
	("I3DL2 Reverb", BASS_FX_DX8_I3DL2REVERB, BASS_DX8_I3DL2REVERB,
	 {"lRoom": -500, "lRoomHF": -100, "flDecayTime": 3.0, "flDecayHFRatio": 0.5, "lReflections": -2000,
	  "flReflectionsDelay": 0.02, "lReverb": 200, "flReverbDelay": 0.04, "flDiffusion": 100, "flDensity": 100, "flHFReference": 5000}),
	("ParamEQ (boost 1kHz)", BASS_FX_DX8_PARAMEQ, BASS_DX8_PARAMEQ,
	 {"fCenter": 1000, "fBandwidth": 12, "fGain": 10}),
	("Reverb", BASS_FX_DX8_REVERB, BASS_DX8_REVERB,
	 {"fInGain": 0, "fReverbMix": -5, "fReverbTime": 2000, "fHighFreqRTRatio": 0.5}),
]


def main():
	print("=== TODOS los efectos DX8 (9) ===\n")
	if len(sys.argv) < 2:
		print("Uso: python 12_efectos_dx8.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

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

	# Sin efectos
	print("[0/{0}] Sin efectos - 2 seg".format(len(EFECTOS)))
	time.sleep(2)

	for i, (nombre, tipo_fx, struct_cls, params) in enumerate(EFECTOS, 1):
		fx = bass.BASS_ChannelSetFX(stream, tipo_fx, 0)
		if fx:
			p = struct_cls()
			for k, v in params.items():
				if hasattr(p, k):
					setattr(p, k, v)
			bass.BASS_FXSetParameters(fx, ctypes.byref(p))
			print(f"[{i}/{len(EFECTOS)}] {nombre} - 3 seg")
			time.sleep(3)
			bass.BASS_ChannelRemoveFX(stream, fx)
		else:
			print(f"[{i}/{len(EFECTOS)}] {nombre} - no disponible")

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


if __name__ == "__main__":
	main()
