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

public final class SamplePlayer<K> extends Object implements 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

    Constructors
    Constructor
    Description
    SamplePlayer(int sampleRate)
    Creates an empty player, initially at full volume.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    captureAudio(int frameCount)
    Mixes samples at their current playback positions for recording.
    void
    Releases all loaded samples.
    int
     
    void
    load(K key, Path path)
    Loads or replaces a sample.
    void
    play(K key, boolean loop)
    Restarts the identified sample, optionally looping it until stopped.
    void
    setVolumePercent(int volumePercent)
    Sets the volume for loaded and future samples, clamped to 0..100.
    void
    stop(K key)
    Stops one sample; unknown identifiers are ignored.
    void
    Stops all samples without unloading them.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      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 identifier
      path - audio file
      Throws:
      IOException - if the file cannot be read
      UnsupportedAudioFileException - if the file is not a supported audio format
      LineUnavailableException - if the audio device cannot open the sample
      IllegalArgumentException - if conversion to the playback format is unsupported
    • play

      public void play(K key, boolean loop)
      Restarts the identified sample, optionally looping it until stopped. Unknown sample identifiers are silent.
      Parameters:
      key - sample identifier
      loop - whether to repeat continuously
    • stop

      public void stop(K key)
      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:
      close in interface AutoCloseable