WeeklySchedule.PAL

{ Comments:
 Do NOT just copy this script into your player and start it. It will NOT work.
 
  There are some things that MUST be in place for this script to work cleanly.
  1. 0 songs in Queue. Adjust in the Config, Playlist Rotation Rules.
  2. No Ghost Queue. Untick Ghost Queue in Config, Playlist Rotation Rules.
  3. You must have SayTime installed in the appropriate directories. If you use
     it of course. If you don't use it, you will have to comment it out.
  4. You must clearly define your Categories. If you change them, change the
     variables here in the config section.
     
  5. To improve execution time, comment out the WriteLn statements. They are
     only for checking execution during construction. They only slow down
     execution otherwise, even worse than it is.
     
 ***********  I dont have many Station IDs yet, so I use a generic one, IDA,
              in places where I don't have appropriate ones.
              That's in Christian Music, and Electronic Music segments.

You should begin by setting out on paper just where your scheduled shows start
and finish. Keep in mind the lag time between execution. I've tried to make
it so it doesn't matter - but have probably failed miserably.

Monday: Tuesday: Wednesday: Thursday: Friday.

Sunday Midnight, or actually 30 seconds after Sunday Midnight,
(Early Monday Morning) through to 6am,          Event 1   Christian Music
then 6am through to Midday,                     Event 2   Country Music
then Midday to 6pm,                             Event 3   Electronic Music
then 6pm to 12pm.                               Event 4   World Music
Each Week Day.                                   = 24 hoursx 5 days

Saturday 00:00:30 to 8am                        Event 5   Dance
then 8am to 8pm                                 Event 6   Country
then 8pm to 12pm                                Event 7   Dance

Sunday 00:00:30 to 24:00:00                     Event 8   Christian


Your other option of course, is to ignore Events, just have the one event, that
goes forever. You maybe only have one Category? and it plays 24x7, in which
case this becomes REALLY easy. Just configure Event 1 variables.

You get the picture. In other words, PLAN what you are going to do. Do NOT just
copy this script into your player and start it. It will NOT work.

ToDo:  Add a bit that writes to the Database so that you can show what 
       schedule is playing on your web pages.

   Copyright: Dodgey Brothers. Australia. And if you take that seriously.....
   Code by: Robert Chalmers. nospam-robert@chalmers.com.au (Australia)
   The Mission of Our Lady of Fatima. http://www.chalmers.com.au
                                      http://www.quantum-radio.net

   I can be contacted at nospam-robert@quantum-radio.net.  If you need help.
   (Take off the nospam- bit of course!)
   Donate to the Mission and be blessed.
}


PAL.Loop := True;
// User Configurable section
// ----------- SayTime variables -------
const TimeHeader  : String = 'E:\StationPromo\Times\';
const TimeHours   : String = 'E:\StationPromo\Times\Hours\';
const TimeMinutes : String = 'E:\StationPromo\Times\Minutes\';
const MyGMTZone   : Integer = +10;

var C,N          : Integer;
var fname        : String;
var fnamehh      : String;
var fnamemm      : String;
var ampm         : String;
var hh,mm,ss,ms : Integer;

// ------------ common ----------------
// fade decks
function ActivePlayer:TPlayer; forward;
// Call SayTime
function SayTime:DateTime; forward;
var E : DateTime;    // Return the length of the queues. Gets modified
var UnAdjE : DateTime; // Used by saytime. Not modified. Full queue ETA
// ------------------------------------

