Skip to content

Commit cc7ce72

Browse files
committed
Updated volume methods for ease of use and added getNumOfBytes method to
microphone. Also added auto-detect "language" to recognizer.
1 parent 5c8ac54 commit cc7ce72

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,28 @@ public void captureAudioToFile(String audioFile) throws Exception {
137137

138138
/**
139139
* Gets the volume of the microphone input
140-
* Note: Do not update more than every 250ms
141-
* unless you specify a smaller numOfBytes
142-
* @return The volume of the microphone input will return -1 if data-line is not available
140+
* Interval is 100ms so allow 100ms for this method to run in your code or specify smaller interval.
141+
* @return The volume of the microphone input or -1 if data-line is not available
143142
*/
144143
public int getAudioVolume(){
145-
return getAudioVolume(2000);
144+
return getAudioVolume(100);
145+
}
146+
147+
/**
148+
* Gets the volume of the microphone input
149+
* @param interval: The length of time you would like to calculate the volume over in milliseconds.
150+
* @return The volume of the microphone input or -1 if data-line is not available.
151+
*/
152+
public int getAudioVolume(int interval){
153+
return calculateAudioVolume(this.getNumOfBytes(interval/1000d));
146154
}
147155

148156
/**
149157
* Gets the volume of microphone input
150158
* @param numOfBytes The number of bytes you want for volume interpretation
151-
* @return The volume over the specified number of bytes or -1 if mic is unavailable.
159+
* @return The volume over the specified number of bytes or -1 if data-line is unavailable.
152160
*/
153-
public int getAudioVolume(int numOfBytes){
161+
private int calculateAudioVolume(int numOfBytes){
154162
if(getTargetDataLine()!=null){
155163
byte[] data = new byte[numOfBytes];
156164
this.getTargetDataLine().read(data, 0, numOfBytes);
@@ -162,7 +170,7 @@ public int getAudioVolume(int numOfBytes){
162170
}
163171

164172
/**
165-
* Calculates the volume of AudioData which may be buffered data from a dataline
173+
* Calculates the volume of AudioData which may be buffered data from a data-line
166174
* @param audioData The byte[] you want to determine the volume of
167175
* @return the calculated volume of audioData
168176
*/
@@ -181,7 +189,24 @@ private int calculateRMSLevel(byte[] audioData){
181189
return (int)(Math.pow(averageMeanSquare,0.5d) + 0.5);
182190
}
183191

184-
192+
/**
193+
* Returns the number of bytes over interval for useful when figuring out how long to record.
194+
* @param seconds The length in seconds
195+
* @return the number of bytes the microphone will save.
196+
*/
197+
public int getNumOfBytes(int seconds){
198+
return getNumOfBytes((double)seconds);
199+
}
200+
201+
/**
202+
* Returns the number of bytes over interval for useful when figuring out how long to record.
203+
* @param seconds The length in seconds
204+
* @return the number of bytes the microphone will output over the specified time.
205+
*/
206+
public int getNumOfBytes(double seconds){
207+
return (int)(seconds*getAudioFormat().getSampleRate()*getAudioFormat().getFrameSize()+.5);
208+
}
209+
185210
/**
186211
* The audio format to save in
187212
*

src/com/darkprograms/speech/recognizer/Recognizer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
public class Recognizer {
1313

1414
public enum Languages{
15-
BASQUE("eu"),
16-
CATALAN("ca"),
15+
AUTO_DETECT(null),//tells Google to auto-detect the language
1716
ARABIC__JORDAN("ar-JO"),
1817
ARABIC__LEBANON("ar-LB"),
1918
ARABIC__QATAR("ar-QA"),
@@ -27,6 +26,8 @@ public enum Languages{
2726
ARABIC__SAUDI_ARABIA("ar-SA"),
2827
ARABIC__TUNISIA("ar-TN"),
2928
ARABIC__YEMEN("ar-YE"),
29+
BASQUE("eu"),
30+
CATALAN("ca"),
3031
CZECH("cs"),
3132
DUTCH("nl-NL"),
3233
ENGLISH__AUSTRALIA("en-AU"),
@@ -87,18 +88,18 @@ public enum Languages{
8788
TURKISH("tr"),
8889
ZULU("zu");
8990

90-
/*
91+
/**
9192
*Stores the LanguageCode
9293
*/
9394
private final String languageCode;
9495

95-
/*
96+
/**
9697
*Constructor
9798
*/
9899
private Languages(final String languageCode){
99100
this.languageCode = languageCode;
100101
}
101-
102+
102103
public String toString(){
103104
return languageCode;
104105
}

0 commit comments

Comments
 (0)