|
|
@@ -3,6 +3,7 @@ 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; |
|
|
@@ -17,8 +18,8 @@ import androidx.core.content.ContextCompat; |
|
|
|
|
|
|
|
import com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex; |
|
|
|
import com.example.ueberwachungssystem.Detection.Signalverarbeitung.FFT; |
|
|
|
import com.example.ueberwachungssystem.Detector.DetectionReport; |
|
|
|
import com.example.ueberwachungssystem.Detector.Detector; |
|
|
|
import com.example.ueberwachungssystem.Detection.DetectionReport; |
|
|
|
import com.example.ueberwachungssystem.Detection.Detector; |
|
|
|
|
|
|
|
public class MicrophoneDetector extends Detector { |
|
|
|
/** |
|
|
@@ -32,27 +33,17 @@ public class MicrophoneDetector extends Detector { |
|
|
|
private AufnahmeTask aufnahmeTask; |
|
|
|
public boolean armed = false; |
|
|
|
public int Schwellwert_Alarm = 100; |
|
|
|
private Activity MainActivityForClass; |
|
|
|
private Context context; |
|
|
|
|
|
|
|
public MicrophoneDetector(Context context) { |
|
|
|
super(context); |
|
|
|
MainActivityForClass = (Activity) context; |
|
|
|
|
|
|
|
if (!istZugriffAufMikrofonErlaubt()) { |
|
|
|
zugriffAufMikrofonAnfordern(); |
|
|
|
} |
|
|
|
super(); |
|
|
|
this.context = context; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void startDetection() { |
|
|
|
|
|
|
|
if (!istZugriffAufMikrofonErlaubt()) { |
|
|
|
zugriffAufMikrofonAnfordern(); |
|
|
|
} |
|
|
|
if (istZugriffAufMikrofonErlaubt()) { |
|
|
|
aufnahmeTask = new AufnahmeTask(); |
|
|
|
aufnahmeTask.execute(); |
|
|
|
} |
|
|
|
aufnahmeTask = new AufnahmeTask(); |
|
|
|
aufnahmeTask.execute(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
@@ -62,20 +53,6 @@ public class MicrophoneDetector extends Detector { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private boolean istZugriffAufMikrofonErlaubt() { |
|
|
|
if (ContextCompat.checkSelfPermission(MainActivityForClass, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { |
|
|
|
Log.d("0","Zugriff auf Mikrofon ist verboten."); |
|
|
|
return false; |
|
|
|
} else { |
|
|
|
Log.d("0","Zugriff auf Mikrofon ist erlaubt."); |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void zugriffAufMikrofonAnfordern() { |
|
|
|
ActivityCompat.requestPermissions(MainActivityForClass, new String[]{Manifest.permission.RECORD_AUDIO}, RECHTEANFORDERUNG_MIKROFON); |
|
|
|
} |
|
|
|
|
|
|
|
class AufnahmeTask extends AsyncTask<Long, Verarbeitungsergebnis, Void> { |
|
|
|
private AudioRecord recorder; |
|
|
|
private final int sampleRateInHz = 44100; |
|
|
@@ -85,24 +62,17 @@ public class MicrophoneDetector extends Detector { |
|
|
|
private int pufferGroesseInBytes; |
|
|
|
private RingPuffer ringPuffer = new RingPuffer(10); |
|
|
|
private float kalibierWert; |
|
|
|
private com.example.ueberwachungssystem.Detector.DetectionReport detectionReport; |
|
|
|
private com.example.ueberwachungssystem.Detection.DetectionReport detectionReport; |
|
|
|
|
|
|
|
@SuppressLint("MissingPermission") |
|
|
|
AufnahmeTask() { |
|
|
|
minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); |
|
|
|
pufferGroesseInBytes = minPufferGroesseInBytes * 2; |
|
|
|
if (ActivityCompat.checkSelfPermission(MainActivityForClass, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { |
|
|
|
// TODO: Consider calling |
|
|
|
// ActivityCompat#requestPermissions |
|
|
|
// here to request the missing permissions, and then overriding |
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions, |
|
|
|
// int[] grantResults) |
|
|
|
// to handle the case where the user grants the permission. See the documentation |
|
|
|
// for ActivityCompat#requestPermissions for more details. |
|
|
|
ActivityCompat.requestPermissions(MainActivityForClass, new String[]{Manifest.permission.RECORD_AUDIO}, RECHTEANFORDERUNG_MIKROFON); |
|
|
|
try { |
|
|
|
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, pufferGroesseInBytes); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, pufferGroesseInBytes); |
|
|
|
|
|
|
|
Log.d("0","Puffergroeße: "+ minPufferGroesseInBytes + " " + pufferGroesseInBytes); |
|
|
|
Log.d("0","Recorder (SR, CH): "+ recorder.getSampleRate() + " " + recorder.getChannelCount()); |
|
|
|
|
|
|
@@ -143,7 +113,6 @@ public class MicrophoneDetector extends Detector { |
|
|
|
Log.d("0","Konfiguration: "+ s); |
|
|
|
|
|
|
|
int pufferGroesseInAnzahlAbtastwerten = pufferGroesseInBytes / anzahlBytesProAbtastwert; |
|
|
|
int pufferGroesseInMillisekunden = 1000 * pufferGroesseInAnzahlAbtastwerten / recorder.getSampleRate(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
@@ -199,6 +168,7 @@ public class MicrophoneDetector extends Detector { |
|
|
|
int n = recorder.read(puffer, 0, puffer.length); |
|
|
|
Verarbeitungsergebnis ergebnis = verarbeiten(puffer, n); |
|
|
|
anzahlVerarbeitet += n; |
|
|
|
|
|
|
|
// __Part of FFT__ |
|
|
|
// spektrum = calculateFFT(puffer); |
|
|
|
// LineGraphSeries<DataPoint> newseries = new LineGraphSeries<DataPoint>(new DataPoint[]{}); |
|
|
@@ -269,8 +239,8 @@ public class MicrophoneDetector extends Detector { |
|
|
|
|
|
|
|
if (progress[0].maxAmp >= Schwellwert_Alarm+kalibierWert && armed == true) { |
|
|
|
armed = false; |
|
|
|
detectionReport = new DetectionReport("Mic1", "Audio", maxAmpPrint); |
|
|
|
reportViolation("Mic1", "Audio", maxAmpPrint); |
|
|
|
detectionReport = new DetectionReport(true, "Audio", maxAmpPrint); |
|
|
|
reportViolation("Audio", maxAmpPrint); |
|
|
|
Log.d("1",detectionReport.toString()); |
|
|
|
} |
|
|
|
} |
|
|
@@ -286,7 +256,6 @@ public class MicrophoneDetector extends Detector { |
|
|
|
} |
|
|
|
|
|
|
|
final int mNumberOfFFTPoints =1024; |
|
|
|
double mMaxFFTSample; |
|
|
|
|
|
|
|
double temp; |
|
|
|
Complex[] y; |
|
|
@@ -300,15 +269,9 @@ public class MicrophoneDetector extends Detector { |
|
|
|
|
|
|
|
y = FFT.fft(complexSignal); |
|
|
|
|
|
|
|
mMaxFFTSample = 0.0; |
|
|
|
for(int i = 0; i < (mNumberOfFFTPoints/2); i++) |
|
|
|
{ |
|
|
|
absSignal[i] = y[i].abs(); |
|
|
|
// absSignal[i] = Math.sqrt(Math.pow(y[i].re(), 2) + Math.pow(y[i].im(), 2)); |
|
|
|
// if(absSignal[i] > mMaxFFTSample) |
|
|
|
// { |
|
|
|
// mMaxFFTSample = absSignal[i]; |
|
|
|
// } |
|
|
|
} |
|
|
|
|
|
|
|
return absSignal; |