1. /**
  2.  *
  3.  */
  4. package com.actura.app.capture;
  5.  
  6. import java.io.File;
  7. import java.io.IOException;
  8.  
  9. import javax.sound.sampled.AudioFileFormat;
  10. import javax.sound.sampled.AudioInputStream;
  11. import javax.sound.sampled.AudioSystem;
  12. import javax.sound.sampled.TargetDataLine;
  13.  
  14. /**
  15.  * It performs all the recording tasks behind the scene.
  16.  *
  17.  * @author Mihir Parekh
  18.  *
  19.  * @version 1.0
  20.  */
  21. public class ActuraRecorder implements Runnable {
  22.  
  23.     /**
  24.      * which is the line from hardware analog mixer from which we capture
  25.      * sounds.
  26.      */
  27.     private TargetDataLine recLine = null;
  28.  
  29.     /**
  30.      * the file in which we stores our recording at client side.
  31.      */
  32.     private File outputFile = null;
  33.  
  34.     /**
  35.      * the recording audio File format Type By default it is WAV
  36.      */
  37.     private AudioFileFormat.Type targetType = null;
  38.  
  39.     /**
  40.      * which is AudioInputStream which takes bits from
  41.      * <Code>TargetDataLine</Code> & automatically converts the bytes into
  42.      * specified format.
  43.      */
  44.     private AudioInputStream myAIS = null;
  45.  
  46.     /**
  47.      * performs the actual task of recording.
  48.      */
  49.     private Thread recorder = null;
  50.  
  51.     /**
  52.      * Default Constructor
  53.      */
  54.     private ActuraRecorder() {
  55.  
  56.     }
  57.  
  58.     /**
  59.      * Explicit Constructor for this class to be called by its clients to
  60.      * instantiate and fulfill the purpose of the recording. It also instantiate
  61.      * the desired <Code>AudioInputStream</Code> from mentioned
  62.      * <Code>TargetDataLine</Code>.
  63.      *
  64.      * @param recordLine
  65.      *            on which we perform recording.
  66.      * @param targetType
  67.      *            the target audio file type.
  68.      * @param outputFile
  69.      *            the audio file in which we store recorded sound.
  70.      */
  71.     public ActuraRecorder(TargetDataLine recordLine,
  72.             AudioFileFormat.Type targetType, File outputFile) {
  73.         this();
  74.         this.recLine = recordLine;
  75.         myAIS = new AudioInputStream(this.recLine);
  76.         this.outputFile = outputFile;
  77.         this.targetType = targetType;
  78.     }
  79.  
  80.     @Override
  81.     public void run() {
  82.  
  83.         try {
  84.             AudioSystem.write(myAIS, targetType, outputFile);
  85.         } catch (IOException e) {
  86.             // TODO provide runtime exception to reach it to presentation
  87.             // layer
  88.             e.printStackTrace();
  89.  
  90.         }
  91.  
  92.     }
  93.  
  94.     /**
  95.      * by calling this methods actual recording starts in specified file from
  96.      * specified <Code>TargetDataLine</Code>.
  97.      */
  98.     public void start() {
  99.         recLine.start();
  100.         recorder = new Thread(this);
  101.         recorder.start();
  102.     }
  103.  
  104.     /**
  105.      * It stops the recording process & stop the <Code>TargetDataLine</Code> and
  106.      * then clear the current Thread.
  107.      */
  108.     public void stop() {
  109.         recLine.stop();
  110.         recLine.close();
  111.         recorder = null;
  112.     }
  113.  
  114. }