com.groovemanager.sampled.waveform
Class PeakWaveForm

java.lang.Object
  extended by com.groovemanager.sampled.waveform.AbstractWaveForm
      extended by com.groovemanager.sampled.waveform.PeakWaveForm
All Implemented Interfaces:
WaveForm
Direct Known Subclasses:
DynamicPeakWaveForm

public class PeakWaveForm
extends AbstractWaveForm

A PeakWaveForm is a WaveForm that gets its data out of a number of peak values that have been created once for a file. Each peak value represents a number of sample frames in the original file and consists of a maximum and a minimum value for each cahnnel. Mostly this peak data is stored in a peak file, that is assigned to an audio file by giving it the name of the original file with an added ".gmpk" extension. The file myfile.wav.gmpk is for example the peak file of the file myfile.wav in the same directory.
A PeakWaveForm can also be created out of another WaveForm. In this case, the peak values are created when they are first needed and then stored in memory.

Author:
Manu Robledo

Field Summary
protected  byte[] data
          The peak data
static java.lang.String DEFAULT_PEAKFILE_EXTENSION
          The default extension for peak files
protected  WaveForm source
          PeakWaveForms can also be created on the fly from a source WaveForm.
 
Fields inherited from class com.groovemanager.sampled.waveform.AbstractWaveForm
channels, displayWidth, intervallSize, nullWave, position, realLength
 
Constructor Summary
  PeakWaveForm(byte[] data)
          Construct a new PeakWaveForm out of the given peak data.
  PeakWaveForm(java.io.File peakFile)
          Create a new PeakWaveForm out of the given peak file
private PeakWaveForm(WaveForm source, int newWidth)
          Private Constructor to be used from getPeak().
 
Method Summary
static boolean checkPeakFile(java.io.File f, java.io.File p)
          Check, if the given peak file is a valid peak file for the given audio file.
static PeakWaveForm createPeakWaveForm(java.io.File source, org.eclipse.swt.widgets.Shell shell)
          Get a PeakWaveForm for the given audio file.
protected  java.lang.String getAdditionalToString()
          Get an additional String describing this WaveForm.
 byte[] getData()
          Get the whole Peak data as array.
static byte[] getHeader(int iSize, long lastModified, long realLength, short channels, int snapshots)
          Create a valid peak file header out of the given data
 byte getMax(int channel)
          Get the maximum value at the current read position in the given channel
 byte getMin(int channel)
          Get the minimum value at the current read position in the given channel
static WaveForm getPeak(WaveForm source, int newWidth, int iSize)
          Get a PeakWaveForm for the given WaveForm
protected  void readFromFile(java.io.File peakFile)
          Read the peak data from the given file
 WaveForm subWaveForm(int begin, int length, int width)
          Get a WaveForm Object that represents the part of this WaveForm specified by begin and length and displays this part in the specified width
static void writeToStream(WaveForm source, java.io.OutputStream out, long lastModified, int iSize)
          Write the given source WaveForm as a peak file into a given output.
 
Methods inherited from class com.groovemanager.sampled.waveform.AbstractWaveForm
canProvide, getChannels, getDisplayableLength, getIntervallSize, getPosition, getRealLength, getRealPosition, getZoomFactor, next, rewind, setPosition, setRealPosition, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

data

protected byte[] data
The peak data


DEFAULT_PEAKFILE_EXTENSION

public static final java.lang.String DEFAULT_PEAKFILE_EXTENSION
The default extension for peak files

See Also:
Constant Field Values

source

protected WaveForm source
PeakWaveForms can also be created on the fly from a source WaveForm. If this PeakWaveForm has been created in this way, the source WaveForm is stored in this variable

Constructor Detail

PeakWaveForm

public PeakWaveForm(byte[] data)
Construct a new PeakWaveForm out of the given peak data. The first 34 bytes of the data must be a valid peak file header.

Parameters:
data - The peak data

PeakWaveForm

public PeakWaveForm(java.io.File peakFile)
             throws java.io.IOException
Create a new PeakWaveForm out of the given peak file

Parameters:
peakFile - The peak file containing the peak data
Throws:
java.io.IOException - If an I/O Error occured during reading

PeakWaveForm

private PeakWaveForm(WaveForm source,
                     int newWidth)
Private Constructor to be used from getPeak().

Parameters:
source - The source WaveForm
newWidth - The width of the WaveForm to create
Method Detail

checkPeakFile

public static boolean checkPeakFile(java.io.File f,
                                    java.io.File p)
Check, if the given peak file is a valid peak file for the given audio file.

Parameters:
f - The audio file
p - The peak file
Returns:
true, if
- both files exists,
- the peak file´s header data matches the properties of the audio file and
- the peak file´s length matches the peak file´s header data,
false otherwise

