|
|
@@ -2,24 +2,16 @@ package com.example.ueberwachungssystem.Detection; |
|
|
|
|
|
|
|
import static java.lang.Math.*; |
|
|
|
|
|
|
|
import android.Manifest; |
|
|
|
import android.annotation.SuppressLint; |
|
|
|
import android.app.Activity; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.pm.PackageManager; |
|
|
|
import android.media.AudioFormat; |
|
|
|
import android.media.AudioRecord; |
|
|
|
import android.media.MediaRecorder; |
|
|
|
import android.os.AsyncTask; |
|
|
|
import android.util.Log; |
|
|
|
|
|
|
|
import androidx.core.app.ActivityCompat; |
|
|
|
import androidx.core.content.ContextCompat; |
|
|
|
|
|
|
|
import com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex; |
|
|
|
import com.example.ueberwachungssystem.Detection.Signalverarbeitung.FFT; |
|
|
|
import com.example.ueberwachungssystem.Detection.DetectionReport; |
|
|
|
import com.example.ueberwachungssystem.Detection.Detector; |
|
|
|
|
|
|
|
public class MicrophoneDetector extends Detector { |
|
|
|
/** |
|
|
@@ -28,11 +20,9 @@ public class MicrophoneDetector extends Detector { |
|
|
|
* @param context |
|
|
|
*/ |
|
|
|
|
|
|
|
private static final int RECHTEANFORDERUNG_MIKROFON = 1; |
|
|
|
|
|
|
|
private AufnahmeTask aufnahmeTask; |
|
|
|
public boolean armed = false; |
|
|
|
public int Schwellwert_Alarm = 100; |
|
|
|
public int schwellwertAlarm = 100; |
|
|
|
private Context context; |
|
|
|
|
|
|
|
public MicrophoneDetector(Context context) { |
|
|
@@ -58,12 +48,17 @@ public class MicrophoneDetector extends Detector { |
|
|
|
private final int sampleRateInHz = 44100; |
|
|
|
private final int channelConfig = AudioFormat.CHANNEL_IN_MONO; |
|
|
|
private final int audioFormat = AudioFormat.ENCODING_PCM_16BIT; |
|
|
|
|
|
|
|
private final int startDelay = 20000; |
|
|
|
private final int threadSleeptime = 10; |
|
|
|
private int minPufferGroesseInBytes; |
|
|
|
private int pufferGroesseInBytes; |
|
|
|
private RingPuffer ringPuffer = new RingPuffer(10); |
|
|
|
private float kalibierWert; |
|
|
|
private com.example.ueberwachungssystem.Detection.DetectionReport detectionReport; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("MissingPermission") |
|
|
|
AufnahmeTask() { |
|
|
|
minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); |
|
|
@@ -112,8 +107,6 @@ public class MicrophoneDetector extends Detector { |
|
|
|
|
|
|
|
Log.d("0","Konfiguration: "+ s); |
|
|
|
|
|
|
|
int pufferGroesseInAnzahlAbtastwerten = pufferGroesseInBytes / anzahlBytesProAbtastwert; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
@@ -129,7 +122,7 @@ public class MicrophoneDetector extends Detector { |
|
|
|
|
|
|
|
//Kalibrierung |
|
|
|
try { |
|
|
|
Thread.sleep(3000); // Time to lay down the phone |
|
|
|
Thread.sleep(startDelay); // Time to lay down the phone |
|
|
|
} catch (InterruptedException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
@@ -139,7 +132,7 @@ public class MicrophoneDetector extends Detector { |
|
|
|
Verarbeitungsergebnis kalibrierErgebnis = verarbeiten(puffer, n); |
|
|
|
kalibierWert += kalibrierErgebnis.maxAmp; |
|
|
|
try { |
|
|
|
Thread.sleep(50); |
|
|
|
Thread.sleep(threadSleeptime); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
@@ -193,7 +186,7 @@ public class MicrophoneDetector extends Detector { |
|
|
|
publishProgress(ergebnis); |
|
|
|
|
|
|
|
try { |
|
|
|
Thread.sleep(10); |
|
|
|
Thread.sleep(threadSleeptime); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
@@ -221,7 +214,7 @@ public class MicrophoneDetector extends Detector { |
|
|
|
|
|
|
|
ringPuffer.hinzufuegen(max); |
|
|
|
maxAmp = ringPuffer.maximum(); |
|
|
|
if (maxAmp <= Schwellwert_Alarm+kalibierWert) { |
|
|
|
if (maxAmp <= schwellwertAlarm + kalibierWert) { |
|
|
|
armed = true; |
|
|
|
} |
|
|
|
} |
|
|
@@ -234,10 +227,10 @@ public class MicrophoneDetector extends Detector { |
|
|
|
super.onProgressUpdate(progress); |
|
|
|
float maxAmpPrint = round(20*log10(abs(progress[0].maxAmp/1.0))); |
|
|
|
float kalibierWertPrint = round(20*log10(abs(kalibierWert))); |
|
|
|
Log.d("0","VR, Max, Kal:" + progress[0].verarbeitungsrate + ", " + maxAmpPrint |
|
|
|
+ " dB, " + kalibierWertPrint + " dB"); |
|
|
|
Log.d("alarmAudio","VR: " + progress[0].verarbeitungsrate + ", Amp: " + maxAmpPrint |
|
|
|
+ " dB, Kal: " + kalibierWertPrint + " dB"); |
|
|
|
|
|
|
|
if (progress[0].maxAmp >= Schwellwert_Alarm+kalibierWert && armed == true) { |
|
|
|
if (progress[0].maxAmp >= schwellwertAlarm + kalibierWert && armed == true) { |
|
|
|
armed = false; |
|
|
|
detectionReport = new DetectionReport(true, "Audio", maxAmpPrint); |
|
|
|
reportViolation("Audio", maxAmpPrint); |
|
|
@@ -359,16 +352,6 @@ public class MicrophoneDetector extends Detector { |
|
|
|
this.wichtungAlterWert = 1 - this.wichtungNeuerWert; |
|
|
|
} |
|
|
|
|
|
|
|
float MittelwertPuffer(short[] puffer) { |
|
|
|
|
|
|
|
for (int i = 0; i < puffer.length; i++) { |
|
|
|
mittelwert = Math.abs(puffer[i]); |
|
|
|
} |
|
|
|
mittelwert = mittelwert/puffer.length; |
|
|
|
|
|
|
|
return mittelwert; |
|
|
|
} |
|
|
|
|
|
|
|
float mittel(float wert) { |
|
|
|
if (istMittelwertGesetzt) { |
|
|
|
mittelwert = wert * wichtungNeuerWert + mittelwert * wichtungAlterWert; |