Package net.paulhertz.pixelaudio.sampler
Class PASamplerVoice
java.lang.Object
net.paulhertz.pixelaudio.sampler.PASamplerVoice
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 wrap-around reads for finite events near the buffer end
- optional zero-crossing start and micro-fade-in
Voices can be smoothly released and are recycled once the envelope finishes.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate float[]private static final booleanprivate intprivate SimpleADSRprivate booleanprivate intprivate floatprivate booleanprivate booleanprivate booleanprivate static longprivate floatprivate floatprivate floatprivate floatprivate booleanprivate intprivate longprivate boolean -
Constructor Summary
ConstructorsConstructorDescriptionPASamplerVoice(float[] buffer, float sampleRate) Constructs an inactive sampler voice. -
Method Summary
Modifier and TypeMethodDescriptionvoidactivate(int start, int length, float gain, ADSRParams envParams, float pitch, float pan, boolean looping) Activates the voice over a buffer region.voidactivate(int start, int length, float gain, ADSRParams envParams, float pitch, float pan, boolean looping, boolean wrapAround) Activates the voice over a buffer region.private voidprivate intfindZeroCrossing(int index, int direction) floatgetPan()longbooleanisActive()booleanbooleanbooleanbooleanbooleanbooleanfloatGenerates the next mono sample for this voice.private floatreadBufferSample(int idx) voidrelease()Starts the release stage and disables looping.voidResets this voice to the beginning of its current buffer and marks it inactive.voidsetBuffer(float[] buffer) Replaces the source buffer and resets the voice.voidsetBuffer(float[] buffer, float playbackSampleRate) Replaces the source buffer and playback sample rate, then resets the voice.voidsetFindZeroCrossing(boolean val) voidsetLooping(boolean looping) voidsetMicroFadeIn(boolean val) voidsetPlaybackSampleRate(float newRate) Sets the playback sample rate.voidsetWrapAround(boolean wrapAround) voidstop()Stops this voice immediately.
-
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 -
wrapAround
private boolean wrapAround -
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
-
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 buffersampleRate- 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 playbacklength- playback length in samplesgain- linear gain multiplierenvParams- optional ADSR envelope parameterspitch- pitch or playback-rate multiplierpan- stereo pan positionlooping- true to loop this voice
-
activate
public void activate(int start, int length, float gain, ADSRParams envParams, float pitch, float pan, boolean looping, boolean wrapAround) Activates the voice over a buffer region.- Parameters:
start- buffer index to start playbacklength- playback length in samplesgain- linear gain multiplierenvParams- optional ADSR envelope parameterspitch- pitch or playback-rate multiplierpan- stereo pan positionlooping- true to loop this voicewrapAround- true to wrap finite source-buffer reads at buffer boundaries
-
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 bufferplaybackSampleRate- 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
-
isWrapAround
public boolean isWrapAround()- Returns:
- true when this voice wraps finite source-buffer reads
-
setWrapAround
public void setWrapAround(boolean wrapAround) - Parameters:
wrapAround- true to wrap finite source-buffer reads
-
getPan
public float getPan()- Returns:
- stereo pan position
-
getVoiceId
public long getVoiceId()- Returns:
- unique voice identifier
-
applyMicroFadeIn
private void applyMicroFadeIn() -
readBufferSample
private float readBufferSample(int idx) -
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
-