org.pscode.ui.audiotrace
Class AudioPlotPanel

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by org.pscode.ui.audiotrace.AudioPlotPanel
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Runnable, Accessible, Configurable

public class AudioPlotPanel
extends JPanel
implements Runnable, Configurable

The AudioPlotPanel (APP) provides a trace of an audio signal. It is very configurable. Besides configuring the class from code, it can be configured using the AudioTraceColorsPanel (ATCP) and AudioTraceOptionsPanel (ATOP) and the methods of the AudioTraceMenu to provide a JMenu that pops those two panels, and allows configuration of volume (trace) options and full-screen mode. Note that the APP can be used without the configuration classes, so long as you do not intend to offer the user the option to change the rendering options. Versions

Version:
2009-09-26
Author:
Andrew Thompson
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
static int MAX_GAIN
           
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
AudioPlotPanel()
           
AudioPlotPanel(TargetDataLine line)
          Construct the plot panel.
 
Method Summary
 void addLissajousPoint(GeneralPath gp, int sampleProgress, double signalSize, double lastSignalSize)
          Plots the amplitude of the signal against its own gradient.
 void addTracePoint(GeneralPath gp, int sampleProgress, double signalSize)
          Plots the amplitude of the signal against time.
 void clearTrace()
           
 void configure(Applet applet)
           
 void copyIntoRenderBuffer(int first, int last)
           
 double[] frameToSignedDoubles(byte[] renderBuffer)
          Converts a single frame of audio bytes to signed doubles ranging from -1 to 1.
 Color getColor(int index)
           
 Color getColorForLevel(double level)
           
 int getColorSchemeIndex()
           
 int getGain()
           
 boolean getGainLoop()
           
 MultipleGradientPaint getGradient(int index)
           
 int getGradientGain()
           
 boolean getGradientsByTraceColor()
           
 double getGradientToAngle(double height1, double height2)
          Returns the arctan of the gradient.
 Font getIdealFont(String message, Graphics g, Font f)
           
 Color getOuterColor(int index)
           
 HashMap getParamMap(Applet applet)
           
 Parameter[] getParams(Applet applet)
           
 int getSampleSize()
           
 boolean getScaledVolume()
           
 double getScalingFactor(int progress)
          This provides a scaling factor intended to allow both ends of the lissajous trace to align.
 boolean getShowVolume()
           
 Color getTransparentBasedOnColor(Color c, int t)
           
 void paintComponent(Graphics g)
          Paint a trace of the current waveform to screen.
 void refreshBufferSize()
           
 void refreshGradients()
           
 void renderPlot()
           
 void run()
          Read data from the TargetDataLine, then paint it to screen.
 double scaledVolume(double[] raw)
           
 void setBuffer(byte[] buffer)
           
 void setByteCount(int byteCount)
           
 void setColor(Color c, int index)
           
 void setColorScheme(int i)
           
 void setColorSchemeIndex(int i)
           
 void setFadeRate(int fadeRate)
           
 void setFormat(AudioFormat af)
           
 void setFramePosition(int framePosition)
           
 void setGain(int gain)
           
 void setGainLoop(boolean loopGain)
           
 void setGradientGain(int gradientGain)
           
 void setGradientsByTraceColor()
           
 void setGradientsByTraceColor(boolean paintGradientsByTraceColor)
           
 void setLine(TargetDataLine line)
           
 void setLineNumber(int lineNumber)
          Informs the class what 'line number' this represents.
 void setLissajous(boolean lissajous)
           
 void setMessage(String message, int messageDisplayTime)
           
 void setOuterColor(Color c, int index)
           
 void setPaintGradient(boolean paintGradient)
           
 void setRenderingHints(RenderingHints renderingHints)
           
 void setSampleSize(int sampleSize)
          Construct the plot panel.
 void setScaledVolume(boolean scaledVolume)
           
 void setScrollStep(int step)
           
 void setShowVolume(boolean showVolume)
           
 void setStrokeSize(int size)
           
 void setThreadSleep(long millis)
           
 void setZoomStep(int step)
           
 void start()
           
 void stop()
           
 double volumeRMS(double[] raw)
          Computes the RMS volume of a group of signal sizes ranging from -1 to 1.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_GAIN

public static int MAX_GAIN
Constructor Detail

AudioPlotPanel

public AudioPlotPanel()

AudioPlotPanel

public AudioPlotPanel(TargetDataLine line)
Construct the plot panel.

Method Detail

getGradientsByTraceColor

public boolean getGradientsByTraceColor()

setGradientsByTraceColor

public void setGradientsByTraceColor()

setGradientsByTraceColor

public void setGradientsByTraceColor(boolean paintGradientsByTraceColor)

getParams

public Parameter[] getParams(Applet applet)
Specified by:
getParams in interface Configurable

getParamMap

public HashMap getParamMap(Applet applet)

configure

public void configure(Applet applet)
Specified by:
configure in interface Configurable