// -------------Schedule variables-----
var Count : Integer;  // loop counter
var K : Integer;      // Used for the Queue.Count
// Scheduled Events, 1 through xx
{ ************** EVENT 1 ***************}
{ Christian Music   8 hours             }
var Event1Start    : string = '00:00:30'; // Really early Weekday Mornings.
var Event1End      : string = '07:59:30'; // untill 8am each week day
var Event1Limit    : string = '08:00:00'; // The do-until limit
{ ************** EVENT 2 ***************}
{ Country Music     4 hours             }
var Event2Start    : string = '07:59:30'; // Weekday from 8am
var Event2End      : string = '11:59:30'; // untill 12 Midday
var Event2Limit    : string = '12:00:00'; // The do-until limit
{ ************** EVENT 3 ***************}
{ World Music       4 hours             }
var Event3Start    : string = '11:59:30'; // Midday Weekdays
var Event3End      : string = '19:59:30'; // untill 8pm
var Event3Limit    : string = '20:00:00'; // The do-until limit
{ ************** EVENT 4 ***************}
{ Electronic Music  4 hours             }
var Event4Start    : string = '19:59:30'; // 8pm Weekdays
var Event4End      : string = '23:59:30'; // untill Midnight
var Event4Limit    : string = '00:00:30'; // The do-until limit
{ ************** EVENT 5 ***************}
{ Country Music     12 hours            }
var Event5Start    : string = '00:00:30'; // Really early Saturday Morning
var Event5End      : string = '11:59:30'; // untill Midday
var Event5Limit    : string = '12:00:00'; // The do-until limit

{ ************** EVENT 6 ***************}
{ World Music       12 hours            }
var Event6Start    : string = '11:59:30'; // Midday Saturday
var Event6End      : string = '23:59:30'; // untill Midnight
var Event6Limit    : string = '00:00:30'; // The do-until limit

{ ************** EVENT 7 ***************}
{ Christian Music   10 hours            }
var Event7Start    : string = '00:00:30'; // Really Early Sunday Morning
var Event7End      : string = '09:59:30'; // untill 10am Sunday
var Event7Limit    : string = '10:00:00'; // The do-until limit

{ **************************************}
{ ************** EVENT 8 ***************}
{ Christian Music   14 hours            }
var Event8Start    : string = '09:59:30'; // Sunday Morning
var Event8End      : string = '23:59:30'; // untill Midnight Sunday
var Event8Limit    : string = '00:00:30'; // The do-until limit
 { This event is basically a test.      }
{ **************************************}

// Category name holders. As many as you need.
var CatA : string = 'World';
var CatB : string = 'Country';
var CatC : string = 'Electronic';
var CatD : string = 'Christian';
// Station IDs, suitable for Category beingplayed.
var CatIDA : string = 'WorldIDs';
var CatIDB : string = 'CountryIDs';
var CatIDC : string = 'ElectronicIDs';
var CatIDD : string = 'ChristianIDs';
   { NOTE. }
   { I dont have many Station IDs done yet, so watch out below. I use the
     the same ones a lot. ie; In Christina segments, I still use WorldIDs. }
     
     
// Boolean switches to watch for various days.
var TodayIsMyDayE1 : Boolean;   // E1 = Event1
var TodayIsMyDayE2 : Boolean;   // E2 = Event2 and so on.
var TodayIsMyDayE3 : Boolean;
var TodayIsMyDayE4 : Boolean;
var TodayIsMyDayE5 : Boolean;
var TodayIsMyDayE6 : Boolean;
var TodayIsMyDayE7 : Boolean;
var TodayIsMyDayE8 : Boolean;   // and so on. Depends how many Events you have.

// End User Configuration Section.
// Do not alter code below this line unless you are confident in what you do!


