public static byte linear2alaw(short pcm_val) 
    /* 2's complement (16-bit range) */
  {
    byte mask;
    byte seg=8;
    byte aval;
    
    if (pcm_val >= 0) {
      mask = (byte) 0xD5;  /* sign (7th) bit = 1 */
    } else {
      mask = 0x55;  /* sign bit = 0 */
      pcm_val = (short) (-pcm_val - 8);
    }
    
    /* Convert the scaled magnitude to segment number. */
    for (int i = 0; i < 8; i++) {
      if (pcm_val <= seg_end[i]) {
        seg=(byte) i;
        break;
      }
    }
    
    /* Combine the sign, segment, and quantization bits. */
    if (seg >= 8)  /* out of range, return maximum value. */
      return (byte) ((0x7F ^ mask) & 0xFF);
    else {
      aval = (byte) (seg << SEG_SHIFT);
      if (seg < 2)
        aval |= (pcm_val >> 4) & QUANT_MASK;
      else
        aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;
      return (byte) ((aval ^ mask) & 0xFF);
    }
  }