public AudioFormat[] getTargetFormats( AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) { if ((AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding) && AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding())) || (AudioFormat.Encoding.ULAW.equals(targetEncoding) && AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding()))) { return getOutputFormats(sourceFormat); } else { return new AudioFormat[0]; } }
public AudioInputStream getAudioInputStream( AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) { AudioFormat sourceFormat = sourceStream.getFormat(); AudioFormat.Encoding sourceEncoding = sourceFormat.getEncoding(); if (sourceEncoding.equals(targetEncoding)) { return sourceStream; } else { AudioFormat targetFormat = null; if (!isConversionSupported(targetEncoding, sourceStream.getFormat())) { throw new IllegalArgumentException( "Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString()); } if (AudioFormat.Encoding.ULAW.equals(sourceEncoding) && AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding)) { targetFormat = new AudioFormat( targetEncoding, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), 2 * sourceFormat.getChannels(), sourceFormat.getSampleRate(), sourceFormat.isBigEndian()); } else if (AudioFormat.Encoding.PCM_SIGNED.equals(sourceEncoding) && AudioFormat.Encoding.ULAW.equals(targetEncoding)) { targetFormat = new AudioFormat( targetEncoding, sourceFormat.getSampleRate(), 8, sourceFormat.getChannels(), sourceFormat.getChannels(), sourceFormat.getSampleRate(), false); } else { throw new IllegalArgumentException( "Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString()); } return getAudioInputStream(targetFormat, sourceStream); } }
UlawCodecStream(AudioInputStream stream, AudioFormat outputFormat) { super(stream, outputFormat, AudioSystem.NOT_SPECIFIED); AudioFormat inputFormat = stream.getFormat(); // throw an IllegalArgumentException if not ok if (!(isConversionSupported(outputFormat, inputFormat))) { throw new IllegalArgumentException( "Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString()); } // $$fb 2002-07-18: fix for 4714846: JavaSound ULAW (8-bit) encoder erroneously depends on // endian-ness boolean PCMIsBigEndian; // determine whether we are encoding or decoding if (AudioFormat.Encoding.ULAW.equals(inputFormat.getEncoding())) { encode = false; encodeFormat = inputFormat; decodeFormat = outputFormat; PCMIsBigEndian = outputFormat.isBigEndian(); } else { encode = true; encodeFormat = outputFormat; decodeFormat = inputFormat; PCMIsBigEndian = inputFormat.isBigEndian(); tempBuffer = new byte[tempBufferSize]; } // setup tables according to byte order if (PCMIsBigEndian) { tabByte1 = ULAW_TABH; tabByte2 = ULAW_TABL; highByte = 0; lowByte = 1; } else { tabByte1 = ULAW_TABL; tabByte2 = ULAW_TABH; highByte = 1; lowByte = 0; } // set the AudioInputStream length in frames if we know it if (stream instanceof AudioInputStream) { frameLength = ((AudioInputStream) stream).getFrameLength(); } // set framePos to zero framePos = 0; frameSize = inputFormat.getFrameSize(); if (frameSize == AudioSystem.NOT_SPECIFIED) { frameSize = 1; } }
/* public AudioFormat[] getOutputFormats(AudioFormat inputFormat) { */ private AudioFormat[] getOutputFormats(AudioFormat inputFormat) { Vector formats = new Vector(); AudioFormat format; if ((inputFormat.getSampleSizeInBits() == 16) && AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding())) { format = new AudioFormat( AudioFormat.Encoding.ULAW, inputFormat.getSampleRate(), 8, inputFormat.getChannels(), inputFormat.getChannels(), inputFormat.getSampleRate(), false); formats.addElement(format); } if (AudioFormat.Encoding.ULAW.equals(inputFormat.getEncoding())) { format = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, inputFormat.getSampleRate(), 16, inputFormat.getChannels(), inputFormat.getChannels() * 2, inputFormat.getSampleRate(), false); formats.addElement(format); format = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, inputFormat.getSampleRate(), 16, inputFormat.getChannels(), inputFormat.getChannels() * 2, inputFormat.getSampleRate(), true); formats.addElement(format); } AudioFormat[] formatArray = new AudioFormat[formats.size()]; for (int i = 0; i < formatArray.length; i++) { formatArray[i] = (AudioFormat) (formats.elementAt(i)); } return formatArray; }
@Override public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) { AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length]; System.arraycopy(types, 0, filetypes, 0, types.length); // make sure we can write this stream AudioFormat format = stream.getFormat(); AudioFormat.Encoding encoding = format.getEncoding(); if (AudioFormat.Encoding.ALAW.equals(encoding) || AudioFormat.Encoding.ULAW.equals(encoding) || AudioFormat.Encoding.PCM_SIGNED.equals(encoding) || AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) { return filetypes; } return new AudioFileFormat.Type[0]; }
public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat) { if (AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding())) { if (sourceFormat.getSampleSizeInBits() == 16) { AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1]; enc[0] = AudioFormat.Encoding.ULAW; return enc; } else { return new AudioFormat.Encoding[0]; } } else if (AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding())) { if (sourceFormat.getSampleSizeInBits() == 8) { AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1]; enc[0] = AudioFormat.Encoding.PCM_SIGNED; return enc; } else { return new AudioFormat.Encoding[0]; } } else { return new AudioFormat.Encoding[0]; } }
/** * Returns the AudioFileFormat describing the file that will be written from this * AudioInputStream. Throws IllegalArgumentException if not supported. */ private AudioFileFormat getAudioFileFormat(AudioFileFormat.Type type, AudioInputStream stream) { if (!isFileTypeSupported(type, stream)) { throw new IllegalArgumentException("File type " + type + " not supported."); } AudioFormat format = null; WaveFileFormat fileFormat = null; AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED; AudioFormat streamFormat = stream.getFormat(); AudioFormat.Encoding streamEncoding = streamFormat.getEncoding(); float sampleRate; int sampleSizeInBits; int channels; int frameSize; float frameRate; int fileSize; int waveType = WaveFileFormat.WAVE_FORMAT_PCM; if (AudioFormat.Encoding.ALAW.equals(streamEncoding) || AudioFormat.Encoding.ULAW.equals(streamEncoding)) { encoding = streamEncoding; sampleSizeInBits = streamFormat.getSampleSizeInBits(); if (streamEncoding.equals(AudioFormat.Encoding.ALAW)) { waveType = WaveFileFormat.WAVE_FORMAT_ALAW; } else { waveType = WaveFileFormat.WAVE_FORMAT_MULAW; } } else if (streamFormat.getSampleSizeInBits() == 8) { encoding = AudioFormat.Encoding.PCM_UNSIGNED; sampleSizeInBits = 8; } else { encoding = AudioFormat.Encoding.PCM_SIGNED; sampleSizeInBits = streamFormat.getSampleSizeInBits(); } format = new AudioFormat( encoding, streamFormat.getSampleRate(), sampleSizeInBits, streamFormat.getChannels(), streamFormat.getFrameSize(), streamFormat.getFrameRate(), false); // WAVE is little endian if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) { fileSize = (int) stream.getFrameLength() * streamFormat.getFrameSize() + WaveFileFormat.getHeaderSize(waveType); } else { fileSize = AudioSystem.NOT_SPECIFIED; } fileFormat = new WaveFileFormat( AudioFileFormat.Type.WAVE, fileSize, format, (int) stream.getFrameLength()); return fileFormat; }