Class SoundFile


public class SoundFile extends AudioSample
This is a Soundfile player which allows to play back and manipulate sound files. Supported formats are: WAV, AIF/AIFF, and MP3. MP3 decoding can be very slow on ARM processors (Android/Raspberry Pi), we generally recommend you use lossless WAV or AIF files.
  • Constructor Summary

    Constructors
    Constructor
    Description
    SoundFile(PApplet parent, String path)
     
    SoundFile(PApplet parent, String path, boolean cache)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    amp(float amp)
    Change the amplitude/volume of this audiosample.
    int
    Returns the number of channels of the soundfile as an int (1 for mono, 2 for stereo).
    void
    cue(float time)
    Cues the playhead to a fixed position in the soundfile.
    float
    Returns the duration of the soundfile in seconds.
    int
    Returns the number of frames of this soundfile.
    boolean
    Check whether this soundfile is currently playing.
    void
    jump(float time)
    Jump to a specific position in the soundfile while continuing to play (or starting to play if it wasn't playing already).
    void
     
    void
    loop(float rate)
     
    void
    loop(float rate, float amp)
     
    void
    loop(float rate, float pos, float amp)
    Starts the playback of the audiosample.
    void
    loop(float rate, float pos, float amp, float add)
    Starts playback which will loop at the end of the soundfile.
    void
    pan(float pos)
    Move the sound in a stereo panorama.-1.0 pans to the left channel and 1.0 to the right channel.
    void
    Stop the playback of the file, but cue it to the current position.
    void
    Starts the generator
    void
    play(float rate)
     
    void
    play(float rate, float amp)
     
    void
    play(float rate, float pos, float amp)
     
    void
    play(float rate, float pos, float amp, float add)
    Starts the playback of the audiosample.
    void
    play(float rate, float pos, float amp, float add, float cue)
    Starts the playback of the soundfile.
    void
    rate(float rate)
    Set the playback rate of the soundfile.
    boolean
    Remove this SoundFile's decoded audio sample from the cache, allowing it to be garbage collected once there are no more references to this SoundFile.

    Methods inherited from class processing.sound.SoundObject

    add, getUnitGenerator

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SoundFile

      public SoundFile(PApplet parent, String path)
    • SoundFile

      public SoundFile(PApplet parent, String path, boolean cache)
      Parameters:
      parent - typically use "this"
      path - filename of the sound file to be loaded
      cache - keep the sound data in RAM once it has been decoded (default: true). Note that caching essentially disables garbage collection for the SoundFile data, so if you are planning to load a large number of audio files, you should set this to false.
  • Method Details

    • removeFromCache

      public boolean removeFromCache()
      Remove this SoundFile's decoded audio sample from the cache, allowing it to be garbage collected once there are no more references to this SoundFile.
      Returns:
      true if the sample was removed from the cache, false if it wasn't . actually cached in the first place.
    • channels

      public int channels()
      Returns the number of channels of the soundfile as an int (1 for mono, 2 for stereo).
      Overrides:
      channels in class AudioSample
      Returns:
      Returns the number of channels of the soundfile (1 for mono, 2 for stereo)
      See Also:
    • cue

      public void cue(float time)
      Cues the playhead to a fixed position in the soundfile. Note that cue() only affects the playhead for future calls to play(), but not to loop().
      Overrides:
      cue in class AudioSample
      Parameters:
      time - position in the soundfile that the next playback should start from, in seconds.
    • duration

      public float duration()
      Returns the duration of the soundfile in seconds.
      Overrides:
      duration in class AudioSample
      Returns:
      The duration of the soundfile in seconds.
    • frames

      public int frames()
      Returns the number of frames of this soundfile.
      Overrides:
      frames in class AudioSample
      Returns:
      The number of frames of this soundfile.
    • play

      public void play()
      Description copied from class: SoundObject
      Starts the generator
      Overrides:
      play in class AudioSample
    • play

      public void play(float rate)
      Overrides:
      play in class AudioSample
    • play

      public void play(float rate, float amp)
      Overrides:
      play in class AudioSample
    • play

      public void play(float rate, float pos, float amp)
      Overrides:
      play in class AudioSample
    • play

      public void play(float rate, float pos, float amp, float add)
      Description copied from class: AudioSample
      Starts the playback of the audiosample. Only plays to the end of the audiosample once. If cue() or pause() were called previously, playback will resume from the cued position.
      Overrides:
      play in class AudioSample
      Parameters:
      rate - relative playback rate to use. 1 is the original speed. 0.5 is half speed and one octave down. 2 is double the speed and one octave up.
      pos - the panoramic position of this sound unit from -1.0 (left) to 1.0 (right). Only works for mono audiosamples!
      amp - the desired playback amplitude of the audiosample as a value from 0.0 (complete silence) to 1.0 (full volume)
      add - position in the audiosample that playback should start from, in seconds.
    • play

      public void play(float rate, float pos, float amp, float add, float cue)
      Starts the playback of the soundfile. Only plays to the end of the audiosample once. If cue() or pause() were called previously, playback will resume from the cued position.
      Overrides:
      play in class AudioSample
      Parameters:
      rate - relative playback rate to use. 1 is the original speed. 0.5 is half speed and one octave down. 2 is double the speed and one octave up.
      amp - the desired playback amplitude of the audiosample as a value from 0.0 (complete silence) to 1.0 (full volume)
      pos - the panoramic position of this sound unit from -1.0 (left) to 1.0 (right). Only works for mono soundfiles!
      cue - position in the audiosample that playback should start from, in seconds.
      add - offset the output of the generator by the given value
    • jump

      public void jump(float time)
      Jump to a specific position in the soundfile while continuing to play (or starting to play if it wasn't playing already).
      Overrides:
      jump in class AudioSample
      Parameters:
      time - position to jump to, in seconds.
      See Also:
    • pause

      public void pause()
      Stop the playback of the file, but cue it to the current position. The next call to play() will continue playing where it left off.
      Overrides:
      pause in class AudioSample
      See Also:
    • isPlaying

      public boolean isPlaying()
      Check whether this soundfile is currently playing.
      Overrides:
      isPlaying in class AudioSample
      Returns:
      `true` if the soundfile is currently playing, `false` if it is not.
    • loop

      public void loop()
      Overrides:
      loop in class AudioSample
    • loop

      public void loop(float rate)
      Overrides:
      loop in class AudioSample
    • loop

      public void loop(float rate, float amp)
      Overrides:
      loop in class AudioSample
    • loop

      public void loop(float rate, float pos, float amp)
      Description copied from class: AudioSample
      Starts the playback of the audiosample. Only plays to the end of the audiosample once. If cue() or pause() were called previously, playback will resume from the cued position.
      Overrides:
      loop in class AudioSample
      Parameters:
      rate - relative playback rate to use. 1 is the original speed. 0.5 is half speed and one octave down. 2 is double the speed and one octave up.
      pos - the panoramic position of this sound unit from -1.0 (left) to 1.0 (right). Only works for mono audiosamples!
      amp - the desired playback amplitude of the audiosample as a value from 0.0 (complete silence) to 1.0 (full volume)
    • loop

      public void loop(float rate, float pos, float amp, float add)
      Starts playback which will loop at the end of the soundfile.
      Overrides:
      loop in class AudioSample
      Parameters:
      rate - relative playback rate to use. 1 is the original speed. 0.5 is half speed and one octave down. 2 is double the speed and one octave up.
      pos - the panoramic position of this sound unit from -1.0 (left) to 1.0 (right). Only works for mono soundfiles!
      amp - the desired playback amplitude of the audiosample as a value from 0.0 (complete silence) to 1.0 (full volume)
      add - offset the output of the generator by the given value
    • amp

      public void amp(float amp)
      Change the amplitude/volume of this audiosample.
      Overrides:
      amp in class AudioSample
      Parameters:
      amp - A float value between 0.0 (complete silence) and 1.0 (full volume) controlling the amplitude/volume of this sound.
    • pan

      public void pan(float pos)
      Move the sound in a stereo panorama.-1.0 pans to the left channel and 1.0 to the right channel. Note that panning is only supported for mono (1 channel) soundfiles.
      Overrides:
      pan in class AudioSample
      Parameters:
      pos - the panoramic position of this sound unit from -1.0 (left) to 1.0 (right).
    • rate

      public void rate(float rate)
      Set the playback rate of the soundfile. 1 is the original speed. 0.5 is half speed and one octave down. 2 is double the speed and one octave up.
      Overrides:
      rate in class AudioSample
      Parameters:
      rate - Relative playback rate to use. 1 is the original speed. 0.5 is half speed and one octave down. 2 is double the speed and one octave up.