use of javax.sound.sampled.UnsupportedAudioFileException in project screenbird by adamhub.
the class AudioRecorder method compileAudio.
/**
*
* @param audioFileName
*/
public void compileAudio(String audioFileName) {
if (this.getAudioFiles().size() > 0) {
try {
AudioFormat format = null;
int audioFilesSize = this.getAudioFiles().size();
for (int i = 0; i < audioFilesSize; i++) {
if (i < audioFilesSize - 1) {
AudioFileItem audioFileItem = this.getAudioFiles().get(i);
File audioFile = new File(audioFileItem.getName());
if (audioFile.isFile()) {
long audioLength = audioFileItem.getEndMS() - audioFileItem.getStartMS();
AudioRecorder.cutAudioFile(audioFile, audioLength);
}
}
}
File file = new File(audioFileName);
this.audioInputStreams = new Vector<AudioInputStream>();
long length = 0;
long correctStartTimestamp = 0;
// Iterate over audio files
for (int i = 0; i < audioFilesSize; i++) {
AudioFileItem audioFileItem = this.getAudioFiles().get(i);
File audioFile = new File(audioFileItem.getName());
if (audioFile.isFile()) {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
// Checks if we need to add empty sound before adding the sound file
if (audioFileItem.getStartMS() > correctStartTimestamp && audioFileItem.isPreviousDropped()) {
// The correct start time for the audio file should
// be 0 if it's the first file or equals the end
// time of the prev audio file
AudioInputStream audioStreamEmpty = addEmptySound(audioFileItem.getStartMS(), correctStartTimestamp);
length += audioStreamEmpty.getFrameLength();
this.audioInputStreams.add(audioStreamEmpty);
}
correctStartTimestamp = audioFileItem.getStartMS() + (long) (1000 * audioStream.getFrameLength() / audioStream.getFormat().getFrameRate());
format = audioStream.getFormat();
length += audioStream.getFrameLength();
this.audioInputStreams.add(audioStream);
}
}
compileAudioStreams(this.audioInputStreams, file, format, length);
} catch (IOException ex) {
log(ex);
} catch (UnsupportedAudioFileException e) {
log(e);
}
}
}
use of javax.sound.sampled.UnsupportedAudioFileException in project ACS by ACS-Community.
the class AlarmSound method dumpAudioInformation.
/**
* Dump info about supported audio, file types and so on...
* <P>
* This method is useful while updating the audio files.
*/
private void dumpAudioInformation() {
// Java supported file types
AudioFileFormat.Type[] fileTypes = AudioSystem.getAudioFileTypes();
if (fileTypes == null || fileTypes.length == 0) {
System.out.println("No audio file types supported.");
} else {
for (AudioFileFormat.Type type : fileTypes) {
System.out.println(type.toString() + ", extension " + type.getExtension());
}
}
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
System.out.println("Mixers found: " + mixerInfos.length);
for (Mixer.Info mi : mixerInfos) {
System.out.println("\tMixer " + mi.getName() + ": " + mi.getVendor() + ", " + mi.getDescription());
}
// Dump info about the alarm files
for (URL url : soundURLs) {
AudioFileFormat format = null;
try {
format = AudioSystem.getAudioFileFormat(url);
} catch (IOException ioe) {
System.err.println("Error " + ioe.getMessage() + " accessing URL " + url.toString());
continue;
} catch (UnsupportedAudioFileException ue) {
System.err.println("Unsupported audio format for " + url + " (" + ue.getMessage() + ")");
}
System.out.println("Properties of " + url);
System.out.println("\tAudio file type " + format.getType().toString());
System.out.println("\tIs file type supported: " + AudioSystem.isFileTypeSupported(format.getType()));
System.out.println("\tLength in byes " + format.getByteLength());
Map<String, Object> props = format.properties();
Set<String> keys = props.keySet();
for (String str : keys) {
System.out.println("\t[" + str + ", " + props.get(str).toString() + "]");
}
AudioFormat aFormat = format.getFormat();
System.out.println("\tEncoding " + aFormat.getEncoding().toString());
System.out.print("\tByte order ");
if (aFormat.isBigEndian()) {
System.out.println("big endian");
} else {
System.out.println("little endian");
}
System.out.println("\tSample rate: " + aFormat.getSampleRate());
System.out.println("\tNum. of bits of a sample: " + aFormat.getSampleSizeInBits());
System.out.println("\tNum. of channels: " + aFormat.getChannels());
}
}
use of javax.sound.sampled.UnsupportedAudioFileException in project jdk8u_jdk by JetBrains.
the class WaveFloatFileReader method getAudioInputStream.
public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException {
AudioFileFormat format = getAudioFileFormat(stream);
RIFFReader riffiterator = new RIFFReader(stream);
if (!riffiterator.getFormat().equals("RIFF"))
throw new UnsupportedAudioFileException();
if (!riffiterator.getType().equals("WAVE"))
throw new UnsupportedAudioFileException();
while (riffiterator.hasNextChunk()) {
RIFFReader chunk = riffiterator.nextChunk();
if (chunk.getFormat().equals("data")) {
return new AudioInputStream(chunk, format.getFormat(), chunk.getSize());
}
}
throw new UnsupportedAudioFileException();
}
use of javax.sound.sampled.UnsupportedAudioFileException in project jdk8u_jdk by JetBrains.
the class AiffFileReader method getCOMM.
//--------------------------------------------------------------------
private AudioFileFormat getCOMM(InputStream is, boolean doReset) throws UnsupportedAudioFileException, IOException {
DataInputStream dis = new DataInputStream(is);
if (doReset) {
dis.mark(MAX_READ_LENGTH);
}
// assumes a stream at the beginning of the file which has already
// passed the magic number test...
// leaves the input stream at the beginning of the audio data
int fileRead = 0;
int dataLength = 0;
AudioFormat format = null;
// Read the magic number
int magic = dis.readInt();
// $$fb: fix for 4369044: javax.sound.sampled.AudioSystem.getAudioInputStream() works wrong with Cp037
if (magic != AiffFileFormat.AIFF_MAGIC) {
// not AIFF, throw exception
if (doReset) {
dis.reset();
}
throw new UnsupportedAudioFileException("not an AIFF file");
}
int length = dis.readInt();
int iffType = dis.readInt();
fileRead += 12;
int totallength;
if (length <= 0) {
length = AudioSystem.NOT_SPECIFIED;
totallength = AudioSystem.NOT_SPECIFIED;
} else {
totallength = length + 8;
}
// Is this an AIFC or just plain AIFF file.
boolean aifc = false;
// $$fb: fix for 4369044: javax.sound.sampled.AudioSystem.getAudioInputStream() works wrong with Cp037
if (iffType == AiffFileFormat.AIFC_MAGIC) {
aifc = true;
}
// Loop through the AIFF chunks until
// we get to the SSND chunk.
boolean ssndFound = false;
while (!ssndFound) {
// Read the chunk name
int chunkName = dis.readInt();
int chunkLen = dis.readInt();
fileRead += 8;
int chunkRead = 0;
// Switch on the chunk name.
switch(chunkName) {
case AiffFileFormat.FVER_MAGIC:
// Ignore format version for now.
break;
case AiffFileFormat.COMM_MAGIC:
// $$fb: fix for 4399551: Repost of bug candidate: cannot replay aif file (Review ID: 108108)
if ((!aifc && chunkLen < 18) || (aifc && chunkLen < 22)) {
throw new UnsupportedAudioFileException("Invalid AIFF/COMM chunksize");
}
// Read header info.
int channels = dis.readUnsignedShort();
if (channels <= 0) {
throw new UnsupportedAudioFileException("Invalid number of channels");
}
// numSampleFrames
dis.readInt();
int sampleSizeInBits = dis.readUnsignedShort();
if (sampleSizeInBits < 1 || sampleSizeInBits > 32) {
throw new UnsupportedAudioFileException("Invalid AIFF/COMM sampleSize");
}
float sampleRate = (float) read_ieee_extended(dis);
chunkRead += (2 + 4 + 2 + 10);
// If this is not AIFC then we assume it's
// a linearly encoded file.
AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
if (aifc) {
int enc = dis.readInt();
chunkRead += 4;
switch(enc) {
case AiffFileFormat.AIFC_PCM:
encoding = AudioFormat.Encoding.PCM_SIGNED;
break;
case AiffFileFormat.AIFC_ULAW:
encoding = AudioFormat.Encoding.ULAW;
// Java Sound convention
sampleSizeInBits = 8;
break;
default:
throw new UnsupportedAudioFileException("Invalid AIFF encoding");
}
}
int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
//$fb what's that ??
//if (sampleSizeInBits == 8) {
// encoding = AudioFormat.Encoding.PCM_SIGNED;
//}
format = new AudioFormat(encoding, sampleRate, sampleSizeInBits, channels, frameSize, sampleRate, true);
break;
case AiffFileFormat.SSND_MAGIC:
// Data chunk.
// we are getting *weird* numbers for chunkLen sometimes;
// this really should be the size of the data chunk....
int dataOffset = dis.readInt();
int blocksize = dis.readInt();
chunkRead += 8;
if (chunkLen < length) {
dataLength = chunkLen - chunkRead;
} else {
// $$kk: 11.03.98: this seems dangerous!
dataLength = length - (fileRead + chunkRead);
}
ssndFound = true;
break;
}
// switch
fileRead += chunkRead;
// skip the remainder of this chunk
if (!ssndFound) {
int toSkip = chunkLen - chunkRead;
if (toSkip > 0) {
fileRead += dis.skipBytes(toSkip);
}
}
}
if (format == null) {
throw new UnsupportedAudioFileException("missing COMM chunk");
}
AudioFileFormat.Type type = aifc ? AudioFileFormat.Type.AIFC : AudioFileFormat.Type.AIFF;
return new AiffFileFormat(type, totallength, format, dataLength / format.getFrameSize());
}
use of javax.sound.sampled.UnsupportedAudioFileException in project jdk8u_jdk by JetBrains.
the class AudioFileSoundbankReader method getSoundbank.
public Soundbank getSoundbank(InputStream stream) throws InvalidMidiDataException, IOException {
stream.mark(512);
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(stream);
Soundbank sbk = getSoundbank(ais);
if (sbk != null)
return sbk;
} catch (UnsupportedAudioFileException e) {
} catch (IOException e) {
}
stream.reset();
return null;
}
Aggregations