Package trclib.archive
Class TrcSongPlayer
java.lang.Object
trclib.archive.TrcSongPlayer
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 Summary
FieldsModifier and TypeFieldDescriptionprivate double
private TrcEvent
private final String
private final TrcTaskMgr.TaskObject
private boolean
private TrcSong
private final TrcTaskMgr.TaskObject
private final TrcTone
private final TrcDbgTrace
-
Constructor Summary
ConstructorsConstructorDescriptionTrcSongPlayer
(String instanceName, TrcTone tone) Constructor: Create and initialize an instance of the object. -
Method Summary
Modifier and TypeMethodDescriptionprivate double
parseDuration
(String note, double barDuration) This method parses the note length from the note string.private boolean
parseDynamics
(String notation) This method checks if the notation is a dynamics.private double
parseDynamicsVolume
(String notation) This method parses the dynamics volume.private double
parseFrequency
(String note) This method parses the note frequency from the note string.void
pause()
This method is called to pause the player.private void
performNotation
(String notation) This method parses and performs the notation action.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.private void
This method parses the note string for frequency and duration and plays it.void
This method plays the specified song.void
This method plays the specified song.void
This method plays the specified song.void
This method plays the specified song.private void
playSongWorker
(TrcSong song, double barDuration, boolean repeat, boolean pause, TrcEvent event) This method plays the specified song.void
resume()
This method is called to resume the player.void
rewind()
This method rewinds the song back to the beginning.private void
setTaskEnabled
(boolean enabled) This method enables/disables the player task.void
stop()
This method stops the sound and disables the player task.private void
stopTask
(TrcTaskMgr.TaskType taskType, TrcRobot.RunMode runMode, boolean slowPeriodicLoop) This method is called when the competition mode is about to end.toString()
This method returns the instance name.
-
Field Details
-
tracer
-
instanceName
-
tone
-
playerTaskObj
-
stopTaskObj
-
song
-
barDuration
private double barDuration -
repeat
private boolean repeat -
event
-
-
Constructor Details
-
TrcSongPlayer
Constructor: Create and initialize an instance of the object.- Parameters:
instanceName
- specifies the instance name.tone
- specifies the Tone player.
-
-
Method Details
-
toString
This method returns the 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
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
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
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
This method plays the specified song.- Parameters:
song
- specifies the song to be played.barDuration
- Specifies the bar duration in seconds.
-
playNote
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
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
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
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
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
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.
-