org.das2.stream
Class MicrophoneStream

java.lang.Object
  extended by java.lang.Thread
      extended by org.das2.stream.MicrophoneStream
All Implemented Interfaces:
java.lang.Runnable

public class MicrophoneStream
extends java.lang.Thread


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
MicrophoneStream(javax.sound.sampled.TargetDataLine line, javax.sound.sampled.AudioFileFormat.Type targetType, java.io.File file)
           
 
Method Summary
static void main(java.lang.String[] args)
           
 void run()
          Main working method.
 void start()
          Starts the recording.
 void stopRecording()
          Stops the recording.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MicrophoneStream

public MicrophoneStream(javax.sound.sampled.TargetDataLine line,
                        javax.sound.sampled.AudioFileFormat.Type targetType,
                        java.io.File file)
Method Detail

start

public void start()
Starts the recording. To accomplish this, (i) the line is started and (ii) the thread is started.

Overrides:
start in class java.lang.Thread

stopRecording

public void stopRecording()
Stops the recording. Note that stopping the thread explicitely is not necessary. Once no more data can be read from the TargetDataLine, no more data be read from our AudioInputStream. And if there is no more data from the AudioInputStream, the method 'AudioSystem.write()' (called in 'run()' returns. Returning from 'AudioSystem.write()' is followed by returning from 'run()', and thus, the thread is terminated automatically. It's not a good idea to call this method just 'stop()' because stop() is a (deprecated) method of the class 'Thread'. And we don't want to override this method.


run

public void run()
Main working method. You may be surprised that here, just 'AudioSystem.write()' is called. But internally, it works like this: AudioSystem.write() contains a loop that is trying to read from the passed AudioInputStream. Since we have a special AudioInputStream that gets its data from a TargetDataLine, reading from the AudioInputStream leads to reading from the TargetDataLine. The data read this way is then written to the passed File. Before writing of audio data starts, a header is written according to the desired audio file type. Reading continues untill no more data can be read from the AudioInputStream. In our case, this happens if no more data can be read from the TargetDataLine. This, in turn, happens if the TargetDataLine is stopped or closed (which implies stopping). (Also see the comment above.) Then, the file is closed and 'AudioSystem.write()' returns.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

main

public static void main(java.lang.String[] args)