Package ej.audio

Class AudioSource

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class AudioSource
    extends Object
    implements Closeable
    Represents an audio recording stream.

    An audio source allocates a native buffer when it is opened. The audio source should be closed with the close() method in order to free the native allocation.

    While the audio source is open, the native implementation records audio data continuously from the input device and writes it in the buffer. The readBuffer(byte[], int, int) method can be used to retrieve and remove a chunk of audio data from the buffer. If the audio data is not read fast enough by the application, the native implementation will discard the oldest audio data from the buffer.

    • Constructor Detail

      • AudioSource

        public AudioSource​(AudioFormat format,
                           int bufferSize)
        Creates and opens an audio source.
        Parameters:
        format - the format.
        bufferSize - the buffer size (in bytes).
        Throws:
        IllegalArgumentException - if the given buffer size is less than or equal to zero.
        AudioException - if the audio source could not be opened.
        SecurityException - if a security manager exists and does not allow the caller to create an audio source.
    • Method Detail

      • getFormat

        public AudioFormat getFormat()
        Returns the format of this audio source.
        Returns:
        the format.
      • getBufferSize

        public int getBufferSize()
        Returns the buffer size of this audio source.
        Returns:
        the buffer size (in bytes).
      • isClosed

        public boolean isClosed()
        Returns whether this audio source is closed.
        Returns:
        whether this audio source is closed.
      • close

        public void close()
        Closes this audio source.

        This method releases the native resources allocated when opening this audio source. This method does nothing if this audio source is already closed.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
      • readBuffer

        public int readBuffer​(byte[] array,
                              int offset,
                              int size)
        Reads audio data from the buffer of this audio source.

        This method blocks until the requested size has been read, until this audio source is closed or until the thread is interrupted.

        The number of bytes read may be different from the given size if this audio source is closed or if the thread is interrupted.

        Parameters:
        array - the array that will contain the data read.
        offset - the offset in the array of the first byte read.
        size - the maximum number of bytes to read.
        Returns:
        the number of bytes read.
        Throws:
        IndexOutOfBoundsException - if the given offset or the given size is invalid.
        SecurityException - if a security manager exists and does not allow the caller to read from the buffer.