/* * Main.java * * Created on August 19, 2006, 12:45 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package myrecord; import java.io.*; import java.nio.*; import java.nio.channels.FileChannel; import javax.sound.sampled.*; import org.xiph.speex.*; import org.xiph.speex.spi.*; public class myRecord extends Thread { private TargetDataLine m_line; private AudioFileFormat.Type m_targetType; private AudioInputStream m_audioInputStream; private File m_outputFile; private byte[] m_buffer; private final int bufSize = 500000; // 500kb ByteBuffer m_bbuff; boolean m_stillGoing; public myRecord(TargetDataLine line, AudioFormat endFormat) { m_line = line; // create a stream for the line m_audioInputStream = new AudioInputStream(line); m_audioInputStream = AudioSystem.getAudioInputStream(endFormat, m_audioInputStream); m_buffer = new byte[bufSize]; m_bbuff.allocate(bufSize); m_stillGoing = true; } public void start() { /* Starting the TargetDataLine. It tells the line that we now want to read data from it. If this method isn't called, we won't be able to read data from the line at all. */ m_line.start(); /* Starting the thread. This call results in the method 'run()' (see below) being called. There, the data is actually read from the line. */ super.start(); } public void stopRecording() { //File outputFile = new File("RECORDED.SPX"); m_stillGoing = false; int readSize; int bytesAvailable = bufSize; byte[] data = new byte[640]; byte[] data1 = new byte[640]; int offset = 0; int totalRead = 0; m_line.stop(); m_line.close(); SpeexEncoder se = new SpeexEncoder(); se.init(1,1,8000,1); do { readSize = m_line.read(data, offset, 640); // why 640? if (readSize > 0) { se.processData(data,0,readSize); se.getProcessedData(data1,0); offset += readSize + 1; totalRead += readSize; data = null; data1 = null; } } while (readSize > 0); System.out.println("Total Bytes Read: " + totalRead); try { ByteBuffer bbuf = ByteBuffer.allocate(bytesAvailable); bbuf.put(data1); File file = new File("RECORDED.SPX"); boolean append = false; try { FileChannel wChannel = new FileOutputStream(file, append).getChannel(); wChannel.write(bbuf); wChannel.close(); } catch (IOException e) { e.printStackTrace(); } } catch (Exception e){ e.printStackTrace(); } } public void run() { // does this thread write to file? try { //trying to append audio data to m_bbuff //while (m_stillGoing) { if (m_line.getBufferSize() > 0) { byte[] tempBuf = new byte[m_line.getBufferSize()]; //System.out.println("buffer: " + m_line.getBufferSize()); m_audioInputStream.read(tempBuf); m_bbuff.put(tempBuf); tempBuf = null; } //} /* *it used to write to file this way AudioSystem.write( m_audioInputStream, m_targetType, m_outputFile); */ } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { TargetDataLine targetDataLine = null; Mixer mixer; AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000.0F, // sampleRate 16, // sampleSize 1, // channels 2, // frame size 8000.0F, // frame rate false); // little endian AudioFormat endFormat = new AudioFormat(SpeexEncoding.SPEEX_Q3, 8000.0F, // sample rate -1, // sample size in bits 1, // channels -1, // frame size -1, // frame rate false); // little endian DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat); try { targetDataLine = (TargetDataLine) AudioSystem.getLine(info); targetDataLine.open(audioFormat); } catch (LineUnavailableException e) { System.out.println("unable to get a recording line"); e.printStackTrace(); System.exit(1); } myRecord recorder = new myRecord(targetDataLine,endFormat); System.out.println("Press ENTER to start the recording."); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } recorder.start(); System.out.println("Recording..."); System.out.println("Press ENTER to stop the recording."); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } recorder.stopRecording(); System.out.println("Recording stopped."); } }