-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSound.java
More file actions
183 lines (164 loc) · 3.68 KB
/
Sound.java
File metadata and controls
183 lines (164 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.io.*;
import java.util.*;
import javax.sound.sampled.*;
/**
* This class represents a sound that can be played.
*
* @author John Kurlak (kurlak)
* Weston Thayer (weston5)
* Panhavorn Hok (onehok1)
* @version 12.07.09
*/
public class Sound implements Runnable
{
private Thread thread;
private static final int BUFFER_SIZE = 128000;
private SourceDataLine dataLine;
private AudioInputStream stream;
private AudioFormat format;
private File file;
private boolean alive = true;
private String path;
private boolean loop = true;
/**
* This constructor creates a new sound that can be played.
*
* @param relativePath The relative file path of the sound
*/
public Sound(String relativePath)
{
path = relativePath;
file = Utilities.loadFile(path);
}
/**
* This method prepares the stream of the sound for reading.
*/
private void prepareStream()
{
try
{
stream = AudioSystem.getAudioInputStream(file);
}
catch (UnsupportedAudioFileException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED)
{
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format
.getSampleRate(), format.getSampleSizeInBits() << 1, format
.getChannels(), format.getFrameSize() << 1, format
.getFrameRate(), true);
stream = AudioSystem.getAudioInputStream(format, stream);
}
}
/**
* This method prepares the data line of the sound.
*/
private void prepareDataLine()
{
DataLine.Info info = new DataLine.Info(SourceDataLine.class, stream
.getFormat(), ((int) stream.getFrameLength() *
format.getFrameSize()));
try
{
dataLine = (SourceDataLine) AudioSystem.getLine(info);
}
catch (LineUnavailableException e)
{
e.printStackTrace();
}
try
{
dataLine.open(stream.getFormat());
}
catch (LineUnavailableException e)
{
e.printStackTrace();
}
}
/**
* This method stops the sound from playing.
*/
@SuppressWarnings("deprecation")
public void stop()
{
thread.stop();
thread = null;
}
/**
* This method plays the sound once.
*/
public void playOnce()
{
loop = false;
thread = new Thread(this);
thread.start();
}
/**
* This method loops the sound.
*/
public void play()
{
loop = true;
thread = new Thread(this);
thread.start();
}
/**
* This method plays the sound in its own thread.
*/
public void run()
{
while (alive)
{
prepareStream();
prepareDataLine();
dataLine.start();
byte[] buffer = new byte[BUFFER_SIZE];
int numBytes = 0;
try
{
numBytes = stream.read(buffer, 0, BUFFER_SIZE);
}
catch (IOException e)
{
e.printStackTrace();
}
while (numBytes !=-1 && Thread.currentThread() == thread)
{
if (numBytes > 0)
{
dataLine.write(buffer, 0, numBytes);
}
try
{
numBytes = stream.read(buffer, 0, BUFFER_SIZE);
}
catch (IOException e)
{
e.printStackTrace();
}
}
dataLine.drain();
dataLine.close();
if (!loop)
{
alive = false;
}
}
}
/**
* This method gives a string representation of the sound.
*
* @return Returns the sound's path
*/
public String toString()
{
return path;
}
}