14
14
import android .os .IBinder ;
15
15
import android .os .SystemClock ;
16
16
import android .util .Log ;
17
+ import android .view .View ;
18
+ import android .widget .Toast ;
17
19
18
20
import androidx .annotation .Nullable ;
19
21
import androidx .core .app .NotificationCompat ;
24
26
25
27
import org .jtransforms .fft .DoubleFFT_1D ;
26
28
29
+ import java .io .FileNotFoundException ;
30
+ import java .io .FileOutputStream ;
31
+ import java .io .IOException ;
32
+
27
33
public class MeasureService extends Service {
28
34
public static final String CHANNEL_ID = "MeasureServiceChannel" ;
35
+ private static final String FILE_NAME = "example.txt" ;
29
36
30
37
31
38
@ Override
@@ -151,23 +158,23 @@ public void run() {
151
158
// Continuously read audio into buffer for measureTime ms
152
159
while (SystemClock .uptimeMillis () < measureTime ) {
153
160
recorder .read (buffer , 0 , bufferSize );
154
-
155
161
//os.write(buffer, 0, buffer.length); for writing data to output file; buffer must be byte
156
-
157
162
dB = doFFT (buffer ); // Perform Fast Fourier Transform
158
163
if (dB != Double .NEGATIVE_INFINITY ) {
159
164
dbSumTotal += dB ;
160
165
count ++;
161
166
}
162
167
average = 20 * Math .log10 (dbSumTotal / count ) + 8.25 + calibration ;
163
- instant = 20 * Math .log10 (dB ) + 8.25 + calibration ;
164
- // Log.i(TAG, "instant: " + instant);
165
- // Log.i(TAG, "average: " + average);
168
+ // instant = 20 * Math.log10(dB) + 8.25 + calibration;
166
169
}
170
+
167
171
recorder .stop ();
168
- Log .i (TAG , "Average dB over " + interval + " seconds: " + average );
169
- // TODO export average and time to file
170
172
173
+ String log = "Average dB over " + (interval / 1000 ) + " seconds: " + average ;
174
+ Log .i (TAG , log );
175
+ write (log );
176
+
177
+ // send data to home fragment
171
178
long endTime = SystemClock .uptimeMillis ();
172
179
long wait = 10000 - (endTime - startTime );
173
180
Log .d (TAG , "Waiting for " + wait /(long ) 1000 + " seconds" );
@@ -235,4 +242,30 @@ private double doFFT(short[] rawData) {
235
242
}
236
243
return avg / rawData .length ;
237
244
}
245
+
246
+ public void write (String text ){
247
+
248
+ FileOutputStream fos = null ;
249
+
250
+ try {
251
+ fos = openFileOutput (FILE_NAME , MODE_APPEND );
252
+ fos .write (text .getBytes ());
253
+
254
+
255
+ Toast .makeText (this ,"Saved to " + getFilesDir () + "/" + FILE_NAME , Toast .LENGTH_LONG ).show ();
256
+ } catch (IOException e ) {
257
+ e .printStackTrace ();
258
+ } finally {
259
+ if (fos !=null ){
260
+ try {
261
+ fos .close ();
262
+ } catch (IOException e ) {
263
+ e .printStackTrace ();
264
+ }
265
+ }
266
+ }
267
+
268
+ }
269
+
270
+
238
271
}
0 commit comments