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 Automatically patches to the provided AudioOutput.
  • 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
  • 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
      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()
    • 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()