com.groovemanager.sampled.waveform
Class CreatePeakFileThread

java.lang.Object
  extended by java.lang.Thread
      extended by com.groovemanager.thread.ProgressThread
          extended by com.groovemanager.sampled.waveform.CreatePeakFileThread
All Implemented Interfaces:
java.lang.Runnable

public class CreatePeakFileThread
extends ProgressThread

This ProgressThread implementationcan be used to create a peak file out of a given AudioInputStream. This peak file can then be used for creating a PeakWaveForm.

Author:
Manu Robledo

Nested Class Summary
 
Nested classes inherited from class java.lang.Thread
java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  byte[] buffer
          Temporary buffer
protected  int buffer_size
          buffer size
protected  int bytesPerFrame
          The AudioFormatīs frame size
protected  int bytesPerSample
          The AudioFormatīs frame size
static int DEFAULT_INTERVALL_SIZE
          Default number of sample frames represented by one snapshot
protected  javax.sound.sampled.AudioFormat format
          The AudioFormat of the source data
protected  javax.sound.sampled.AudioInputStream in
          The AudioInputStream to read the peak data from
protected  int intervallPosition
          Current position inside one interval
protected  int iSize
          Number of sample frames represented by one snapshot
protected  int numBytesRead
          Number of bytes read in the last read operation.
protected  java.io.OutputStream out
          The OutputStream to write the peak file to
protected  byte[] p
          Temporary buffer
protected  long snapshots
          Number of snapshots to write
protected  java.io.File sourceFile
          The source audio file if known
protected  byte value
          temporary variable
protected  int valuesWritten
          Number of snapshot values written so far
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
CreatePeakFileThread(javax.sound.sampled.AudioInputStream in, java.io.File peak)
          Construct a new CreatePeakFileThread that reads audio data from the given AudioInputStream and writes the peak data into the given file
CreatePeakFileThread(javax.sound.sampled.AudioInputStream in, java.io.File peak, int intervallSize)
          Construct a new CreatePeakFileThread that reads audio data from the given AudioInputStream and writes the peak data into the given file
CreatePeakFileThread(javax.sound.sampled.AudioInputStream in, java.io.OutputStream out)
          Construct a new CreatePeakFileThread that reads audio data from the given AudioInputStream and writes the peak data to the given OutputStream
CreatePeakFileThread(javax.sound.sampled.AudioInputStream in, java.io.OutputStream out, int intervallSize)
          Construct a new CreatePeakFileThread that reads audio data from the given AudioInputStream and writes the peak data to the given OutputStream
CreatePeakFileThread(java.io.File source, java.io.File peak)
          Construct a new CreatePeakFileThread that reads audio data from the given audio file and writes the peak data to the given OutputStream
CreatePeakFileThread(java.io.File source, java.io.File peak, int intervallSize)
          Construct a new CreatePeakFileThread that reads audio data from the given audio file and writes the peak data to the given OutputStream
CreatePeakFileThread(java.io.File source, java.io.OutputStream out)
          Construct a new CreatePeakFileThread that reads audio data from the given audio file and writes the peak data to the given OutputStream
CreatePeakFileThread(java.io.File source, java.io.OutputStream out, int intervallSize)
          Construct a new CreatePeakFileThread that reads audio data from the given audio file and writes the peak data to the given OutputStream
 
Method Summary
protected  boolean breakCondition()
          The break condition to indicate that the operation has finished
protected  void cleanUp()
          Free any ressources and perform clean up operations.
protected  void init()
          Any kind of initialization in the moment right before starting the operation should be performed here
protected  void processNext()
          Perform the next step.
protected  java.lang.Object result()
          Specify the result of this operation, if any.
protected  int tellElapsed()
          Tell the length of the operation elapsed so for
protected  int tellTotal()
          Tell the total length of the operation
protected  byte[] writeHeader()
          Generate the peak fileīs header data.
 
