LinerOverlay.PAL

{ About:
   This script will play a liner in Aux1 as soon as a new track starts
   The liner will only be played if
    a) The song has an intro of specified minimem duration
    b) The song is of type S, i.e. a normal song.

   Then the script will wait the specified amount of time before
   it tries to play another liner.

   This script can help brand your station and make it sound like a true
   commercial terrestrial station.
    any source connected

   Usage:
    a) Make sure you use the song information editor to specify intro times for your tracks!
    b) Make sure the AGC settings on Aux1 is to your liking. Also set the volume a bit louder
       on Aux1 so you cna clearly hear the liner above the active Deck audio.
    c) Edit the configuration details below.
       Make sure to change the category to the one you use to store your liners.
}
{ CONFIGURATION }
{==================================================}
const MIN_INTRO = 5*1000; //5 seconds
const MIN_WAIT  = '+00:15:00'; //Wait 15 minutes between liners
const LINERS_CATEGORY = 'Liners';


{ IMPLEMENTATION }
{--------------------------------------------------}
function ExtractIntro(Song : TSongInfo):Integer; forward;

var Song, Liner : TSongInfo;
var Waiting : Boolean = True;
var Intro : Integer = 0;
Aux1.Eject;

{Step1: Queue up the deck, ready for play}
Liner := CAT[LINERS_CATEGORY].ChooseSong(smLRP,NoRules);
if (Liner=nil) then
 WriteLn('No valid liner found')
else if (not Aux1.QueueSong(Liner)) then
 WriteLn('Failed to queue song: '+Liner['filename']);

{Wait for a valid song with intro}
while Waiting do
 begin
  {Step2: Wait for the song to change}
  PAL.WaitForPlayCount(1);

  {Step3: Grab current song information}
  Song := ActivePlayer.GetSongInfo;

  if (Song=nil) then
   WriteLn('The active player contained no song info??')
  else
   begin
    {Extract the intro time - this is a bit tricky}
    Intro := ExtractIntro(Song);

    {Start playing the liner if the current song matches our rules}
    if(Song['songtype']='S') and (Intro>=MIN_INTRO) then
     begin
      Aux1.Play;
      Waiting := False;
     end;
    Song.Free; Song := nil;
   end;
end;

{Wait 5 minutes before we do this all again}
PAL.WaitForTime(MIN_WAIT);
PAL.Loop := True;

{................................................}
function ExtractIntro(Song : TSongInfo):Integer;
var
 P : Integer;
 XFade : String;
begin
 Result := -1;
 XFade := Trim(Song['xfade']);

 WriteLn('Decoding XFade string');
 WriteLn('XFade: '+XFade);

 if XFade = '' then
  Result := -1
 else
  begin
   P := Pos('&i=',XFade);
   if (P > 0) then
    begin
     Delete(XFade,1,P+2);
     P := Pos('&',XFade);
     if (P>0) then
      Delete(XFade,P,Length(XFade));

     Result := StrToIntDef(XFade,-1);
     WriteLn('Intro time detected: '+XFade);
    end;
  end;
end;

{--------------------------------------------------}