下面列出了javax.sound.sampled.AudioFormat#getFrameSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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());
}
private InputStream getFileStream(AuFileFormat auFileFormat, AudioInputStream audioStream) throws IOException {
// private method ... assumes auFileFormat is a supported file type
AudioFormat format = auFileFormat.getFormat();
int headerSize = AuFileFormat.AU_HEADERSIZE;
long dataSize = auFileFormat.getFrameLength();
//$$fb fix for Bug 4351296
//int dataSizeInBytes = dataSize * format.getFrameSize();
long dataSizeInBytes = (dataSize==AudioSystem.NOT_SPECIFIED)?UNKNOWN_SIZE:dataSize * format.getFrameSize();
if (dataSizeInBytes>0x7FFFFFFFl) {
dataSizeInBytes=UNKNOWN_SIZE;
}
int auType = auFileFormat.getAuType();
int sampleRate = (int)format.getSampleRate();
int channels = format.getChannels();
// if we need to do any format conversion, we do it here.
//$$ fb 2001-07-13: Bug 4391108
audioStream = AudioSystem.getAudioInputStream(format, audioStream);
final byte[] header;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos)) {
dos.writeInt(AuFileFormat.AU_SUN_MAGIC);
dos.writeInt(headerSize);
dos.writeInt((int) dataSizeInBytes);
dos.writeInt(auType);
dos.writeInt(sampleRate);
dos.writeInt(channels);
header = baos.toByteArray();
}
// Now create a new InputStream from headerStream and the InputStream
// in audioStream
return new SequenceInputStream(new ByteArrayInputStream(header),
new NoCloseInputStream(audioStream));
}
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);
}
@Override
public void open(AudioFormat format) throws LineUnavailableException {
if (bufferSize == -1)
bufferSize = ((int) (format.getFrameRate() / 2))
* format.getFrameSize();
open(format, bufferSize);
}
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 AudioFloatInputStream getInputStream(AudioFormat format,
byte[] buffer, int offset, int len) {
AudioFloatConverter converter = AudioFloatConverter
.getConverter(format);
if (converter != null)
return new BytaArrayAudioFloatInputStream(converter, buffer,
offset, len);
InputStream stream = new ByteArrayInputStream(buffer, offset, len);
long aLen = format.getFrameSize() == AudioSystem.NOT_SPECIFIED
? AudioSystem.NOT_SPECIFIED : len / format.getFrameSize();
AudioInputStream astream = new AudioInputStream(stream, format, aLen);
return getInputStream(astream);
}
public void open(AudioFormat format, int bufferSize)
throws LineUnavailableException {
this.format = format;
this.bufferSize = bufferSize;
this.framesize = format.getFrameSize();
opened = 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;
}
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());
}
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;
}
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;
}
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;
}
}
static public byte[] toBytes(float[] in, AudioFormat format) {
byte[] out = new byte[in.length * format.getFrameSize()];
return AudioFloatConverter.getConverter(format).toByteArray(in, out);
}
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);
}
}
static long micros2bytes(AudioFormat format, long micros) {
long result = (long) (micros * format.getFrameRate() / 1000000.0f * format.getFrameSize());
return align(result, format.getFrameSize());
}
@Override
StandardFileFormat getAudioFileFormatImpl(final InputStream stream)
throws UnsupportedAudioFileException, IOException {
RIFFReader riffiterator = new RIFFReader(stream);
if (!riffiterator.getFormat().equals("RIFF"))
throw new UnsupportedAudioFileException();
if (!riffiterator.getType().equals("WAVE"))
throw new UnsupportedAudioFileException();
boolean fmt_found = false;
boolean data_found = false;
int channels = 1;
long samplerate = 1;
int framesize = 1;
int bits = 1;
long dataSize = 0;
while (riffiterator.hasNextChunk()) {
RIFFReader chunk = riffiterator.nextChunk();
if (chunk.getFormat().equals("fmt ")) {
fmt_found = true;
int format = chunk.readUnsignedShort();
if (format != WaveFileFormat.WAVE_FORMAT_IEEE_FLOAT) {
throw new UnsupportedAudioFileException();
}
channels = chunk.readUnsignedShort();
samplerate = chunk.readUnsignedInt();
/* framerate = */chunk.readUnsignedInt();
framesize = chunk.readUnsignedShort();
bits = chunk.readUnsignedShort();
}
if (chunk.getFormat().equals("data")) {
dataSize = chunk.getSize();
data_found = true;
break;
}
}
if (!fmt_found || !data_found) {
throw new UnsupportedAudioFileException();
}
AudioFormat audioformat = new AudioFormat(
Encoding.PCM_FLOAT, samplerate, bits, channels,
framesize, samplerate, false);
return new StandardFileFormat(AudioFileFormat.Type.WAVE, audioformat,
dataSize / audioformat.getFrameSize());
}
public void open(AudioFormat format) throws LineUnavailableException {
if (bufferSize == -1)
bufferSize = ((int) (format.getFrameRate() / 2))
* format.getFrameSize();
open(format, bufferSize);
}
static long millis2bytes(AudioFormat format, long millis) {
long result = (long) (millis * format.getFrameRate() / 1000.0f * format.getFrameSize());
return align(result, format.getFrameSize());
}
public void open(AudioFormat format) throws LineUnavailableException {
if (bufferSize == -1)
bufferSize = ((int) (format.getFrameRate() / 2))
* format.getFrameSize();
open(format, bufferSize);
}