2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2303
|
19 |
3332
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} loadaudio (@var{name}, @var{ext}, @var{bps}) |
|
22 ## Loads audio data from the file @file{@var{name}.@var{ext}} into the |
3426
|
23 ## vector @var{x}. |
2311
|
24 ## |
3332
|
25 ## The extension @var{ext} determines how the data in the audio file is |
|
26 ## interpreted; the extensions @file{lin} (default) and @file{raw} |
|
27 ## correspond to linear, the extensions @file{au}, @file{mu}, or @file{snd} |
|
28 ## to mu-law encoding. |
3426
|
29 ## |
3332
|
30 ## The argument @var{bps} can be either 8 (default) or 16, and specifies |
|
31 ## the number of bits per sample used in the audio file. |
|
32 ## @end deftypefn |
5053
|
33 ## |
3408
|
34 ## @seealso{lin2mu, mu2lin, saveaudio, playaudio, setaudio, and record} |
2311
|
35 |
2312
|
36 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
|
37 ## Created: 10 April 1994 |
|
38 ## Adapted-By: jwe |
|
39 |
1636
|
40 function X = loadaudio (name, ext, bit) |
2325
|
41 |
1636
|
42 if (nargin == 0 || nargin > 3) |
3456
|
43 usage ("loadaudio (name, ext, bit)"); |
1636
|
44 endif |
|
45 |
|
46 if (nargin == 1) |
|
47 ext = "lin"; |
|
48 endif |
|
49 |
|
50 if (nargin < 3) |
|
51 bit = 8; |
|
52 elseif (bit != 8 && bit != 16) |
|
53 error ("loadaudio: bit must be either 8 or 16"); |
|
54 endif |
|
55 |
|
56 name = [name, ".", ext]; |
3167
|
57 num = fopen (name, "rb"); |
1636
|
58 |
3167
|
59 if (strcmp (ext, "lin") || strcmp (ext, "raw") || strcmp (ext, "pcm")) |
1636
|
60 if (bit == 8) |
|
61 [Y, c] = fread (num, inf, "uchar"); |
|
62 X = Y - 127; |
|
63 else |
|
64 [X, c] = fread (num, inf, "short"); |
|
65 endif |
3471
|
66 elseif (strcmp (ext, "mu") || strcmp (ext, "au") |
|
67 || strcmp (ext, "snd") || strcmp(ext, "ul")) |
1636
|
68 [Y, c] = fread (num, inf, "uchar"); |
2303
|
69 ## remove file header |
1636
|
70 m = max (find (Y(1:64) == 0)); |
|
71 if (! isempty (m)) |
|
72 Y(1:m) = []; |
|
73 endif |
|
74 X = mu2lin (Y, bit); |
|
75 else |
|
76 fclose (num); |
|
77 error ("loadaudio does not support given extension"); |
|
78 endif |
|
79 |
|
80 fclose (num); |
2325
|
81 |
1636
|
82 endfunction |