Class PcmBuffer

java.lang.Object
net.avadeaux.klipspringer.codec.PcmBuffer
All Implemented Interfaces:
Closeable, AutoCloseable

public abstract class PcmBuffer extends Object implements Closeable
Intermediary for passing PCM data to a writer with matching sample rate, bit depth and number of channels, but whose coding may differ in signedness, endianness, or padding (see blog post about various formats). Wraps a byte buffer to provide additional operations on it, without duplicating the operations that work on the wrapped buffer directly. Operates on direct byte buffers (located in external memory), to make bulk data transfer efficient from decoders and device readers implemented using external libraries, and from files read using NIO. It is less efficient when input comes via a TargetDataLine, AudioInputStream, or plain InputStream, which all operate on JVM-internal byte arrays.
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets the underlying byte buffer.
    void
    Gets the output format, used in writing to the wrapped buffer.
    final int
    Reads a sample as a signed integer at the current position, and advances position.
    abstract int
    getSample(int index)
    Reads a sample as a signed integer given the position of its first byte.
    final void
    monomix(int pos, int frames)
    Mixes the samples in a segment of the buffer so that all channels get the same value (the arithmetic mean of the original values.
    static PcmBuffer
    of(ByteBuffer buf, PcmFormat format, Device.BitLayout lout)
    Constructor that wraps a byte buffer as a PCM buffer for the given output format and layout.
    final void
    put(ByteBuffer data, PcmFormat dataFormat)
    Transfers data from a byte buffer into this buffer.
    final void
    putSample(int value)
    Writes a signed integer sample value at the current position, and advances position.
    abstract void
    putSample(int index, int value)
    Writes a signed integer sample value given the position of its first byte.

    Methods inherited from class java.lang.Object

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

    • format

      public PcmFormat format()
      Gets the output format, used in writing to the wrapped buffer.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • byteBuffer

      public final ByteBuffer byteBuffer()
      Gets the underlying byte buffer.
    • getSample

      public final int getSample()
      Reads a sample as a signed integer at the current position, and advances position.
    • getSample

      public abstract int getSample(int index)
      Reads a sample as a signed integer given the position of its first byte.
    • putSample

      public final void putSample(int value)
      Writes a signed integer sample value at the current position, and advances position.
    • putSample

      public abstract void putSample(int index, int value)
      Writes a signed integer sample value given the position of its first byte.
    • put

      public final void put(ByteBuffer data, PcmFormat dataFormat) throws IOException
      Transfers data from a byte buffer into this buffer. The formats must match in quality, but may differ in signedness, endianness and frame size. If the data buffer is not direct, this method copies its contents to a temporary direct buffer before transferring to the internal buffer.
      Throws:
      IOException
    • monomix

      public final void monomix(int pos, int frames)
      Mixes the samples in a segment of the buffer so that all channels get the same value (the arithmetic mean of the original values.
    • of

      public static PcmBuffer of(ByteBuffer buf, PcmFormat format, Device.BitLayout lout)
      Constructor that wraps a byte buffer as a PCM buffer for the given output format and layout. Sets the byte order of the byte buffer to match the format.