#!../src/tops  -i -s ../sys  -u ../usr
/*
Program Tops - a stack-based computing environment
Copyright (C) 1999-2013  Dale R. Williamson

Author: Dale R. Williamson <dale.williamson@prodigy.net>

File test/tsnd2  December 2008

Using the sound file queue.  

The sound file queuing system detects when the file playing will end
(within about 0.5 sec) and waits before playing the next one.

This example queues up three sound files and plays them in succession.

Files for sounds GONG, BONG and TING are all made from the same file,
BONG1.WAV, by varying the sample rate; see sys/snd.v.

References: 
   Words for sound (including sound file queuing system), file sys/snd.v
   Sound file server, file usr/tops_snd

Updates:
   Fri Dec 28 14:21:59 PST 2012
     Skip sound file server branch; its timing may run this test poorly.

   Sun Jun 27 13:16:44 PDT 2010
     Word wavque_add() now calls wavque_play(), so remove redundant
     calls to wavque_play() in this script to avoid a busy message.

----------------------------------------------------------------------*/

   { S = -1; }

   source(catmsg(no), "snd.v"); // load words for sound

   if(!fname("/dev", "dsp"))
      exit(nl(.(" tsnd2: sound device not found, test skipped")));

   if(!dsp_ready)
      exit(nl(.(" tsnd2: sound device not found, test skipped")));

   nl(.("tsnd2: run sound files in succession using sound file queue"));
   nl(.("tsnd2: next file is not played until resonance dies out")); 
   nl(.("tsnd2: is speaker on?"));
   .("tsnd2: writing bytes to sound device for about 25 seconds...");

 //IF(SNDSERVER_ON) // is sound file server running? (see usr/tops_snd)
   IF(no) // skip this branch; its timing may run this test poorly

   /* In this branch, the sound file server is already running the
      wave file queuing system.  The following lines connect to the
      sound file server and run the same lines remotely that are run
      locally in the ELSE branch below. */

      S = CLIENT(IPloop, SNDSERVER); // socket S connects to server

      W1 = GONG(2);
   /* Note << ... >> to enclose a postfix phrase that is sent through
      socket S to run on the sound file server: */
      remoterun((<< W1 quoted " wavque_add" + >>), S); // W1 queued up

      W2 = BONG(2);
      << W2 quoted " wavque_add" + S remoterun >>      // W2 queued up

      W3 = TING(2);
      remoterun((<< W3 quoted " wavque_add" + >>), S); // W3 queued up

   ELSE // SNDSERVER is not running
      wavque_start(); // start the queue

      wavque_add((W1 = GONG(2))); // W1 queued up
      wavque_add((W2 = BONG(2))); // W2 queued up
      wavque_add((W3 = TING(2))); // W3 queued up
   THEN

   t = wavTime(W1) + wavTime(W2) + wavTime(W3); // total playing time

   idle(t+5); // add a 5 second pad and idle here to prevent script from
              // running to completion before the last file starts

   sclose(S);

   .(nl, "tsnd2: sound queuing test complete");
   nl; nl; nl;

   
