@@ -137,20 +137,28 @@ public void captureAudioToFile(String audioFile) throws Exception {
137
137
138
138
/**
139
139
* 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
143
142
*/
144
143
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 ));
146
154
}
147
155
148
156
/**
149
157
* Gets the volume of microphone input
150
158
* @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.
152
160
*/
153
- public int getAudioVolume (int numOfBytes ){
161
+ private int calculateAudioVolume (int numOfBytes ){
154
162
if (getTargetDataLine ()!=null ){
155
163
byte [] data = new byte [numOfBytes ];
156
164
this .getTargetDataLine ().read (data , 0 , numOfBytes );
@@ -162,7 +170,7 @@ public int getAudioVolume(int numOfBytes){
162
170
}
163
171
164
172
/**
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
166
174
* @param audioData The byte[] you want to determine the volume of
167
175
* @return the calculated volume of audioData
168
176
*/
@@ -181,7 +189,24 @@ private int calculateRMSLevel(byte[] audioData){
181
189
return (int )(Math .pow (averageMeanSquare ,0.5d ) + 0.5 );
182
190
}
183
191
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
+
185
210
/**
186
211
* The audio format to save in
187
212
*
0 commit comments