Class PASamplerVoice

java.lang.Object
net.paulhertz.pixelaudio.sampler.PASamplerVoice

public class PASamplerVoice extends Object
PASamplerVoice - a single playback "voice" reading from a shared mono buffer. Each voice handles: - playback position and pitch - per-voice amplitude and pan - one independent SimpleADSR envelope (sample-accurate) - optional zero-crossing start and micro-fade-in Voices can be smoothly released and are recycled once the envelope finishes.
  • Field Details

    • NEXT_VOICE_ID

      private static long NEXT_VOICE_ID
    • buffer

      private float[] buffer
    • playbackSampleRate

      private float playbackSampleRate
    • voiceId

      private long voiceId
    • active

      private boolean active
    • released

      private boolean released
    • finished

      private boolean finished
    • looping

      private boolean looping
    • start

      private int start
    • end

      private int end
    • position

      private float position
    • rate

      private float rate
    • gain

      private float gain
    • pan

      private float pan
    • envelope

      private SimpleADSR envelope
    • isFindZeroCrossing

      private boolean isFindZeroCrossing
    • isMicroFadeIn

      private boolean isMicroFadeIn
    • DEBUG

      private static final boolean DEBUG
      See Also:
    • frameCounter

      private int frameCounter
  • Constructor Details

    • PASamplerVoice

      public PASamplerVoice(float[] buffer, float sampleRate)
      Constructs an inactive sampler voice.
      Parameters:
      buffer - shared mono source buffer
      sampleRate - playback sample rate in Hz
  • Method Details

    • activate

      public void activate(int start, int length, float gain, ADSRParams envParams, float pitch, float pan, boolean looping)
      Activates the voice over a buffer region.
      Parameters:
      start - buffer index to start playback
      length - playback length in samples
      gain - linear gain multiplier
      envParams - optional ADSR envelope parameters
      pitch - pitch or playback-rate multiplier
      pan - stereo pan position
      looping - true to loop this voice
    • nextSample

      public float nextSample()
      Generates the next mono sample for this voice.
      Returns:
      next sample value, or 0 when inactive/finished
    • release

      public void release()
      Starts the release stage and disables looping.
    • stop

      public void stop()
      Stops this voice immediately.
    • resetPosition

      public void resetPosition()
      Resets this voice to the beginning of its current buffer and marks it inactive.
    • setBuffer

      public void setBuffer(float[] buffer)
      Replaces the source buffer and resets the voice.
      Parameters:
      buffer - shared mono source buffer
    • setBuffer

      public void setBuffer(float[] buffer, float playbackSampleRate)
      Replaces the source buffer and playback sample rate, then resets the voice.
      Parameters:
      buffer - shared mono source buffer
      playbackSampleRate - playback sample rate in Hz
    • isActive

      public boolean isActive()
      Returns:
      true while the voice is in its active playback window
    • isReleasing

      public boolean isReleasing()
      Returns:
      true while the voice is releasing but not finished
    • isFinished

      public boolean isFinished()
      Returns:
      true when the voice has finished playback
    • isLooping

      public boolean isLooping()
      Returns:
      true when this voice is configured to loop
    • setLooping

      public void setLooping(boolean looping)
      Parameters:
      looping - true to loop this voice
    • getPan

      public float getPan()
      Returns:
      stereo pan position
    • getVoiceId

      public long getVoiceId()
      Returns:
      unique voice identifier
    • applyMicroFadeIn

      private void applyMicroFadeIn()
    • findZeroCrossing

      private int findZeroCrossing(int index, int direction)
    • isFindZeroCrossing

      public boolean isFindZeroCrossing()
      Returns:
      true when activation searches for a nearby zero crossing
    • setFindZeroCrossing

      public void setFindZeroCrossing(boolean val)
      Parameters:
      val - true to search for a nearby zero crossing on activation
    • isMicroFadeIn

      public boolean isMicroFadeIn()
      Returns:
      true when activation applies a short fade-in
    • setMicroFadeIn

      public void setMicroFadeIn(boolean val)
      Parameters:
      val - true to apply a short fade-in on activation
    • setPlaybackSampleRate

      public void setPlaybackSampleRate(float newRate)
      Sets the playback sample rate.
      Parameters:
      newRate - playback sample rate in Hz