/* * Projecte Fressa a JAVA * GravarSoWav.java * Created on 29 / desembre / 2008, 21:54 * * @author Jordi Lagares Roset "jlagares@xtec.cat - www.lagares.org" * amb el suport del Departament d'Educacio de la Generalitat de Catalunya * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details (see the LICENSE file). */ import javax.sound.sampled.*; import java.io.*; public class GravarSoWav { double duration, seconds; AudioInputStream audioInputStream; String errStr; String fileName = "untitled"; File file; Capture capture = new Capture(); /** * Creates a new instance of GravarSoWav */ public GravarSoWav() { } public void IniciarGravacio() { capture.start(); } public String PararGravacio() { capture.stop(); return errStr; } public String DesarFitxerWav(String name) { saveToFile(name,AudioFileFormat.Type.WAVE); return errStr; } public void createAudioInputStream(File file, boolean updateComponents) { if (file != null && file.isFile()) { try { this.file = file; errStr = null; audioInputStream = AudioSystem.getAudioInputStream(file); fileName = file.getName(); long milliseconds = (long)((audioInputStream.getFrameLength() * 1000) / audioInputStream.getFormat().getFrameRate()); duration = milliseconds / 1000.0; if (updateComponents) { //formatControls.setFormat(audioInputStream.getFormat()); //samplingGraph.createWaveForm(null); } } catch (Exception ex) { reportStatus(ex.toString()); } } else { reportStatus("Audio file required."); } } public void saveToFile(String name, AudioFileFormat.Type fileType) { if (audioInputStream == null) { reportStatus("No loaded audio to save"); return; } else if (file != null) { createAudioInputStream(file, false); } // reset to the beginnning of the captured data try { audioInputStream.reset(); } catch (Exception e) { reportStatus("Unable to reset stream " + e); return; } File file = new File(fileName = name); try { if (AudioSystem.write(audioInputStream, fileType, file) == -1) { throw new IOException("Problems writing to file"); } } catch (Exception ex) { reportStatus(ex.toString()); } } private void reportStatus(String msg) { if ((errStr = msg) != null) { System.out.println(errStr); } } class Capture implements Runnable { TargetDataLine line; Thread thread; public void start() { errStr = null; thread = new Thread(this); thread.setName("Capture"); thread.start(); } public void stop() { thread = null; } private void shutDown(String message) { if ((errStr = message) != null && thread != null) { thread = null; System.err.println(errStr); } } private AudioFormat getAudioFormat(){ float sampleRate = 11025.0F; //8000,11025,16000,22050,44100 int sampleSizeInBits = 8; //8,16 int channels = 1; //1,2 boolean signed = true; //true,false boolean bigEndian = false; //true,false return new AudioFormat(sampleRate,sampleSizeInBits,channels,signed,bigEndian); } public void run() { duration = 0; audioInputStream = null; // define the required attributes for our line, // and make sure a compatible line is supported. AudioFormat format = getAudioFormat(); DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); if (!AudioSystem.isLineSupported(info)) { shutDown("Line matching " + info + " not supported."); return; } // get and open the target data line for capture. try { line = (TargetDataLine) AudioSystem.getLine(info); line.open(format, line.getBufferSize()); } catch (LineUnavailableException ex) { shutDown("Unable to open the line: " + ex); return; } catch (SecurityException ex) { shutDown(ex.toString()); //JavaSound.showInfoDialog(); return; } catch (Exception ex) { shutDown(ex.toString()); return; } // play back the captured audio data ByteArrayOutputStream out = new ByteArrayOutputStream(); int frameSizeInBytes = format.getFrameSize(); int bufferLengthInFrames = line.getBufferSize() / 8; int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes; byte[] data = new byte[bufferLengthInBytes]; int numBytesRead; line.start(); while (thread != null) { if((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) { break; } out.write(data, 0, numBytesRead); } // we reached the end of the stream. stop and close the line. line.stop(); line.close(); line = null; // stop and close the output stream try { out.flush(); out.close(); } catch (IOException ex) { ex.printStackTrace(); } // load bytes into the audio input stream for playback byte audioBytes[] = out.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes); audioInputStream = new AudioInputStream(bais, format, audioBytes.length / frameSizeInBytes); long milliseconds = (long)((audioInputStream.getFrameLength() * 1000) / format.getFrameRate()); duration = milliseconds / 1000.0; try { audioInputStream.reset(); } catch (Exception ex) { ex.printStackTrace(); return; } } } // End class Capture }