com.groovemanager.sampled.waveform
Class AbstractWaveFormDisplay

java.lang.Object
  extended by com.groovemanager.sampled.waveform.AbstractWaveFormDisplay
Direct Known Subclasses:
SelectableWaveFormDisplay

public abstract class AbstractWaveFormDisplay
extends java.lang.Object

A WaveFormDisplay can be used to display WaveForm objects provided by a WaveFormProvider. This abstract class is intended to be independent of any graphics framework so that implementations are possible in Swing, SWT or whatever.

Author:
Manu Robledo

Field Summary
protected  int channelSpacing
          Left and right offset in pixels
protected  int leftOffset
          Left and right offset in pixels
protected  java.util.ArrayList listeners
          List of WaveDisplayListeners registered with this WaveFormDisplay
protected  double scrollFactor
          The scroll factor: 0 means scroll to the left, 0.5 means scroll to center and 1 means scroll to the right.
protected  WaveFormProvider source
          The source provider from which the WaveForm data is being provided
protected  int topOffset
          Left and right offset in pixels
protected  double zoomFactor
          Factor describing the current zoom state of this WaveFormDisplay.
 
Constructor Summary
AbstractWaveFormDisplay()
           
 
Method Summary
 void addWaveDisplayListener(WaveDisplayListener listener)
          Add a WaveDisplayListener to this display that will be notified when the visible area of this display changes, e.g. when scrolling or zooming
protected static int calcY(int top, int maxheight, int value)
          Calculate the y coordinate for a given WaveForm sample value
protected  int dataToPixel(int dataPos)
          Convert a data position into a pixel position
protected  void drawBackground()
          Draw the Background
protected abstract  void drawChannelBackground(int x, int y, int width, int height)
          Draw one channel´s background into the given area
protected abstract  void drawChannelForeground(int x, int y, int width, int height)
          Draw the foreground elements of one channel into the given area
protected  void drawForeground()
          Draw the Foreground elements
protected abstract  void drawGlobalBackground(int x, int y, int width, int height)
          Draw the global background in the given area.
protected abstract  void drawGlobalForeground(int x, int y, int width, int height)
          Draw the global foreground elements into the given area
protected  void drawWave()
          Draw the WaveForm
protected abstract  void drawWaveLine(int x, int y, int x2, int y2)
          Draw one line of the WaveForm, this is either a vertical connection from a maximum to a minmum or a connection from the average value of pixel x to the average value of pixel x + 1
protected  int getChannelHeight()
          Get the usable height for displaying a single channel
 int getChannels()
          Get the number of channels
 int getChannelSpacing()
          Get the space between two channels
protected  int getChannelTop(int channel)
          Get the y-coordinate of the top border of the specified channel
protected abstract  int getDisplayHeight()
          Get the total height available to this WaveDisplay
protected abstract  int getDisplayWidth()
          Get the total width available to this WaveDisplay
 int getFirstData()
          Get the frame position of the first visible data
 int getLastData()
          Get the frame position of the last visible data
 int getLeftOffset()
          Get the space between the left border and the beginning of the WaveForm as well as the space between the end of the WaveForm and the right border
protected  double getPixelFactor()
          Get the quotient between one pixel and the real number of sample frames represented by this pixel
 double getScroll()
          Get the current scroll factor
 WaveFormProvider getSource()
          Get the current WaveFormProvider of this WaveFormDisplay
 int getTopOffset()
          Get the space between the top border and the beginning of the WaveForm as well as the space between the end of the WaveForm and the bottom border
 int getTotalLength()
          Get the total length of the audio data represented by this WaveFormDisplay
protected  int getUsableDisplayHeight()
          Get the height in pixels that can be used for drawing the WaveForm
protected  int getUsableDisplayWidth()
          Get the width in pixels that can be used for drawing the WaveForm
protected  WaveForm getVisibleWaveForm()
          Get the visible WaveForm depending on zommFactor, scrollFactor and source
 double getZoom()
          Get the current zoom factor
 boolean hasData()
          Ask this WaveFormDisplay, if it has data to display
 int mouseToData(int mousePos)
          Convert a mouse position to a data position
protected  int pixelToData(int pixelPos)
          Convert a pixel position to a data position
 void redraw()
          Redraw the current display
 void removeSource()
          Remove the current WaveFormProvider if any
 void removeWaveDisplayListener(WaveDisplayListener listener)
          Remove a WaveDisplayListener from this display
 void scroll(double d)
          Scroll to the given scroll factor.
 void scrollCenter()
          Scroll to center position
 void scrollLeft()
          Scroll to the most left position
 void scrollRight()
          Scroll to the most right position
 void setChannelSpacing(int spacing)
          Set the space between two channels
 void setLeftOffset(int offset)
          Set the space between the left border and the beginning of the WaveForm as well as the space between the end of the WaveForm and the right border
 void setSource(WaveFormProvider source)
          Set the WaveFormProvider from which to get the WaveForm data
 void setTopOffset(int offset)
          Set the space between the top border and the beginning of the WaveForm as well as the space between the end of the WaveForm and the bottom border
 void showAll()
          Show the whole WaveForm
 void showData(int first, int last)
          Adapt the zoom and scroll factor so that the given part of the source will be visible
 void zoom(double d)
          Zoom to the specified zoom factor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

