Package net.paulhertz.pixelaudio.granular

# Class PAGranularSampler

java.lang.Object
ddf.minim.UGen
net.paulhertz.pixelaudio.granular.PAGranularSampler

public class PAGranularSampler extends ddf.minim.UGen
PAGranularSampler UGen-based multi-voice granular sampler. Features: - Voice pooling (PAGranularVoice instances) - Per-voice ADSR, gain, and pan - Optional looping of the grain path - Thread-safe play() method - Per-sample mixing (like PASharedBufferSampler)

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `private static final class`
  `PAGranularSampler.ScheduledPlay`

  ## 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 int`
  `blockSize`
  `private float`
  `globalMakeUpGain`
  `private int`
  `maxVoices`
  `private float`
  `mixNorm`
  `private final ddf.minim.AudioOutput`
  `out`
  `private long`
  `sampleCursor`
  `private final AudioScheduler<PAGranularSampler.ScheduledPlay>`
  `scheduler`
  `private boolean`
  `smoothSteal`
  `private final float[]`
  `tmpStereo`
  `private final List<PAGranularVoice>`
  `voices`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `PAGranularSampler(ddf.minim.AudioOutput out)`
  `PAGranularSampler(ddf.minim.AudioOutput out, int maxVoices)`
  Initializes this PAGranularSampler, which extends UGen and gets patched to an AudioOutput, with the result that its uGenerate method is called on each audio block.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `cancelAndReleaseAll()`
  Clear pending scheduled starts and release currently sounding voices.
  `void`
  `cancelAndStopAll()`
  Clear pending scheduled starts and immediately stop all active voices.
  `void`
  `clearScheduled()`
  Remove all pending scheduled starts that have not yet been launched.
  `ddf.minim.AudioOutput`
  `getAudioOutput()`
  `private PAGranularVoice`
  `getAvailableVoice(PASource src, ADSRParams env, float gain, float pan, boolean looping)`
  `private PAGranularVoice`
  `getAvailableVoice(PASource src, ADSRParams env, float gain, float pan, boolean looping, ddf.minim.analysis.WindowFunction grainWindow, int grainLenSamples)`
  Allocate a PAGranularVoice instance.
  `long`
  `getCurrentSampleTime()`
  Expose the current absolute sample cursor (for higher-level scheduling).
  `int`
  `getMaxVoices()`
  `List<PAGranularVoice>`
  `getVoices()`
  `boolean`
  `isSmoothSteal()`
  `long`
  `play(PASource src, float gain, float pan, ADSRParams defaultEnv, boolean looping)`
  `long`
  `play(PASource src, ADSRParams env, float gain, float pan)`
  `long`
  `play(PASource src, ADSRParams env, float gain, float pan, boolean looping)`
  Play a granular source as a voice.
  `void`
  `releaseAll()`
  Release all currently active or releasing voices.
  `void`
  `setMaxVoices(int maxVoices)`
  `void`
  `setSmoothSteal(boolean smoothSteal)`
  `private static float`
  `softClipSoftsign(float x, float drive)`
  `void`
  `startAfterDelaySamples(PASource src, ADSRParams env, float gain, float pan, boolean looping, long delaySamples)`
  Schedule a new voice to start after a given delay in samples.
  `void`
  `startAtSampleTime(PASource src, ADSRParams env, float gain, float pan, boolean looping, long startSample)`
  Schedule a new voice to start at an absolute sample time.
  `void`
  `startAtSampleTime(PASource src, ADSRParams env, float gain, float pan, boolean looping, long startSample, ddf.minim.analysis.WindowFunction grainWindow, int grainLenSamples)`
  Called by PAGranularInstrument.startAtSampleTime(...), creates a ScheduledPlay instance with PASource src and other arguments, then passes it to AudioScheduler scheduler, which will be handled through Minim's uGenerate call to this instance of PAGranularSampler.
  `void`
  `stopAll()`
  `protected void`
  `uGenerate(float[] channels)`
  Provides per-sample frame processing with AudioScheduler, called through Minim.

  ### 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`

- ## Field Details

  - ### out

    private final ddf.minim.AudioOutput out

  - ### voices

    private final List<PAGranularVoice> voices

  - ### maxVoices

    private int maxVoices

  - ### blockSize

    private int blockSize

  - ### smoothSteal

    private boolean smoothSteal

  - ### scheduler

    private final AudioScheduler<PAGranularSampler.ScheduledPlay> scheduler

  - ### sampleCursor

    private long sampleCursor

  - ### tmpStereo

    private final float[] tmpStereo

  - ### mixNorm

    private float mixNorm

  - ### globalMakeUpGain

    private float globalMakeUpGain

