conor       01/02/04 02:51:02

  Modified:    src/main/org/apache/tools/ant/taskdefs/optional/sound
                        AntSoundPlayer.java SoundTask.java
  Log:
  Enhancements to SoundTask to allow a directory to be specified from which
  a random sound will be chosen.
  
  Submitted by: Diane Holt <[EMAIL PROTECTED]>
  
  Revision  Changes    Path
  1.3       +14 -12    
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java
  
  Index: AntSoundPlayer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AntSoundPlayer.java       2001/01/03 14:18:45     1.2
  +++ AntSoundPlayer.java       2001/02/04 10:51:00     1.3
  @@ -75,17 +75,17 @@
    * I have only tested this with .WAV and .AIFF sound file formats. Both seem 
to work fine.
    *
    * @author Nick Pellow
  - * @version $Revision: 1.2 $, $Date: 2001/01/03 14:18:45 $
  + * @version $Revision: 1.3 $, $Date: 2001/02/04 10:51:00 $
    */
   
   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 @@
       }
   
       /**
  -     * @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 @@
       }
   
       /**
  -     * 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 @@
   
                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 @@
               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 @@
       }
   
       /**
  -     * 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 @@
           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);
           }
       }
  
  
  
  1.3       +57 -33    
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/sound/SoundTask.java
  
  Index: SoundTask.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/sound/SoundTask.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SoundTask.java    2001/01/03 14:18:45     1.2
  +++ SoundTask.java    2001/02/04 10:51:01     1.3
  @@ -62,24 +62,26 @@
   /**
    * 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: 1.2 $, $Date: 2001/01/03 14:18:45 $
  + * @version $Revision: 1.3 $, $Date: 2001/02/04 10:51:01 $
    */
   
   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 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 @@
           /**
            * 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
            */
  @@ -149,27 +157,43 @@
           }
   
           /**
  -         * 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;
           }
       }
   }
  
  
  

Reply via email to