home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / audio / loadaud.m < prev    next >
Text File  |  1999-04-29  |  3KB  |  81 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:  X = loadaud (name [, ext [, bit]])
  21. ##
  22. ## Loads audio data from the file "name.ext" into the data vector X.
  23. ## Default value for the "ext" argument, which has to be written
  24. ## without the initial ".", is "lin".
  25. ## Currently, the following audio formats are supported:
  26. ## *) mu-law encoding with extension "mu", "au", or "snd"
  27. ## *) linear encoding with extension "lin", "pcm", or "raw"
  28. ##
  29. ## The `bit' argument can be either 8 (default) or 16.
  30. ## Depending on the value of bit, linearly encoded files are
  31. ## interpreted as being in 8 and 16 bit format, respectively, and
  32. ## mu-law encoded files are transformed to 8 and 16-bit linear
  33. ## format, respectively.
  34.  
  35. ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
  36. ## Created: 10 April 1994
  37. ## Adapted-By: jwe
  38.  
  39. function X = loadaud (name, ext, bit)
  40.  
  41.   if (nargin == 0 || nargin > 3)
  42.     usage ("loadaud (name [, ext [, bit]])");
  43.   endif
  44.  
  45.   if (nargin == 1)
  46.     ext = "lin";
  47.   endif
  48.  
  49.   if (nargin < 3)
  50.     bit = 8;
  51.   elseif (bit != 8 && bit != 16)
  52.     error ("loadaud: bit must be either 8 or 16");
  53.   endif
  54.  
  55.   name = [name, ".", ext];
  56.   num = fopen (name, "rb");
  57.  
  58.   if (strcmp (ext, "lin") || strcmp (ext, "raw") || strcmp (ext, "pcm"))
  59.     if (bit == 8)
  60.       [Y, c] = fread (num, inf, "uchar");
  61.       X = Y - 127;
  62.     else
  63.       [X, c] = fread (num, inf, "short");
  64.     endif
  65.   elseif (strcmp (ext, "mu") || strcmp (ext, "au") || strcmp (ext, "snd"))
  66.     [Y, c] = fread (num, inf, "uchar");
  67.     ## remove file header
  68.     m = max (find (Y(1:64) == 0));
  69.     if (! isempty (m))
  70.       Y(1:m) = [];
  71.     endif
  72.     X = mu2lin (Y, bit);
  73.   else
  74.     fclose (num);
  75.     error ("loadaud does not support given extension");
  76.   endif
  77.  
  78.   fclose (num);
  79.  
  80. endfunction
  81.