use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.
the class WaveExtensibleFileReader method internal_getAudioFileFormat.
private AudioFileFormat internal_getAudioFileFormat(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;
// long framerate = 1;
int framesize = 1;
int bits = 1;
int validBitsPerSample = 1;
long channelMask = 0;
GUID subFormat = null;
while (riffiterator.hasNextChunk()) {
RIFFReader chunk = riffiterator.nextChunk();
if (chunk.getFormat().equals("fmt ")) {
fmt_found = true;
int format = chunk.readUnsignedShort();
if (format != 0xFFFE)
// WAVE_FORMAT_EXTENSIBLE
throw new UnsupportedAudioFileException();
// only
channels = chunk.readUnsignedShort();
samplerate = chunk.readUnsignedInt();
/* framerate = */
chunk.readUnsignedInt();
framesize = chunk.readUnsignedShort();
bits = chunk.readUnsignedShort();
int cbSize = chunk.readUnsignedShort();
if (cbSize != 22)
throw new UnsupportedAudioFileException();
validBitsPerSample = chunk.readUnsignedShort();
if (validBitsPerSample > bits)
throw new UnsupportedAudioFileException();
channelMask = chunk.readUnsignedInt();
subFormat = GUID.read(chunk);
}
if (chunk.getFormat().equals("data")) {
data_found = true;
break;
}
}
if (!fmt_found)
throw new UnsupportedAudioFileException();
if (!data_found)
throw new UnsupportedAudioFileException();
Map<String, Object> p = new HashMap<String, Object>();
String s_channelmask = decodeChannelMask(channelMask);
if (s_channelmask != null)
p.put("channelOrder", s_channelmask);
if (channelMask != 0)
p.put("channelMask", channelMask);
// validBitsPerSample is only informational for PCM data,
// data is still encode according to SampleSizeInBits.
p.put("validBitsPerSample", validBitsPerSample);
AudioFormat audioformat = null;
if (subFormat.equals(SUBTYPE_PCM)) {
if (bits == 8) {
audioformat = new AudioFormat(Encoding.PCM_UNSIGNED, samplerate, bits, channels, framesize, samplerate, false, p);
} else {
audioformat = new AudioFormat(Encoding.PCM_SIGNED, samplerate, bits, channels, framesize, samplerate, false, p);
}
} else if (subFormat.equals(SUBTYPE_IEEE_FLOAT)) {
audioformat = new AudioFormat(Encoding.PCM_FLOAT, samplerate, bits, channels, framesize, samplerate, false, p);
} else
throw new UnsupportedAudioFileException();
AudioFileFormat fileformat = new AudioFileFormat(AudioFileFormat.Type.WAVE, audioformat, AudioSystem.NOT_SPECIFIED);
return fileformat;
}
use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.
the class WaveFileWriter method getAudioFileFormat.
//--------------------------------------------------------------------
/**
* 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) {
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;
if (!types[0].equals(type)) {
throw new IllegalArgumentException("File type " + type + " not supported.");
}
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 = WAVE_FORMAT_ALAW;
} else {
waveType = 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(), // WAVE is little endian
false);
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;
}
use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.
the class WaveFileWriter method getAudioFileTypes.
// METHODS TO IMPLEMENT AudioFileWriter
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];
}
use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.
the class PCMtoPCMCodec method getConvertedStream.
// OLD CODE
/**
* Opens the codec with the specified parameters.
* @param stream stream from which data to be processed should be read
* @param outputFormat desired data format of the stream after processing
* @return stream from which processed data may be read
* @throws IllegalArgumentException if the format combination supplied is
* not supported.
*/
/* public AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {*/
private AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {
AudioInputStream cs = null;
AudioFormat inputFormat = stream.getFormat();
if (inputFormat.matches(outputFormat)) {
cs = stream;
} else {
cs = (AudioInputStream) (new PCMtoPCMCodecStream(stream, outputFormat));
tempBuffer = new byte[tempBufferSize];
}
return cs;
}
use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.
the class PCMtoPCMCodec method getTargetFormats.
/**
*/
public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) {
// filter out targetEncoding from the old getOutputFormats( sourceFormat ) method
AudioFormat[] formats = getOutputFormats(sourceFormat);
Vector newFormats = new Vector();
for (int i = 0; i < formats.length; i++) {
if (formats[i].getEncoding().equals(targetEncoding)) {
newFormats.addElement(formats[i]);
}
}
AudioFormat[] formatArray = new AudioFormat[newFormats.size()];
for (int i = 0; i < formatArray.length; i++) {
formatArray[i] = (AudioFormat) (newFormats.elementAt(i));
}
return formatArray;
}
Aggregations