Class IndexGranularSource

java.lang.Object
net.paulhertz.pixelaudio.granular.IndexGranularSource
All Implemented Interfaces:
PAFloatSource, PASource

public class IndexGranularSource extends Object implements PASource
TODO Candidate for removal May still prove useful IndexGranularSource "Standard" linear granular source that: - reads from a mono float[] source buffer, - starts at a given source sample index, - uses a fixed grain length and hop in samples, - plays either: * a fixed number of grains, or * as many grains as needed to fill a target duration, - applies a window and equal-power pan. This is the classic time-domain granulator: sourceIndex(g, k) = startSampleIndex + g * indexHopSamples + k time(g) = noteStartSample + g * timeHopSamples For now, indexHopSamples == timeHopSamples gives 1x pitch. Later you can decouple them for time-stretch/pitch-shift. TODO remove pan from IndexGranularSource entirely and make it mono.
  • Field Details

    • source

      private final float[] source
    • settings

      private final GranularSettings settings
    • windowCache

      private final WindowCache windowCache
    • startSampleIndex

      private final int startSampleIndex
    • grainLength

      private int grainLength
    • timeHopSamples

      private final int timeHopSamples
    • indexHopSamples

      private final int indexHopSamples
    • numGrains

      private final int numGrains
    • pitchRatio

      private final float pitchRatio
    • windowFunction

      private final ddf.minim.analysis.WindowFunction windowFunction
    • noteStartSample

      private long noteStartSample
    • noteStarted

      private boolean noteStarted
    • grainWindow

      private ddf.minim.analysis.WindowFunction grainWindow
    • windowCurve

      private float[] windowCurve
  • Constructor Details

    • IndexGranularSource

      public IndexGranularSource(float[] source, GranularSettings settings, int startSampleIndex, int grainLength, int timeHopSamples, int indexHopSamples, int numGrains, float pitchRatio)
      Construct by explicit grain count.
      Parameters:
      source - mono source buffer
      settings - granular settings (window, default grain length, hop, etc.)
      startSampleIndex - first source sample for grain 0
      grainLength - grain length in samples (or <=0 → settings.defaultGrainLength)
      timeHopSamples - hop in output time (samples between grain starts)
      indexHopSamples - hop in source index (samples between grain starts in source)
      numGrains - number of grains to play (>= 1)
      pitchRatio - proportional change in pitch
    • IndexGranularSource

      public IndexGranularSource(float[] source, GranularSettings settings, int startSampleIndex, int grainLength, int timeHopSamples, int indexHopSamples, int numGrains)
    • IndexGranularSource

      public IndexGranularSource(float[] source, GranularSettings settings, int startSampleIndex, int grainLength, int hopSamples, int numGrains)
      Convenience constructor: no pitch-shift yet, use timeHop for source hop.
  • Method Details

    • fromDurationMs

      public static IndexGranularSource fromDurationMs(float[] source, GranularSettings settings, int startSampleIndex, int grainLength, int hopSamples, float durationMs, float sampleRate)
      Factory: build by duration in milliseconds.
      Parameters:
      source - mono source buffer
      settings - granular settings
      startSampleIndex - first source sample
      grainLength - grain length in samples (<=0 → default)
      hopSamples - time hop between grains (samples)
      durationMs - total output duration in ms
      sampleRate - playback sample rate (e.g., 44100)
    • fromDurationSeconds

      public static IndexGranularSource fromDurationSeconds(float[] source, GranularSettings settings, int startSampleIndex, int grainLength, int hopSamples, float durationSeconds, float sampleRate)
      Factory: build by duration in seconds.
    • seekTo

      public void seekTo(long absoluteSample)
      Description copied from interface: PASource
      Optional seek/rewind hook, mainly for transport or "note-on" start time. Default implementation does nothing.
      Specified by:
      seekTo in interface PASource
      Parameters:
      absoluteSample - absolute sample index to seek to.
    • renderBlock

      public void renderBlock(long blockStart, int blockSize, float[] outL, float[] outR)
      Description copied from interface: PAFloatSource
      Render audio into the given block buffers. Implementations should: - Assume outL/outR length >= blockSize. - Mix into outL/outR (add), not clear them. - Avoid allocation on the audio thread.
      Specified by:
      renderBlock in interface PAFloatSource
      Parameters:
      blockStart - the absolute sample index in the source’s own sample domain (e.g., buffer index space).
      blockSize - number of samples in this block.
      outL - left channel buffer to mix into.
      outR - right channel buffer to mix into (may be same as outL for mono).
    • lengthSamples

      public long lengthSamples()
      Description copied from interface: PAFloatSource
      Duration in samples, or Long.MAX_VALUE if effectively infinite/streaming.
      Specified by:
      lengthSamples in interface PAFloatSource
    • pitchPolicy

      public PitchPolicy pitchPolicy()
      Description copied from interface: PASource
      Pitch policy hint for the instrument: should the instrument apply its pitch (playback rate) on top of this source?
      Specified by:
      pitchPolicy in interface PASource
    • setGrainWindow

      public void setGrainWindow(ddf.minim.analysis.WindowFunction wf, int grainLenSamples)
      Specified by:
      setGrainWindow in interface PASource
    • getSource

      public float[] getSource()
    • getSettings

      public GranularSettings getSettings()
    • getStartSampleIndex

      public int getStartSampleIndex()
    • getGrainLength

      public int getGrainLength()
    • getTimeHopSamples

      public int getTimeHopSamples()
    • getIndexHopSamples

      public int getIndexHopSamples()
    • getNumGrains

      public int getNumGrains()
    • readLinear

      private static float readLinear(float[] buf, float pos)