]> Creatis software - CreaPhase.git/blob - octave_packages/m/audio/loadaudio.m
update packages
[CreaPhase.git] / octave_packages / m / audio / loadaudio.m
1 ## Copyright (C) 1995-2012 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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} loadaudio (@var{name}, @var{ext}, @var{bps})
21 ## Load audio data from the file @file{@var{name}.@var{ext}} into the
22 ## vector @var{x}.
23 ##
24 ## The extension @var{ext} determines how the data in the audio file is
25 ## interpreted; the extensions @file{lin} (default) and @file{raw}
26 ## correspond to linear, the extensions @file{au}, @file{mu}, or @file{snd}
27 ## to mu-law encoding.
28 ##
29 ## The argument @var{bps} can be either 8 (default) or 16, and specifies
30 ## the number of bits per sample used in the audio file.
31 ## @seealso{lin2mu, mu2lin, saveaudio, playaudio, setaudio, record}
32 ## @end deftypefn
33
34
35 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
36 ## Created: 10 April 1994
37 ## Adapted-By: jwe
38
39 function X = loadaudio (name, ext, bps)
40
41   if (nargin == 0 || nargin > 3)
42     print_usage ();
43   endif
44
45   if (nargin == 1)
46     ext = "lin";
47   endif
48
49   if (nargin < 3)
50     bps = 8;
51   elseif (bps != 8 && bps != 16)
52     error ("loadaudio: BPS 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 (bps == 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")
66           || strcmp (ext, "snd") || strcmp(ext, "ul"))
67     [Y, c] = fread (num, inf, "uchar");
68     ## remove file header
69     m = find (Y(1:64) == 0, 1, "last");
70     if (! isempty (m))
71       Y(1:m) = [];
72     endif
73     X = mu2lin (Y, bps);
74   else
75     fclose (num);
76     error ("loadaudio: unsupported extension");
77   endif
78
79   fclose (num);
80
81 endfunction