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} {} mu2lin (@var{x}, @var{bps}) |
|
22 ## If the vector @var{x} represents mono audio data in mu-law encoding, |
|
23 ## @code{mu2lin} converts it to linear encoding. The optional argument |
|
24 ## @var{bps} specifies whether the input data uses 8 bit per sample |
|
25 ## (default) or 16 bit. |
|
26 ## @end deftypefn |
|
27 |
|
28 ## See also: lin2mu, loadaudio, saveaudio, playaudio, setaudio, record |
2311
|
29 |
2312
|
30 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at> |
|
31 ## Created: 18 October 1994 |
|
32 ## Adapted-By: jwe |
|
33 |
1636
|
34 function y = mu2lin (x, bit) |
2325
|
35 |
1636
|
36 if (nargin == 1) |
|
37 bit = 8; |
|
38 elseif (nargin == 2) |
|
39 if (bit != 8 && bit != 16) |
|
40 error ("mu2lin: bit must be either 8 or 16"); |
|
41 endif |
|
42 else |
|
43 usage ("y = mu2lin (x [, bit])"); |
|
44 endif |
|
45 |
|
46 if (! is_vector (x)) |
|
47 error ("mu2lin: x must be a vector"); |
|
48 endif |
2325
|
49 |
1636
|
50 exp_lut = [0; 132; 396; 924; 1980; 4092; 8316; 16764]; |
|
51 |
2303
|
52 ## invert x bitwise |
1636
|
53 x = 255 - x; |
|
54 |
2303
|
55 ## determine sign of y |
1636
|
56 sig = (x > 127); |
|
57 |
2303
|
58 ## determine exponent and fraction of y |
1636
|
59 e = fix (x / 16) - 8 .* sig + 1; |
|
60 f = rem (x, 16); |
|
61 |
|
62 sig = 1 - 2 .* sig; |
|
63 y = (pow2 (f, e + 2) + exp_lut (e)) .* sig; |
|
64 |
2303
|
65 ## convert to 8-bit |
1636
|
66 if (bit == 8) |
|
67 ld = max (abs (y)); |
|
68 if (ld < 16384) |
|
69 sc = 64 / ld; |
|
70 else |
|
71 sc = 1 / 256; |
|
72 endif |
|
73 y = fix (y * sc); |
|
74 endif |
|
75 |
|
76 endfunction |