Skip to content

Commit fc833b3

Browse files
author
Luke Kuza
committed
Fixed state functions in Microphone class
Removed String based State checking (getState()) Enum is now used, ex. microphone.getState() != Microphone.CaptureState.CLOSED Fixed placing of setState(state) to better reflect the current state of the Microphone
1 parent 9a63745 commit fc833b3

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

src/com/darkprograms/speech/microphone/Microphone.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Microphone {
1818
/**
1919
* Enum for current Microphone state
2020
*/
21-
private enum CaptureState {
21+
public enum CaptureState {
2222
PROCESSING_AUDIO, STARTING_CAPTURE, CLOSED
2323
}
2424

@@ -44,18 +44,8 @@ private enum CaptureState {
4444
* STARTING_CAPTURE is returned if the Thread is setting variables<br>
4545
* CLOSED is returned if the Thread is not doing anything/not capturing audio
4646
*/
47-
public String getState() {
48-
switch (state) {
49-
case PROCESSING_AUDIO:
50-
return "PROCESSING_AUDIO";
51-
case STARTING_CAPTURE:
52-
return "STARTING_CAPTURE";
53-
case CLOSED:
54-
return "CLOSED";
55-
56-
default:
57-
return "CLOSED";
58-
}
47+
public CaptureState getState() {
48+
return state;
5949
}
6050

6151
/**
@@ -111,8 +101,8 @@ public Microphone(AudioFileFormat.Type fileType) {
111101
* @throws Exception Throws an exception if something went wrong
112102
*/
113103
public void captureAudioToFile(File audioFile) throws Exception {
114-
setAudioFile(audioFile);
115104
setState(CaptureState.STARTING_CAPTURE);
105+
setAudioFile(audioFile);
116106

117107
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, getAudioFormat());
118108
setTargetDataLine((TargetDataLine) AudioSystem.getLine(dataLineInfo));
@@ -131,9 +121,9 @@ public void captureAudioToFile(File audioFile) throws Exception {
131121
* @throws Exception Throws an exception if something went wrong
132122
*/
133123
public void captureAudioToFile(String audioFile) throws Exception {
124+
setState(CaptureState.STARTING_CAPTURE);
134125
File file = new File(audioFile);
135126
setAudioFile(file);
136-
setState(CaptureState.STARTING_CAPTURE);
137127

138128
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, getAudioFormat());
139129
setTargetDataLine((TargetDataLine) AudioSystem.getLine(dataLineInfo));
@@ -169,9 +159,8 @@ private AudioFormat getAudioFormat() {
169159
* If already closed, this does nothing
170160
*/
171161
public void close() {
172-
if (getState().equals("CLOSED")) {
162+
if (getState() == CaptureState.CLOSED) {
173163
} else {
174-
setState(CaptureState.CLOSED);
175164
getTargetDataLine().stop();
176165
getTargetDataLine().close();
177166
}
@@ -187,12 +176,13 @@ private class CaptureThread implements Runnable {
187176
*/
188177
public void run() {
189178
try {
190-
state = CaptureState.PROCESSING_AUDIO;
179+
setState(CaptureState.PROCESSING_AUDIO);
191180
AudioFileFormat.Type fileType = getFileType();
192181
File audioFile = getAudioFile();
193182
getTargetDataLine().open(getAudioFormat());
194183
getTargetDataLine().start();
195184
AudioSystem.write(new AudioInputStream(getTargetDataLine()), fileType, audioFile);
185+
setState(CaptureState.CLOSED);
196186
} catch (Exception ex) {
197187
ex.printStackTrace();
198188
}

0 commit comments

Comments
 (0)