Skip to content

Commit 344511b

Browse files
authored
Merge pull request #14 from imabigdipper/WriteToFileTest
2 parents c674dab + 2012981 commit 344511b

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

.idea/gradle.xml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/example/projectnoise/MainActivity.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import android.Manifest;
44
import android.app.NotificationChannel;
55
import android.app.NotificationManager;
6+
import android.content.BroadcastReceiver;
7+
import android.content.Context;
8+
import android.content.Intent;
9+
import android.content.IntentFilter;
610
import android.content.pm.PackageManager;
711
import android.os.Bundle;
812
import android.util.Log;
@@ -53,7 +57,9 @@ private void createNotificationChannel() {
5357
}
5458

5559

56-
/** Helper functions to establish mic permissions **/
60+
/**
61+
* Helper functions to establish mic permissions
62+
**/
5763

5864
private void setupPermissions() {
5965
if (ContextCompat.checkSelfPermission(this,
@@ -71,4 +77,4 @@ protected void makeRequest() {
7177
}
7278

7379

74-
}
80+
}

app/src/main/java/com/example/projectnoise/services/MeasureService.java

+40-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import android.os.IBinder;
1515
import android.os.SystemClock;
1616
import android.util.Log;
17+
import android.view.View;
18+
import android.widget.Toast;
1719

1820
import androidx.annotation.Nullable;
1921
import androidx.core.app.NotificationCompat;
@@ -24,8 +26,13 @@
2426

2527
import org.jtransforms.fft.DoubleFFT_1D;
2628

29+
import java.io.FileNotFoundException;
30+
import java.io.FileOutputStream;
31+
import java.io.IOException;
32+
2733
public class MeasureService extends Service {
2834
public static final String CHANNEL_ID = "MeasureServiceChannel";
35+
private static final String FILE_NAME = "example.txt";
2936

3037

3138
@Override
@@ -151,23 +158,23 @@ public void run() {
151158
// Continuously read audio into buffer for measureTime ms
152159
while (SystemClock.uptimeMillis() < measureTime) {
153160
recorder.read(buffer, 0, bufferSize);
154-
155161
//os.write(buffer, 0, buffer.length); for writing data to output file; buffer must be byte
156-
157162
dB = doFFT(buffer); // Perform Fast Fourier Transform
158163
if (dB != Double.NEGATIVE_INFINITY) {
159164
dbSumTotal += dB;
160165
count++;
161166
}
162167
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;
166169
}
170+
167171
recorder.stop();
168-
Log.i(TAG, "Average dB over " + interval + " seconds: " + average);
169-
// TODO export average and time to file
170172

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
171178
long endTime = SystemClock.uptimeMillis();
172179
long wait = 10000 - (endTime - startTime);
173180
Log.d(TAG, "Waiting for " + wait/(long) 1000 + " seconds");
@@ -235,4 +242,30 @@ private double doFFT(short[] rawData) {
235242
}
236243
return avg / rawData.length;
237244
}
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+
238271
}

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath "com.android.tools.build:gradle:4.1.1"
8+
classpath 'com.android.tools.build:gradle:4.1.2'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

0 commit comments

Comments
 (0)