- ## Constructor Details

  - ### PAGranularSampler

    public PAGranularSampler(ddf.minim.AudioOutput out, int maxVoices)
    Initializes this PAGranularSampler, which extends UGen and gets patched to an AudioOutput, with the result that its uGenerate method is called on each audio block.
    Parameters:
      `out` - a Minim AudioOutput that this PAGranularSampler will patch to
      `maxVoices` - maximum number of voices to allocate

  - ### PAGranularSampler

    public PAGranularSampler(ddf.minim.AudioOutput out)

- ## Method Details

  - ### getAvailableVoice

    private PAGranularVoice getAvailableVoice(PASource src, ADSRParams env, float gain, float pan, boolean looping, ddf.minim.analysis.WindowFunction grainWindow, int grainLenSamples)
    Allocate a PAGranularVoice instance. Called from play() and uGenerate() methods.
    Parameters:
      `src` - A PASource
      `env` - ADSRParams envelope, could be null
      `gain` - gain as a decimal value scaling amplitude
      `pan` - pan in stereo space, but grains can set individually
      `looping` - looping flag, best be false
      `grainWindow` - a Minim WindowFunction
      `grainLenSamples` - number of samples in one grain
    Returns:

  - ### getAvailableVoice

    private PAGranularVoice getAvailableVoice(PASource src, ADSRParams env, float gain, float pan, boolean looping)

  - ### play

    public long play(PASource src, ADSRParams env, float gain, float pan, boolean looping)
    Play a granular source as a voice.
    Parameters:
      `src` - PASource (a PABurstGranularSource, in the current design using PAGranularInstrumentDirector)
      `env` - ADSR for the macro envelope
      `gain` - amplitude
      `pan` - -1..+1
      `looping` - loop granular path
    Returns:
      voiceId or -1

  - ### play

    public long play(PASource src, ADSRParams env, float gain, float pan)

  - ### play

    public long play(PASource src, float gain, float pan, ADSRParams defaultEnv, boolean looping)

  - ### startAtSampleTime

    public void startAtSampleTime(PASource src, ADSRParams env, float gain, float pan, boolean looping, long startSample)
    Schedule a new voice to start at an absolute sample time.
    Parameters:
      `src` - PASource
      `env` - ADSR (already resolved: either custom or default)
      `gain` - final gain
      `pan` - final pan
      `looping` - loop flag
      `startSample` - absolute sample index at which to start the voice

  - ### startAtSampleTime

    public void startAtSampleTime(PASource src, ADSRParams env, float gain, float pan, boolean looping, long startSample, ddf.minim.analysis.WindowFunction grainWindow, int grainLenSamples)
    Called by PAGranularInstrument.startAtSampleTime(...), creates a ScheduledPlay instance with PASource src and other arguments, then passes it to AudioScheduler scheduler, which will be handled through Minim's uGenerate call to this instance of PAGranularSampler.
    Parameters:
      `src` - PASource
      `env` - ADSR (already resolved: either custom or default)
      `gain` - final gain
      `pan` - final pan
      `looping` - loop flag
      `startSample` - absolute sample index at which to start the voice
      `grainWindow` - a window function for shaping grain amplitude
      `grainLenSamples` - number of samples in one grain

  - ### startAfterDelaySamples

    public void startAfterDelaySamples(PASource src, ADSRParams env, float gain, float pan, boolean looping, long delaySamples)
    Schedule a new voice to start after a given delay in samples.
    Parameters:
      `src` - PASource
      `env` - ADSR
      `gain` - final gain
      `pan` - final pan
      `looping` - loop flag
      `delaySamples` - how many samples from "now" to start

  - ### getCurrentSampleTime

    public long getCurrentSampleTime()
    Expose the current absolute sample cursor (for higher-level scheduling).

  - ### uGenerate

    protected void uGenerate(float[] channels)
    Provides per-sample frame processing with AudioScheduler, called through Minim.
    Specified by:
      `uGenerate` in class `ddf.minim.UGen`

  - ### softClipSoftsign

    private static float softClipSoftsign(float x, float drive)

  - ### stopAll

    public void stopAll()

  - ### clearScheduled

    public void clearScheduled()
    Remove all pending scheduled starts that have not yet been launched. Does not affect currently active voices.

  - ### releaseAll

    public void releaseAll()
    Release all currently active or releasing voices. Useful for a musical stop after clearing pending scheduled starts.

  - ### cancelAndStopAll

    public void cancelAndStopAll()
    Clear pending scheduled starts and immediately stop all active voices.

  - ### cancelAndReleaseAll

    public void cancelAndReleaseAll()
    Clear pending scheduled starts and release currently sounding voices.

  - ### setMaxVoices

    public void setMaxVoices(int maxVoices)

  - ### getMaxVoices

    public int getMaxVoices()

  - ### setSmoothSteal

    public void setSmoothSteal(boolean smoothSteal)

  - ### isSmoothSteal

    public boolean isSmoothSteal()

  - ### getVoices

    public List<PAGranularVoice> getVoices()

  - ### getAudioOutput

    public ddf.minim.AudioOutput getAudioOutput()