List of usage examples for javax.sound.sampled AudioInputStream getFormat
public AudioFormat getFormat()
From source file:com.skratchdot.electribe.model.esx.impl.SampleImpl.java
/** * @param file/*from ww w . j a v a 2s. c om*/ * @throws EsxException */ protected SampleImpl(File file) throws EsxException { super(); init(); // Declare our streams and formats AudioFormat audioFormatEncoded; AudioFormat audioFormatDecoded; AudioInputStream audioInputStreamEncoded; AudioInputStream audioInputStreamDecoded; try { // Initialize our streams and formats audioInputStreamEncoded = AudioSystem.getAudioInputStream(file); audioFormatEncoded = audioInputStreamEncoded.getFormat(); audioFormatDecoded = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, audioFormatEncoded.getSampleRate(), 16, audioFormatEncoded.getChannels(), audioFormatEncoded.getChannels() * 2, audioFormatEncoded.getSampleRate(), true); audioInputStreamDecoded = AudioSystem.getAudioInputStream(audioFormatDecoded, audioInputStreamEncoded); // We have a decoded stereo audio stream // Now we need to get the stream info into a list we can manipulate byte[] audioData = new byte[4096]; int nBytesRead = 0; long nTotalBytesRead = 0; List<Byte> audioDataListChannel1 = new ArrayList<Byte>(); List<Byte> audioDataListChannel2 = new ArrayList<Byte>(); boolean isAudioDataStereo = false; // Set isAudioDataStereo if (audioFormatEncoded.getChannels() == 1) { isAudioDataStereo = false; } else if (audioFormatEncoded.getChannels() == 2) { isAudioDataStereo = true; } else { throw new EsxException("Sample has too many channels: " + file.getAbsolutePath()); } // Convert stream to list. This needs to be optimized. Converting // a byte at a time is probably too slow... while (nBytesRead >= 0) { nBytesRead = audioInputStreamDecoded.read(audioData, 0, audioData.length); // If we aren't at the end of the stream if (nBytesRead > 0) { for (int i = 0; i < nBytesRead; i++) { // MONO if (!isAudioDataStereo) { audioDataListChannel1.add(audioData[i]); audioDataListChannel2.add(audioData[i]); } // STEREO (LEFT) else if (nTotalBytesRead % 4 < 2) { audioDataListChannel1.add(audioData[i]); } // STEREO (RIGHT) else { audioDataListChannel2.add(audioData[i]); } // Update the total amount of bytes we've read nTotalBytesRead++; } } // Throw Exception if sample is too big if (nTotalBytesRead > EsxUtil.MAX_SAMPLE_MEM_IN_BYTES) { throw new EsxException("Sample is too big: " + file.getAbsolutePath()); } } // Set member variables int frameLength = audioDataListChannel1.size() / 2; this.setNumberOfSampleFrames(frameLength); this.setEnd(frameLength - 1); this.setLoopStart(frameLength - 1); this.setSampleRate((int) audioFormatEncoded.getSampleRate()); this.setAudioDataChannel1(EsxUtil.listToByteArray(audioDataListChannel1)); this.setAudioDataChannel2(EsxUtil.listToByteArray(audioDataListChannel2)); this.setStereoOriginal(isAudioDataStereo); // Set calculated Sample Tune (from Sample Rate) SampleTune newSampleTune = EsxFactory.eINSTANCE.createSampleTune(); float newFloat = newSampleTune.calculateSampleTuneFromSampleRate(this.getSampleRate()); newSampleTune.setValue(newFloat); this.setSampleTune(newSampleTune); // Set name String newSampleName = new String(); newSampleName = StringUtils.left(StringUtils.trim(file.getName()), 8); this.setName(newSampleName); // Attempt to set loopStart and End from .wav smpl chunk if (file.getAbsolutePath().toLowerCase().endsWith(".wav")) { try { RIFFWave riffWave = WavFactory.eINSTANCE.createRIFFWave(file); ChunkSampler chunkSampler = (ChunkSampler) riffWave .getFirstChunkByEClass(WavPackage.Literals.CHUNK_SAMPLER); if (chunkSampler != null && chunkSampler.getSampleLoops().size() > 0) { SampleLoop sampleLoop = chunkSampler.getSampleLoops().get(0); Long tempLoopStart = sampleLoop.getStart(); Long tempLoopEnd = sampleLoop.getEnd(); if (tempLoopStart < this.getEnd() && tempLoopStart >= 0) { this.setLoopStart(tempLoopStart.intValue()); } if (tempLoopEnd < this.getEnd() && tempLoopEnd > this.getLoopStart()) { this.setEnd(tempLoopEnd.intValue()); } } } catch (Exception e) { e.printStackTrace(); } } } catch (UnsupportedAudioFileException e) { e.printStackTrace(); throw new EsxException("Invalid audio file: " + file.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); throw new EsxException("Invalid audio file: " + file.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); throw new EsxException("Invalid audio file: " + file.getAbsolutePath()); } }