下面列出了javax.sound.sampled.AudioFormat#getSampleSizeInBits ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
AudioFloatLSBFilter(AudioFloatConverter converter, AudioFormat format) {
int bits = format.getSampleSizeInBits();
boolean bigEndian = format.isBigEndian();
this.converter = converter;
stepsize = (bits + 7) / 8;
offset = bigEndian ? (stepsize - 1) : 0;
int lsb_bits = bits % 8;
if (lsb_bits == 0)
mask = (byte) 0x00;
else if (lsb_bits == 1)
mask = (byte) 0x80;
else if (lsb_bits == 2)
mask = (byte) 0xC0;
else if (lsb_bits == 3)
mask = (byte) 0xE0;
else if (lsb_bits == 4)
mask = (byte) 0xF0;
else if (lsb_bits == 5)
mask = (byte) 0xF8;
else if (lsb_bits == 6)
mask = (byte) 0xFC;
else if (lsb_bits == 7)
mask = (byte) 0xFE;
else
mask = (byte) 0xFF;
}
public AudioInputStream getAudioInputStream(Encoding targetEncoding,
AudioInputStream sourceStream) {
if (sourceStream.getFormat().getEncoding().equals(targetEncoding))
return sourceStream;
AudioFormat format = sourceStream.getFormat();
int channels = format.getChannels();
Encoding encoding = targetEncoding;
float samplerate = format.getSampleRate();
int bits = format.getSampleSizeInBits();
boolean bigendian = format.isBigEndian();
if (targetEncoding.equals(Encoding.PCM_FLOAT))
bits = 32;
AudioFormat targetFormat = new AudioFormat(encoding, samplerate, bits,
channels, channels * bits / 8, samplerate, bigendian);
return getAudioInputStream(targetFormat, sourceStream);
}
public AudioInputStream getAudioInputStream(Encoding targetEncoding,
AudioInputStream sourceStream) {
if (sourceStream.getFormat().getEncoding().equals(targetEncoding))
return sourceStream;
AudioFormat format = sourceStream.getFormat();
int channels = format.getChannels();
Encoding encoding = targetEncoding;
float samplerate = format.getSampleRate();
int bits = format.getSampleSizeInBits();
boolean bigendian = format.isBigEndian();
if (targetEncoding.equals(Encoding.PCM_FLOAT))
bits = 32;
AudioFormat targetFormat = new AudioFormat(encoding, samplerate, bits,
channels, channels * bits / 8, samplerate, bigendian);
return getAudioInputStream(targetFormat, sourceStream);
}
static boolean isFullySpecifiedPCMFormat(AudioFormat format) {
if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)
&& !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
return false;
}
if ((format.getFrameRate() <= 0)
|| (format.getSampleRate() <= 0)
|| (format.getSampleSizeInBits() <= 0)
|| (format.getFrameSize() <= 0)
|| (format.getChannels() <= 0)) {
return false;
}
return true;
}
AudioFloatInputStreamResampler(AudioFloatInputStream ais,
AudioFormat format) {
this.ais = ais;
AudioFormat sourceFormat = ais.getFormat();
targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
.getSampleRate(), sourceFormat.getSampleSizeInBits(),
sourceFormat.getChannels(), sourceFormat.getFrameSize(),
format.getSampleRate(), sourceFormat.isBigEndian());
nrofchannels = targetFormat.getChannels();
Object interpolation = format.getProperty("interpolation");
if (interpolation != null && (interpolation instanceof String)) {
String resamplerType = (String) interpolation;
if (resamplerType.equalsIgnoreCase("point"))
this.resampler = new SoftPointResampler();
if (resamplerType.equalsIgnoreCase("linear"))
this.resampler = new SoftLinearResampler2();
if (resamplerType.equalsIgnoreCase("linear1"))
this.resampler = new SoftLinearResampler();
if (resamplerType.equalsIgnoreCase("linear2"))
this.resampler = new SoftLinearResampler2();
if (resamplerType.equalsIgnoreCase("cubic"))
this.resampler = new SoftCubicResampler();
if (resamplerType.equalsIgnoreCase("lanczos"))
this.resampler = new SoftLanczosResampler();
if (resamplerType.equalsIgnoreCase("sinc"))
this.resampler = new SoftSincResampler();
}
if (resampler == null)
resampler = new SoftLinearResampler2(); // new
// SoftLinearResampler2();
pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
pad = resampler.getPadding();
pad2 = pad * 2;
ibuffer = new float[nrofchannels][buffer_len + pad2];
ibuffer2 = new float[nrofchannels * buffer_len];
ibuffer_index = buffer_len + pad;
ibuffer_len = buffer_len;
}
@Override
public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat) {
final int sampleSize = sourceFormat.getSampleSizeInBits();
AudioFormat.Encoding encoding = sourceFormat.getEncoding();
if (sampleSize == 8) {
if (encoding.equals(AudioFormat.Encoding.PCM_SIGNED)) {
return new AudioFormat.Encoding[]{
AudioFormat.Encoding.PCM_UNSIGNED
};
}
if (encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
return new AudioFormat.Encoding[]{
AudioFormat.Encoding.PCM_SIGNED
};
}
} else if (sampleSize == 16) {
if (encoding.equals(AudioFormat.Encoding.PCM_SIGNED)
|| encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
return new AudioFormat.Encoding[]{
AudioFormat.Encoding.PCM_UNSIGNED,
AudioFormat.Encoding.PCM_SIGNED
};
}
}
return new AudioFormat.Encoding[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;
}
}
}
AudioFloatInputStreamResampler(AudioFloatInputStream ais,
AudioFormat format) {
this.ais = ais;
AudioFormat sourceFormat = ais.getFormat();
targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
.getSampleRate(), sourceFormat.getSampleSizeInBits(),
sourceFormat.getChannels(), sourceFormat.getFrameSize(),
format.getSampleRate(), sourceFormat.isBigEndian());
nrofchannels = targetFormat.getChannels();
Object interpolation = format.getProperty("interpolation");
if (interpolation != null && (interpolation instanceof String)) {
String resamplerType = (String) interpolation;
if (resamplerType.equalsIgnoreCase("point"))
this.resampler = new SoftPointResampler();
if (resamplerType.equalsIgnoreCase("linear"))
this.resampler = new SoftLinearResampler2();
if (resamplerType.equalsIgnoreCase("linear1"))
this.resampler = new SoftLinearResampler();
if (resamplerType.equalsIgnoreCase("linear2"))
this.resampler = new SoftLinearResampler2();
if (resamplerType.equalsIgnoreCase("cubic"))
this.resampler = new SoftCubicResampler();
if (resamplerType.equalsIgnoreCase("lanczos"))
this.resampler = new SoftLanczosResampler();
if (resamplerType.equalsIgnoreCase("sinc"))
this.resampler = new SoftSincResampler();
}
if (resampler == null)
resampler = new SoftLinearResampler2(); // new
// SoftLinearResampler2();
pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
pad = resampler.getPadding();
pad2 = pad * 2;
ibuffer = new float[nrofchannels][buffer_len + pad2];
ibuffer2 = new float[nrofchannels * buffer_len];
ibuffer_index = buffer_len + pad;
ibuffer_len = buffer_len;
}
AudioFloatInputStreamResampler(AudioFloatInputStream ais,
AudioFormat format) {
this.ais = ais;
AudioFormat sourceFormat = ais.getFormat();
targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
.getSampleRate(), sourceFormat.getSampleSizeInBits(),
sourceFormat.getChannels(), sourceFormat.getFrameSize(),
format.getSampleRate(), sourceFormat.isBigEndian());
nrofchannels = targetFormat.getChannels();
Object interpolation = format.getProperty("interpolation");
if (interpolation != null && (interpolation instanceof String)) {
String resamplerType = (String) interpolation;
if (resamplerType.equalsIgnoreCase("point"))
this.resampler = new SoftPointResampler();
if (resamplerType.equalsIgnoreCase("linear"))
this.resampler = new SoftLinearResampler2();
if (resamplerType.equalsIgnoreCase("linear1"))
this.resampler = new SoftLinearResampler();
if (resamplerType.equalsIgnoreCase("linear2"))
this.resampler = new SoftLinearResampler2();
if (resamplerType.equalsIgnoreCase("cubic"))
this.resampler = new SoftCubicResampler();
if (resamplerType.equalsIgnoreCase("lanczos"))
this.resampler = new SoftLanczosResampler();
if (resamplerType.equalsIgnoreCase("sinc"))
this.resampler = new SoftSincResampler();
}
if (resampler == null)
resampler = new SoftLinearResampler2(); // new
// SoftLinearResampler2();
pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
pad = resampler.getPadding();
pad2 = pad * 2;
ibuffer = new float[nrofchannels][buffer_len + pad2];
ibuffer2 = new float[nrofchannels * buffer_len];
ibuffer_index = buffer_len + pad;
ibuffer_len = buffer_len;
}
private AudioInputStream toLittleEndian(AudioInputStream ais) {
AudioFormat format = ais.getFormat();
AudioFormat targetFormat = new AudioFormat(format.getEncoding(), format
.getSampleRate(), format.getSampleSizeInBits(), format
.getChannels(), format.getFrameSize(), format.getFrameRate(),
false);
return AudioSystem.getAudioInputStream(targetFormat, ais);
}
/**
* Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.
* Throws IllegalArgumentException if not supported.
*/
private AudioFileFormat getAudioFileFormat(Type type, AudioInputStream stream) {
if (!isFileTypeSupported(type, stream)) {
throw new IllegalArgumentException("File type " + type + " not supported.");
}
AudioFormat streamFormat = stream.getFormat();
AudioFormat.Encoding encoding = streamFormat.getEncoding();
if (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) {
encoding = AudioFormat.Encoding.PCM_SIGNED;
}
// We always write big endian au files, this is by far the standard
AudioFormat format = new AudioFormat(encoding,
streamFormat.getSampleRate(),
streamFormat.getSampleSizeInBits(),
streamFormat.getChannels(),
streamFormat.getFrameSize(),
streamFormat.getFrameRate(), true);
int fileSize;
if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AuFileFormat.AU_HEADERSIZE;
} else {
fileSize = AudioSystem.NOT_SPECIFIED;
}
return new AuFileFormat(Type.AU, fileSize, format,
(int) stream.getFrameLength());
}
public AudioFloatInputStreamResampler(AudioFloatInputStream ais,
AudioFormat format) {
this.ais = ais;
AudioFormat sourceFormat = ais.getFormat();
targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
.getSampleRate(), sourceFormat.getSampleSizeInBits(),
sourceFormat.getChannels(), sourceFormat.getFrameSize(),
format.getSampleRate(), sourceFormat.isBigEndian());
nrofchannels = targetFormat.getChannels();
Object interpolation = format.getProperty("interpolation");
if (interpolation != null && (interpolation instanceof String)) {
String resamplerType = (String) interpolation;
if (resamplerType.equalsIgnoreCase("point"))
this.resampler = new SoftPointResampler();
if (resamplerType.equalsIgnoreCase("linear"))
this.resampler = new SoftLinearResampler2();
if (resamplerType.equalsIgnoreCase("linear1"))
this.resampler = new SoftLinearResampler();
if (resamplerType.equalsIgnoreCase("linear2"))
this.resampler = new SoftLinearResampler2();
if (resamplerType.equalsIgnoreCase("cubic"))
this.resampler = new SoftCubicResampler();
if (resamplerType.equalsIgnoreCase("lanczos"))
this.resampler = new SoftLanczosResampler();
if (resamplerType.equalsIgnoreCase("sinc"))
this.resampler = new SoftSincResampler();
}
if (resampler == null)
resampler = new SoftLinearResampler2(); // new
// SoftLinearResampler2();
pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
pad = resampler.getPadding();
pad2 = pad * 2;
ibuffer = new float[nrofchannels][buffer_len + pad2];
ibuffer2 = new float[nrofchannels * buffer_len];
ibuffer_index = buffer_len + pad;
ibuffer_len = buffer_len;
}
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;
}
}
}
public static AudioFormat getOtherEndianOrSign(AudioFormat format) {
AudioFormat.Encoding newEnc = null;
boolean newEndian = format.isBigEndian();
boolean isSigned = format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED);
boolean isUnsigned = format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED);
if ((isSigned || isUnsigned) && format.getSampleSizeInBits() > 0) {
if (format.getSampleSizeInBits() == 8) {
// return the other signed'ness
if (isSigned) {
newEnc = AudioFormat.Encoding.PCM_UNSIGNED;
} else {
newEnc = AudioFormat.Encoding.PCM_SIGNED;
}
} else {
newEnc = format.getEncoding();
newEndian = !newEndian;
}
if (newEnc != null) {
return new AudioFormat(newEnc, format.getSampleRate(),
format.getSampleSizeInBits(),
format.getChannels(),
format.getFrameSize(),
format.getFrameRate(),
newEndian);
}
}
return null;
}
AudioFloatInputStreamChannelMixer(AudioFloatInputStream ais,
int targetChannels) {
this.sourceChannels = ais.getFormat().getChannels();
this.targetChannels = targetChannels;
this.ais = ais;
AudioFormat format = ais.getFormat();
targetFormat = new AudioFormat(format.getEncoding(), format
.getSampleRate(), format.getSampleSizeInBits(),
targetChannels, (format.getFrameSize() / sourceChannels)
* targetChannels, format.getFrameRate(), format
.isBigEndian());
}
public static AudioFloatConverter getConverter(AudioFormat format) {
AudioFloatConverter conv = null;
if (format.getFrameSize() == 0)
return null;
if (format.getFrameSize() !=
((format.getSampleSizeInBits() + 7) / 8) * format.getChannels()) {
return null;
}
if (format.getEncoding().equals(Encoding.PCM_SIGNED)) {
if (format.isBigEndian()) {
if (format.getSampleSizeInBits() <= 8) {
conv = new AudioFloatConversion8S();
} else if (format.getSampleSizeInBits() > 8 &&
format.getSampleSizeInBits() <= 16) {
conv = new AudioFloatConversion16SB();
} else if (format.getSampleSizeInBits() > 16 &&
format.getSampleSizeInBits() <= 24) {
conv = new AudioFloatConversion24SB();
} else if (format.getSampleSizeInBits() > 24 &&
format.getSampleSizeInBits() <= 32) {
conv = new AudioFloatConversion32SB();
} else if (format.getSampleSizeInBits() > 32) {
conv = new AudioFloatConversion32xSB(((format
.getSampleSizeInBits() + 7) / 8) - 4);
}
} else {
if (format.getSampleSizeInBits() <= 8) {
conv = new AudioFloatConversion8S();
} else if (format.getSampleSizeInBits() > 8 &&
format.getSampleSizeInBits() <= 16) {
conv = new AudioFloatConversion16SL();
} else if (format.getSampleSizeInBits() > 16 &&
format.getSampleSizeInBits() <= 24) {
conv = new AudioFloatConversion24SL();
} else if (format.getSampleSizeInBits() > 24 &&
format.getSampleSizeInBits() <= 32) {
conv = new AudioFloatConversion32SL();
} else if (format.getSampleSizeInBits() > 32) {
conv = new AudioFloatConversion32xSL(((format
.getSampleSizeInBits() + 7) / 8) - 4);
}
}
} else if (format.getEncoding().equals(Encoding.PCM_UNSIGNED)) {
if (format.isBigEndian()) {
if (format.getSampleSizeInBits() <= 8) {
conv = new AudioFloatConversion8U();
} else if (format.getSampleSizeInBits() > 8 &&
format.getSampleSizeInBits() <= 16) {
conv = new AudioFloatConversion16UB();
} else if (format.getSampleSizeInBits() > 16 &&
format.getSampleSizeInBits() <= 24) {
conv = new AudioFloatConversion24UB();
} else if (format.getSampleSizeInBits() > 24 &&
format.getSampleSizeInBits() <= 32) {
conv = new AudioFloatConversion32UB();
} else if (format.getSampleSizeInBits() > 32) {
conv = new AudioFloatConversion32xUB(((
format.getSampleSizeInBits() + 7) / 8) - 4);
}
} else {
if (format.getSampleSizeInBits() <= 8) {
conv = new AudioFloatConversion8U();
} else if (format.getSampleSizeInBits() > 8 &&
format.getSampleSizeInBits() <= 16) {
conv = new AudioFloatConversion16UL();
} else if (format.getSampleSizeInBits() > 16 &&
format.getSampleSizeInBits() <= 24) {
conv = new AudioFloatConversion24UL();
} else if (format.getSampleSizeInBits() > 24 &&
format.getSampleSizeInBits() <= 32) {
conv = new AudioFloatConversion32UL();
} else if (format.getSampleSizeInBits() > 32) {
conv = new AudioFloatConversion32xUL(((
format.getSampleSizeInBits() + 7) / 8) - 4);
}
}
} else if (format.getEncoding().equals(Encoding.PCM_FLOAT)) {
if (format.getSampleSizeInBits() == 32) {
if (format.isBigEndian())
conv = new AudioFloatConversion32B();
else
conv = new AudioFloatConversion32L();
} else if (format.getSampleSizeInBits() == 64) {
if (format.isBigEndian())
conv = new AudioFloatConversion64B();
else
conv = new AudioFloatConversion64L();
}
}
if ((format.getEncoding().equals(Encoding.PCM_SIGNED) ||
format.getEncoding().equals(Encoding.PCM_UNSIGNED)) &&
(format.getSampleSizeInBits() % 8 != 0)) {
conv = new AudioFloatLSBFilter(conv, format);
}
if (conv != null)
conv.format = format;
return conv;
}
/**
* Loads and returns a Clip from an audio input stream.
* @param ref the resource name
* @param audioIn the audio input stream
* @param isMP3 true if MP3, false if WAV
* @return the loaded and opened clip
*/
private static MultiClip loadClip(String ref, AudioInputStream audioIn, boolean isMP3)
throws IOException, LineUnavailableException {
AudioFormat format = audioIn.getFormat();
if (isMP3) {
AudioFormat decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), 16,
format.getChannels(), format.getChannels() * 2, format.getSampleRate(), false);
AudioInputStream decodedAudioIn = AudioSystem.getAudioInputStream(decodedFormat, audioIn);
format = decodedFormat;
audioIn = decodedAudioIn;
}
DataLine.Info info = new DataLine.Info(Clip.class, format);
if (AudioSystem.isLineSupported(info))
return new MultiClip(ref, audioIn);
// try to find closest matching line
Clip clip = AudioSystem.getClip();
AudioFormat[] formats = ((DataLine.Info) clip.getLineInfo()).getFormats();
int bestIndex = -1;
float bestScore = 0;
float sampleRate = format.getSampleRate();
if (sampleRate < 0)
sampleRate = clip.getFormat().getSampleRate();
float oldSampleRate = sampleRate;
while (true) {
for (int i = 0; i < formats.length; i++) {
AudioFormat curFormat = formats[i];
AudioFormat newFormat = new AudioFormat(
sampleRate, curFormat.getSampleSizeInBits(),
curFormat.getChannels(), true, curFormat.isBigEndian());
formats[i] = newFormat;
DataLine.Info newLine = new DataLine.Info(Clip.class, newFormat);
if (AudioSystem.isLineSupported(newLine) &&
AudioSystem.isConversionSupported(newFormat, format)) {
float score = 1
+ (newFormat.getSampleRate() == sampleRate ? 5 : 0)
+ (newFormat.getSampleSizeInBits() == format.getSampleSizeInBits() ? 5 : 0)
+ (newFormat.getChannels() == format.getChannels() ? 5 : 0)
+ (newFormat.isBigEndian() == format.isBigEndian() ? 1 : 0)
+ newFormat.getSampleRate() / 11025
+ newFormat.getChannels()
+ newFormat.getSampleSizeInBits() / 8;
if (score > bestScore) {
bestIndex = i;
bestScore = score;
}
}
}
if (bestIndex < 0) {
if (oldSampleRate < 44100) {
if (sampleRate > 44100)
break;
sampleRate *= 2;
} else {
if (sampleRate < 44100)
break;
sampleRate /= 2;
}
} else
break;
}
if (bestIndex >= 0)
return new MultiClip(ref, AudioSystem.getAudioInputStream(formats[bestIndex], audioIn));
// still couldn't find anything, try the default clip format
return new MultiClip(ref, AudioSystem.getAudioInputStream(clip.getFormat(), audioIn));
}
/**
* If this is a mono sound, obtains the single sample contained
* within this frame, else obtains the first (left) sample
* contained in the specified frame.
*
* @param frameNum the index of the frame to access
* @return an integer representation of the bytes contained within
* the specified frame
* @throws SoundException if the frame number is invalid.
*/
public int getSampleValue(int frameNum) throws SoundException {
//Before we get started, lets make sure that frame exists
if (frameNum >= getAudioFileFormat().getFrameLength()) {
printError("You are trying to access the sample at index: "
+ (frameNum) + ", but the last valid index is at " +
(getAudioFileFormat().getFrameLength() - 1));
} else if (frameNum < 0) {
printError("You asked for the sample at index: " + (frameNum) +
". This number is less than zero. Please try" +
"again using an index in the range [0," +
(getAudioFileFormat().getFrameLength() - 1) + "]");
}
AudioFormat format = getAudioFileFormat().getFormat();
int sampleSizeInBits = format.getSampleSizeInBits();
boolean isBigEndian = format.isBigEndian();
byte[] theFrame = getFrame(frameNum);
if (format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) {
//since we're always returning the left sample,
//we don't care if we're mono or stereo, left is
//always first in the frame
if (sampleSizeInBits == 8) { //8 bits == 1 byte
return theFrame[0];
} else if (sampleSizeInBits == 16)
return TConversionTool.bytesToInt16(theFrame, 0,
isBigEndian);
else if (sampleSizeInBits == 24)
return TConversionTool.bytesToInt24(theFrame, 0,
isBigEndian);
else if (sampleSizeInBits == 32)
return TConversionTool.bytesToInt32(theFrame, 0,
isBigEndian);
else {
printError("Unsupported audio encoding. The sample " +
"size is not recognized as a standard " +
"format.");
return -1;
}
} else if (format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
if (sampleSizeInBits == 8)
return TTTConversionTool.unsignedByteToInt(theFrame[0]) -
(int)Math.pow(2, 7);
else if (sampleSizeInBits == 16)
return TTTConversionTool.unsignedByteToInt16(theFrame, 0,
isBigEndian) -
(int)Math.pow(2, 15);
else if (sampleSizeInBits == 24)
return TTTConversionTool.unsignedByteToInt24(theFrame, 0,
isBigEndian) -
(int)Math.pow(2, 23);
else if (sampleSizeInBits == 32)
return TTTConversionTool.unsignedByteToInt32(theFrame, 0,
isBigEndian) -
(int)Math.pow(2, 31);
else {
printError("Unsupported audio encoding. The sample " +
"size is not recognized as a standard " +
"format.");
return -1;
}
} else if (format.getEncoding().equals(AudioFormat.Encoding.ALAW)) {
return TTTConversionTool.alaw2linear(buffer[0]);
} else if (format.getEncoding().equals(AudioFormat.Encoding.ULAW)) {
return TTTConversionTool.ulaw2linear(buffer[0]);
} else {
printError("unsupported audio encoding: " +
format.getEncoding() + ". Currently only PCM, " +
"ALAW and ULAW are supported. Please try again" +
"with a different file.");
return -1;
}
}
AudioFloatFormatConverterInputStream(AudioFormat targetFormat,
AudioFloatInputStream stream) {
this.stream = stream;
converter = AudioFloatConverter.getConverter(targetFormat);
fsize = ((targetFormat.getSampleSizeInBits() + 7) / 8);
}