channelSpacing

protected int channelSpacing
Left and right offset in pixels


leftOffset

protected int leftOffset
Left and right offset in pixels


listeners

protected java.util.ArrayList listeners
List of WaveDisplayListeners registered with this WaveFormDisplay


scrollFactor

protected double scrollFactor
The scroll factor: 0 means scroll to the left, 0.5 means scroll to center and 1 means scroll to the right. Any inbetween value is possible.


source

protected WaveFormProvider source
The source provider from which the WaveForm data is being provided


topOffset

protected int topOffset
Left and right offset in pixels


zoomFactor

protected double zoomFactor
Factor describing the current zoom state of this WaveFormDisplay. A zoom factor of 1.0 means, that the whole WaveForm is visible, a greater value means zooming in and a smaller value zooming out.

Constructor Detail

AbstractWaveFormDisplay

public AbstractWaveFormDisplay()
Method Detail

addWaveDisplayListener

public void addWaveDisplayListener(WaveDisplayListener listener)
Add a WaveDisplayListener to this display that will be notified when the visible area of this display changes, e.g. when scrolling or zooming

Parameters:
listener - The WaveDisplayListener to add

calcY

protected static int calcY(int top,
                           int maxheight,
                           int value)
Calculate the y coordinate for a given WaveForm sample value

Parameters:
top - The top offset
maxheight - The total height of the area into which the WaveForm should be drawn
value - The sample value gotten from the WaveForm
Returns:
An y-coordinate that corresponds to the given WaveForm sample

dataToPixel

protected int dataToPixel(int dataPos)
Convert a data position into a pixel position

Parameters:
dataPos - The position in the audio data in sample frames
Returns:
The x-coordinate of the corresponding pixel, not including the left offset

drawBackground

protected void drawBackground()
Draw the Background


drawChannelBackground

protected abstract void drawChannelBackground(int x,
                                              int y,
                                              int width,
                                              int height)
Draw one channel´s background into the given area

Parameters:
x - The x-coordinate of the area
y - The y-coordinate of the area
width - The width of the area
height - The height of the area

drawChannelForeground

protected abstract void drawChannelForeground(int x,
                                              int y,
                                              int width,
                                              int height)
Draw the foreground elements of one channel into the given area

Parameters:
x - The x-coordinate of the area
y - The y-coordinate of the area
width - The width of the area
height - The height of the area

drawForeground

protected void drawForeground()
Draw the Foreground elements


drawGlobalBackground

protected abstract void drawGlobalBackground(int x,
                                             int y,
                                             int width,
                                             int height)
Draw the global background in the given area. Most implementations will just fill this background

Parameters:
x - The x-coordinate of the area
y - The y-coordinate of the area
width - The width of the area
height - The height of the area

drawGlobalForeground

protected abstract void drawGlobalForeground(int x,
                                             int y,
                                             int width,
                                             int height)
Draw the global foreground elements into the given area

Parameters:
x - The x-coordinate of the area
y - The y-coordinate of the area
width - The width of the area
height - The height of the area

drawWave

protected void drawWave()
Draw the WaveForm


drawWaveLine

protected abstract void drawWaveLine(int x,
                                     int y,
                                     int x2,
                                     int y2)
Draw one line of the WaveForm, this is either a vertical connection from a maximum to a minmum or a connection from the average value of pixel x to the average value of pixel x + 1

Parameters:
x - The x-coordinate of the first point
y - The y-coordinate of the first point
x2 - The x-coordinate of the second point
y2 - The y-coordinate of the second point

getChannelHeight

protected int getChannelHeight()
Get the usable height for displaying a single channel

Returns:
The height in pixels available for displaying one channel´s data

getChannels

public int getChannels()
Get the number of channels

Returns:
The number of channels currently displayed

getChannelSpacing

public int getChannelSpacing()
Get the space between two channels

Returns:
The space in pixels

getChannelTop

protected int getChannelTop(int channel)
Get the y-coordinate of the top border of the specified channel

Parameters:
channel - The zero-based channel
Returns:
The y-coordinate of the channel´s top in pixels

getDisplayHeight

protected abstract int getDisplayHeight()
Get the total height available to this WaveDisplay

Returns:
This WaveDisplay´s total height

getDisplayWidth

protected abstract int getDisplayWidth()
Get the total width available to this WaveDisplay

