1. /*
  2. * Copyright 2000-2002,2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.sound;
  18. import java.io.File;
  19. import java.util.Random;
  20. import java.util.Vector;
  21. import org.apache.tools.ant.BuildException;
  22. import org.apache.tools.ant.Project;
  23. import org.apache.tools.ant.Task;
  24. /**
  25. * Plays a sound file at the end of the build, according to whether the build failed or succeeded.
  26. *
  27. * There are three attributes to be set:
  28. *
  29. * <code>source</code>: the location of the audio file to be played
  30. * <code>duration</code>: play the sound file continuously until "duration" milliseconds has expired
  31. * <code>loops</code>: the number of times the sound file should be played until stopped
  32. *
  33. * I have only tested this with .WAV and .AIFF sound file formats. Both seem
  34. * to work fine.
  35. *
  36. * plans for the future:
  37. * - use the midi api to define sounds (or drum beat etc) in xml and have
  38. * Ant play them back
  39. *
  40. * @version $Revision: 1.12.2.4 $, $Date: 2004/03/09 17:01:53 $
  41. */
  42. public class SoundTask extends Task {
  43. private BuildAlert success = null;
  44. private BuildAlert fail = null;
  45. /**
  46. * add a sound when the build succeeds
  47. */
  48. public BuildAlert createSuccess() {
  49. success = new BuildAlert();
  50. return success;
  51. }
  52. /**
  53. * add a sound when the build fails
  54. */
  55. public BuildAlert createFail() {
  56. fail = new BuildAlert();
  57. return fail;
  58. }
  59. public SoundTask() {
  60. }
  61. public void init() {
  62. }
  63. public void execute() {
  64. AntSoundPlayer soundPlayer = new AntSoundPlayer();
  65. if (success == null) {
  66. log("No nested success element found.", Project.MSG_WARN);
  67. } else {
  68. soundPlayer.addBuildSuccessfulSound(success.getSource(),
  69. success.getLoops(), success.getDuration());
  70. }
  71. if (fail == null) {
  72. log("No nested failure element found.", Project.MSG_WARN);
  73. } else {
  74. soundPlayer.addBuildFailedSound(fail.getSource(),
  75. fail.getLoops(), fail.getDuration());
  76. }
  77. getProject().addBuildListener(soundPlayer);
  78. }
  79. /**
  80. * A class to be extended by any BuildAlert's that require the output
  81. * of sound.
  82. */
  83. public class BuildAlert {
  84. private File source = null;
  85. private int loops = 0;
  86. private Long duration = null;
  87. /**
  88. * Sets the duration in milliseconds the file should be played; optional.
  89. */
  90. public void setDuration(Long duration) {
  91. this.duration = duration;
  92. }
  93. /**
  94. * Sets the location of the file to get the audio; required.
  95. *
  96. * @param source the name of a sound-file directory or of the audio file
  97. */
  98. public void setSource(File source) {
  99. this.source = source;
  100. }
  101. /**
  102. * Sets the number of times the source file should be played; optional.
  103. *
  104. * @param loops the number of loops to play the source file
  105. */
  106. public void setLoops(int loops) {
  107. this.loops = loops;
  108. }
  109. /**
  110. * Gets the location of the file to get the audio.
  111. */
  112. public File getSource() {
  113. File nofile = null;
  114. // Check if source is a directory
  115. if (source.exists()) {
  116. if (source.isDirectory()) {
  117. // get the list of files in the dir
  118. String[] entries = source.list();
  119. Vector files = new Vector();
  120. for (int i = 0; i < entries.length; i++) {
  121. File f = new File(source, entries[i]);
  122. if (f.isFile()) {
  123. files.addElement(f);
  124. }
  125. }
  126. if (files.size() < 1) {
  127. throw new BuildException("No files found in directory " + source);
  128. }
  129. int numfiles = files.size();
  130. // get a random number between 0 and the number of files
  131. Random rn = new Random();
  132. int x = rn.nextInt(numfiles);
  133. // set the source to the file at that location
  134. this.source = (File) files.elementAt(x);
  135. }
  136. } else {
  137. log(source + ": invalid path.", Project.MSG_WARN);
  138. this.source = nofile;
  139. }
  140. return this.source;
  141. }
  142. /**
  143. * Sets the number of times the source file should be played.
  144. *
  145. * @return the number of loops to play the source file
  146. */
  147. public int getLoops() {
  148. return this.loops;
  149. }
  150. /**
  151. * Gets the duration in milliseconds the file should be played.
  152. */
  153. public Long getDuration() {
  154. return this.duration;
  155. }
  156. }
  157. }