1 /* 2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.media.sound; 27 28 import java.io.IOException; 29 import java.util.Objects; 30 import java.util.Vector; 31 32 import javax.sound.sampled.AudioFormat; 33 import javax.sound.sampled.AudioInputStream; 34 import javax.sound.sampled.AudioSystem; 35 36 /** 37 * U-law encodes linear data, and decodes u-law data to linear data. 38 * 39 * @author Kara Kytle 40 */ 41 public final class UlawCodec extends SunCodec { 42 43 /* Tables used for U-law decoding */ 44 45 private static final byte[] ULAW_TABH = new byte[256]; 46 private static final byte[] ULAW_TABL = new byte[256]; 47 48 private static final AudioFormat.Encoding[] ulawEncodings = {AudioFormat.Encoding.ULAW, 49 AudioFormat.Encoding.PCM_SIGNED}; 50 51 private static final short seg_end [] = {0xFF, 0x1FF, 0x3FF, 52 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF}; 53 54 /** 55 * Initializes the decode tables. 56 */ 57 static { 58 for (int i=0;i<256;i++) { 59 int ulaw = ~i; 60 int t; 61 62 ulaw &= 0xFF; 63 t = ((ulaw & 0xf)<<3) + 132; 64 t <<= ((ulaw & 0x70) >> 4); 65 t = ( (ulaw&0x80) != 0 ) ? (132-t) : (t-132); 66 67 ULAW_TABL[i] = (byte) (t&0xff); 68 ULAW_TABH[i] = (byte) ((t>>8) & 0xff); 69 } 70 } 71 72 /** 73 * Constructs a new ULAW codec object. 74 */ 75 public UlawCodec() { 76 super(ulawEncodings, ulawEncodings); 77 } 78 79 @Override 80 public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){ 81 if( AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding()) ) { 82 if( sourceFormat.getSampleSizeInBits() == 16 ) { 83 AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1]; 84 enc[0] = AudioFormat.Encoding.ULAW; 85 return enc; 86 } else { 87 return new AudioFormat.Encoding[0]; 88 } 89 } else if (AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding())) { 90 if (sourceFormat.getSampleSizeInBits() == 8) { 91 AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1]; 92 enc[0] = AudioFormat.Encoding.PCM_SIGNED; 93 return enc; 94 } else { 95 return new AudioFormat.Encoding[0]; 96 } | 1 /* 2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.media.sound; 27 28 import java.io.IOException; 29 import java.util.Objects; 30 import java.util.Vector; 31 32 import javax.sound.sampled.AudioFormat; 33 import javax.sound.sampled.AudioFormat.Encoding; 34 import javax.sound.sampled.AudioInputStream; 35 import javax.sound.sampled.AudioSystem; 36 import javax.sound.sampled.spi.FormatConversionProvider; 37 38 /** 39 * U-law encodes linear data, and decodes u-law data to linear data. 40 * 41 * @author Kara Kytle 42 */ 43 public final class UlawCodec extends FormatConversionProvider { 44 45 /* Tables used for U-law decoding */ 46 47 private static final byte[] ULAW_TABH = new byte[256]; 48 private static final byte[] ULAW_TABL = new byte[256]; 49 50 private static final short seg_end[] = { 51 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF 52 }; 53 54 /** 55 * Initializes the decode tables. 56 */ 57 static { 58 for (int i=0;i<256;i++) { 59 int ulaw = ~i; 60 int t; 61 62 ulaw &= 0xFF; 63 t = ((ulaw & 0xf)<<3) + 132; 64 t <<= ((ulaw & 0x70) >> 4); 65 t = ( (ulaw&0x80) != 0 ) ? (132-t) : (t-132); 66 67 ULAW_TABL[i] = (byte) (t&0xff); 68 ULAW_TABH[i] = (byte) ((t>>8) & 0xff); 69 } 70 } 71 72 @Override 73 public AudioFormat.Encoding[] getSourceEncodings() { 74 return new Encoding[]{Encoding.ULAW, Encoding.PCM_SIGNED}; 75 } 76 77 @Override 78 public AudioFormat.Encoding[] getTargetEncodings() { 79 return getSourceEncodings(); 80 } 81 82 @Override 83 public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){ 84 if( AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding()) ) { 85 if( sourceFormat.getSampleSizeInBits() == 16 ) { 86 AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1]; 87 enc[0] = AudioFormat.Encoding.ULAW; 88 return enc; 89 } else { 90 return new AudioFormat.Encoding[0]; 91 } 92 } else if (AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding())) { 93 if (sourceFormat.getSampleSizeInBits() == 8) { 94 AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1]; 95 enc[0] = AudioFormat.Encoding.PCM_SIGNED; 96 return enc; 97 } else { 98 return new AudioFormat.Encoding[0]; 99 } |