Class TrcSongPlayer

java.lang.Object
trclib.archive.TrcSongPlayer

public class TrcSongPlayer extends Object
This class implements a song player that can parse a notated song in a string buffer and play the notes on a Tone device.
  • Field Details

  • Constructor Details

    • TrcSongPlayer

      public TrcSongPlayer(String instanceName, TrcTone tone)
      Constructor: Create and initialize an instance of the object.
      Parameters:
      instanceName - specifies the instance name.
      tone - specifies the Tone player.
  • Method Details

    • toString

      public String toString()
      This method returns the instance name.
      Overrides:
      toString in class Object
      Returns:
      instance name.
    • setTaskEnabled

      private void setTaskEnabled(boolean enabled)
      This method enables/disables the player task.
      Parameters:
      enabled - specifies true to enable, false otherwise.
    • stop

      public void stop()
      This method stops the sound and disables the player task.
    • pause

      public void pause()
      This method is called to pause the player.
    • resume

      public void resume()
      This method is called to resume the player.
    • rewind

      public void rewind()
      This method rewinds the song back to the beginning.
    • playSongWorker

      private void playSongWorker(TrcSong song, double barDuration, boolean repeat, boolean pause, TrcEvent event)
      This method plays the specified song. This method is made private because the parameters repeat and event can only have one or the other but not both. If the song is repeating, it will not fire an event.
      Parameters:
      song - specifies the song to be played.
      barDuration - specifies the bar duration in seconds.
      repeat - specifies true to play the song repeatedly, false otherwise.
      pause - specifies true to pause the song, false to start it immediately.
      event - specifies the event to be notified on song completion.
    • playSong

      public void playSong(TrcSong song, double barDuration, boolean repeat, boolean pause)
      This method plays the specified song.
      Parameters:
      song - specifies the song to be played.
      barDuration - specifies the bar duration in seconds.
      repeat - specifies true to play the song repeatedly, false otherwise.
      pause - specifies true to pause the song, false to start it immediately.
    • playSong

      public void playSong(TrcSong song, double barDuration, boolean pause, TrcEvent event)
      This method plays the specified song.
      Parameters:
      song - specifies the song to be played.
      barDuration - specifies the bar duration in seconds.
      pause - specifies true to pause the song, false to start it immediately.
      event - specifies the event to be notified on song completion.
    • playSong

      public void playSong(TrcSong song, double barDuration, boolean pause)
      This method plays the specified song.
      Parameters:
      song - specifies the song to be played.
      barDuration - specifies the bar duration in seconds.
      pause - specifies true to pause the song, false to start it immediately.
    • playSong

      public void playSong(TrcSong song, double barDuration)
      This method plays the specified song.
      Parameters:
      song - specifies the song to be played.
      barDuration - Specifies the bar duration in seconds.
    • playNote

      private void playNote(String note, double barDuration, double volume)
      This method parses the note string for frequency and duration and plays it.
      Parameters:
      note - Specifies the note string in the format: [#|b].[+]{.[+]} where - 'A' through 'G' # - sharp b - flat - 1 through 8 - note type (1: whole, 2: half, 4: quarter, ...) + - add half time
      barDuration - Specifies the bar duration in seconds.
      volume - specifies the volume of the note.
    • parseFrequency

      private double parseFrequency(String note)
      This method parses the note frequency from the note string.
      Parameters:
      note - Specifies the note string in the format: [#|b] where - 'A' through 'G' # - sharp b - flat - 1 through 8
      Returns:
      note frequency on success and throws IllegalArgumentException on failure.
    • parseDuration

      private double parseDuration(String note, double barDuration)
      This method parses the note length from the note string.
      Parameters:
      note - Specifies the note string in the format: [+]{.[+]} where - note type (1: whole, 2: half, 4: quarter, ...) + - add half time
      barDuration - Specifies the bar duration in seconds.
      Returns:
      note length in seconds on success and throws IllegalArgumentException on failure.
    • parseDynamicsVolume

      private double parseDynamicsVolume(String notation)
      This method parses the dynamics volume.
      Parameters:
      notation - Specifies the dynamics volume notation.
      Returns:
      note volume in the range of 1.0, or -1.0 if not a valid dynamics notation.
    • parseDynamics

      private boolean parseDynamics(String notation)
      This method checks if the notation is a dynamics. If so, parses it and performs the appropriate action.
      Parameters:
      notation - specifies the notation string.
      Returns:
      true if the notation is a dynamics, false otherwise.
    • performNotation

      private void performNotation(String notation)
      This method parses and performs the notation action. It throws an IllegalArgumentException if it is not a recognized notation.
      Parameters:
      notation - specifies the notation string.
    • playerTask

      private void playerTask(TrcTaskMgr.TaskType taskType, TrcRobot.RunMode runMode, boolean slowPeriodicLoop)
      This method is called periodically to check and play the next note in the song.
      Parameters:
      taskType - specifies the type of task being run.
      runMode - specifies the competition mode that is running.
      slowPeriodicLoop - specifies true if it is running the slow periodic loop on the main robot thread, false otherwise.
    • stopTask

      private void stopTask(TrcTaskMgr.TaskType taskType, TrcRobot.RunMode runMode, boolean slowPeriodicLoop)
      This method is called when the competition mode is about to end. It stops the player if sound is playing.
      Parameters:
      taskType - specifies the type of task being run.
      runMode - specifies the competition mode that is running.
      slowPeriodicLoop - specifies true if it is running the slow periodic loop on the main robot thread, false otherwise.