(******************************************************************************
 *
 * COPYRIGHT: Unless otherwise noted, all files are Copyright (c) 1992, 1993
 * Commodore-Amiga, Inc.  All rights reserved.
 *
 * DISCLAIMER: This software is provided "as is".  No representations or
 * warranties are made with respect to the accuracy, reliability, performance,
 * currentness, or operation of this software, and all use is at your own risk.
 * Neither commodore nor the authors assume any responsibility or liability
 * whatsoever with respect to your use of this software.
 *
 ******************************************************************************
 * playsound.c
 * Simple example showing how to play a sound file
 * Written by David N. Junod
 * ported to Oberon by Albert Weinert (Feb. 1994) needs V40 Interfaces
 *
 *)
MODULE playsound;

IMPORT
  Dos,
  clf := Classface,
  I := Intuition,
  dt := Datatypes,
  e := Exec,
  pf := Printf,
  u := Utility,
  y := SYSTEM;

(*****************************************************************************)

CONST template = "NAME/A";

(*****************************************************************************)

VAR
    (* Argument parsing variables *)
    opts : STRUCT( d : Dos.ArgsStruct );
             name : e.STRPTR;
           END;
    rdargs : Dos.RDArgsPtr;

    (* Object variables *)
    dtt : dt.Trigger;
    name : e.STRPTR;
    o : I.ObjectPtr;
    errnum : LONGINT;
    errbuff : ARRAY 80 OF CHAR;

BEGIN
  IF dt.base = NIL THEN HALT( 20 ) END;
  rdargs := Dos.ReadArgs( template, opts, NIL );
  IF rdargs # NIL THEN
    (* Open the sound object *)
    o := dt.NewDTObject( opts.name^,
                        (* Say that the source is a file *)
                         dt.sourceType,        dt.stFile,

                        (* We will only accept sound DataTypes *)
                         dt.groupID,           dt.sound,

                        (* Set to maximum volume *)
                         dt.volume,           64,

                        (* Only play the sound once *)
                         dt.cycles,           1,

                        (* We want to be notified when the sound stops playing, so
                         * we provide a signal task and a signal *)
                         dt.signalTask,      e.FindTask (NIL),
                         dt.signalBit,       LONGSET{Dos.ctrlF},

                        (* No more attributes *)
                         u.done );
    IF o # NIL THEN
      IF dt.GetDTAttrs( o, dt.objName, y.ADR( name ), u.done ) # 0 THEN
        IF name # NIL THEN
          Dos.PrintF( "     Name: %s\n", name );
        END;
      END;

      (* Fill out the method message *)
      dtt.msg.methodID := dt.mTrigger;
      dtt.gInfo    := NIL;
      dtt.function := dt.play;
      dtt.data     := NIL;

      (* Play the sound *)
      Dos.PrintF ("\nplay sound...\n");
      IF clf.DoMethodA (o, dtt) = 0 THEN END;

      IF e.Wait ( LONGSET{Dos.ctrlF} ) = LONGSET{} THEN END;
      Dos.PrintF ("done\n");

      (* Get rid of the object *)
      dt.DisposeDTObject (o);
    ELSE
      errnum := Dos.IoErr();
      IF errnum >= dt.errorUnknownDatatype THEN
        pf.SPrintf1(errbuff, dt.GetDTString (errnum)^, opts.name);
      ELSE
        IF Dos.Fault (errnum, NIL, errbuff, SIZE(errbuff)) = 0 THEN END;
      END;
      Dos.PrintF ("%s\nerror #%ld\n", y.ADR( errbuff ), errnum);
    END;
   (* Free the allocated memory after ReadArgs *)
    Dos.FreeArgs (rdargs);
  ELSE
    (* Show the failure message *)
    IF Dos.PrintFault (Dos.IoErr(), NIL) THEN END;
  END;
END playsound.