getColorSchemeIndex

public int getColorSchemeIndex()

setColorSchemeIndex

public void setColorSchemeIndex(int i)

getTransparentBasedOnColor

public Color getTransparentBasedOnColor(Color c,
                                        int t)

setSampleSize

public void setSampleSize(int sampleSize)
Construct the plot panel.


refreshBufferSize

public void refreshBufferSize()

setFormat

public void setFormat(AudioFormat af)

start

public void start()

run

public void run()
Read data from the TargetDataLine, then paint it to screen.

Specified by:
run in interface Runnable

stop

public void stop()
          throws Throwable
Throws:
Throwable

setThreadSleep

public void setThreadSleep(long millis)

setLine

public void setLine(TargetDataLine line)

setLineNumber

public void setLineNumber(int lineNumber)
Informs the class what 'line number' this represents.


setBuffer

public void setBuffer(byte[] buffer)

setFramePosition

public void setFramePosition(int framePosition)

setMessage

public void setMessage(String message,
                       int messageDisplayTime)

setByteCount

public void setByteCount(int byteCount)

copyIntoRenderBuffer

public void copyIntoRenderBuffer(int first,
                                 int last)

setColor

public void setColor(Color c,
                     int index)

getColor

public Color getColor(int index)

setColorScheme

public void setColorScheme(int i)

setOuterColor

public void setOuterColor(Color c,
                          int index)

getOuterColor

public Color getOuterColor(int index)

setPaintGradient

public void setPaintGradient(boolean paintGradient)

refreshGradients

public void refreshGradients()

setRenderingHints

public void setRenderingHints(RenderingHints renderingHints)

getGradient

public MultipleGradientPaint getGradient(int index)

setStrokeSize

public void setStrokeSize(int size)

setScrollStep

public void setScrollStep(int step)

setZoomStep

public void setZoomStep(int step)

setGain

public void setGain(int gain)

setGainLoop

public void setGainLoop(boolean loopGain)

getGainLoop

public boolean getGainLoop()

getGain

public int getGain()

getSampleSize

public int getSampleSize()

getGradientGain

public int getGradientGain()

setLissajous

public void setLissajous(boolean lissajous)

setGradientGain

public void setGradientGain(int gradientGain)

setFadeRate

public void setFadeRate(int fadeRate)

setShowVolume

public void setShowVolume(boolean showVolume)

getShowVolume

public boolean getShowVolume()

clearTrace

public void clearTrace()

paintComponent

public void paintComponent(Graphics g)
Paint a trace of the current waveform to screen.

Overrides:
paintComponent in class JComponent

renderPlot

public void renderPlot()

getIdealFont

public Font getIdealFont(String message,
                         Graphics g,
                         Font f)

getColorForLevel

public Color getColorForLevel(double level)

setScaledVolume

public void setScaledVolume(boolean scaledVolume)

getScaledVolume

public boolean getScaledVolume()

scaledVolume

public double scaledVolume(double[] raw)

volumeRMS

public double volumeRMS(double[] raw)
Computes the RMS volume of a group of signal sizes ranging from -1 to 1.


addTracePoint

public void addTracePoint(GeneralPath gp,
                          int sampleProgress,
                          double signalSize)
Plots the amplitude of the signal against time. AKA an oscilloscope trace.


addLissajousPoint

public void addLissajousPoint(GeneralPath gp,
                              int sampleProgress,
                              double signalSize,
                              double lastSignalSize)
Plots the amplitude of the signal against its own gradient.


getGradientToAngle

public double getGradientToAngle(double height1,
                                 double height2)
Returns the arctan of the gradient.


getScalingFactor

public double getScalingFactor(int progress)
This provides a scaling factor intended to allow both ends of the lissajous trace to align.


frameToSignedDoubles

public double[] frameToSignedDoubles(byte[] renderBuffer)
Converts a single frame of audio bytes to signed doubles ranging from -1 to 1. It will produce one sample value for each channel. Though frame will usually be two channels - stereo, it might (theoretically) range from one - mono, to five - for the 5.1 channel sound supported by some video formats. This method is quite fragile in that it presumes a stereo, 16 bit, little-endian audio signal. It is expressed in 4 bytes, arranged as follows. | byte index | Channel | L/S | |____________________________| | 0 | 1 | S | | 1 | 1 | L | | 2 | 2 | S | | 3 | 2 | L | No other configurations available for further testing. Update 1. After hooking directly into AudioInputStream's, we were able to test and correct for a lot of different sound stream types. This version supports both of - big/little endian - 16 bit or 8 bit - stereo or mono - signed or unsigned To correctly plot the trace, the encoding must be PCM, but it seems the AudioSystem prefers PCM when it comes to playing a sound in any case, and it provides methods to to that.

Parameters:
renderBuffer - bytearray The bytes of a single audio frame
Returns:
An array of doubles ranging from -1 to 1, representing the audio signal strength of a single frame sample.