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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
2313
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
2303
|
18 |
3332
|
19 ## -*- texinfo -*- |
3911
|
20 ## @deftypefn {Function File} {} lin2mu (@var{x}, @var{n}) |
|
21 ## Converts audio data from linear to mu-law. Mu-law values use 8-bit |
|
22 ## unsigned integers. Linear values use @var{n}-bit signed integers or |
|
23 ## floating point values in the range -1<=@var{x}<=1 if @var{n} is 0. |
|
24 ## If @var{n} is not specified it defaults to 0, 8 or 16 depending on |
|
25 ## the range values in @var{x}. |
5642
|
26 ## @seealso{mu2lin, loadaudio, saveaudio, playaudio, setaudio, record} |
3332
|
27 ## @end deftypefn |
5642
|
28 |
2311
|
29 |
3911
|
30 ## Author: Andreas Weingessel <Andreas.Weingessel@ci.tuwien.ac.at> |
2312
|
31 ## Created: 17 October 1994 |
|
32 ## Adapted-By: jwe |
|
33 |
3911
|
34 function y = lin2mu (x, bit) |
2325
|
35 |
3911
|
36 if (nargin == 1) |
|
37 range = max (abs (x (:))); |
|
38 if (range <= 1) |
|
39 bit = 0; |
|
40 elseif (range <= 128) |
|
41 bit = 8; |
|
42 warning ("lin2mu: no precision specified, so using %d", bit); |
|
43 else |
|
44 bit = 16; |
|
45 endif |
|
46 elseif (nargin == 2) |
|
47 if (bit != 0 && bit != 8 && bit != 16) |
|
48 error ("lin2mu: bit must be either 0, 8 or 16"); |
|
49 endif |
|
50 else |
6046
|
51 print_usage (); |
1636
|
52 endif |
2325
|
53 |
3911
|
54 ## Transform real and n-bit format to 16-bit. |
|
55 if (bit == 0) |
|
56 ## [-1,1] -> [-32768, 32768] |
|
57 x = 32768 * x; |
|
58 elseif (bit != 16) |
|
59 x = 2^(16-bit) .* x; |
1636
|
60 endif |
|
61 |
3911
|
62 ## Determine sign of x, set sign(0) = 1. |
1636
|
63 sig = sign(x) + (x == 0); |
|
64 |
3911
|
65 ## Take absolute value of x, but force it to be smaller than 32636; |
|
66 ## add bias. |
|
67 x = min (abs (x), 32635) + 132; |
1636
|
68 |
3911
|
69 ## Find exponent and fraction of bineary representation. |
1636
|
70 [f, e] = log2 (x); |
|
71 |
|
72 y = 64 * sig - 16 * e - fix (32 * f) + 335; |
|
73 |
|
74 endfunction |