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