下面列出了javax.sound.sampled.AudioFormat#Encoding ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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];
}
AuFileFormat(AudioFileFormat.Type type, int lengthInBytes, AudioFormat format, int lengthInFrames) {
super(type,lengthInBytes,format,lengthInFrames);
AudioFormat.Encoding encoding = format.getEncoding();
auType = -1;
if( AudioFormat.Encoding.ALAW.equals(encoding) ) {
if( format.getSampleSizeInBits()==8 ) {
auType = AU_ALAW_8;
}
} else if( AudioFormat.Encoding.ULAW.equals(encoding) ) {
if( format.getSampleSizeInBits()==8 ) {
auType = AU_ULAW_8;
}
} else if( AudioFormat.Encoding.PCM_SIGNED.equals(encoding) ) {
if( format.getSampleSizeInBits()==8 ) {
auType = AU_LINEAR_8;
} else if( format.getSampleSizeInBits()==16 ) {
auType = AU_LINEAR_16;
} else if( format.getSampleSizeInBits()==24 ) {
auType = AU_LINEAR_24;
} else if( format.getSampleSizeInBits()==32 ) {
auType = AU_LINEAR_32;
}
}
}
AuFileFormat(AudioFileFormat.Type type, int lengthInBytes, AudioFormat format, int lengthInFrames) {
super(type,lengthInBytes,format,lengthInFrames);
AudioFormat.Encoding encoding = format.getEncoding();
auType = -1;
if( AudioFormat.Encoding.ALAW.equals(encoding) ) {
if( format.getSampleSizeInBits()==8 ) {
auType = AU_ALAW_8;
}
} else if( AudioFormat.Encoding.ULAW.equals(encoding) ) {
if( format.getSampleSizeInBits()==8 ) {
auType = AU_ULAW_8;
}
} else if( AudioFormat.Encoding.PCM_SIGNED.equals(encoding) ) {
if( format.getSampleSizeInBits()==8 ) {
auType = AU_LINEAR_8;
} else if( format.getSampleSizeInBits()==16 ) {
auType = AU_LINEAR_16;
} else if( format.getSampleSizeInBits()==24 ) {
auType = AU_LINEAR_24;
} else if( format.getSampleSizeInBits()==32 ) {
auType = AU_LINEAR_32;
}
}
}
/**
* Indicates whether the format converter supports conversion to a particular encoding
* from a particular format.
* @param targetEncoding desired encoding of the outgoing data
* @param sourceFormat format of the incoming data
* @return <code>true</code> if the conversion is supported, otherwise <code>false</code>
*/
public boolean isConversionSupported(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
AudioFormat.Encoding targetEncodings[] = getTargetEncodings(sourceFormat);
for(int i=0; i<targetEncodings.length; i++) {
if( targetEncoding.equals( targetEncodings[i]) ) {
return true;
}
}
return false;
}
/**
* Indicates whether the format converter supports conversion to a particular encoding
* from a particular format.
* @param targetEncoding desired encoding of the outgoing data
* @param sourceFormat format of the incoming data
* @return <code>true</code> if the conversion is supported, otherwise <code>false</code>
*/
public boolean isConversionSupported(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
AudioFormat.Encoding targetEncodings[] = getTargetEncodings(sourceFormat);
for(int i=0; i<targetEncodings.length; i++) {
if( targetEncoding.equals( targetEncodings[i]) ) {
return true;
}
}
return false;
}
/**
*/
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 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];
}
}
@Override
public AudioFormat.Encoding[] getTargetEncodings() {
return getSourceEncodings();
}
PCMtoPCMCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
super(stream, outputFormat, -1);
int sampleSizeInBits = 0;
AudioFormat.Encoding inputEncoding = null;
AudioFormat.Encoding outputEncoding = null;
boolean inputIsBigEndian;
boolean outputIsBigEndian;
AudioFormat inputFormat = stream.getFormat();
// throw an IllegalArgumentException if not ok
if ( ! (isConversionSupported(inputFormat, outputFormat)) ) {
throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
}
inputEncoding = inputFormat.getEncoding();
outputEncoding = outputFormat.getEncoding();
inputIsBigEndian = inputFormat.isBigEndian();
outputIsBigEndian = outputFormat.isBigEndian();
sampleSizeInBits = inputFormat.getSampleSizeInBits();
sampleSizeInBytes = sampleSizeInBits/8;
// determine conversion to perform
if( sampleSizeInBits==8 ) {
if( AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) &&
AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) ) {
conversionType = PCM_SWITCH_SIGNED_8BIT;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_SIGNED_8BIT");
} else if( AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) &&
AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) ) {
conversionType = PCM_SWITCH_SIGNED_8BIT;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_SIGNED_8BIT");
}
} else {
if( inputEncoding.equals(outputEncoding) && (inputIsBigEndian != outputIsBigEndian) ) {
conversionType = PCM_SWITCH_ENDIAN;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_ENDIAN");
} else if (AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) && !inputIsBigEndian &&
AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) && outputIsBigEndian) {
conversionType = PCM_UNSIGNED_LE2SIGNED_BE;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_UNSIGNED_LE2SIGNED_BE");
} else if (AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) && !inputIsBigEndian &&
AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) && outputIsBigEndian) {
conversionType = PCM_SIGNED_LE2UNSIGNED_BE;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SIGNED_LE2UNSIGNED_BE");
} else if (AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) && inputIsBigEndian &&
AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) && !outputIsBigEndian) {
conversionType = PCM_UNSIGNED_BE2SIGNED_LE;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_UNSIGNED_BE2SIGNED_LE");
} else if (AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) && inputIsBigEndian &&
AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) && !outputIsBigEndian) {
conversionType = PCM_SIGNED_BE2UNSIGNED_LE;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SIGNED_BE2UNSIGNED_LE");
}
}
// set the audio stream length in frames if we know it
frameSize = inputFormat.getFrameSize();
if( frameSize == AudioSystem.NOT_SPECIFIED ) {
frameSize=1;
}
if( stream instanceof AudioInputStream ) {
frameLength = stream.getFrameLength();
} else {
frameLength = AudioSystem.NOT_SPECIFIED;
}
// set framePos to zero
framePos = 0;
}
PCMtoPCMCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
super(stream, outputFormat, -1);
int sampleSizeInBits = 0;
AudioFormat.Encoding inputEncoding = null;
AudioFormat.Encoding outputEncoding = null;
boolean inputIsBigEndian;
boolean outputIsBigEndian;
AudioFormat inputFormat = stream.getFormat();
// throw an IllegalArgumentException if not ok
if ( ! (isConversionSupported(inputFormat, outputFormat)) ) {
throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
}
inputEncoding = inputFormat.getEncoding();
outputEncoding = outputFormat.getEncoding();
inputIsBigEndian = inputFormat.isBigEndian();
outputIsBigEndian = outputFormat.isBigEndian();
sampleSizeInBits = inputFormat.getSampleSizeInBits();
sampleSizeInBytes = sampleSizeInBits/8;
// determine conversion to perform
if( sampleSizeInBits==8 ) {
if( AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) &&
AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) ) {
conversionType = PCM_SWITCH_SIGNED_8BIT;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_SIGNED_8BIT");
} else if( AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) &&
AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) ) {
conversionType = PCM_SWITCH_SIGNED_8BIT;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_SIGNED_8BIT");
}
} else {
if( inputEncoding.equals(outputEncoding) && (inputIsBigEndian != outputIsBigEndian) ) {
conversionType = PCM_SWITCH_ENDIAN;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_ENDIAN");
} else if (AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) && !inputIsBigEndian &&
AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) && outputIsBigEndian) {
conversionType = PCM_UNSIGNED_LE2SIGNED_BE;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_UNSIGNED_LE2SIGNED_BE");
} else if (AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) && !inputIsBigEndian &&
AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) && outputIsBigEndian) {
conversionType = PCM_SIGNED_LE2UNSIGNED_BE;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SIGNED_LE2UNSIGNED_BE");
} else if (AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) && inputIsBigEndian &&
AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) && !outputIsBigEndian) {
conversionType = PCM_UNSIGNED_BE2SIGNED_LE;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_UNSIGNED_BE2SIGNED_LE");
} else if (AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) && inputIsBigEndian &&
AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) && !outputIsBigEndian) {
conversionType = PCM_SIGNED_BE2UNSIGNED_LE;
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SIGNED_BE2UNSIGNED_LE");
}
}
// set the audio stream length in frames if we know it
frameSize = inputFormat.getFrameSize();
if( frameSize == AudioSystem.NOT_SPECIFIED ) {
frameSize=1;
}
if( stream instanceof AudioInputStream ) {
frameLength = stream.getFrameLength();
} else {
frameLength = AudioSystem.NOT_SPECIFIED;
}
// set framePos to zero
framePos = 0;
}
/**
* Obtains an audio input stream with the specified encoding from the given audio
* input stream.
* @param targetEncoding desired encoding of the stream after processing
* @param sourceStream stream from which data to be processed should be read
* @return stream from which processed data with the specified target encoding may be read
* @throws IllegalArgumentException if the format combination supplied is
* not supported.
*/
public abstract AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream);
/**
* Obtains the set of target formats with the encoding specified
* supported by the format converter
* If no target formats with the specified encoding are supported
* for this source format, an array of length 0 is returned.
* @param targetEncoding desired encoding of the stream after processing
* @param sourceFormat format of the incoming data
* @return array of supported target formats.
*/
public abstract AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat);
/**
* Obtains the set of source format encodings from which format
* conversion services are provided by this provider.
* @return array of source format encodings. If for some reason provider
* does not provide any conversion services, an array of length 0 is
* returned.
*/
public abstract AudioFormat.Encoding[] getSourceEncodings();
/**
* Obtains the set of target formats with the encoding specified
* supported by the format converter
* If no target formats with the specified encoding are supported
* for this source format, an array of length 0 is returned.
* @param targetEncoding desired encoding of the stream after processing
* @param sourceFormat format of the incoming data
* @return array of supported target formats.
*/
public abstract AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat);
/**
* Obtains the set of target format encodings supported by the format converter
* given a particular source format.
* If no target format encodings are supported for this source format,
* an array of length 0 is returned.
* @param sourceFormat format of the incoming data
* @return array of supported target format encodings.
*/
public abstract AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat);
/**
* Obtains the set of target format encodings to which format
* conversion services are provided by this provider.
* @return array of target format encodings. If for some reason provider
* does not provide any conversion services, an array of length 0 is
* returned.
*/
public abstract AudioFormat.Encoding[] getTargetEncodings();
/**
* Obtains the set of target formats with the encoding specified
* supported by the format converter
* If no target formats with the specified encoding are supported
* for this source format, an array of length 0 is returned.
* @param targetEncoding desired encoding of the stream after processing
* @param sourceFormat format of the incoming data
* @return array of supported target formats.
*/
public abstract AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat);
/**
*/
public abstract AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat);
/**
* Obtains an audio input stream with the specified encoding from the given audio
* input stream.
* @param targetEncoding desired encoding of the stream after processing
* @param sourceStream stream from which data to be processed should be read
* @return stream from which processed data with the specified target encoding may be read
* @throws IllegalArgumentException if the format combination supplied is
* not supported.
*/
public abstract AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream);
/**
* Obtains the set of target format encodings to which format
* conversion services are provided by this provider.
* @return array of target format encodings. If for some reason provider
* does not provide any conversion services, an array of length 0 is
* returned.
*/
public abstract AudioFormat.Encoding[] getTargetEncodings();