at.ofai.music.beatroot

Class AudioPlayer

public class AudioPlayer extends Object implements Runnable

Field Summary
protected SourceDataLineaudioOut
The object to which audio output is written
protected intbeatIndex
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 AudioFilecurrentFile
The file currently loaded by this AudioPlayer
protected longcurrentPosition
The position (in bytes) of playback, relative to the beginning of the file
protected booleandebug
Flag for debugging output
protected static intdefaultOutputBufferSize
The default buffer size for audio output
protected longendPosition
The byte position in the current file to stop playing
protected GUIgui
BeatRoot's graphical user interface
protected JFileChooserjfc
The file chooser dialog window for opening and saving files
protected int[]level
The metrical level of each beat (see beats[])
protected intoutputBufferSize
The buffer size used by audioOut
protected booleanplayAudio
Flag indicating the current mode of playing, whether audio should be played
protected booleanplayBeats
Flag indicating the current mode of playing, whether the beats should be played
protected booleanplaying
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 intreadBufferSize
The size of the buffer for reading audio input
protected AudioFilerequestedFile
A new file that has been loaded to play next
protected longrequestedPosition
The requested playback position, in bytes, relative to the beginning of the file
protected longstartNanoTime
The time that the audio playback last started, in nanoseconds
protected doublestartTime
The time in the playback file where the audio playback last started
protected booleanstopRequested
A flag indicating to the play thread that playing should stop
protected static doublevolume
The relative volume of percussion (for the click track) relative to the input audio
Constructor Summary
AudioPlayer(AudioFile f, JFileChooser ch)
Constructor
Method Summary
protected voidaddBeat(byte[] buffer, int offset, int len, int level)
Adds a percussion sound to an audio sample at a specified time.
protected voidaddBeats(byte[] buffer, int bytes)
Adds a sequence of percussive sounds marking the beats at various metrical levels to an audio sample.
longcorrectedPosition()
Returns current playback position, corrected for audio buffered in the soundcard.
longgetCurrentFileLength()
Returns the length of the current or requested file.
doublegetCurrentTime()
Returns the current playback time in seconds from when play was started.
voidifSetPosition(double time)
Conditionally changes the position and possibly the file for input data, if the play thread is idle.
protected voidinitBeats()
Initialise the beat list ready for audio playback with beats.
protected voidinitClicks()
Initialise the percussion sounds for audio playback with beats.
voidload()
Open a new audio input file, with the path being chosen by a file chooser dialog.
voidload(String fileName)
Open a new audio input file with the given path.
voidpause()
Notifies play thread to pause playing
voidplay()
Notify play thread to play audio with beats
voidplay(boolean audioOnly)
Notify play thread to play either audio or beats.
voidplay(boolean audio, boolean beats)
Notify play thread to play audio, beats or both.
voidrun()
Implements the Runnable interface for the audio playback thread.
voidsave()
Saves audio with beats as a WAV file, with name determined by a file chooser dialog.
voidsetCurrentFile(AudioFile newFile)
Change the input file for audio playback (in a thread-safe way).
protected voidsetPosition(long positionRequested)
Changes the position and possibly the file for input data, and updates the GUI accordingly.
protected voidsetPosition(long positionRequested, boolean update)
Changes the position and possibly the file for input data.
voidskip(double time)
Changes the playback position relative to the current playback position.
voidskip(long bytes)
Changes the playback position relative to the current playback position.
voidstop(boolean resetPosition)
Notifies play thread to stop playing.
voidstop()
Notifies play thread to stop playing or reset position if not playing.
voidtogglePlay()
Notifies the play thread to pause if playing or play if paused/stopped.

Field Detail

audioOut

protected 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

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 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

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, JFileChooser ch)
Constructor

Parameters: f The input audio file ch The FileChooser object for opening and saving files

Method Detail

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)

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

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.

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

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)

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

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.

load

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

load

public void load(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.

pause

public void pause()
Notifies play thread to pause playing

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

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.

save

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

setCurrentFile

public void setCurrentFile(AudioFile newFile)
Change the input file for audio playback (in a thread-safe way).

Parameters: newFile The new input file.

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

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.

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.