//Event 1 ----------------------------------------------------------------------
TodayIsMyDayE1 := ((DayOfWeek(Now) = Monday) or
(DayOfWeek(Now) = Tuesday) or
(DayOfWeek(Now) = Wednesday) or
(DayOfWeek(Now) = Thursday) or
(DayOfWeek(Now) = Friday));
while (TodayIsMyDayE1) and (now >= T[Event1Start]) and (now <= T[Event1End])
do
begin
           WriteLn('Event 1 starts at '+Event1Start);
           WriteLn('Event 1  ends at '+Event1End);
        Queue.Clear;
        cat[CatIDA].QueueBottom(smRandom,NoRules);
        ActivePlayer.FadeToNext;
         repeat
           cat [CatD].QueueBottom(smLRP,EnforceRules);
                 E := Queue.ETA;
                 Count := Count+1;
 	               E := (E-(30/(60*60*24)));
 	               WriteLn('Next track loads at '+TimeToStr(E));
                 PAL.WaitForTime(E);
                     // every 4 tracks put in a station ID and Say The Time.
                     If Count = 4 then
                   begin
	                  cat[CatIDA].QueueBottom(smRandom,NoRules);
	                  // Call SayTime
	                  SayTime;
                   // Sayime code ends here.
	                  Count := 0;
	                  end;
         until (now >= T[Event1End]); //and (now <= T[Event1Limit]);
 TodayIsMyDayE1 := False; {Skip loop}
end;
// -----------------------------  End of Event 1 -------------------------------
//Event 2 ----------------------------------------------------------------------
TodayIsMyDayE2 := ((DayOfWeek(Now) = Monday) or
(DayOfWeek(Now) = Tuesday) or
(DayOfWeek(Now) = Wednesday) or
(DayOfWeek(Now) = Thursday) or
(DayOfWeek(Now) = Friday));
while (TodayIsMyDayE2) and (now >= T[Event2Start]) and (now <= T[Event2End])
do
begin
           WriteLn('Event 2 starts at '+Event2Start);
           WriteLn('Event 2  ends at '+Event2End);
        Queue.Clear;
        cat[CatIDB].QueueBottom(smRandom,NoRules);
        ActivePlayer.FadeToNext;
         repeat
           cat[CatB].QueueBottom(smLRP,EnforceRules);
                 E := Queue.ETA;
                 Count := Count+1;
 	               E := (E-(30/(60*60*24)));
 	               WriteLn('Next track loads at '+TimeToStr(E));
                 PAL.WaitForTime(E);
                     // every 4 tracks put in a station ID and Say The Time.
                     If Count = 4 then
                   begin
	                  cat[CatIDB].QueueBottom(smRandom,NoRules);
	                  // Call SayTime
	                  SayTime;
                   // Sayime code ends here.
	                  Count := 0;
	                  end;
         until (now >= T[Event2End]); // and (now <= T[Event2Limit]);
 TodayIsMyDayE2 := False; {Skip loop}
end;
// -----------------------------  End of Event 2 -------------------------------
//Event 3 ----------------------------------------------------------------------
TodayIsMyDayE3 := ((DayOfWeek(Now) = Monday) or
(DayOfWeek(Now) = Tuesday) or
(DayOfWeek(Now) = Wednesday) or
(DayOfWeek(Now) = Thursday) or
(DayOfWeek(Now) = Friday));
while (TodayIsMyDayE3) and (now >= T[Event3Start]) and (now <= T[Event3End])
do
begin
           WriteLn('Event 3 starts at '+Event3Start);
           WriteLn('Event 3  ends at '+Event3End);
        Queue.Clear;
        cat[CatIDA].QueueBottom(smRandom,NoRules);
        ActivePlayer.FadeToNext;
         repeat
           cat[CatA].QueueBottom(smLRP,EnforceRules);
                 E := Queue.ETA;
                 Count := Count+1;
 	               E := (E-(30/(60*60*24)));
 	               WriteLn('Next track loads at '+TimeToStr(E));
                 PAL.WaitForTime(E);
                     // every 4 tracks put in a station ID and Say The Time.
                     If Count = 4 then
                   begin
	                  cat[CatIDA].QueueBottom(smRandom,NoRules);
	                  // Call SayTime
	                  SayTime;
                   // Sayime code ends here.
	                  Count := 0;
	                  end;
         until (now >= T[Event3End]); // and (now <= T[Event3Limit]);
 TodayIsMyDayE3 := False; {Skip loop}
