Class AudioPlayer

  • java.lang.Object
    • at.ofai.music.beatroot.AudioPlayer
  • All Implemented Interfaces:
    java.lang.Runnable


    public class AudioPlayer
    extends java.lang.Object
    implements java.lang.Runnable
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      protected javax.sound.sampled.SourceDataLine audioOut
      The object to which audio output is written
      protected int beatIndex
      The index in beats[] of the next beat (relative to the current playback position
      protected long[] beats
      The positions in samples of beats for playback of audio with click marking beat times
      protected int[][] click
      An array of percussion sounds
      protected AudioFile currentFile
      The file currently loaded by this AudioPlayer
      protected long currentPosition
      The position (in bytes) of playback, relative to the beginning of the file
      protected boolean debug
      Flag for debugging output
      protected static int defaultOutputBufferSize
      The default buffer size for audio output
      protected long endPosition
      The byte position in the current file to stop playing
      protected GUI gui
      BeatRoot's graphical user interface
      protected javax.swing.JFileChooser jfc
      The file chooser dialog window for opening and saving files
      protected int[] level
      The metrical level of each beat (see beats[])
      protected int outputBufferSize
      The buffer size used by audioOut
      protected boolean playAudio
      Flag indicating the current mode of playing, whether audio should be played
      protected boolean playBeats
      Flag indicating the current mode of playing, whether the beats should be played
      protected boolean playing
      A flag set by the play thread indicating whether audio playback is active
      protected byte[] readBuffer
      Audio input buffer for the current input file
      protected byte[] readBuffer2
      A second audio input buffer used when crossfading between two audio files
      protected static int readBufferSize
      The size of the buffer for reading audio input
      protected AudioFile requestedFile
      A new file that has been loaded to play next
      protected long requestedPosition
      The requested playback position, in bytes, relative to the beginning of the file
      protected long startNanoTime
      The time that the audio playback last started, in nanoseconds
      protected double startTime
      The time in the playback file where the audio playback last started
      protected boolean stopRequested
      A flag indicating to the play thread that playing should stop
      protected static double volume
      The relative volume of percussion (for the click track) relative to the input audio
    • Constructor Summary

      Constructors 
      Constructor and Description
      AudioPlayer(AudioFile f, javax.swing.JFileChooser ch)
      Constructor
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      protected void addBeat(byte[] buffer, int offset, int len, int level)
      Adds a percussion sound to an audio sample at a specified time.
      protected void addBeats(byte[] buffer, int bytes)
      Adds a sequence of percussive sounds marking the beats at various metrical levels to an audio sample.
      long correctedPosition()
      Returns current playback position, corrected for audio buffered in the soundcard.
      long getCurrentFileLength()
      Returns the length of the current or requested file.
      double getCurrentTime()
      Returns the current playback time in seconds from when play was started.
      void ifSetPosition(double time)
      Conditionally changes the position and possibly the file for input data, if the play thread is idle.
      protected void initBeats()
      Initialise the beat list ready for audio playback with beats.
      protected void initClicks()
      Initialise the percussion sounds for audio playback with beats.
      void load()
      Open a new audio input file, with the path being chosen by a file chooser dialog.
      void load(java.lang.String fileName)
      Open a new audio input file with the given path.
      void pause()
      Notifies play thread to pause playing
      void play()
      Notify play thread to play audio with beats
      void play(boolean audioOnly)
      Notify play thread to play either audio or beats.
      void play(boolean audio, boolean beats)
      Notify play thread to play audio, beats or both.
      void run()
      Implements the Runnable interface for the audio playback thread.
      void save()
      Saves audio with beats as a WAV file, with name determined by a file chooser dialog.
      void setCurrentFile(AudioFile newFile)
      Change the input file for audio playback (in a thread-safe way).
      protected void setPosition(long positionRequested)
      Changes the position and possibly the file for input data, and updates the GUI accordingly.
      protected void setPosition(long positionRequested, boolean update)
      Changes the position and possibly the file for input data.
      void skip(double time)
      Changes the playback position relative to the current playback position.
      void skip(long bytes)
      Changes the playback position relative to the current playback position.
      void stop()
      Notifies play thread to stop playing or reset position if not playing.
      void stop(boolean resetPosition)
      Notifies play thread to stop playing.
      void togglePlay()
      Notifies the play thread to pause if playing or play if paused/stopped.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • audioOut

        protected javax.sound.sampled.SourceDataLine audioOut
        The object to which audio output is written
      • beatIndex

        protected int beatIndex
        The index in beats[] of the next beat (relative to the current playback position
      • beats

        protected long[] beats
        The positions in samples of beats for playback of audio with click marking beat times
      • click

        protected int[][] click
        An array of percussion sounds
      • currentFile

        protected AudioFile currentFile
        The file currently loaded by this AudioPlayer
      • currentPosition

        protected long currentPosition
        The position (in bytes) of playback, relative to the beginning of the file
      • debug

        protected boolean debug
        Flag for debugging output
      • defaultOutputBufferSize

        protected static final int defaultOutputBufferSize
        The default buffer size for audio output
        See Also:
        Constant Field Values
      • endPosition

        protected long endPosition
        The byte position in the current file to stop playing
      • gui

        protected GUI gui
        BeatRoot's graphical user interface
      • jfc

        protected javax.swing.JFileChooser jfc
        The file chooser dialog window for opening and saving files
      • level

        protected int[] level
        The metrical level of each beat (see beats[])
      • outputBufferSize

        protected int outputBufferSize
        The buffer size used by audioOut
      • playAudio

        protected boolean playAudio
        Flag indicating the current mode of playing, whether audio should be played
      • playBeats

        protected boolean playBeats
        Flag indicating the current mode of playing, whether the beats should be played
      • playing

        protected boolean playing
        A flag set by the play thread indicating whether audio playback is active
      • readBuffer

        protected byte[] readBuffer
        Audio input buffer for the current input file
      • readBuffer2

        protected byte[] readBuffer2
        A second audio input buffer used when crossfading between two audio files
      • readBufferSize

        protected static final int readBufferSize
        The size of the buffer for reading audio input
        See Also:
        Constant Field Values
      • requestedFile

        protected AudioFile requestedFile
        A new file that has been loaded to play next
      • requestedPosition

        protected long requestedPosition
        The requested playback position, in bytes, relative to the beginning of the file
      • startNanoTime

        protected long startNanoTime
        The time that the audio playback last started, in nanoseconds
      • startTime

        protected double startTime
        The time in the playback file where the audio playback last started
      • stopRequested

        protected boolean stopRequested
        A flag indicating to the play thread that playing should stop
      • volume

        protected static double volume
        The relative volume of percussion (for the click track) relative to the input audio
    • Constructor Detail

      • AudioPlayer

        public AudioPlayer(AudioFile f,
                           javax.swing.JFileChooser ch)
        Constructor
        Parameters:
        f - The input audio file
        ch - The FileChooser object for opening and saving files
    • Method Detail

      • play

        public void play()
        Notify play thread to play audio with beats
      • play

        public void play(boolean audioOnly)
        Notify play thread to play either audio or beats.
        Parameters:
        audioOnly - Flag indicating whether to play audio (true) or play beats (false)
      • play

        public void play(boolean audio,
                         boolean beats)
        Notify play thread to play audio, beats or both.
        Parameters:
        audio - Flag indicating whether audio should be played
        beats - Flag indicating whether beats should be played
      • pause

        public void pause()
        Notifies play thread to pause playing
      • stop

        public void stop(boolean resetPosition)
        Notifies play thread to stop playing.
        Parameters:
        resetPosition - Flag indicating whether to reset the playback position to the beginning of the file
      • stop

        public void stop()
        Notifies play thread to stop playing or reset position if not playing.
      • togglePlay

        public void togglePlay()
        Notifies the play thread to pause if playing or play if paused/stopped.
      • setPosition

        protected void setPosition(long positionRequested)
        Changes the position and possibly the file for input data, and updates the GUI accordingly.
        Parameters:
        positionRequested - The new file position (in bytes) for audio input
      • setPosition

        protected void setPosition(long positionRequested,
                                   boolean update)
        Changes the position and possibly the file for input data.
        Parameters:
        positionRequested - The new file position (in bytes) for audio input
        update - Flag to indicate whether the GUI should be updated or not
      • ifSetPosition

        public void ifSetPosition(double time)
        Conditionally changes the position and possibly the file for input data, if the play thread is idle. The GUI is not updated.
        Parameters:
        time - New play position (in seconds)
      • skip

        public void skip(double time)
        Changes the playback position relative to the current playback position.
        Parameters:
        time - Offset of new play position (in seconds) relative to present play position.
      • skip

        public void skip(long bytes)
        Changes the playback position relative to the current playback position.
        Parameters:
        bytes - Offset of new play position (in bytes) relative to present play position.
      • getCurrentTime

        public double getCurrentTime()
        Returns the current playback time in seconds from when play was started. Note that DataLine.getLongFramePosition() is not reliable - it counts in units of output buffers or simple fractions thereof - i.e. what has been sent to the soundcard rather than what has been rendered by the soundcard.
        Returns:
        Playback time in seconds
      • correctedPosition

        public long correctedPosition()
        Returns current playback position, corrected for audio buffered in the soundcard.
        Returns:
        Current playback position in bytes relative to the beginning of the file.
      • getCurrentFileLength

        public long getCurrentFileLength()
        Returns the length of the current or requested file.
        Returns:
        Length in bytes of the current or requested audio input file.
      • save

        public void save()
        Saves audio with beats as a WAV file, with name determined by a file chooser dialog.
      • load

        public void load()
        Open a new audio input file, with the path being chosen by a file chooser dialog.
      • load

        public void load(java.lang.String fileName)
        Open a new audio input file with the given path.
        Parameters:
        fileName - The relative or absolate path name of the new audio input file.
      • setCurrentFile

        public void setCurrentFile(AudioFile newFile)
        Change the input file for audio playback (in a thread-safe way).
        Parameters:
        newFile - The new input file.
      • run

        public void run()
        Implements the Runnable interface for the audio playback thread. This method has two loops: an outer "request" loop and an inner "play" loop. The outer loop waits for a signal (notify()) from the play() method, and then initialises audio output and click track data, and enters the inner play loop. The inner loop reads audio from the input file and writes to the audio device, exiting only at the end of file or an external request to stop or pause playback, or to switch playback to a new file.
        Specified by:
        run in interface java.lang.Runnable
      • initClicks

        protected void initClicks()
        Initialise the percussion sounds for audio playback with beats. Reads each of the sounds into an array for quick and simple playback.
      • initBeats

        protected void initBeats()
        Initialise the beat list ready for audio playback with beats. Copies beats into an array for quick and simple retrieval during playback. Might not be the best solution; e.g. direct use of the beat list would be possible if checks were made for concurrent modification
      • addBeats

        protected void addBeats(byte[] buffer,
                                int bytes)
        Adds a sequence of percussive sounds marking the beats at various metrical levels to an audio sample.
        Parameters:
        buffer - The audio sample without beats
        bytes - The length of the audio sample in bytes
      • addBeat

        protected void addBeat(byte[] buffer,
                               int offset,
                               int len,
                               int level)
        Adds a percussion sound to an audio sample at a specified time.
        Parameters:
        buffer - The audio sample without the beats being marked
        offset - Offset of the percussion sound relative to the audio sample
        len - Length of the sound to add (in bytes)
        level - Metrical level of the percussion sound (determines instrument)