Package net.paulhertz.pixelaudio.sampler

# Class PASharedBufferSampler

java.lang.Object
ddf.minim.UGen
net.paulhertz.pixelaudio.sampler.PASharedBufferSampler

All Implemented Interfaces:
  `PASampler`

public class PASharedBufferSampler extends ddf.minim.UGen implements PASampler
UGen-based sampler that plays multiple PASamplerVoice instances from a single shared mono buffer (channel 0 of a MultiChannelBuffer) which is an array of floating point samples over (-1.0..1.0).

Features:

- Shared buffer (no duplication)
- Polyphony with voice pooling
- Looping (global default + per-voice)
- ADSR per voice via ADSRParams
- Oldest-first voice recycling, optional smooth stealing
- Thread-safe triggering
- Presets for noise reduction with MixProfile and `PASamplerInstrumentPool` cycleMixProfile().

Automatically patches to the provided AudioOutput.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static enum`
  `PASharedBufferSampler.MixProfile`

  ## Nested classes/interfaces inherited from class ddf.minim.UGen

  `ddf.minim.UGen.InputType, ddf.minim.UGen.UGenInput`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `private float[]`
  `buffer`
  `private int`
  `bufferLen`
  `protected boolean`
  `DEBUG`
  `private boolean`
  `globalLooping`
  `private float`
  `masterGain`
  `private int`
  `maxVoices`
  `private float`
  `mixNorm`
  `private PASharedBufferSampler.MixProfile`
  `mixProfile`
  `private final ddf.minim.AudioOutput`
  `out`
  `private float`
  `playbackSampleRate`
  `private boolean`
  `smoothSteal`
  `private final List<PASamplerVoice>`
  `voices`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `PASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, float sampleRate, ddf.minim.AudioOutput out)`
  Construct a sampler over a shared MultiChannelBuffer.
  `PASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, float playbackSampleRate, ddf.minim.AudioOutput out, int maxVoices)`
  `PASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, ddf.minim.AudioOutput out)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int`
  `countAvailableVoices()`
  `void`
  `cycleMixProfile()`
  `ddf.minim.AudioOutput`
  `getAudioOutput()`
  `private PASamplerVoice`
  `getAvailableVoice()`
  Get a free voice, or recycle the oldest active one if at the polyphony limit.
  `int`
  `getBufferLength()`
  `float`
  `getMasterGain()`
  `float`
  `getMasterGainDb()`
  `int`
  `getMaxVoices()`
  `PASharedBufferSampler.MixProfile`
  `getMixProfile()`
  `String`
  `getMixProfileName()`
  `float`
  `getPlaybackSampleRate()`
  Returns the current sample rate of this sampler.
  `List<PASamplerVoice>`
  `getVoices()`
  Read-only list of voices for GUI or debugging.
  `boolean`
  `isGlobalLooping()`
  `boolean`
  `isLooping()`
  Returns true if any currently active voice is looping.
  `boolean`
  `isSmoothSteal()`
  `int`
  `play(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)`
  Play command with all the useful arguments in standard order, overrides PASampler.play().
  `void`
  `setBuffer(float[] buffer)`
  `void`
  `setBuffer(float[] buffer, float playbackSampleRate)`
  `void`
  `setGlobalLooping(boolean looping)`
  Default looping for newly triggered voices.
  `void`
  `setMasterGain(float linear)`
  `void`
  `setMasterGainDb(float db)`
  `void`
  `setMaxVoices(int maxVoices)`
  Change maximum polyphony at runtime.
  `void`
  `setMixProfile(PASharedBufferSampler.MixProfile profile)`
  `void`
  `setPlaybackSampleRate(float newRate)`
  Updates the playback sample rate used for reading from the buffer.
  `void`
  `setSmoothSteal(boolean smoothSteal)`
  Enable/disable smooth stealing (release envelope) on voice recycle.
  `(package private) static float`
  `softClipSoftsign(float x, float drive)`
  `void`
  `stopAll()`
  Stops all voices immediately.
  `protected void`
  `uGenerate(float[] channels)`
  Required by Minim.UGen, core method for audio synthesis called by Minim.
  `void`
  `updatePlaybackRateFromOutput()`
  Convenience: synchronize playback rate with AudioOutput's sample rate.

  ### Methods inherited from class ddf.minim.UGen

  `addAudio, addControl, addControl, addInput, channelCount, channelCountChanged, getLastValues, patch, patch, patch, printInputs, removeInput, sampleRate, sampleRateChanged, setChannelCount, setSampleRate, tick, unpatch, unpatch`

  ### 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.PASampler

  `setSampleRate`

- ## Field Details

  - ### buffer

    private float[] buffer

  - ### bufferLen

    private int bufferLen

  - ### playbackSampleRate

    private float playbackSampleRate

  - ### out

    private final ddf.minim.AudioOutput out

  - ### voices

    private final List<PASamplerVoice> voices

  - ### maxVoices

    private int maxVoices

  - ### globalLooping

    private boolean globalLooping

  - ### smoothSteal

    private boolean smoothSteal

  - ### mixNorm

    private float mixNorm

  - ### masterGain

    private volatile float masterGain

  - ### DEBUG

    protected boolean DEBUG

  - ### mixProfile

    private volatile PASharedBufferSampler.MixProfile mixProfile

- ## Constructor Details

  - ### PASharedBufferSampler

    public PASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, float sampleRate, ddf.minim.AudioOutput out)
    Construct a sampler over a shared MultiChannelBuffer. Automatically patches to the provided AudioOutput.
    Parameters:
      `multiBuffer` - shared source buffer (mono or stereo)
      `sampleRate` - sample rate of the buffer
      `out` - target AudioOutput for playback

  - ### PASharedBufferSampler

    public PASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, float playbackSampleRate, ddf.minim.AudioOutput out, int maxVoices)

  - ### PASharedBufferSampler

    public PASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, ddf.minim.AudioOutput out)

- ## Method Details

  - ### play

    public int play(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan)
    Play command with all the useful arguments in standard order, overrides PASampler.play(). TODO We plan to make this the only play() method PASharedBufferSampler.
    Specified by:
      `play` in interface `PASampler`
    Parameters:
      `samplePos` - index into the buffer to start sampling
      `sampleLen` - expected duration of the audio event, which is expected to return its actual duration
      `amplitude` - maximum amplitude of signal generated by the sampler, linear value
      `env` - ADSR style envelope for the entire event
      `pitch` - pitch multiplier, where 1.0f implies pitch is unchanged and all samples are read
      `pan` - location of audio event in stereo field, -1.0f = left, 0.0f = center, 1.0f = right
    Returns:
      actual duration of event in samples, typically sampleLen + envelope release time

  - ### getAvailableVoice

    private PASamplerVoice getAvailableVoice()
    Get a free voice, or recycle the oldest active one if at the polyphony limit.

  - ### uGenerate

    protected void uGenerate(float[] channels)
    Required by Minim.UGen, core method for audio synthesis called by Minim. Constant-power panning and soft limiter added to support polyphonic voices.
    Specified by:
      `uGenerate` in class `ddf.minim.UGen`

  - ### softClipSoftsign

    static float softClipSoftsign(float x, float drive)

  - ### stopAll

    public void stopAll()
    Description copied from interface: `PASampler`
    Stops all voices immediately.
    Specified by:
      `stopAll` in interface `PASampler`

  - ### isLooping

    public boolean isLooping()
    Description copied from interface: `PASampler`
    Returns true if any currently active voice is looping.
    Specified by:
      `isLooping` in interface `PASampler`

  - ### setGlobalLooping

    public void setGlobalLooping(boolean looping)
    Default looping for newly triggered voices.

  - ### isGlobalLooping

    public boolean isGlobalLooping()

  - ### setSmoothSteal

    public void setSmoothSteal(boolean smoothSteal)
    Enable/disable smooth stealing (release envelope) on voice recycle.

  - ### isSmoothSteal

    public boolean isSmoothSteal()

  - ### setMaxVoices

    public void setMaxVoices(int maxVoices)
    Change maximum polyphony at runtime.

  - ### getMaxVoices

    public int getMaxVoices()

  - ### getVoices

    public List<PASamplerVoice> getVoices()
    Read-only list of voices for GUI or debugging.

  - ### setMasterGain

    public void setMasterGain(float linear)

  - ### getMasterGain

    public float getMasterGain()

  - ### setMasterGainDb

    public void setMasterGainDb(float db)

  - ### getMasterGainDb

    public float getMasterGainDb()

  - ### setBuffer

    public void setBuffer(float[] buffer)
    Specified by:
      `setBuffer` in interface `PASampler`

  - ### setBuffer

    public void setBuffer(float[] buffer, float playbackSampleRate)
    Specified by:
      `setBuffer` in interface `PASampler`

  - ### countAvailableVoices

    public int countAvailableVoices()

  - ### setMixProfile

    public void setMixProfile(PASharedBufferSampler.MixProfile profile)

  - ### getMixProfile

    public PASharedBufferSampler.MixProfile getMixProfile()

  - ### getMixProfileName

    public String getMixProfileName()

  - ### cycleMixProfile

    public void cycleMixProfile()

  - ### getPlaybackSampleRate

    public float getPlaybackSampleRate()
    Returns the current sample rate of this sampler.

  - ### setPlaybackSampleRate

    public void setPlaybackSampleRate(float newRate)
    Updates the playback sample rate used for reading from the buffer. Does not affect Minim's UGen sample rate.

  - ### updatePlaybackRateFromOutput

    public void updatePlaybackRateFromOutput()
    Convenience: synchronize playback rate with AudioOutput's sample rate. Useful if you want playback speed tied to system rate.

  - ### getBufferLength

    public int getBufferLength()

  - ### getAudioOutput

    public ddf.minim.AudioOutput getAudioOutput()