下面列出了javax.sound.sampled.AudioFormat#getChannels ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*/
public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) {
if( isConversionSupported(targetEncoding, sourceStream.getFormat()) ) {
AudioFormat sourceFormat = sourceStream.getFormat();
AudioFormat targetFormat = new AudioFormat( targetEncoding,
sourceFormat.getSampleRate(),
sourceFormat.getSampleSizeInBits(),
sourceFormat.getChannels(),
sourceFormat.getFrameSize(),
sourceFormat.getFrameRate(),
sourceFormat.isBigEndian() );
return getAudioInputStream( targetFormat, sourceStream );
} else {
throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString() );
}
}
/**
*/
public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) {
if( isConversionSupported(targetEncoding, sourceStream.getFormat()) ) {
AudioFormat sourceFormat = sourceStream.getFormat();
AudioFormat targetFormat = new AudioFormat( targetEncoding,
sourceFormat.getSampleRate(),
sourceFormat.getSampleSizeInBits(),
sourceFormat.getChannels(),
sourceFormat.getFrameSize(),
sourceFormat.getFrameRate(),
sourceFormat.isBigEndian() );
return getAudioInputStream( targetFormat, sourceStream );
} else {
throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString() );
}
}
/**
*/
public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) {
if( isConversionSupported(targetEncoding, sourceStream.getFormat()) ) {
AudioFormat sourceFormat = sourceStream.getFormat();
AudioFormat targetFormat = new AudioFormat( targetEncoding,
sourceFormat.getSampleRate(),
sourceFormat.getSampleSizeInBits(),
sourceFormat.getChannels(),
sourceFormat.getFrameSize(),
sourceFormat.getFrameRate(),
sourceFormat.isBigEndian() );
return getAudioInputStream( targetFormat, sourceStream );
} else {
throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString() );
}
}
public AudioInputStream getAudioInputStream(AudioFormat targetFormat,
AudioFloatInputStream sourceStream) {
if (!isConversionSupported(targetFormat, sourceStream.getFormat()))
throw new IllegalArgumentException("Unsupported conversion: "
+ sourceStream.getFormat().toString() + " to "
+ targetFormat.toString());
if (targetFormat.getChannels() != sourceStream.getFormat()
.getChannels())
sourceStream = new AudioFloatInputStreamChannelMixer(sourceStream,
targetFormat.getChannels());
if (Math.abs(targetFormat.getSampleRate()
- sourceStream.getFormat().getSampleRate()) > 0.000001)
sourceStream = new AudioFloatInputStreamResampler(sourceStream,
targetFormat);
return new AudioInputStream(new AudioFloatFormatConverterInputStream(
targetFormat, sourceStream), targetFormat, sourceStream
.getFrameLength());
}
public static AudioInputStream getPCMConvertedAudioInputStream(AudioInputStream ais) {
// we can't open the device for non-PCM playback, so we have
// convert any other encodings to PCM here (at least we try!)
AudioFormat af = ais.getFormat();
if( (!af.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) &&
(!af.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED))) {
try {
AudioFormat newFormat =
new AudioFormat( AudioFormat.Encoding.PCM_SIGNED,
af.getSampleRate(),
16,
af.getChannels(),
af.getChannels() * 2,
af.getSampleRate(),
Platform.isBigEndian());
ais = AudioSystem.getAudioInputStream(newFormat, ais);
} catch (Exception e) {
if (Printer.err) e.printStackTrace();
ais = null;
}
}
return 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());
}
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;
}
public boolean isConversionSupported(AudioFormat targetFormat,
AudioFormat sourceFormat) {
if (AudioFloatConverter.getConverter(sourceFormat) == null)
return false;
if (AudioFloatConverter.getConverter(targetFormat) == null)
return false;
if (sourceFormat.getChannels() <= 0)
return false;
if (targetFormat.getChannels() <= 0)
return false;
return true;
}
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);
}
public boolean isConversionSupported(AudioFormat targetFormat,
AudioFormat sourceFormat) {
if (AudioFloatConverter.getConverter(sourceFormat) == null)
return false;
if (AudioFloatConverter.getConverter(targetFormat) == null)
return false;
if (sourceFormat.getChannels() <= 0)
return false;
if (targetFormat.getChannels() <= 0)
return false;
return true;
}
private void setFormat(AudioFormat format) {
if (format.getChannels() > 2) {
throw new IllegalArgumentException(
"Only mono and stereo audio supported.");
}
if (AudioFloatConverter.getConverter(format) == null)
throw new IllegalArgumentException("Audio format not supported.");
this.format = format;
}
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;
}
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;
}
private AudioFormat getProcessedAudioFormat(final AudioBuffer buffer) {
final AudioFormat sourceAudioFormat = buffer.getAudioFormat();
return new AudioFormat(sourceAudioFormat.getSampleRate() * resampler.getFactor(),
sourceAudioFormat.getSampleSizeInBits(),
sourceAudioFormat.getChannels(),
AudioFormat.Encoding.PCM_SIGNED.equals(sourceAudioFormat.getEncoding()),
sourceAudioFormat.isBigEndian());
}
public AudioFormat[] getTargetFormats(Encoding targetEncoding,
AudioFormat sourceFormat) {
if (AudioFloatConverter.getConverter(sourceFormat) == null)
return new AudioFormat[0];
int channels = sourceFormat.getChannels();
ArrayList<AudioFormat> formats = new ArrayList<AudioFormat>();
if (targetEncoding.equals(Encoding.PCM_SIGNED))
formats.add(new AudioFormat(Encoding.PCM_SIGNED,
AudioSystem.NOT_SPECIFIED, 8, channels, channels,
AudioSystem.NOT_SPECIFIED, false));
if (targetEncoding.equals(Encoding.PCM_UNSIGNED))
formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
AudioSystem.NOT_SPECIFIED, 8, channels, channels,
AudioSystem.NOT_SPECIFIED, false));
for (int bits = 16; bits < 32; bits += 8) {
if (targetEncoding.equals(Encoding.PCM_SIGNED)) {
formats.add(new AudioFormat(Encoding.PCM_SIGNED,
AudioSystem.NOT_SPECIFIED, bits, channels, channels
* bits / 8, AudioSystem.NOT_SPECIFIED, false));
formats.add(new AudioFormat(Encoding.PCM_SIGNED,
AudioSystem.NOT_SPECIFIED, bits, channels, channels
* bits / 8, AudioSystem.NOT_SPECIFIED, true));
}
if (targetEncoding.equals(Encoding.PCM_UNSIGNED)) {
formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
AudioSystem.NOT_SPECIFIED, bits, channels, channels
* bits / 8, AudioSystem.NOT_SPECIFIED, true));
formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
AudioSystem.NOT_SPECIFIED, bits, channels, channels
* bits / 8, AudioSystem.NOT_SPECIFIED, false));
}
}
if (targetEncoding.equals(Encoding.PCM_FLOAT)) {
formats.add(new AudioFormat(Encoding.PCM_FLOAT,
AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4,
AudioSystem.NOT_SPECIFIED, false));
formats.add(new AudioFormat(Encoding.PCM_FLOAT,
AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4,
AudioSystem.NOT_SPECIFIED, true));
formats.add(new AudioFormat(Encoding.PCM_FLOAT,
AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8,
AudioSystem.NOT_SPECIFIED, false));
formats.add(new AudioFormat(Encoding.PCM_FLOAT,
AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8,
AudioSystem.NOT_SPECIFIED, true));
}
return formats.toArray(new AudioFormat[formats.size()]);
}
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;
}
public void open(AudioFormat format, byte[] data, int offset, int bufferSize)
throws LineUnavailableException {
synchronized (control_mutex) {
if (isOpen()) {
throw new IllegalStateException(
"Clip is already open with format " + getFormat()
+ " and frame lengh of " + getFrameLength());
}
if (AudioFloatConverter.getConverter(format) == null)
throw new IllegalArgumentException("Invalid format : "
+ format.toString());
if (bufferSize % format.getFrameSize() != 0)
throw new IllegalArgumentException(
"Buffer size does not represent an integral number of sample frames!");
if (data != null) {
this.data = Arrays.copyOf(data, data.length);
}
this.offset = offset;
this.bufferSize = bufferSize;
this.format = format;
this.framesize = format.getFrameSize();
loopstart = 0;
loopend = -1;
loop_sg = true;
if (!mixer.isOpen()) {
mixer.open();
mixer.implicitOpen = true;
}
outputformat = mixer.getFormat();
out_nrofchannels = outputformat.getChannels();
in_nrofchannels = format.getChannels();
open = true;
mixer.getMainMixer().openLine(this);
}
}
public AudioFormat[] getTargetFormats(Encoding targetEncoding,
AudioFormat sourceFormat) {
if (AudioFloatConverter.getConverter(sourceFormat) == null)
return new AudioFormat[0];
int channels = sourceFormat.getChannels();
ArrayList<AudioFormat> formats = new ArrayList<AudioFormat>();
if (targetEncoding.equals(Encoding.PCM_SIGNED))
formats.add(new AudioFormat(Encoding.PCM_SIGNED,
AudioSystem.NOT_SPECIFIED, 8, channels, channels,
AudioSystem.NOT_SPECIFIED, false));
if (targetEncoding.equals(Encoding.PCM_UNSIGNED))
formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
AudioSystem.NOT_SPECIFIED, 8, channels, channels,
AudioSystem.NOT_SPECIFIED, false));
for (int bits = 16; bits < 32; bits += 8) {
if (targetEncoding.equals(Encoding.PCM_SIGNED)) {
formats.add(new AudioFormat(Encoding.PCM_SIGNED,
AudioSystem.NOT_SPECIFIED, bits, channels, channels
* bits / 8, AudioSystem.NOT_SPECIFIED, false));
formats.add(new AudioFormat(Encoding.PCM_SIGNED,
AudioSystem.NOT_SPECIFIED, bits, channels, channels
* bits / 8, AudioSystem.NOT_SPECIFIED, true));
}
if (targetEncoding.equals(Encoding.PCM_UNSIGNED)) {
formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
AudioSystem.NOT_SPECIFIED, bits, channels, channels
* bits / 8, AudioSystem.NOT_SPECIFIED, true));
formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
AudioSystem.NOT_SPECIFIED, bits, channels, channels
* bits / 8, AudioSystem.NOT_SPECIFIED, false));
}
}
if (targetEncoding.equals(Encoding.PCM_FLOAT)) {
formats.add(new AudioFormat(Encoding.PCM_FLOAT,
AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4,
AudioSystem.NOT_SPECIFIED, false));
formats.add(new AudioFormat(Encoding.PCM_FLOAT,
AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4,
AudioSystem.NOT_SPECIFIED, true));
formats.add(new AudioFormat(Encoding.PCM_FLOAT,
AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8,
AudioSystem.NOT_SPECIFIED, false));
formats.add(new AudioFormat(Encoding.PCM_FLOAT,
AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8,
AudioSystem.NOT_SPECIFIED, true));
}
return formats.toArray(new AudioFormat[formats.size()]);
}
private InputStream getFileStream(WaveFileFormat waveFileFormat, InputStream audioStream) throws IOException {
// private method ... assumes audioFileFormat is a supported file type
// WAVE header fields
AudioFormat audioFormat = waveFileFormat.getFormat();
int headerLength = waveFileFormat.getHeaderSize();
int riffMagic = WaveFileFormat.RIFF_MAGIC;
int waveMagic = WaveFileFormat.WAVE_MAGIC;
int fmtMagic = WaveFileFormat.FMT_MAGIC;
int fmtLength = WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType());
short wav_type = (short) waveFileFormat.getWaveType();
short channels = (short) audioFormat.getChannels();
short sampleSizeInBits = (short) audioFormat.getSampleSizeInBits();
int sampleRate = (int) audioFormat.getSampleRate();
int frameSizeInBytes = audioFormat.getFrameSize();
int frameRate = (int) audioFormat.getFrameRate();
int avgBytesPerSec = channels * sampleSizeInBits * sampleRate / 8;
short blockAlign = (short) ((sampleSizeInBits / 8) * channels);
int dataMagic = WaveFileFormat.DATA_MAGIC;
int dataLength = waveFileFormat.getFrameLength() * frameSizeInBytes;
int length = waveFileFormat.getByteLength();
int riffLength = dataLength + headerLength - 8;
AudioFormat audioStreamFormat = null;
AudioFormat.Encoding encoding = null;
InputStream codedAudioStream = audioStream;
// if audioStream is an AudioInputStream and we need to convert, do it here...
if(audioStream instanceof AudioInputStream) {
audioStreamFormat = ((AudioInputStream)audioStream).getFormat();
encoding = audioStreamFormat.getEncoding();
if(AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) {
if( sampleSizeInBits==8 ) {
wav_type = WaveFileFormat.WAVE_FORMAT_PCM;
// plug in the transcoder to convert from PCM_SIGNED to PCM_UNSIGNED
codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat(
AudioFormat.Encoding.PCM_UNSIGNED,
audioStreamFormat.getSampleRate(),
audioStreamFormat.getSampleSizeInBits(),
audioStreamFormat.getChannels(),
audioStreamFormat.getFrameSize(),
audioStreamFormat.getFrameRate(),
false),
(AudioInputStream)audioStream);
}
}
if( (AudioFormat.Encoding.PCM_SIGNED.equals(encoding) && audioStreamFormat.isBigEndian()) ||
(AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) && !audioStreamFormat.isBigEndian()) ||
(AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) && audioStreamFormat.isBigEndian()) ) {
if( sampleSizeInBits!=8) {
wav_type = WaveFileFormat.WAVE_FORMAT_PCM;
// plug in the transcoder to convert to PCM_SIGNED_LITTLE_ENDIAN
codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
audioStreamFormat.getSampleRate(),
audioStreamFormat.getSampleSizeInBits(),
audioStreamFormat.getChannels(),
audioStreamFormat.getFrameSize(),
audioStreamFormat.getFrameRate(),
false),
(AudioInputStream)audioStream);
}
}
}
// Now push the header into a stream, concat, and return the new SequenceInputStream
final byte[] header;
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final DataOutputStream dos = new DataOutputStream(baos)) {
// we write in littleendian...
dos.writeInt(riffMagic);
dos.writeInt(big2little(riffLength));
dos.writeInt(waveMagic);
dos.writeInt(fmtMagic);
dos.writeInt(big2little(fmtLength));
dos.writeShort(big2littleShort(wav_type));
dos.writeShort(big2littleShort(channels));
dos.writeInt(big2little(sampleRate));
dos.writeInt(big2little(avgBytesPerSec));
dos.writeShort(big2littleShort(blockAlign));
dos.writeShort(big2littleShort(sampleSizeInBits));
//$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
if (wav_type != WaveFileFormat.WAVE_FORMAT_PCM) {
// add length 0 for "codec specific data length"
dos.writeShort(0);
}
dos.writeInt(dataMagic);
dos.writeInt(big2little(dataLength));
header = baos.toByteArray();
}
return new SequenceInputStream(new ByteArrayInputStream(header),
new NoCloseInputStream(codedAudioStream));
}
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;
}