Class WindowedBuffer

java.lang.Object
net.paulhertz.pixelaudio.WindowedBuffer

public class WindowedBuffer extends Object
A windowed buffer class to permit loading large audio sources into memory and stepping through them.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final float[]
     
    private int
     
    private int
     
    private final float[]
     
    private final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    WindowedBuffer(float[] buffer, int windowSize, int hopSize)
    Create a windowed buffer with the given source, window size, and hop size.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Expose the underlying array size.
    int
    Expose hop size, the number of samples to advance for each window.
    int
    Return current buffer index
    float[]
    Returns the window at a supplied index.
    int
    Expose the reusable window array size.
    float[]
    Returns the next window, advancing the read index by hopSize.
    void
    Reset reader to start of buffer
    void
    setHopSize(int hopSize)
    Set the hop size, the number of samples to advance for each window.
    void
    setIndex(int index)
    Set current index.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • buffer

      private final float[] buffer
    • window

      private final float[] window
    • windowSize

      private final int windowSize
    • hopSize

      private int hopSize
    • index

      private int index
  • Constructor Details

    • WindowedBuffer

      public WindowedBuffer(float[] buffer, int windowSize, int hopSize)
      Create a windowed buffer with the given source, window size, and hop size.
      Parameters:
      buffer - the source audio data
      windowSize - the number of samples in each window
      hopSize - the number of samples to advance for each window (must be > 0)
      Throws:
      IllegalArgumentException - if buffer is empty, or if windowSize or hopSize are not positive
  • Method Details

    • nextWindow

      public float[] nextWindow()
      Returns the next window, advancing the read index by hopSize. Wraps around the buffer as needed.
      Returns:
      a window of audio samples from the buffer, starting at the current index and wrapping around if necessary
    • gettWindowAtIndex

      public float[] gettWindowAtIndex(int idx)
      Returns the window at a supplied index. Wraps around the buffer as needed. Updates current index and advances it by hopSize.
      Parameters:
      idx - the starting index for the window (can be any integer, will be normalized to buffer length)
      Returns:
      a window of audio samples from the buffer, starting at the normalized index and wrapping around if necessary
    • reset

      public void reset()
      Reset reader to start of buffer
    • getIndex

      public int getIndex()
      Return current buffer index
    • setIndex

      public void setIndex(int index)
      Set current index. Wraps around the buffer as needed.
      Parameters:
      index - the new index to set (can be any integer, will be normalized to buffer length)
    • getBufferSize

      public int getBufferSize()
      Expose the underlying array size.
      Returns:
      the length of the backing buffer
    • getWindowSize

      public int getWindowSize()
      Expose the reusable window array size.
      Returns:
      the size of the window array, the number of samples in each window
    • getHopSize

      public int getHopSize()
      Expose hop size, the number of samples to advance for each window.
      Returns:
      the number of samples to advance for each window, which determines how much the index moves forward after each call to nextWindow()
    • setHopSize

      public void setHopSize(int hopSize)
      Set the hop size, the number of samples to advance for each window.
      Parameters:
      hopSize - the new hop size to set (must be > 0)
      Throws:
      IllegalArgumentException - if hopSize is not positive