home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / audio / saveaudio.m < prev    next >
Encoding:
Text File  |  1999-11-02  |  2.4 KB  |  90 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. ## -*- texinfo -*- 
  21. ## @deftypefn {Function File} {} saveaudio (@var{name}, @var{x}, @var{ext}, @var{bps})
  22. ## Saves a vector @var{x} of audio data to the file
  23. ## @file{@var{name}.@var{ext}}.  The optional parameters @var{ext} and
  24. ## @var{bps} determine the encoding and the number of bits per sample used
  25. ## in the audio file (see @code{loadaudio});  defaults are @file{lin} and
  26. ## 8, respectively.
  27. ## @end deftypefn
  28.  
  29. ## See also: lin2mu, mu2lin, loadaudio, playaudio, setaudio, record
  30.  
  31. ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
  32. ## Created: 5 September 1994
  33. ## Adapted-By: jwe
  34.  
  35. function saveaudio (name, X, ext, bit)
  36.  
  37.   if (nargin < 2 || nargin > 4)
  38.     usage ("saveaudio (X, name [, ext [, bit]])");
  39.   endif
  40.  
  41.   if (nargin == 2)
  42.     ext = "lin";
  43.   endif
  44.  
  45.   if (nargin < 4)
  46.     bit = 8;
  47.   elseif (bit != 8 && bit != 16)
  48.     error ("saveaudio: bit must be either 8 or 16");
  49.   endif
  50.  
  51.   [nr, nc] = size (X);
  52.   if (nc != 1)
  53.     if (nr == 1)
  54.       X = X';
  55.       nr = nc;
  56.     else
  57.       error ("saveaudio: X must be a vector.");
  58.     endif
  59.   endif
  60.  
  61.   num = fopen ([name, ".", ext], "wb");
  62.  
  63.   if (strcmp (ext, "lin") || strcmp (ext, "raw"))
  64.     if (bit == 8)
  65.       ld = max (abs (X));
  66.       if (ld > 127)   # convert 16 to 8 bit
  67.     if (ld < 16384)
  68.       sc = 64 / ld;
  69.     else
  70.       sc = 1 / 256;
  71.     endif
  72.     X = fix (X * sc);
  73.       endif
  74.       X = X + 127;
  75.       c = fwrite (num, X, "uchar");
  76.     else
  77.       c = fwrite (num, X, "short");
  78.     endif
  79.   elseif (strcmp (ext, "mu") || strcmp (ext, "au") || strcmp (ext, "snd"))
  80.     Y = lin2mu (X);
  81.     c = fwrite (num, Y, "uchar");
  82.   else
  83.     fclose (num);
  84.     error ("saveaudio does not support given extension");
  85.   endif
  86.  
  87.   fclose (num);
  88.  
  89. endfunction
  90.