Si lo que queremos es alternar entre stream1 y stream2 constantemente con crossfades de 8 segundos, lo que necesitás es algo como un sistema de doble canal con fading automático, donde uno sube mientras el otro baja, y viceversa.
Mantenemos ambos streams en loop o reproducibles.
Creamos un estado booleano currentIsStream1 para saber cuál está "activo".
Cada vez que quieras hacer crossfade, inviertes el estado y disparas el timer.
🔸 Variables globales:
Código:
HSTREAM stream1, stream2;
float volume1, volume2;
int fadeStepCount = 0;
const int fadeTotalSteps = 160;
bool currentIsStream1 = true; // true = stream1 activo, false = stream2
🔸 Al iniciar la app:
Código:
void __fastcall TForm1::StartStreams()
{
BASS_Init(-1, 44100, 0, Handle, NULL);
stream1 = BASS_StreamCreateFile(FALSE, "C:\\audio\\tema1.mp3", 0, 0, BASS_SAMPLE_LOOP);
stream2 = BASS_StreamCreateFile(FALSE, "C:\\audio\\tema2.mp3", 0, 0, BASS_SAMPLE_LOOP);
BASS_ChannelSetAttribute(stream1, BASS_ATTRIB_VOL, 1.0f);
BASS_ChannelSetAttribute(stream2, BASS_ATTRIB_VOL, 0.0f);
BASS_ChannelPlay(stream1, FALSE);
BASS_ChannelPlay(stream2, FALSE);
volume1 = 1.0f;
volume2 = 0.0f;
currentIsStream1 = true;
}
🔸 Función para iniciar el crossfade:
Código:
void __fastcall TForm1::StartCrossfade()
{
fadeStepCount = 0;
FadeTimer->Enabled = true;
}
🔸 Timer de crossfade:
Código:
void __fastcall TForm1::FadeTimerTimer(TObject *Sender)
{
fadeStepCount++;
float stepSize = 1.0f / fadeTotalSteps;
if (currentIsStream1)
{
volume1 -= stepSize;
volume2 += stepSize;
}
else
{
volume1 += stepSize;
volume2 -= stepSize;
}
// Clamp
if (volume1 < 0.0f) volume1 = 0.0f;
if (volume1 > 1.0f) volume1 = 1.0f;
if (volume2 < 0.0f) volume2 = 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;
currentIsStream1 = !currentIsStream1;
}
}
🔸 Para automatizar el cambio cada X segundos:
Agrega un segundo TTimer (llamalo AutoSwitchTimer) con Interval = 30000 (por ejemplo, 30 segundos), y su código sería:
Código:
void __fastcall TForm1::AutoSwitchTimerTimer(TObject *Sender)
{
StartCrossfade();
}