Returns:
This WaveDisplay´s total width

getFirstData

public int getFirstData()
Get the frame position of the first visible data

Returns:
The frame position of the first visible sample frame

getLastData

public int getLastData()
Get the frame position of the last visible data

Returns:
The frame position of the last visible sample frame

getLeftOffset

public int getLeftOffset()
Get the space between the left border and the beginning of the WaveForm as well as the space between the end of the WaveForm and the right border

Returns:
The space in pixels

getPixelFactor

protected double getPixelFactor()
Get the quotient between one pixel and the real number of sample frames represented by this pixel

Returns:
1 / [number of sample frames per pixel]

getScroll

public double getScroll()
Get the current scroll factor

Returns:
The current scroll factor. A scroll factor of 0 means scroll to the left, 0.5 means scroll to center and 1 means scroll to the right. Any inbetween value is possible.

getSource

public WaveFormProvider getSource()
Get the current WaveFormProvider of this WaveFormDisplay

Returns:
The current WaveFormProvider or null, if no provider is set

getTopOffset

public int getTopOffset()
Get the space between the top border and the beginning of the WaveForm as well as the space between the end of the WaveForm and the bottom border

Returns:
The space in pixels

getTotalLength

public int getTotalLength()
Get the total length of the audio data represented by this WaveFormDisplay

Returns:
The total length in sample frames

getUsableDisplayHeight

protected int getUsableDisplayHeight()
Get the height in pixels that can be used for drawing the WaveForm

Returns:
The height in pixels available for drawing the Waveform, this is the total height minus 2 times the topOffset

getUsableDisplayWidth

protected int getUsableDisplayWidth()
Get the width in pixels that can be used for drawing the WaveForm

Returns:
The width in pixels available for drawing the Waveform, this is the total width minus 2 times the leftOffset

getVisibleWaveForm

protected WaveForm getVisibleWaveForm()
Get the visible WaveForm depending on zommFactor, scrollFactor and source

Returns:
The WaveForm representing the currently visible audio data or null, if no data is available for displaying

getZoom

public double getZoom()
Get the current zoom factor

Returns:
The current zoom factor. A zoom factor of 1.0 means, that the whole WaveForm is visible, a greater value means zooming in and a smaller value zooming out.

hasData

public boolean hasData()
Ask this WaveFormDisplay, if it has data to display

Returns:
true, if there is data available for displaying, false otherwise

mouseToData

public int mouseToData(int mousePos)
Convert a mouse position to a data position

Parameters:
mousePos - The x-coordinate of a mouse position including the left offset
Returns:
The position in the audio data corresponding to this mouse position in sample frames

pixelToData

protected int pixelToData(int pixelPos)
Convert a pixel position to a data position

Parameters:
pixelPos - The x-coordinate of a pixel, not including the left offset
Returns:
The position in the audio data corresponding to this pixel in sample frames

redraw

public void redraw()
Redraw the current display


removeSource

public void removeSource()
Remove the current WaveFormProvider if any


removeWaveDisplayListener

public void removeWaveDisplayListener(WaveDisplayListener listener)
Remove a WaveDisplayListener from this display

Parameters:
listener - The WaveDisplayListener to remove

scroll

public void scroll(double d)
Scroll to the given scroll factor.

Parameters:
d - The new scroll factor. A scroll factor of 0 means scroll to the left, 0.5 means scroll to center and 1 means scroll to the right. Any inbetween value is possible.

scrollCenter

public void scrollCenter()
Scroll to center position


scrollLeft

public void scrollLeft()
Scroll to the most left position


scrollRight

public void scrollRight()
Scroll to the most right position


setChannelSpacing

public void setChannelSpacing(int spacing)
Set the space between two channels

Parameters:
spacing - The new space in pixels

setLeftOffset

public void setLeftOffset(int offset)
Set the space between the left border and the beginning of the WaveForm as well as the space between the end of the WaveForm and the right border

Parameters:
offset - The new space in pixels

setSource

public void setSource(WaveFormProvider source)
Set the WaveFormProvider from which to get the WaveForm data

Parameters:
source - The new provider

setTopOffset

public void setTopOffset(int offset)
Set the space between the top border and the beginning of the WaveForm as well as the space between the end of the WaveForm and the bottom border

Parameters:
offset - The new space in pixels

showAll

public void showAll()
Show the whole WaveForm


showData

public void showData(int first,
                     int last)
Adapt the zoom and scroll factor so that the given part of the source will be visible

Parameters:
first - The position of the first sample frame to be made visible
last - The position of the last sample frame to be made visible

zoom

public void zoom(double d)
Zoom to the specified zoom factor.

Parameters:
d - The new zoom factor. A zoom factor of 1.0 means, that the whole WaveForm is visible, a greater value means zooming in and a smaller value zooming out.