1
|
|
/*
|
2
|
|
* The Apache Software License, Version 1.1
|
3
|
|
*
|
4
|
|
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
|
5
|
|
* reserved.
|
6
|
|
*
|
7
|
|
* Redistribution and use in source and binary forms, with or without
|
8
|
|
* modification, are permitted provided that the following conditions
|
9
|
|
* are met:
|
10
|
|
*
|
11
|
|
* 1. Redistributions of source code must retain the above copyright
|
12
|
|
* notice, this list of conditions and the following disclaimer.
|
13
|
|
*
|
14
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
15
|
|
* notice, this list of conditions and the following disclaimer in
|
16
|
|
* the documentation and/or other materials provided with the
|
17
|
|
* distribution.
|
18
|
|
*
|
19
|
|
* 3. The end-user documentation included with the redistribution, if
|
20
|
|
* any, must include the following acknowlegement:
|
21
|
|
* "This product includes software developed by the
|
22
|
|
* Apache Software Foundation (http://www.apache.org/)."
|
23
|
|
* Alternately, this acknowlegement may appear in the software itself,
|
24
|
|
* if and wherever such third-party acknowlegements normally appear.
|
25
|
|
*
|
26
|
|
* 4. The names "Ant" and "Apache Software
|
27
|
|
* Foundation" must not be used to endorse or promote products derived
|
28
|
|
* from this software without prior written permission. For written
|
29
|
|
* permission, please contact apache@apache.org.
|
30
|
|
*
|
31
|
|
* 5. Products derived from this software may not be called "Apache"
|
32
|
|
* nor may "Apache" appear in their names without prior written
|
33
|
|
* permission of the Apache Group.
|
34
|
|
*
|
35
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
36
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
37
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
38
|
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
39
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
40
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
41
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
42
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
43
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
44
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
45
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
46
|
|
* SUCH DAMAGE.
|
47
|
|
* ====================================================================
|
48
|
|
*
|
49
|
|
* This software consists of voluntary contributions made by many
|
50
|
|
* individuals on behalf of the Apache Software Foundation. For more
|
51
|
|
* information on the Apache Software Foundation, please see
|
52
|
|
* <http://www.apache.org/>.
|
53
|
|
*/
|
54
|
|
|
55
|
|
package org.apache.tools.ant.taskdefs.optional.sound;
|
56
|
|
|
57
|
|
import java.io.File;
|
58
|
|
import java.util.Random;
|
59
|
|
import java.util.Vector;
|
60
|
|
import org.apache.tools.ant.BuildException;
|
61
|
|
import org.apache.tools.ant.Project;
|
62
|
|
import org.apache.tools.ant.Task;
|
63
|
|
|
64
|
|
/**
|
65
|
|
* Plays a sound file at the end of the build, according to whether the build failed or succeeded.
|
66
|
|
*
|
67
|
|
* There are three attributes to be set:
|
68
|
|
*
|
69
|
|
* <code>source</code>: the location of the audio file to be played
|
70
|
|
* <code>duration</code>: play the sound file continuously until "duration" milliseconds has expired
|
71
|
|
* <code>loops</code>: the number of times the sound file should be played until stopped
|
72
|
|
*
|
73
|
|
* I have only tested this with .WAV and .AIFF sound file formats. Both seem
|
74
|
|
* to work fine.
|
75
|
|
*
|
76
|
|
* plans for the future:
|
77
|
|
* - use the midi api to define sounds (or drum beat etc) in xml and have
|
78
|
|
* Ant play them back
|
79
|
|
*
|
80
|
|
* @author Nick Pellow
|
81
|
|
* @version $Revision: 1.10 $, $Date: 2003/02/10 14:14:26 $
|
82
|
|
*/
|
83
|
|
|
84
|
|
public class SoundTask extends Task {
|
85
|
|
|
86
|
|
private BuildAlert success = null;
|
87
|
|
private BuildAlert fail = null;
|
88
|
|
|
89
|
|
/**
|
90
|
|
* add a sound when the build succeeds
|
91
|
|
*/
|
92
|
0
|
public BuildAlert createSuccess() {
|
93
|
0
|
success = new BuildAlert();
|
94
|
0
|
return success;
|
95
|
|
}
|
96
|
|
|
97
|
|
/**
|
98
|
|
* add a sound when the build fails
|
99
|
|
*/
|
100
|
0
|
public BuildAlert createFail() {
|
101
|
0
|
fail = new BuildAlert();
|
102
|
0
|
return fail;
|
103
|
|
}
|
104
|
|
|
105
|
0
|
public SoundTask() {
|
106
|
|
}
|
107
|
|
|
108
|
0
|
public void init(){
|
109
|
|
}
|
110
|
|
|
111
|
0
|
public void execute() {
|
112
|
|
|
113
|
0
|
AntSoundPlayer soundPlayer = new AntSoundPlayer();
|
114
|
|
|
115
|
0
|
if (success == null) {
|
116
|
0
|
log("No nested success element found.", Project.MSG_WARN);
|
117
|
|
} else {
|
118
|
0
|
soundPlayer.addBuildSuccessfulSound(success.getSource(),
|
119
|
|
success.getLoops(), success.getDuration());
|
120
|
|
}
|
121
|
|
|
122
|
0
|
if (fail == null) {
|
123
|
0
|
log("No nested failure element found.", Project.MSG_WARN);
|
124
|
|
} else {
|
125
|
0
|
soundPlayer.addBuildFailedSound(fail.getSource(),
|
126
|
|
fail.getLoops(), fail.getDuration());
|
127
|
|
}
|
128
|
|
|
129
|
0
|
getProject().addBuildListener(soundPlayer);
|
130
|
|
|
131
|
|
}
|
132
|
|
|
133
|
|
/**
|
134
|
|
* A class to be extended by any BuildAlert's that require the output
|
135
|
|
* of sound.
|
136
|
|
*/
|
137
|
|
public class BuildAlert {
|
138
|
|
private File source = null;
|
139
|
|
private int loops = 0;
|
140
|
|
private Long duration = null;
|
141
|
|
|
142
|
|
/**
|
143
|
|
* Sets the duration in milliseconds the file should be played; optional.
|
144
|
|
*/
|
145
|
0
|
public void setDuration(Long duration) {
|
146
|
0
|
this.duration = duration;
|
147
|
|
}
|
148
|
|
|
149
|
|
/**
|
150
|
|
* Sets the location of the file to get the audio; required.
|
151
|
|
*
|
152
|
|
* @param source the name of a sound-file directory or of the audio file
|
153
|
|
*/
|
154
|
0
|
public void setSource(File source) {
|
155
|
0
|
this.source = source;
|
156
|
|
}
|
157
|
|
|
158
|
|
/**
|
159
|
|
* Sets the number of times the source file should be played; optional.
|
160
|
|
*
|
161
|
|
* @param loops the number of loops to play the source file
|
162
|
|
*/
|
163
|
0
|
public void setLoops(int loops) {
|
164
|
0
|
this.loops = loops;
|
165
|
|
}
|
166
|
|
|
167
|
|
/**
|
168
|
|
* Gets the location of the file to get the audio.
|
169
|
|
*/
|
170
|
0
|
public File getSource() {
|
171
|
0
|
File nofile = null ;
|
172
|
|
// Check if source is a directory
|
173
|
0
|
if (source.exists()) {
|
174
|
0
|
if (source.isDirectory()) {
|
175
|
|
// get the list of files in the dir
|
176
|
0
|
String[] entries = source.list() ;
|
177
|
0
|
Vector files = new Vector() ;
|
178
|
0
|
for (int i = 0 ; i < entries.length ; i++) {
|
179
|
0
|
File f = new File(source, entries[i]) ;
|
180
|
0
|
if (f.isFile()) {
|
181
|
0
|
files.addElement(f) ;
|
182
|
|
}
|
183
|
|
}
|
184
|
0
|
if (files.size() < 1) {
|
185
|
0
|
throw new BuildException("No files found in directory " + source);
|
186
|
|
}
|
187
|
0
|
int numfiles = files.size() ;
|
188
|
|
// get a random number between 0 and the number of files
|
189
|
0
|
Random rn = new Random() ;
|
190
|
0
|
int x = rn.nextInt(numfiles) ;
|
191
|
|
// set the source to the file at that location
|
192
|
0
|
this.source = (File) files.elementAt(x);
|
193
|
|
}
|
194
|
|
} else {
|
195
|
0
|
log(source + ": invalid path.", Project.MSG_WARN) ;
|
196
|
0
|
this.source = nofile ;
|
197
|
|
}
|
198
|
0
|
return this.source ;
|
199
|
|
}
|
200
|
|
|
201
|
|
/**
|
202
|
|
* Sets the number of times the source file should be played.
|
203
|
|
*
|
204
|
|
* @return the number of loops to play the source file
|
205
|
|
*/
|
206
|
0
|
public int getLoops() {
|
207
|
0
|
return this.loops;
|
208
|
|
}
|
209
|
|
|
210
|
|
/**
|
211
|
|
* Gets the duration in milliseconds the file should be played.
|
212
|
|
*/
|
213
|
0
|
public Long getDuration() {
|
214
|
0
|
return this.duration;
|
215
|
|
}
|
216
|
|
}
|
217
|
|
}
|
218
|
|
|
219
|
|
|