home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / audio / playaud.m < prev    next >
Text File  |  1999-04-29  |  3KB  |  84 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage: playaud (name [, ext])
  21. ##        playaud (X)
  22. ##
  23. ## `playaud ("name" [, "ext"])' plays the audio file "name.ext". The
  24. ## default value for the "ext" argument, which has to be written
  25. ## without the initial ".", is "lin".
  26. ## Currently, the following audio formats are suppored:
  27. ## *) linear encoding with extension "lin" or "raw", played using
  28. ##    /dev/dsp
  29. ## *) mu-law encoding with extension "mu", "au" or "snd", played
  30. ##    using /dev/audio
  31. ##
  32. ## `playaud (X)' plays the audio data contained in the vector X.
  33.  
  34. ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
  35. ## Created: 11 April 1994
  36. ## Adapted-By: jwe
  37.  
  38. function playaud (name, ext)
  39.  
  40.   usage_msg = "playaud (name [, ext])  or  playaud (X)";
  41.  
  42.   if (nargin == 1 && is_vec (name) && ! isstr (name))
  43.     ## play a vector
  44.     [nr, nc] = size (name);
  45.     if (nc != 1)
  46.       if (nr == 1)
  47.     name = name';
  48.     nr = nc;
  49.       else
  50.     error ("playaud: X must be a vector");
  51.       endif
  52.     endif
  53.     X = name + 127;
  54.     unwind_protect
  55.       file = tmpnam ();
  56.       num = fopen (file, "wb");
  57.       c = fwrite (num, X, "uchar");
  58.       fclose (num);
  59.       system (sprintf ("cat %s > /dev/dsp", file));
  60.     unwind_protect_cleanup
  61.       unlink (file);
  62.     end_unwind_protect
  63.   elseif (nargin >= 1 && isstr (name))
  64.     ## play a file
  65.     if (nargin == 1)
  66.       name = [name, ".lin"];
  67.     elseif (nargin == 2)
  68.       name = [name, ".", ext];
  69.     else
  70.       usage (usage_msg);
  71.     endif
  72.     if (strcmp (ext, "lin") || strcmp (ext, "raw"))
  73.       system (sprintf ("cat %s > /dev/dsp", name));
  74.     elseif (strcmp (ext, "mu") || strcmp (ext, "au") || strcmp (ext, "snd"))
  75.       system (sprintf ("cat %s > /dev/audio", name));
  76.     else
  77.       error ("playaud does not support given extension");
  78.     endif
  79.   else
  80.     usage (usage_msg);
  81.   endif
  82.  
  83. endfunction
  84.