createPeakWaveForm

public static PeakWaveForm createPeakWaveForm(java.io.File source,
                                              org.eclipse.swt.widgets.Shell shell)
                                       throws javax.sound.sampled.UnsupportedAudioFileException,
                                              NotFinishedException,
                                              java.io.IOException
Get a PeakWaveForm for the given audio file. If a peak file for this file can be found and if it is a valif peak file, it will be used. Otherwise a peak file for the given audio file will be created and a ProgressMonitor will be shown during the operation.
The peak file for a given audio file will be located in the same directory as the source file and will have the source file´s file name with an additional ".gmpk" extension. If no peak file is found and no peak file can be written to this filename, a temporary file is created in the temp directory.

Parameters:
source - The audio file to create the peak file for
shell - The parent Shell, under which the ProgressMonitor should be opened
Returns:
The PeakWaveForm for the given audio file
Throws:
javax.sound.sampled.UnsupportedAudioFileException - If the audio file´s format cannot be read
NotFinishedException - If no valid peak file was found and the operation for creating it didn´t finish correctly.
java.io.IOException - If an I/O Error occured

getAdditionalToString

protected java.lang.String getAdditionalToString()
Description copied from class: AbstractWaveForm
Get an additional String describing this WaveForm. Will be used in toString()

Overrides:
getAdditionalToString in class AbstractWaveForm
Returns:
An optional String describing special characteristics of this WaveForm

getData

public byte[] getData()
Description copied from interface: WaveForm
Get the whole Peak data as array. This method could take very long for WaveForms which don't store their data in such an array. Should be very fast for peak Wave Forms.

Specified by:
getData in interface WaveForm
Overrides:
getData in class AbstractWaveForm
Returns:
The Peak Data as byte-Array.
See Also:
WaveForm.getData()

getHeader

public static byte[] getHeader(int iSize,
                               long lastModified,
                               long realLength,
                               short channels,
                               int snapshots)
Create a valid peak file header out of the given data

Parameters:
iSize - The interval size
lastModified - The last modification date of the audio file represented by this peak file or -1, if it is not known
realLength - The length of the audio data in the audio file in sample frames
channels - The number of channels of the audio file
snapshots - The number of peak values (snapshots) contained in this peak file
Returns:
A byte-Array of length 34 containing the header data for the peak file

getMax

public byte getMax(int channel)
Description copied from interface: WaveForm
Get the maximum value at the current read position in the given channel

Parameters:
channel - the zero-based index of the channel
Returns:
the maximum value at this position as byte

getMin

public byte getMin(int channel)
Description copied from interface: WaveForm
Get the minimum value at the current read position in the given channel

Parameters:
channel - the zero-based index of the channel
Returns:
the minimum value at this position as byte

getPeak

public static WaveForm getPeak(WaveForm source,
                               int newWidth,
                               int iSize)
Get a PeakWaveForm for the given WaveForm

Parameters:
source - The WaveForm to get a PeakWaveForm for.
newWidth - The width of the new PeakWaveForm
iSize - The interval size to use for the new PeakWaveForm
Returns:
A PeakWaveForm with the given interval size and the given width or the given source WaveForm, if no PeakWaveForm is needed for the desired width.

readFromFile

protected void readFromFile(java.io.File peakFile)
                     throws java.io.IOException
Read the peak data from the given file

Parameters:
peakFile - The file to read from
Throws:
java.io.IOException - If an I/O Error occured during reading

subWaveForm

public WaveForm subWaveForm(int begin,
                            int length,
                            int width)
Description copied from interface: WaveForm
Get a WaveForm Object that represents the part of this WaveForm specified by begin and length and displays this part in the specified width

Specified by:
subWaveForm in interface WaveForm
Overrides:
subWaveForm in class AbstractWaveForm
Parameters:
begin - The offset to the beginning of this WaveForm. May also be negative.
length - The length in steps of this WaveForm that should be represented by the returned WaveForm.
width - The new width in which the part should be displayed.
Returns:
A WaveForm Object that represents the specified Part of this WaveForm and displays it in the given width. Can also be the same Object with adapted attributes
See Also:
WaveForm.subWaveForm(int, int, int)

writeToStream

public static void writeToStream(WaveForm source,
                                 java.io.OutputStream out,
                                 long lastModified,
                                 int iSize)
                          throws java.io.IOException
Write the given source WaveForm as a peak file into a given output. The peak header will also be written.

Parameters:
source - The WaveForm to get the peak data from
out - The OutputStream to write the data to
lastModified - The last modification date of the audio file this peak data belongs to or -1, if it is not known
iSize - The interval size of the WabeForm
Throws:
java.io.IOException - If an I/O Error occurs during writing