Class PASamplerInstrumentPoolMulti

java.lang.Object
net.paulhertz.pixelaudio.sampler.PASamplerInstrumentPoolMulti
All Implemented Interfaces:
PAPlayable, PASamplerPlayable, AudioSampleClock

public class PASamplerInstrumentPoolMulti extends Object implements PASamplerPlayable, PAPlayable, AudioSampleClock
PASamplerInstrumentPoolMulti Manages multiple PASamplerInstrumentPool instances, each keyed by name. Each sub-pool may represent a distinct sample source or timbral layer.

Features:

  • Dynamic add/remove of sub-pools.
  • Shared AudioOutput.
  • Optional active-key routing, where only one pool plays at a time.
  • Global stop, buffer, and sample-rate propagation.
  • Graceful resizing using PASamplerInstrumentPool gentle reinitialization.
Thread-safe and compatible with both PAPlayable and PASamplerPlayable.
TODO example sketches.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private String
     
    private boolean
     
    private float
     
    private final ddf.minim.AudioOutput
     
     
    private boolean
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    PASamplerInstrumentPoolMulti(ddf.minim.AudioOutput out)
    Constructs an empty multi-pool manager.
    PASamplerInstrumentPoolMulti(String key, ddf.minim.MultiChannelBuffer buffer, float sampleRate, int poolSize, int perInstrumentVoices, ddf.minim.AudioOutput out, ADSRParams adsr)
    Convenience constructor for single-pool initialization.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addPool(String key, ddf.minim.MultiChannelBuffer buffer, float bufferSampleRate, int poolSize, int perInstrumentVoices, ddf.minim.AudioOutput out, ADSRParams env)
    Adds a named sampler pool.
    void
    Clears scheduled starts across all sub-pools.
    void
    Close all sub-pools and release shared resources.
    Returns the currently active sub-pool (may be null).
    void
    Prints current pool state to standard output.
     
    long
    Returns a representative sample clock from the active pool.
    Returns a named pool.
    int
     
     
    float
    Returns the sample rate used by this clock.
    boolean
    Reports whether a named pool exists.
    boolean
     
    boolean
     
    boolean
     
    int
    play(float amplitude, float pitch, float pan)
    Plays the instrument's entire buffer, from PAPlayable interface.
    int
    play(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)
    PASamplerPlayable's play method, all six standard parameters in standard order, called by other methods.
    int
    playSample(int samplePos, int sampleLen, float amplitude)
    Play subrange with amplitude only.
    int
    playSample(int samplePos, int sampleLen, float amplitude, float pitch)
    Play subrange with amplitude and pitch (no envelope).
    int
    playSample(int samplePos, int sampleLen, float amplitude, float pitch, float pan)
    Plays a sample range with pitch and pan on the active pool.
    int
    playSample(int samplePos, int sampleLen, float amplitude, ADSRParams env)
    int
    playSample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch)
    Plays a sample range with envelope and pitch on the active pool.
    int
    playSample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)
    Play a subrange of the buffer with full parameters.
    int
    playSample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch)
    Plays a range from a temporary buffer on the active pool.
    int
    playSample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)
    Plays a range from a temporary buffer on the active pool.
    int
    playSample(String tag, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)
    Plays a sample range on a named pool.
    void
    Remove a pool by key and close it safely.
    void
    resizeAllPools(int newPoolSize, int newVoicesPerInstrument)
    Adjust size or polyphony of all sub-pools gracefully.
    void
    Activate the given pool key for subsequent play() calls.
    void
    setBuffer(float[] newBuffer, float newSampleRate)
    Propagate float[] buffer to all sub-pools (mono assumption).
    void
    setBuffer(ddf.minim.MultiChannelBuffer newBuffer)
    Propagate new buffer to all sub-pools, keeping sample rates intact.
    void
    setBuffer(ddf.minim.MultiChannelBuffer newBuffer, float newSampleRate)
    Propagate new buffer and sample rate to all sub-pools.
    void
    setMasterGain(float linear)
    Sets master gain on every sub-pool.
    void
    setWrapAround(boolean wrapAround)
    Sets whether finite sampler events wrap source-buffer reads at the buffer end.
    void
    startAfterDelaySamples(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan, long delaySamples)
    Schedules playback on the active pool after a delay in samples.
    void
    startAtSampleTime(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan, long startSample)
    Schedules playback on the active pool at an absolute sample time.
    void
    startNow(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)
    Schedules playback on the active pool at its current sampler clock.
    void
    Stop playback immediately (implementation-defined).
    void
    Stops all voices in every sub-pool.
    void
    Update sample rates of all sub-pools from AudioOutput.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface net.paulhertz.pixelaudio.sampler.PAPlayable

    play, play, play, play, play

    Methods inherited from interface net.paulhertz.pixelaudio.sampler.PASamplerPlayable

    playSample, playSample, playSample, playSample, playSample
  • Field Details

    • out

      private final ddf.minim.AudioOutput out
    • pools

      private final Map<String,PASamplerInstrumentPool> pools
    • activeKey

      private String activeKey
    • isClosed

      private boolean isClosed
    • masterGain

      private volatile float masterGain
    • wrapAround

      private volatile boolean wrapAround
  • Constructor Details

    • PASamplerInstrumentPoolMulti

      public PASamplerInstrumentPoolMulti(ddf.minim.AudioOutput out)
      Constructs an empty multi-pool manager.
      Parameters:
      out - shared AudioOutput for sub-pools
    • PASamplerInstrumentPoolMulti

      public PASamplerInstrumentPoolMulti(String key, ddf.minim.MultiChannelBuffer buffer, float sampleRate, int poolSize, int perInstrumentVoices, ddf.minim.AudioOutput out, ADSRParams adsr)
      Convenience constructor for single-pool initialization.
      Parameters:
      key - key for the initial pool
      buffer - source buffer for the initial pool
      sampleRate - sample rate of the source buffer in Hz
      poolSize - number of instruments in the initial pool
      perInstrumentVoices - voices per instrument
      out - shared AudioOutput
      adsr - default ADSR envelope
  • Method Details

    • addPool

      public void addPool(String key, ddf.minim.MultiChannelBuffer buffer, float bufferSampleRate, int poolSize, int perInstrumentVoices, ddf.minim.AudioOutput out, ADSRParams env)
      Adds a named sampler pool.
      Parameters:
      key - pool key
      buffer - source buffer for the pool
      bufferSampleRate - sample rate of the source buffer in Hz
      poolSize - number of instruments in the pool
      perInstrumentVoices - voices per instrument
      out - AudioOutput for the pool
      env - default ADSR envelope
    • removePool

      public void removePool(String key)
      Remove a pool by key and close it safely.
      Parameters:
      key - pool key to remove
    • current

      private PASamplerInstrumentPool current()
      Returns the currently active sub-pool (may be null).
    • setActive

      public void setActive(String key)
      Activate the given pool key for subsequent play() calls.
      Parameters:
      key - pool key to activate
    • getActiveKey

      public String getActiveKey()
      Returns:
      currently active pool key
    • play

      public int play(float amplitude, float pitch, float pan)
      Plays the instrument's entire buffer, from PAPlayable interface.
      Specified by:
      play in interface PAPlayable
      Parameters:
      amplitude - gain multiplier (0..1+)
      pitch - playback rate or pitch factor (implementation-defined)
      pan - stereo pan (-1 left .. +1 right)
      Returns:
      non-zero if a voice/event was triggered
    • stop

      public void stop()
      Description copied from interface: PAPlayable
      Stop playback immediately (implementation-defined).
      Specified by:
      stop in interface PAPlayable
    • play

      public int play(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)
      PASamplerPlayable's play method, all six standard parameters in standard order, called by other methods.
      Specified by:
      play in interface PASamplerPlayable
      Parameters:
      samplePos - buffer index to start playback
      sampleLen - requested duration in samples
      amplitude - gain multiplier
      env - optional ADSR envelope
      pitch - pitch or playback-rate multiplier
      pan - stereo pan
      Returns:
      actual event duration in samples
    • startAtSampleTime

      public void startAtSampleTime(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan, long startSample)
      Schedules playback on the active pool at an absolute sample time.
      Parameters:
      samplePos - buffer index to start playback
      sampleLen - requested duration in samples
      amplitude - gain multiplier
      env - ADSR envelope parameters, or null to use the default
      pitch - pitch or playback-rate multiplier
      pan - stereo pan
      startSample - absolute sample index at which playback should start
    • startAfterDelaySamples

      public void startAfterDelaySamples(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan, long delaySamples)
      Schedules playback on the active pool after a delay in samples.
      Parameters:
      samplePos - buffer index to start playback
      sampleLen - requested duration in samples
      amplitude - gain multiplier
      env - ADSR envelope parameters, or null to use the default
      pitch - pitch or playback-rate multiplier
      pan - stereo pan
      delaySamples - delay from the current sample time
    • startNow

      public void startNow(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)
      Schedules playback on the active pool at its current sampler clock.
    • getCurrentSampleTime

      public long getCurrentSampleTime()
      Returns a representative sample clock from the active pool.
      Specified by:
      getCurrentSampleTime in interface AudioSampleClock
      Returns:
      current sample clock, or 0 if no active pool is available
    • getSampleRate

      public float getSampleRate()
      Description copied from interface: AudioSampleClock
      Returns the sample rate used by this clock.
      Specified by:
      getSampleRate in interface AudioSampleClock
      Returns:
      sample rate used by the active pool's representative audio clock
    • clearScheduled

      public void clearScheduled()
      Clears scheduled starts across all sub-pools.
    • setWrapAround

      public void setWrapAround(boolean wrapAround)
      Sets whether finite sampler events wrap source-buffer reads at the buffer end.
      Parameters:
      wrapAround - true to wrap source-buffer reads for newly triggered events
    • isWrapAround

      public boolean isWrapAround()
      Returns:
      true when finite sampler events wrap source-buffer reads
    • isLooping

      public boolean isLooping()
      Returns:
      true if any sub-pool has looping voices
    • stopAll

      public void stopAll()
      Stops all voices in every sub-pool.
    • playSample

      public int playSample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)
      Play a subrange of the buffer with full parameters.
      Specified by:
      playSample in interface PASamplerPlayable
      Parameters:
      samplePos - buffer index to start playback
      sampleLen - requested duration in samples
      amplitude - gain multiplier
      env - optional ADSR envelope
      pitch - pitch or playback-rate multiplier
      pan - stereo pan
      Returns:
      actual event duration in samples
    • playSample

      public int playSample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch)
      Plays a sample range with envelope and pitch on the active pool.
      Parameters:
      samplePos - buffer index to start playback
      sampleLen - requested duration in samples
      amplitude - gain multiplier
      env - optional ADSR envelope
      pitch - pitch or playback-rate multiplier
      Returns:
      actual event duration in samples
    • playSample

      public int playSample(int samplePos, int sampleLen, float amplitude, float pitch, float pan)
      Plays a sample range with pitch and pan on the active pool.
      Parameters:
      samplePos - buffer index to start playback
      sampleLen - requested duration in samples
      amplitude - gain multiplier
      pitch - pitch or playback-rate multiplier
      pan - stereo pan
      Returns:
      actual event duration in samples
    • playSample

      public int playSample(int samplePos, int sampleLen, float amplitude)
      Play subrange with amplitude only.
      Specified by:
      playSample in interface PASamplerPlayable
      Parameters:
      samplePos - buffer index to start playback
      sampleLen - requested duration in samples
      amplitude - gain multiplier
      Returns:
      actual event duration in samples
    • playSample

      public int playSample(int samplePos, int sampleLen, float amplitude, float pitch)
      Play subrange with amplitude and pitch (no envelope).
      Specified by:
      playSample in interface PASamplerPlayable
      Parameters:
      samplePos - buffer index to start playback
      sampleLen - requested duration in samples
      amplitude - gain multiplier
      pitch - pitch or playback-rate multiplier
      Returns:
      actual event duration in samples
    • playSample

      public int playSample(int samplePos, int sampleLen, float amplitude, ADSRParams env)
    • playSample

      public int playSample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)
      Plays a range from a temporary buffer on the active pool.
      Parameters:
      buffer - source buffer for playback
      samplePos - buffer index to start playback
      sampleLen - requested duration in samples
      amplitude - gain multiplier
      env - optional ADSR envelope
      pitch - pitch or playback-rate multiplier
      pan - stereo pan
      Returns:
      actual event duration in samples
    • playSample

      public int playSample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch)
      Plays a range from a temporary buffer on the active pool.
      Parameters:
      buffer - source buffer for playback
      samplePos - buffer index to start playback
      sampleLen - requested duration in samples
      amplitude - gain multiplier
      env - optional ADSR envelope
      pitch - pitch or playback-rate multiplier
      Returns:
      actual event duration in samples
    • playSample

      public int playSample(String tag, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)
      Plays a sample range on a named pool.
      Parameters:
      tag - pool key
      samplePos - buffer index to start playback
      sampleLen - requested duration in samples
      amplitude - gain multiplier
      env - optional ADSR envelope
      pitch - pitch or playback-rate multiplier
      pan - stereo pan
      Returns:
      actual event duration in samples
    • hasPool

      public boolean hasPool(String tag)
      Reports whether a named pool exists.
      Parameters:
      tag - pool key
      Returns:
      true when the pool exists
    • getPoolByTag

      public PASamplerInstrumentPool getPoolByTag(String tag)
      Returns a named pool.
      Parameters:
      tag - pool key
      Returns:
      matching pool, or null
    • setBuffer

      public void setBuffer(ddf.minim.MultiChannelBuffer newBuffer)
      Propagate new buffer to all sub-pools, keeping sample rates intact.
      Parameters:
      newBuffer - replacement source buffer
    • setBuffer

      public void setBuffer(ddf.minim.MultiChannelBuffer newBuffer, float newSampleRate)
      Propagate new buffer and sample rate to all sub-pools.
      Parameters:
      newBuffer - replacement source buffer
      newSampleRate - sample rate of the replacement buffer in Hz
    • setBuffer

      public void setBuffer(float[] newBuffer, float newSampleRate)
      Propagate float[] buffer to all sub-pools (mono assumption).
      Parameters:
      newBuffer - replacement mono source buffer
      newSampleRate - sample rate of the replacement buffer in Hz
    • updateRateFromOutput

      public void updateRateFromOutput()
      Update sample rates of all sub-pools from AudioOutput.
    • setMasterGain

      public void setMasterGain(float linear)
      Sets master gain on every sub-pool.
      Parameters:
      linear - linear gain value
    • resizeAllPools

      public void resizeAllPools(int newPoolSize, int newVoicesPerInstrument)
      Adjust size or polyphony of all sub-pools gracefully.
      Parameters:
      newPoolSize - number of instruments per pool
      newVoicesPerInstrument - voices per instrument
    • close

      public void close()
      Close all sub-pools and release shared resources.
    • isClosed

      public boolean isClosed()
      Returns:
      true after this multi-pool manager has been closed
    • getPoolCount

      public int getPoolCount()
      Returns:
      number of managed pools
    • getPools

      public Map<String,PASamplerInstrumentPool> getPools()
      Returns:
      map of pool keys to pools
    • debugPrintState

      public void debugPrintState()
      Prints current pool state to standard output.