Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write to file test #14

Merged
merged 5 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions app/src/main/java/com/example/projectnoise/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import android.Manifest;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
Expand Down Expand Up @@ -53,7 +57,9 @@ private void createNotificationChannel() {
}


/** Helper functions to establish mic permissions **/
/**
* Helper functions to establish mic permissions
**/

private void setupPermissions() {
if (ContextCompat.checkSelfPermission(this,
Expand All @@ -71,4 +77,4 @@ protected void makeRequest() {
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
Expand All @@ -24,8 +26,13 @@

import org.jtransforms.fft.DoubleFFT_1D;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MeasureService extends Service {
public static final String CHANNEL_ID = "MeasureServiceChannel";
private static final String FILE_NAME = "example.txt";


@Override
Expand Down Expand Up @@ -151,23 +158,23 @@ public void run() {
// Continuously read audio into buffer for measureTime ms
while (SystemClock.uptimeMillis() < measureTime) {
recorder.read(buffer, 0, bufferSize);

//os.write(buffer, 0, buffer.length); for writing data to output file; buffer must be byte

dB = doFFT(buffer); // Perform Fast Fourier Transform
if (dB != Double.NEGATIVE_INFINITY) {
dbSumTotal += dB;
count++;
}
average = 20 * Math.log10(dbSumTotal / count) + 8.25 + calibration;
instant = 20 * Math.log10(dB) + 8.25 + calibration;
// Log.i(TAG, "instant: " + instant);
// Log.i(TAG, "average: " + average);
// instant = 20 * Math.log10(dB) + 8.25 + calibration;
}

recorder.stop();
Log.i(TAG, "Average dB over " + interval + " seconds: " + average);
// TODO export average and time to file

String log = "Average dB over " + (interval / 1000) + " seconds: " + average;
Log.i(TAG, log);
write(log);

// send data to home fragment
long endTime = SystemClock.uptimeMillis();
long wait = 10000 - (endTime - startTime);
Log.d(TAG, "Waiting for " + wait/(long) 1000 + " seconds");
Expand Down Expand Up @@ -235,4 +242,30 @@ private double doFFT(short[] rawData) {
}
return avg / rawData.length;
}

public void write(String text){

FileOutputStream fos = null;

try {
fos = openFileOutput(FILE_NAME, MODE_APPEND);
fos.write(text.getBytes());


Toast.makeText(this,"Saved to " + getFilesDir() + "/" + FILE_NAME, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}


}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath 'com.android.tools.build:gradle:4.1.2'

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