end;
// -----------------------------  End of Event 3 -------------------------------
//Event 4 ----------------------------------------------------------------------
TodayIsMyDayE4 := ((DayOfWeek(Now) = Monday) or
(DayOfWeek(Now) = Tuesday) or
(DayOfWeek(Now) = Wednesday) or
(DayOfWeek(Now) = Thursday) or
(DayOfWeek(Now) = Friday));
while (TodayIsMyDayE4) and (now >= T[Event4Start]) and (now <= T[Event4End])
do
begin
           WriteLn('Event 4 starts at '+Event4Start);
           WriteLn('Event 4  ends at '+Event4End);
        Queue.Clear;
        cat[CatIDA].QueueBottom(smRandom,NoRules);
        ActivePlayer.FadeToNext;
         repeat
           cat[CatC].QueueBottom(smLRP,EnforceRules);
                 E := Queue.ETA;
                 Count := Count+1;
 	               E := (E-(30/(60*60*24)));
 	               WriteLn('Next track loads at '+TimeToStr(E));
                 PAL.WaitForTime(E);
                     // every 4 tracks put in a station ID and Say The Time.
                     If Count = 4 then
                   begin
	                  cat[CatIDA].QueueBottom(smRandom,NoRules);
	                  // Call SayTime
	                  SayTime;
                   // Sayime code ends here.
	                  Count := 0;
	                  end;
         until (now >= T[Event4End]); // and (now <= T[Event4Limit]);
 TodayIsMyDayE4 := False; {Skip loop}
end;
// -----------------------------  End of Event 4 -------------------------------
//Event 5 ----------------------------------------------------------------------
TodayIsMyDayE5 := ((DayOfWeek(Now) = Saturday));
while (TodayIsMyDayE5) and (now >= T[Event5Start]) and (now <= T[Event5End])
  do
  begin
           WriteLn('Event 5 starts at '+Event5Start);
           WriteLn('Event 5  ends at '+Event5End);
        Queue.Clear;
        cat[CatIDB].QueueBottom(smRandom,NoRules);
        ActivePlayer.FadeToNext;
         repeat
           cat[CatB].QueueBottom(smLRP,EnforceRules);
                 E := Queue.ETA;
                 Count := Count+1;
 	               E := (E-(30/(60*60*24)));
 	               WriteLn('Next track loads at '+TimeToStr(E));
                 PAL.WaitForTime(E);
                     // every 4 tracks put in a station ID and Say The Time.
                     If Count = 4 then
                   begin
	                  cat[CatIDB].QueueBottom(smRandom,NoRules);
	                  // Call SayTime
	                  SayTime;
                   // Sayime code ends here.
	                  Count := 0;
	                  end;
         until (now >= T[Event5End]); // and (now <= T[Event5Limit]);
  TodayIsMyDayE5 := False; {Skip loop}
  end;
// -----------------------------  End of Event 5 -------------------------------
//Event 6 ----------------------------------------------------------------------
TodayIsMyDayE6 := ((DayOfWeek(Now) = Saturday));
while (TodayIsMyDayE6) and (now >= T[Event6Start]) and (now <= T[Event6End])
  do
  begin
           WriteLn('Event 6 starts at '+Event6Start);
           WriteLn('Event 6  ends at '+Event6End);
        Queue.Clear;
        cat[CatIDA].QueueBottom(smRandom,NoRules);
        ActivePlayer.FadeToNext;
         repeat
           cat[CatA].QueueBottom(smLRP,EnforceRules);
                 E := Queue.ETA;
                 Count := Count+1;
 	               E := (E-(30/(60*60*24)));
 	               WriteLn('Next track loads at '+TimeToStr(E));
                 PAL.WaitForTime(E);
                     // every 4 tracks put in a station ID and Say The Time.
                     If Count = 4 then
                   begin
	                  cat[CatIDA].QueueBottom(smRandom,NoRules);
	                  // Call SayTime
	                  SayTime;
                   // Sayime code ends here.
	                  Count := 0;
	                  end;
         until (now >= T[Event6End]); // and (now <= T[Event6Limit]);
  TodayIsMyDayE6 := False; {Skip loop}
  end;
