Browse Source

Enhancements to SoundTask to allow a directory to be specified from which

a random sound will be chosen.

Submitted by:	Diane Holt <holtdl@yahoo.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268577 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 24 years ago
parent
commit
784d54ca48
2 changed files with 69 additions and 43 deletions
  1. +13
    -11
      src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java
  2. +56
    -32
      src/main/org/apache/tools/ant/taskdefs/optional/sound/SoundTask.java

+ 13
- 11
src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java View File

@@ -81,11 +81,11 @@ import java.util.*;
public class AntSoundPlayer implements LineListener, BuildListener {

private File fileSuccess = null;
private int loopsSuccess = 1;
private int loopsSuccess = 0;
private Long durationSuccess = null;

private File fileFail = null;
private int loopsFail = 1;
private int loopsFail = 0;
private Long durationFail = null;

public AntSoundPlayer() {
@@ -93,11 +93,11 @@ public class AntSoundPlayer implements LineListener, BuildListener {
}

/**
* @param fileName the location of the audio file to be played when the build is succesful
* @param loops the number of times the file should be played when the build is succesful
* @param duration the number of milliseconds the file should be played when the build is succesful
* @param source the location of the audio file to be played when the build is successful
* @param loops the number of times the file should be played when the build is successful
* @param duration the number of milliseconds the file should be played when the build is successful
*/
public void addBuildSuccesfulSound(File file, int loops, Long duration) {
public void addBuildSuccessfulSound(File file, int loops, Long duration) {
this.fileSuccess = file;
this.loopsSuccess = loops;
this.durationSuccess = duration;
@@ -116,7 +116,7 @@ public class AntSoundPlayer implements LineListener, BuildListener {
}

/**
* Plays the file for duration milliseconds or loops loops.
* Plays the file for duration milliseconds or loops.
*/
private void play(Project project, File file, int loops, Long duration) {

@@ -137,7 +137,8 @@ public class AntSoundPlayer implements LineListener, BuildListener {

if (audioInputStream != null) {
AudioFormat format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format, AudioSystem.NOT_SPECIFIED);
DataLine.Info info = new DataLine.Info(Clip.class, format,
AudioSystem.NOT_SPECIFIED);
try {
audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this);
@@ -160,7 +161,7 @@ public class AntSoundPlayer implements LineListener, BuildListener {
audioClip.close();
}
else {
project.log("SoundTask: can't get data from file " + file.getName());
project.log("Can't get data from file " + file.getName());
}
}

@@ -183,7 +184,8 @@ public class AntSoundPlayer implements LineListener, BuildListener {
}

/**
* This is implemented to listen for any line events and closes the clip if required.
* This is implemented to listen for any line events and closes the
* clip if required.
*/
public void update(LineEvent event) {
if (event.getType().equals(LineEvent.Type.STOP)) {
@@ -217,7 +219,7 @@ public class AntSoundPlayer implements LineListener, BuildListener {
if (event.getException() == null && fileSuccess != null) {
// build successfull!
play(event.getProject(), fileSuccess, loopsSuccess, durationSuccess);
} else if (fileFail != null) {
} else if ( event.getException() != null && fileFail != null) {
play(event.getProject(), fileFail, loopsFail, durationFail);
}
}


+ 56
- 32
src/main/org/apache/tools/ant/taskdefs/optional/sound/SoundTask.java View File

@@ -62,16 +62,18 @@ import java.util.*;
/**
* This is an example of an AntTask that makes of use of the AntSoundPlayer.
*
* There are four attributes to be set:
* There are three attributes to be set:
*
* <code>source</code>: the location of the audio file to be played
* <code>duration</code>: play the sound file continuously until "duration" milliseconds has expired
* <code>loops</code>: the number of times the sound file should be played until stopped
*
* I have only tested this with .WAV and .AIFF sound file formats. Both seem to work fine.
* I have only tested this with .WAV and .AIFF sound file formats. Both seem
* to work fine.
*
* plans for the future:
* - use the midi api to define sounds (or drum beat etc) in xml and have Ant play them back
* - use the midi api to define sounds (or drum beat etc) in xml and have
* Ant play them back
*
* @author Nick Pellow
* @version $Revision$, $Date$
@@ -79,7 +81,7 @@ import java.util.*;

public class SoundTask extends Task {

private BuildAlert success = null;
private BuildAlert success = null;
private BuildAlert fail = null;

public BuildAlert createSuccess() {
@@ -98,28 +100,35 @@ public class SoundTask extends Task {
public void init(){
}

public void execute() throws BuildException {
if ( success == null && fail == null) {
throw new BuildException("No nested elements provided.");
}
public void execute() {

AntSoundPlayer soundPlayer = new AntSoundPlayer();
if (success != null) {
soundPlayer.addBuildSuccesfulSound(success.getSource(), success.getLoops(), success.getDuration());

if ( success == null ) {
log("No nested success element found.", Project.MSG_WARN);
} else {
soundPlayer.addBuildSuccessfulSound(success.getSource(),
success.getLoops(), success.getDuration());
}
if (fail != null) {
soundPlayer.addBuildFailedSound(fail.getSource(), fail.getLoops(), fail.getDuration());

if (fail == null) {
log("No nested failure element found.", Project.MSG_WARN);
} else {
soundPlayer.addBuildFailedSound(fail.getSource(),
fail.getLoops(), fail.getDuration());
}

getProject().addBuildListener(soundPlayer);

}

/**
* A static class to be extended by any BuildAlert's that require the output of sound.
* A class to be extended by any BuildAlert's that require the output
* of sound.
*/
public static class BuildAlert {
private File file = null;
private int loops = 1;
public class BuildAlert {
private File source = null;
private int loops = 0;
private Long duration = null;

/**
@@ -132,15 +141,14 @@ public class SoundTask extends Task {
/**
* Sets the location of the file to get the audio.
*
* @param fileName the location of the audio file
* @param source the name a sound-file directory or of the audio file
*/
public void setSource(File file) {
this.file = file;
public void setSource(File source) {
this.source = source;
}

/**
* This attribute sets the number of times the source file should
* be played.
* Sets the number of times the source file should be played.
*
* @param loops the number of loops to play the source file
*/
@@ -148,29 +156,45 @@ public class SoundTask extends Task {
this.loops = loops;
}

/**
* Gets the duration in milliseconds the file should be played.
*/
public Long getDuration() {
return this.duration;
}

/**
* Gets the location of the file to get the audio.
*/
public File getSource() {
return this.file;
File nofile = null ;
// Check if source is a directory
if( source.exists() ) {
if( source.isDirectory() ) {
// get the list of files in the dir
File[] files = source.listFiles() ;
int numfiles = files.length ;
// get a random number between 0 and the number of files
Random rn = new Random() ;
int i = rn.nextInt(numfiles) ;
// set the source to the file at that location
this.source = files[i] ;
}
} else {
log(source + ": invalid path.", Project.MSG_WARN) ;
this.source = nofile ;
}
return this.source ;
}

/**
* This attribute sets the number of times the source file should
* be played.
* Sets the number of times the source file should be played.
*
* @return the number of loops to play the source file
*/
public int getLoops() {
return this.loops;
}

/**
* Gets the duration in milliseconds the file should be played.
*/
public Long getDuration() {
return this.duration;
}
}
}


Loading…
Cancel
Save