�� Requisitos
bass.dll (en la carpeta del .exe)
bass.h y bass.lib enlazados al proyecto
Dos archivos MP3 válidos (ej: tema1.mp3 y tema2.mp3)
Un TTimer en tu formulario (llamalo FadeTimer) con Interval = 50
�� Resultado
Esto hace un crossfade suave de 8 segundos entre dos MP3 en tiempo real, sin detener la reproducción, y sin usar un mezclador complejo.
Declaraciones en Form1.h por ejemplo...
Código:
HSTREAM stream1, stream2;
float volume1 = 1.0f;
float volume2 = 0.0f;
int fadeStepCount = 0;
const int fadeTotalSteps = 160; // 8 segundos / 0.05s = 160 pasos
Código:
void __fastcall TForm1::StartCrossfade()
{
if (!BASS_Init(-1, 44100, 0, Handle, NULL))
{
ShowMessage("No se pudo inicializar BASS");
return;
}
// Cargar los dos MP3
stream1 = BASS_StreamCreateFile(FALSE, "C:\\audio\\tema1.mp3", 0, 0, 0);
stream2 = BASS_StreamCreateFile(FALSE, "C:\\audio\\tema2.mp3", 0, 0, 0);
if (!stream1 || !stream2)
{
ShowMessage("Error cargando los MP3");
return;
}
// Seteamos volumen inicial
BASS_ChannelSetAttribute(stream1, BASS_ATTRIB_VOL, 1.0f);
BASS_ChannelSetAttribute(stream2, BASS_ATTRIB_VOL, 0.0f);
// Reproducimos ambos
BASS_ChannelPlay(stream1, FALSE);
BASS_ChannelPlay(stream2, FALSE);
// Iniciar timer
volume1 = 1.0f;
volume2 = 0.0f;
fadeStepCount = 0;
FadeTimer->Enabled = true;
}
�� Código del FadeTimer (cada 50ms):
Código:
void __fastcall TForm1::FadeTimerTimer(TObject *Sender)
{
fadeStepCount++;
float stepSize = 1.0f / fadeTotalSteps;
volume1 -= stepSize;
volume2 += stepSize;
if (volume1 < 0.0f) volume1 = 0.0f;
if (volume2 > 1.0f) volume2 = 1.0f;
BASS_ChannelSetAttribute(stream1, BASS_ATTRIB_VOL, volume1);
BASS_ChannelSetAttribute(stream2, BASS_ATTRIB_VOL, volume2);
if (fadeStepCount >= fadeTotalSteps)
{
FadeTimer->Enabled = false;
// (Opcional) Detener stream1 si ya terminó el crossfade
BASS_ChannelStop(stream1);
}
}
⏱️ ¿Por qué 160 pasos?
Interval = 50ms (0.05s por paso)
8s / 0.05s = 160 pasos
Entonces cada paso debe cambiar el volumen por 1 / 160 = 0.00625