[comp.sys.next] what is the mu-law

das15@cunixa.cc.columbia.edu (Douglas A Scott) (02/26/91)

Hi.  Would someone kindly explain how mu-law encoding works in the 8-bit
NeXT soundfiles?  I am trying to expand a soundfile-editing program to allow
it to read and display that type of file (it currently reads 16- and 32-bit
soundfiles).
The program I have is not native to the NeXT, so it does not automatically
handle such encoded files.

Thank you very much for any information.
___________________________________________________________________________
Douglas Scott
zardoz!doug%woof.columbia.edu

eps@toaster.SFSU.EDU (Eric P. Scott) (02/26/91)

mu-law is a logarithmic transformation that provides
approximately the same fidelity in 8 bits as 12-13 bits of
linear representation would.  Treat it as compressed data;
don't do any math on it.  (Specifics of mu-law and A-law
encoding can be found in telecommunications texts; they
are standardized.)

In 16-bit linear, you deal with signed integer values.
0 is zero, and the range is +-32767.

mu-law data is stored in an inverted sign+magnitude form:

+-+-+-+-+-+-+-+-+
| |             |
+-+-+-+-+-+-+-+-+
 ^   level; all ones is zero amplitude; all zeros is maximum
 |
0 = negative
1 = positive

The levels are nonlinear; there is finer resolution at the
low end.


NeXT provides two functions to convert between 16-bit linear
and 8-bit mu-law:

#import <sound/convertsound.h>
unsigned char SNDMulaw(short)	/* linear to mu-law */
short SNDiMulaw(unsigned char)	/* mu-law to linear */

To "mix" two mu-law sounds, you have to do something like:
SNDMulaw(SNDiMulaw(one)+SNDiMulaw(two))

On SunOS 4.1 and later, the equivalents are:
#include <multimedia/ulaw2linear.h>
unsigned char audio_s2u(short)	/* linear to mu-law */
short audio_u2s(unsigned char)	/* mu-law to linear */


Note that sound files with data format SND_FORMAT_MULAW_8,
sampling rate 8012 and channel count 1 are binary compatible
and fully interchangeable between the NeXT and the Sun!

Note also that you shouldn't attempt to use the full dynamic
range of mu-law; "normal" tends to be 9-12 dB down.

					-=EPS=-

		8-bit mu-Law to 16-bit Linear

  0 -32124 |  52  -3388 | 104   -244 | 156   9852 | 208    876
  1 -31100 |  53  -3260 | 105   -228 | 157   9340 | 209    844
  2 -30076 |  54  -3132 | 106   -212 | 158   8828 | 210    812
  3 -29052 |  55  -3004 | 107   -196 | 159   8316 | 211    780
  4 -28028 |  56  -2876 | 108   -180 | 160   7932 | 212    748
  5 -27004 |  57  -2748 | 109   -164 | 161   7676 | 213    716
  6 -25980 |  58  -2620 | 110   -148 | 162   7420 | 214    684
  7 -24956 |  59  -2492 | 111   -132 | 163   7164 | 215    652
  8 -23932 |  60  -2364 | 112   -120 | 164   6908 | 216    620
  9 -22908 |  61  -2236 | 113   -112 | 165   6652 | 217    588
 10 -21884 |  62  -2108 | 114   -104 | 166   6396 | 218    556
 11 -20860 |  63  -1980 | 115    -96 | 167   6140 | 219    524
 12 -19836 |  64  -1884 | 116    -88 | 168   5884 | 220    492
 13 -18812 |  65  -1820 | 117    -80 | 169   5628 | 221    460
 14 -17788 |  66  -1756 | 118    -72 | 170   5372 | 222    428
 15 -16764 |  67  -1692 | 119    -64 | 171   5116 | 223    396
 16 -15996 |  68  -1628 | 120    -56 | 172   4860 | 224    372
 17 -15484 |  69  -1564 | 121    -48 | 173   4604 | 225    356
 18 -14972 |  70  -1500 | 122    -40 | 174   4348 | 226    340
 19 -14460 |  71  -1436 | 123    -32 | 175   4092 | 227    324
 20 -13948 |  72  -1372 | 124    -24 | 176   3900 | 228    308
 21 -13436 |  73  -1308 | 125    -16 | 177   3772 | 229    292
 22 -12924 |  74  -1244 | 126     -8 | 178   3644 | 230    276
 23 -12412 |  75  -1180 | 127      0 | 179   3516 | 231    260
 24 -11900 |  76  -1116 | 128  32124 | 180   3388 | 232    244
 25 -11388 |  77  -1052 | 129  31100 | 181   3260 | 233    228
 26 -10876 |  78   -988 | 130  30076 | 182   3132 | 234    212
 27 -10364 |  79   -924 | 131  29052 | 183   3004 | 235    196
 28  -9852 |  80   -876 | 132  28028 | 184   2876 | 236    180
 29  -9340 |  81   -844 | 133  27004 | 185   2748 | 237    164
 30  -8828 |  82   -812 | 134  25980 | 186   2620 | 238    148
 31  -8316 |  83   -780 | 135  24956 | 187   2492 | 239    132
 32  -7932 |  84   -748 | 136  23932 | 188   2364 | 240    120
 33  -7676 |  85   -716 | 137  22908 | 189   2236 | 241    112
 34  -7420 |  86   -684 | 138  21884 | 190   2108 | 242    104
 35  -7164 |  87   -652 | 139  20860 | 191   1980 | 243     96
 36  -6908 |  88   -620 | 140  19836 | 192   1884 | 244     88
 37  -6652 |  89   -588 | 141  18812 | 193   1820 | 245     80
 38  -6396 |  90   -556 | 142  17788 | 194   1756 | 246     72
 39  -6140 |  91   -524 | 143  16764 | 195   1692 | 247     64
 40  -5884 |  92   -492 | 144  15996 | 196   1628 | 248     56
 41  -5628 |  93   -460 | 145  15484 | 197   1564 | 249     48
 42  -5372 |  94   -428 | 146  14972 | 198   1500 | 250     40
 43  -5116 |  95   -396 | 147  14460 | 199   1436 | 251     32
 44  -4860 |  96   -372 | 148  13948 | 200   1372 | 252     24
 45  -4604 |  97   -356 | 149  13436 | 201   1308 | 253     16
 46  -4348 |  98   -340 | 150  12924 | 202   1244 | 254      8
 47  -4092 |  99   -324 | 151  12412 | 203   1180 | 255      0
 48  -3900 | 100   -308 | 152  11900 | 204   1116 |
 49  -3772 | 101   -292 | 153  11388 | 205   1052 |
 50  -3644 | 102   -276 | 154  10876 | 206    988 |
 51  -3516 | 103   -260 | 155  10364 | 207    924 |

ddj@zardoz.club.cc.cmu.edu (Doug DeJulio) (02/26/91)

Does anybody have a utility to extract the data from a soundfile and
put it into an ASCII form (ie. digit digit digit newline)?  I'm
looking to generate a list of numbers that I can feed into
Mathematica, so I can do graphs and fourier transforms and other sorts
of data manipulation on sounds (I'm practicing 'cause I need to do the
same sorts of things on NMR data in the near future).
-- 
Doug DeJulio
ddj@zardoz.club.cc.cmu.edu (NeXT mail)
dd26+@andrew.cmu.edu (AMS/ATK mail)