(*--------------------------------------------------------------------*)
(*                                                                    *)
(* Program  . . . . . .  CDTEST                                       *)
(* Source file  . . . .  CDTEST.PAS                                   *)
(* Language . . . . . .  Object Pascal                                *)
(* Compiler . . . . . .  Virtual Pascal/2 V1.10                       *)
(* Operating system . .  OS/2 Warp V3                                 *)
(* Author . . . . . . .  Th. Trepl, Munich Germany                    *)
(*                       101353,671@CompuServ.com                     *)
(* Reference  . . . . .  -                                            *)
(*                                                                    *)
(*--------------------------------------------------------------------*)
(*                                                                    *)
(* Description                                                        *)
(* -----------                                                        *)
(*                                                                    *)
(* A very simple program to play audio tracks of a CD-ROM drive.      *)
(* It build a random queue of all the titles and plays them.          *)
(* Control is available using the keyboard.                           *)
(*                                                                    *)
(* The only sence of the program is to demonstrate the usage of the   *)
(* modules CDAUDIO.PAS and CDBASE.PAS.                                *)
(*                                                                    *)
(* Key     Function                                                   *)
(*  X      Exit the program                                           *)
(*  +      Play next title of random queue                            *)
(*  P      Set CD-Player in Pause Mode                                *)
(*  R      Resume from Pause Mode                                     *)
(*                                                                    *)
(*--------------------------------------------------------------------*)
Program CdTest;
Uses Crt, CDAudio;

Var CD : TCDPlayer;
    SelectedSongs : Array[1..255] Of Byte;
    N, Cnt : Integer;
    c : Char;
    EndLoop : Boolean;

Procedure BuildRandom;
Var i, t, Cnt : Integer;
    TrackTbl : Array[1..255] Of Byte;
Begin
  WriteLn('Random sequence:');
  i := 1;
  For t:=CD.FirstTrack To CD.LastTrack Do
    Begin
      TrackTbl[i] := t;
      Inc(i);
    End;

  Randomize;
  Cnt := CD.LastTrack - CD.FirstTrack + 1;
  t := 1;
  While Cnt > 1 Do
    Begin
      i := Random(Cnt) + 1;
      SelectedSongs[t] := TrackTbl[i];
      Write(' ',SelectedSongs[t]);
      Move(TrackTbl[i+1], TrackTbl[i], 255-i);
      Dec(Cnt);
      Inc(t);
    End;
  SelectedSongs[t] := TrackTbl[1];
  Write(' ',SelectedSongs[t]);
  WriteLn;
  WriteLn;
End;

Procedure WriteStatus(Fnct : String);
Var Track, Minute, Second : Byte;
Begin
  CD.CurrentPos(Track, Minute, Second);
  Write('(',Fnct,') -',Track:3,': ');
  If Minute < 10 Then Write('0');
  Write(Minute, ':');
  If Second < 10 Then Write('0');
  Write(Second);
  ClrEoL;
  Write(^M);
End;

Begin
  WriteLn;
  WriteLn('Simplest CD-Player');
  WriteLn('------------------');
  WriteLn;
  WriteLn('X - Exit program');
  Writeln('+ - Next title of queue');
  Writeln('P - Pause');
  Writeln('R - Resume from pause');
  Writeln;
  CD := TCDPlayer.Create(Nil);
  CD.OpenPlayer(cDefaultDrive);

  BuildRandom;

  Cnt := CD.LastTrack - CD.FirstTrack + 1;
  For N:=1 To Cnt Do
    Begin
      CD.Play(SelectedSongs[N]);
      EndLoop := False;
      Repeat
        WriteStatus('Play ');
        Delay(500);
        C := #0;
        If KeyPressed Then
          Begin
            c := UpCase(ReadKey);
            Case c Of
              '+' : Begin
                      CD.Stop;
                      EndLoop := True;
                    End;
              'P' : Begin { Pause }
                      CD.Stop;
                      WriteStatus('Pause');
                      Repeat
                        c := UpCase(ReadKey);
                      Until c In ['X','R'];
                      If c = 'R'
                        Then CD.Resume
                        Else EndLoop := True;
                    End;
              'X' : Begin { eXit }
                      CD.Stop;
                      EndLoop := True;
                    End;
            End;
          End;
        EndLoop := EndLoop Or Not CD.Playing;
      Until EndLoop;
      If C = 'X' Then Break;
    End;

  CD.Destroy;
End.

