MODULE NarDemo;

(** ------------------------------------------------------------------

          Commodore Amiga narrator software demonstration module

      (c) Copyright 1986 Modula-2 Software Ltd.  All Rights Reserved
      (c) Copyright 1986 TDI Software, Inc.      All Rights Reserved

    ------------------------------------------------------------------ **)


(* VERSION FOR COMMODORE AMIGA

     Original Author : Paul Curtis, Modula-2 Software Ltd.  29-Dec-85

     Version         : 0.10b  10-Jul-86  Paul Curtis, Modula-2 Software Ltd.
                         Compiler 2.20a modifications.
                       0.10a  24-Jan-86  Paul Curtis, Modula-2 Software Ltd.
                         Finally found the elusive bug in this program,
                         after many unhappy hours. Hey! it seems to work.
                       0.00a  29-Dec-85  Paul Curtis, Modula-2 Software Ltd.
                         Original

     Notes           : To use this program, type "NarDemo something to say".

  *)


(*$S-,$T-,$A+*)


FROM AudioDevice IMPORT AudioChannelSet, AudioChannels;
FROM TranslatorLibrary IMPORT Translate, TranslatorName, TranslatorBase;
FROM IO IMPORT BeginIO, WaitIO, CmdWrite, ioFlagSet;
FROM NarratorDevice IMPORT NarratorName, NarratorRB, Male, Natural;
FROM Devices IMPORT OpenDevice, CloseDevice;
FROM Libraries IMPORT OpenLibrary, CloseLibrary;
FROM PortUtils IMPORT CreatePort, DeletePort;
FROM SYSTEM IMPORT BYTE, ADDRESS, ADR, TSIZE;
FROM AMIGAX IMPORT CLineLen, CLinePtr;


CONST
  defStr = "Type NarDemo and something to say.";
  defLen = 34;  (* length(defStr) *)

VAR
  say: POINTER TO ARRAY [0..1] OF CHAR;
  phons: ARRAY [0..1999] OF CHAR;
  NRB: NarratorRB;
  chanMasks: ARRAY [0..3] OF AudioChannelSet;
  err: LONGINT;


BEGIN
  TranslatorBase := OpenLibrary(TranslatorName,0);
  IF TranslatorBase # 0 THEN

    (* create a reply port for DoIO *)
    NRB.message.ioReq.ioMessage.mnReplyPort := CreatePort("sound example",0);
    IF ADDRESS(NRB.message.ioReq.ioMessage.mnReplyPort) # 0 THEN

      NRB.message.ioLength := 0;
      NRB.message.ioReq.ioMessage.mnLength := TSIZE(NarratorRB);

      (* open the narrator device *)
      IF OpenDevice(NarratorName,0,ADR(NRB),0) = 0 THEN

        IF CLineLen <= 2 THEN (* nothing given to say *)
          err := Translate(defStr,defLen,phons,HIGH(phons));
        ELSE
          say := CLinePtr;
          err := Translate(say^,CLineLen,phons,HIGH(phons));
        END;

        IF err = 0 THEN

          NRB.sex := Male;
          NRB.mode := Natural;
          NRB.mouths := BYTE(0);

          WITH NRB.message DO
            ioReq.ioCommand := CmdWrite;
            ioData := ADR(phons);
            ioLength := 0;
            ioOffset := 0;
              
            (* calculate length of phoneme string, assign to ioLength *)
            WHILE phons[CARDINAL(ioLength)] # 0C DO
              INC(ioLength);
            END;

            ioActual := 0;
            ioReq.ioError := BYTE(0);
            ioReq.ioFlags := ioFlagSet{};

          END;

          (* set up preferences for channel masks *)
          chanMasks[0] := AudioChannelSet{Left0,Right0};
          chanMasks[1] := AudioChannelSet{Left1,Right1};
          chanMasks[2] := AudioChannelSet{Left0};
          chanMasks[3] := AudioChannelSet{Right0};
          NRB.chMasks := ADR(chanMasks);
          NRB.nmMasks := 4;

          BeginIO(NRB.message.ioReq);

          err := WaitIO(NRB.message.ioReq);

        END;
        CloseDevice(ADR(NRB));
      END;
      DeletePort(NRB.message.ioReq.ioMessage.mnReplyPort);
    END;
    CloseLibrary(TranslatorBase);
  END;
END NarDemo.