// -----------------------------  End of Event 6 -------------------------------
//Event 7 ----------------------------------------------------------------------
TodayIsMyDayE7 := ((DayOfWeek(Now) = Sunday));
while (TodayIsMyDayE7) and (now >= T[Event7Start]) and (now <= T[Event7End])
do
begin
           WriteLn('Event 7 starts at '+Event7Start);
           WriteLn('Event 7  ends at '+Event7End);
        Queue.Clear;
        cat[CatIDA].QueueBottom(smRandom,NoRules);
        ActivePlayer.FadeToNext;
         repeat
           cat[CatD].QueueBottom(smLRP,EnforceRules);
                 E := Queue.ETA;
                 Count := Count+1;
 	               E := (E-(30/(60*60*24)));
 	               WriteLn('Next track loads at '+TimeToStr(E));
                 PAL.WaitForTime(E);
                     // every 4 tracks put in a station ID and Say The Time.
                     If Count = 4 then
                   begin
	                  cat[CatIDA].QueueBottom(smRandom,NoRules);
	                  // Call SayTime
	                  SayTime;
                   // Sayime code ends here.
	                  Count := 0;
	                  end;
         until (now >= T[Event7End]); // and (now <= T[Event7Limit]);
 TodayIsMyDayE7 := False; {Skip loop}
end;
// -----------------------------  End of Event 7 -------------------------------
//Event 8 ----------------------------------------------------------------------
TodayIsMyDayE8 := ((DayOfWeek(Now) = Sunday));
while (TodayIsMyDayE8) and (now >= T[Event8Start]) and (now <= T[Event8End])
do
begin
           WriteLn('Event 8 starts at '+Event8Start);
           WriteLn('Event 8  ends at '+Event8End);
        Queue.Clear;
        cat[CatIDA].QueueBottom(smRandom,NoRules);
        ActivePlayer.FadeToNext;
         repeat
           cat[CatD].QueueBottom(smLRP,EnforceRules);
                 E := Queue.ETA;
                 Count := Count+1;
 	               E := (E-(30/(60*60*24)));
 	               WriteLn('Next track loads at '+TimeToStr(E));
                 PAL.WaitForTime(E);
                     // every 4 tracks put in a station ID and Say The Time.
                     If Count = 4 then
                   begin
	                  cat[CatIDA].QueueBottom(smRandom,NoRules);
	                  // Call SayTime
	                  SayTime;
                   // Sayime code ends here.
	                  Count := 0;
	                  end;
         until (now >= T[Event8End]); // and (now <= T[Event8Limit]);
 TodayIsMyDayE8 := False; {Skip loop}
end;
// -----------------------------  End of Event 8 -------------------------------

//---------- Functions --------------------------
function ActivePlayer:TPlayer; 
begin 
if DeckA.Status = psPlaying then 
   Result := DeckA
   else
   Result := DeckB;
end; 
//-----------------------------------------------
//-----------------------------------------------
function SayTime:DateTime;
begin
	                  //SayTime code begins here.
	                  UnAdjE := Queue.ETA;
	                  DecodeTime(UnAdjE,hh,mm,ss,ms);
                     if (hh <= 9) then
                       fnamehh := '0'+IntToStr(hh)+'.mp3'   //right file name
                     else
                       fnamehh := IntToStr(hh)+'.mp3';      //right file name
                     if (mm <= 9) then
                       fnamemm := '0'+IntToStr(mm)+'.mp3'   //right file name
                     else
                       fnamemm := IntToStr(mm)+'.mp3';      //right file name
                       fname := 'Header.mp3';
                       WriteLn('SayTime at '+IntToStr(hh)+':'+IntToStr(mm));
                       Queue.AddFile(TimeHeader+fname,ipBottom);
                       Queue.AddFile(TimeHours+fnamehh,ipBottom);
                       Queue.AddFile(TimeMinutes+fnamemm,ipBottom);
                    // Sayime code ends here.
end;
//---------------------------------------------------