Class AudioReadBuffer

  • All Implemented Interfaces:
    java.lang.Cloneable, Actor, Executable, FiringsRecordable, Initializable, TypedActor, Changeable, Debuggable, DebugListener, Derivable, Instantiable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

    public class AudioReadBuffer
    extends Transformer
    This actor reads a sound file into a buffer and outputs the the sample value at the specified buffer element. The sound file is read from the URL specified by the sourceURL parameter. Although the sound file must be specified as a URL, it is still possible to specify files on the local file system.

    In each iteration, an IntToken is read from the input port, if there is a token available. This token specifies the address of the buffer element to read from. The address is zero-based, so address 0 corresponds to the first element and addres bufferLength -1 corresponds to the last element. The sample value at the specified buffer location is converted to a DoubleToken and sent to the output port.

    If an element outside of the valid address range is specified, then a DoubleToken with value 0.0 is output. If the the specified address is greater than bufferLength -1, then this actor will return false in postfire.

    This actor does not currently support multichannel sound files, so if a stereo sound file is specified, only the left channel will be used. This limitation may be lifted in a future version of this actor.

    The audio samples that are read from the file are converted to DoubleTokens that may range from [-1.0, 1.0]. Thus, the output type of this actor is DoubleToken.

    Usage

    The sourceURL parameter should be set to the name of the file, specified as a fully qualified URL. It is possible to load a file from the local file system by using the prefix "file://" instead of "http://". Relative file paths are allowed. To specify a file relative to the current directory, use "../" or "./". For example, if the current directory contains a file called "test.wav", then sourceURL should be set to "file:./test.wav". If the parent directory contains a file called "test.wav", then sourceURL should be set to "file:../test.wav". To reference the file test.wav, located at "/tmp/test.wav", sourceURL should be set to "file:///tmp/test.wav" The default value is "file:///tmp/test.wav".

    There are security issues involved with accessing files and audio resources in applets. Applets are only allowed access to files specified by a URL and located on the machine from which the applet is loaded. The .java.policy file may be modified to grant applets more privileges.

    FIXME: This actor should extend AudioReadBuffer?

    Note: Requires Java 2 v1.3.0 or later.

    Since:
    Ptolemy II 2.0
    Version:
    $Id$
    Author:
    Brian K. Vogel
    See Also:
    LiveSound
    Pt.AcceptedRating:
    Red (vogel)
    Pt.ProposedRating:
    Red (vogel)
    • Field Detail

      • sourceURL

        public StringAttribute sourceURL
        The URL of the file to read from. The default value of this parameter is the URL "file:///tmp/test.wav". Supported file formats are WAV, AU, and AIFF. The sound file format is determined from the file extension.

        An exception will occur if the path references a non-existent or unsupported sound file.

      • bufferLength

        public Parameter bufferLength
        The length of the audio buffer to use. The default value is 8000.
    • Constructor Detail

      • AudioReadBuffer

        public AudioReadBuffer​(CompositeEntity container,
                               java.lang.String name)
                        throws NameDuplicationException,
                               IllegalActionException
        Construct an actor with the given container and name. In addition to invoking the base class constructors, construct the parameters and initialize them to their default values.
        Parameters:
        container - The container.
        name - The name of this actor.
        Throws:
        IllegalActionException - If the actor cannot be contained by the proposed container.
        NameDuplicationException - If the container already has an actor with this name.
    • Method Detail

      • fire

        public void fire()
                  throws IllegalActionException
        Read the buffer address from the input port and output the corresponding audio sample, if there is a token available on the input port.

        The IntToken read from the input port specifies the buffer address to read from. The valid address range is 0 to bufferLength -1. If an invalid address is read, then the value 0.0 is output. If the the specified address is greater than bufferLength -1, then this actor will return false in postfire. The audio sample is converted to a DoubleToken with range [-1.0, 1.0] and sent to the output. Note that if a stereo sound file is specified, only the left channel will be used.

        Specified by:
        fire in interface Executable
        Overrides:
        fire in class AtomicActor<TypedIOPort>
        Throws:
        IllegalActionException - Not thrown in this base class.
      • postfire

        public boolean postfire()
                         throws IllegalActionException
        This method causes one audio sample per channel to be read from the specified file. Each sample is converted to a double token, with a maximum range of -1.0 to 1.0. One double token per channel is written to the output port.
        Specified by:
        postfire in interface Executable
        Overrides:
        postfire in class AtomicActor<TypedIOPort>
        Returns:
        True if there are samples available from the audio source. False if there are no more samples (end of sound file reached).
        Throws:
        IllegalActionException - If there is a problem reading from the specified sound file.