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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, 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 |
|
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. |
|
29 ## |
|
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 |
3408
|
33 ## @seealso{lin2mu, mu2lin, saveaudio, playaudio, setaudio, and record} |
2311
|
34 |
2312
|
35 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
|
36 ## Created: 10 April 1994 |
|
37 ## Adapted-By: jwe |
|
38 |
1636
|
39 function X = loadaudio (name, ext, bit) |
2325
|
40 |
1636
|
41 if (nargin == 0 || nargin > 3) |
|
42 usage ("loadaudio (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 ("loadaudio: bit must be either 8 or 16"); |
|
53 endif |
|
54 |
|
55 name = [name, ".", ext]; |
3167
|
56 num = fopen (name, "rb"); |
1636
|
57 |
3167
|
58 if (strcmp (ext, "lin") || strcmp (ext, "raw") || strcmp (ext, "pcm")) |
1636
|
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"); |
2303
|
67 ## remove file header |
1636
|
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 ("loadaudio does not support given extension"); |
|
76 endif |
|
77 |
|
78 fclose (num); |
2325
|
79 |
1636
|
80 endfunction |