Methods inherited from class com.groovemanager.thread.ProgressThread
cancelOperation, getElapsed, getErrorMessage, getResult, getTotal, hasFinished, run, setMonitor
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buffer

protected byte[] buffer
Temporary buffer


buffer_size

protected int buffer_size
buffer size


bytesPerFrame

protected int bytesPerFrame
The AudioFormatīs frame size


bytesPerSample

protected int bytesPerSample
The AudioFormatīs frame size


DEFAULT_INTERVALL_SIZE

public static final int DEFAULT_INTERVALL_SIZE
Default number of sample frames represented by one snapshot

See Also:
Constant Field Values

format

protected javax.sound.sampled.AudioFormat format
The AudioFormat of the source data


in

protected javax.sound.sampled.AudioInputStream in
The AudioInputStream to read the peak data from


intervallPosition

protected int intervallPosition
Current position inside one interval


iSize

protected int iSize
Number of sample frames represented by one snapshot


numBytesRead

protected int numBytesRead
Number of bytes read in the last read operation. This value will be -1 when the end of the input stream has been reached


out

protected java.io.OutputStream out
The OutputStream to write the peak file to


p

protected byte[] p
Temporary buffer


snapshots

protected long snapshots
Number of snapshots to write


sourceFile

protected java.io.File sourceFile
The source audio file if known


value

protected byte value
temporary variable


valuesWritten

protected int valuesWritten
Number of snapshot values written so far

Constructor Detail

CreatePeakFileThread

public CreatePeakFileThread(javax.sound.sampled.AudioInputStream in,
                            java.io.File peak)
                     throws java.io.FileNotFoundException
Construct a new CreatePeakFileThread that reads audio data from the given AudioInputStream and writes the peak data into the given file

Parameters:
in - The AudioInputStream to read from
peak - The file to write the data to
Throws:
java.io.FileNotFoundException - If the file could not be opened for writing

CreatePeakFileThread

public CreatePeakFileThread(javax.sound.sampled.AudioInputStream in,
                            java.io.File peak,
                            int intervallSize)
                     throws java.io.FileNotFoundException
Construct a new CreatePeakFileThread that reads audio data from the given AudioInputStream and writes the peak data into the given file

Parameters:
in - The AudioInputStream to read from
peak - The file to write the data to
intervallSize - The interval size to use
Throws:
java.io.FileNotFoundException - If the file could not be opened for writing

CreatePeakFileThread

public CreatePeakFileThread(javax.sound.sampled.AudioInputStream in,
                            java.io.OutputStream out)
Construct a new CreatePeakFileThread that reads audio data from the given AudioInputStream and writes the peak data to the given OutputStream

Parameters:
in - The AudioInputStream to read from
out - The OutputStream to write to

CreatePeakFileThread

public CreatePeakFileThread(javax.sound.sampled.AudioInputStream in,
                            java.io.OutputStream out,
                            int intervallSize)
Construct a new CreatePeakFileThread that reads audio data from the given AudioInputStream and writes the peak data to the given OutputStream

Parameters:
in - The AudioInputStream to read from
out - The OutputStream to write to
intervallSize - The interval size to use

CreatePeakFileThread

public CreatePeakFileThread(java.io.File source,
                            java.io.File peak)
                     throws javax.sound.sampled.UnsupportedAudioFileException,
                            java.io.IOException
Construct a new CreatePeakFileThread that reads audio data from the given audio file and writes the peak data to the given OutputStream

Parameters:
source - The audio file to read from
peak - The file to write to
Throws:
javax.sound.sampled.UnsupportedAudioFileException - If the given audio fileīs format can not be read
java.io.IOException - If an I/O Error occured during file reading or OutputStream creation

CreatePeakFileThread

public CreatePeakFileThread(java.io.File source,
                            java.io.File peak,
                            int intervallSize)
                     throws javax.sound.sampled.UnsupportedAudioFileException,
                            java.io.IOException
Construct a new CreatePeakFileThread that reads audio data from the given audio file and writes the peak data to the given OutputStream

