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