Class SamplePlayer<K>
java.lang.Object
net.emustudio.emulib.audio.SamplePlayer<K>
- Type Parameters:
K- sample identifier, such as a plugin's own enum
- All Implemented Interfaces:
AutoCloseable
Loads and plays independent samples identified by caller-defined keys.
Files are converted to signed 16-bit little-endian mono PCM at the chosen sample rate.
Samples can overlap, restart or loop; playback calls do not wait for effects to finish.
-
Constructor Summary
ConstructorsConstructorDescriptionSamplePlayer(int sampleRate) Creates an empty player, initially at full volume. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]captureAudio(int frameCount) Mixes samples at their current playback positions for recording.voidclose()Releases all loaded samples.intvoidLoads or replaces a sample.voidRestarts the identified sample, optionally looping it until stopped.voidsetVolumePercent(int volumePercent) Sets the volume for loaded and future samples, clamped to 0..100.voidStops one sample; unknown identifiers are ignored.voidstopAll()Stops all samples without unloading them.
-
Constructor Details
-
SamplePlayer
public SamplePlayer(int sampleRate) Creates an empty player, initially at full volume. No device is opened until a sample is loaded.- Parameters:
sampleRate- playback and captured-audio frames per second
-
-
Method Details
-
load
public void load(K key, Path path) throws IOException, UnsupportedAudioFileException, LineUnavailableException Loads or replaces a sample. A failed load keeps any previously loaded sample. Callers decide how to report missing files or unavailable audio devices.- Parameters:
key- sample identifierpath- audio file- Throws:
IOException- if the file cannot be readUnsupportedAudioFileException- if the file is not a supported audio formatLineUnavailableException- if the audio device cannot open the sampleIllegalArgumentException- if conversion to the playback format is unsupported
-
play
Restarts the identified sample, optionally looping it until stopped. Unknown sample identifiers are silent.- Parameters:
key- sample identifierloop- whether to repeat continuously
-
stop
Stops one sample; unknown identifiers are ignored.- Parameters:
key- sample identifier
-
stopAll
public void stopAll()Stops all samples without unloading them. -
getVolumePercent
public int getVolumePercent()- Returns:
- the current volume, from 0 to 100
-
setVolumePercent
public void setVolumePercent(int volumePercent) Sets the volume for loaded and future samples, clamped to 0..100.- Parameters:
volumePercent- volume in percent
-
captureAudio
public byte[] captureAudio(int frameCount) Mixes samples at their current playback positions for recording. Returns signed 16-bit little-endian stereo PCM with identical left/right channels, applying the current volume and saturating overlapping samples. Missing or stopped samples contribute silence. This reads playback positions; it does not advance them.- Parameters:
frameCount- number of stereo frames to capture- Returns:
- an independent PCM buffer
-
close
public void close()Releases all loaded samples. Repeated calls are safe; volume is retained and new samples may be loaded afterwards.- Specified by:
closein interfaceAutoCloseable
-