Parameters:
source - The audio file to read from
peak - The file to write to
intervallSize - The interval size to use
Throws:
javax.sound.sampled.UnsupportedAudioFileException - If the given audio fileīs format can not be read
java.io.IOException - If an I/O Error occured during file reading or OutputStream creation

CreatePeakFileThread

public CreatePeakFileThread(java.io.File source,
                            java.io.OutputStream out)
                     throws javax.sound.sampled.UnsupportedAudioFileException,
                            java.io.IOException
Construct a new CreatePeakFileThread that reads audio data from the given audio file and writes the peak data to the given OutputStream

Parameters:
source - The audio file to read from
out - The OutputStream to write to
Throws:
javax.sound.sampled.UnsupportedAudioFileException - If the given audio fileīs format can not be read
java.io.IOException - If an I/O Error occured during file reading

CreatePeakFileThread

public CreatePeakFileThread(java.io.File source,
                            java.io.OutputStream out,
                            int intervallSize)
                     throws javax.sound.sampled.UnsupportedAudioFileException,
                            java.io.IOException
Construct a new CreatePeakFileThread that reads audio data from the given audio file and writes the peak data to the given OutputStream

Parameters:
source - The audio file to read from
out - The OutputStream to write to
intervallSize - The interval size to use
Throws:
javax.sound.sampled.UnsupportedAudioFileException - If the given audio fileīs format can not be read
java.io.IOException - If an I/O Error occured during file reading
Method Detail

breakCondition

protected boolean breakCondition()
Description copied from class: ProgressThread
The break condition to indicate that the operation has finished

Specified by:
breakCondition in class ProgressThread
Returns:
true, if the operation has finished and the thread should therefore come to its end, false otherwise

cleanUp

protected void cleanUp()
Description copied from class: ProgressThread
Free any ressources and perform clean up operations. This methos will be called, if the operation has been cancelled, if an Exception is thrown by processNext(), or if the operation has been finished as expected.

Specified by:
cleanUp in class ProgressThread

init

protected void init()
Description copied from class: ProgressThread
Any kind of initialization in the moment right before starting the operation should be performed here

Specified by:
init in class ProgressThread

processNext

protected void processNext()
Description copied from class: ProgressThread
Perform the next step. This method will be called repeatedly as long as
- tellElpased() < tellTotal() and
- breakCondition() != true and
- The thread has not been cancelled

Specified by:
processNext in class ProgressThread

result

protected java.lang.Object result()
Description copied from class: ProgressThread
Specify the result of this operation, if any.

Specified by:
result in class ProgressThread
Returns:
Any kind of object representing the result of the performed operation. null should onlx be returned, if the operation didnīt finish correctly. If it did finish correctly, but it has no result, a dummy object should be returned.

tellElapsed

protected int tellElapsed()
Description copied from class: ProgressThread
Tell the length of the operation elapsed so for

Specified by:
tellElapsed in class ProgressThread
Returns:
The elapsed length of the operation. The unit to use for this value is free defineable

tellTotal

protected int tellTotal()
Description copied from class: ProgressThread
Tell the total length of the operation

Specified by:
tellTotal in class ProgressThread
Returns:
The total length of the operation. The unit to use for this value is free defineable

writeHeader

protected byte[] writeHeader()
Generate the peak fileīs header data. The peak file header is defined as follows:
- First 4 bytes: "GMPK" as ASCII-String
- Next 4 bytes: Integer containing used intervall size (= number of sample frames represented by one snapshot)
- Next 8 bytes: Long containing the modification date of the audio file this peak file belongs to or -1 if it is not known.
- Next 8 bytes: Number of sample frames contained in the original audio file
- Next 2 bytes: Short containing the number of channels of the source audio file
- Next 8 bytes: Number of snapshots contained in this file.
A snapshot always consists of two bytes for each channel: A minimum value (first byte) and a maximum value (second byte)

Returns:
The header data as byte Array