1 | /*
|
---|
2 | * Copyright 2000-2002,2004-2005 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 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional.sound;
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 | import java.util.Random;
|
---|
22 | import java.util.Vector;
|
---|
23 | import org.apache.tools.ant.BuildException;
|
---|
24 | import org.apache.tools.ant.Project;
|
---|
25 | import org.apache.tools.ant.Task;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Plays a sound file at the end of the build, according to whether the build failed or succeeded.
|
---|
29 | *
|
---|
30 | * There are three attributes to be set:
|
---|
31 | *
|
---|
32 | * <code>source</code>: the location of the audio file to be played
|
---|
33 | * <code>duration</code>: play the sound file continuously until "duration" milliseconds has expired
|
---|
34 | * <code>loops</code>: the number of times the sound file should be played until stopped
|
---|
35 | *
|
---|
36 | * I have only tested this with .WAV and .AIFF sound file formats. Both seem
|
---|
37 | * to work fine.
|
---|
38 | *
|
---|
39 | * plans for the future:
|
---|
40 | * - use the midi api to define sounds (or drum beat etc) in xml and have
|
---|
41 | * Ant play them back
|
---|
42 | *
|
---|
43 | */
|
---|
44 |
|
---|
45 | public class SoundTask extends Task {
|
---|
46 |
|
---|
47 | private BuildAlert success = null;
|
---|
48 | private BuildAlert fail = null;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * add a sound when the build succeeds
|
---|
52 | */
|
---|
53 | public BuildAlert createSuccess() {
|
---|
54 | success = new BuildAlert();
|
---|
55 | return success;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * add a sound when the build fails
|
---|
60 | */
|
---|
61 | public BuildAlert createFail() {
|
---|
62 | fail = new BuildAlert();
|
---|
63 | return fail;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public SoundTask() {
|
---|
67 | }
|
---|
68 |
|
---|
69 | public void init() {
|
---|
70 | }
|
---|
71 |
|
---|
72 | public void execute() {
|
---|
73 |
|
---|
74 | AntSoundPlayer soundPlayer = new AntSoundPlayer();
|
---|
75 |
|
---|
76 | if (success == null) {
|
---|
77 | log("No nested success element found.", Project.MSG_WARN);
|
---|
78 | } else {
|
---|
79 | soundPlayer.addBuildSuccessfulSound(success.getSource(),
|
---|
80 | success.getLoops(), success.getDuration());
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (fail == null) {
|
---|
84 | log("No nested failure element found.", Project.MSG_WARN);
|
---|
85 | } else {
|
---|
86 | soundPlayer.addBuildFailedSound(fail.getSource(),
|
---|
87 | fail.getLoops(), fail.getDuration());
|
---|
88 | }
|
---|
89 |
|
---|
90 | getProject().addBuildListener(soundPlayer);
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * A class to be extended by any BuildAlert's that require the output
|
---|
96 | * of sound.
|
---|
97 | */
|
---|
98 | public class BuildAlert {
|
---|
99 | private File source = null;
|
---|
100 | private int loops = 0;
|
---|
101 | private Long duration = null;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Sets the duration in milliseconds the file should be played; optional.
|
---|
105 | */
|
---|
106 | public void setDuration(Long duration) {
|
---|
107 | this.duration = duration;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Sets the location of the file to get the audio; required.
|
---|
112 | *
|
---|
113 | * @param source the name of a sound-file directory or of the audio file
|
---|
114 | */
|
---|
115 | public void setSource(File source) {
|
---|
116 | this.source = source;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Sets the number of times the source file should be played; optional.
|
---|
121 | *
|
---|
122 | * @param loops the number of loops to play the source file
|
---|
123 | */
|
---|
124 | public void setLoops(int loops) {
|
---|
125 | this.loops = loops;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Gets the location of the file to get the audio.
|
---|
130 | */
|
---|
131 | public File getSource() {
|
---|
132 | File nofile = null;
|
---|
133 | // Check if source is a directory
|
---|
134 | if (source.exists()) {
|
---|
135 | if (source.isDirectory()) {
|
---|
136 | // get the list of files in the dir
|
---|
137 | String[] entries = source.list();
|
---|
138 | Vector files = new Vector();
|
---|
139 | for (int i = 0; i < entries.length; i++) {
|
---|
140 | File f = new File(source, entries[i]);
|
---|
141 | if (f.isFile()) {
|
---|
142 | files.addElement(f);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | if (files.size() < 1) {
|
---|
146 | throw new BuildException("No files found in directory " + source);
|
---|
147 | }
|
---|
148 | int numfiles = files.size();
|
---|
149 | // get a random number between 0 and the number of files
|
---|
150 | Random rn = new Random();
|
---|
151 | int x = rn.nextInt(numfiles);
|
---|
152 | // set the source to the file at that location
|
---|
153 | this.source = (File) files.elementAt(x);
|
---|
154 | }
|
---|
155 | } else {
|
---|
156 | log(source + ": invalid path.", Project.MSG_WARN);
|
---|
157 | this.source = nofile;
|
---|
158 | }
|
---|
159 | return this.source;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Sets the number of times the source file should be played.
|
---|
164 | *
|
---|
165 | * @return the number of loops to play the source file
|
---|
166 | */
|
---|
167 | public int getLoops() {
|
---|
168 | return this.loops;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Gets the duration in milliseconds the file should be played.
|
---|
173 | */
|
---|
174 | public Long getDuration() {
|
---|
175 | return this.duration;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|