Class SimpleAudioRecorder

java.lang.Object
  extended byjava.lang.Thread
      extended bySimpleAudioRecorder
All Implemented Interfaces:
java.lang.Runnable

public class SimpleAudioRecorder
extends java.lang.Thread

SimpleAudioRecorder Recording to an audio file (simple version) Purpose Records audio data and stores it in a file. The data is recorded in CD quality (44.1 kHz, 16 bit linear, stereo) and stored in a .wav file. Usage java SimpleAudioRecorder java SimpleAudioRecorder audiofile Parameters print usage information, then exit the file name of the audio file that should be produced from the recorded data Bugs, limitations You cannot select audio formats and the audio file type on the command line. See AudioRecorder for a version that has more advanced options. Due to a bug in the Sun jdk1.3/1.4, this program does not work with it. Source code SimpleAudioRecorder.java


Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SimpleAudioRecorder(javax.sound.sampled.TargetDataLine line, javax.sound.sampled.AudioFileFormat.Type targetType, java.io.File file)
           
 
Method Summary
 void run()
          You may be surprised that here, just 'AudioSystem.write()' is called.
 void start()
          Starts the recording.
 void stopRecording()
          Stops the recording.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, 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

SimpleAudioRecorder

public SimpleAudioRecorder(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.


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()
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.