|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static java.lang.Math.*; |
|
|
import static java.lang.Math.*; |
|
|
|
|
|
|
|
|
import android.Manifest; |
|
|
|
|
|
import android.annotation.SuppressLint; |
|
|
import android.annotation.SuppressLint; |
|
|
import android.app.Activity; |
|
|
|
|
|
import android.content.Context; |
|
|
import android.content.Context; |
|
|
import android.content.pm.PackageManager; |
|
|
|
|
|
import android.media.AudioFormat; |
|
|
import android.media.AudioFormat; |
|
|
import android.media.AudioRecord; |
|
|
import android.media.AudioRecord; |
|
|
import android.media.MediaRecorder; |
|
|
import android.media.MediaRecorder; |
|
|
import android.os.AsyncTask; |
|
|
import android.os.AsyncTask; |
|
|
import android.util.Log; |
|
|
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.Complex; |
|
|
import com.example.ueberwachungssystem.Detection.Signalverarbeitung.FFT; |
|
|
import com.example.ueberwachungssystem.Detection.Signalverarbeitung.FFT; |
|
|
import com.example.ueberwachungssystem.Detection.DetectionReport; |
|
|
|
|
|
import com.example.ueberwachungssystem.Detection.Detector; |
|
|
|
|
|
|
|
|
|
|
|
public class MicrophoneDetector extends Detector { |
|
|
public class MicrophoneDetector extends Detector { |
|
|
/** |
|
|
/** |
|
|
|
|
|
|
|
|
* @param context |
|
|
* @param context |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
private static final int RECHTEANFORDERUNG_MIKROFON = 1; |
|
|
|
|
|
|
|
|
|
|
|
private AufnahmeTask aufnahmeTask; |
|
|
private AufnahmeTask aufnahmeTask; |
|
|
public boolean armed = false; |
|
|
public boolean armed = false; |
|
|
public int Schwellwert_Alarm = 100; |
|
|
|
|
|
|
|
|
public int schwellwertAlarm = 100; |
|
|
private Context context; |
|
|
private Context context; |
|
|
|
|
|
|
|
|
public MicrophoneDetector(Context context) { |
|
|
public MicrophoneDetector(Context context) { |
|
|
|
|
|
|
|
|
private final int sampleRateInHz = 44100; |
|
|
private final int sampleRateInHz = 44100; |
|
|
private final int channelConfig = AudioFormat.CHANNEL_IN_MONO; |
|
|
private final int channelConfig = AudioFormat.CHANNEL_IN_MONO; |
|
|
private final int audioFormat = AudioFormat.ENCODING_PCM_16BIT; |
|
|
private final int audioFormat = AudioFormat.ENCODING_PCM_16BIT; |
|
|
|
|
|
|
|
|
|
|
|
private final int startDelay = 20000; |
|
|
|
|
|
private final int threadSleeptime = 10; |
|
|
private int minPufferGroesseInBytes; |
|
|
private int minPufferGroesseInBytes; |
|
|
private int pufferGroesseInBytes; |
|
|
private int pufferGroesseInBytes; |
|
|
private RingPuffer ringPuffer = new RingPuffer(10); |
|
|
private RingPuffer ringPuffer = new RingPuffer(10); |
|
|
private float kalibierWert; |
|
|
private float kalibierWert; |
|
|
private com.example.ueberwachungssystem.Detection.DetectionReport detectionReport; |
|
|
private com.example.ueberwachungssystem.Detection.DetectionReport detectionReport; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("MissingPermission") |
|
|
@SuppressLint("MissingPermission") |
|
|
AufnahmeTask() { |
|
|
AufnahmeTask() { |
|
|
minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); |
|
|
minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.d("0","Konfiguration: "+ s); |
|
|
Log.d("0","Konfiguration: "+ s); |
|
|
|
|
|
|
|
|
int pufferGroesseInAnzahlAbtastwerten = pufferGroesseInBytes / anzahlBytesProAbtastwert; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Kalibrierung |
|
|
//Kalibrierung |
|
|
try { |
|
|
try { |
|
|
Thread.sleep(3000); // Time to lay down the phone |
|
|
|
|
|
|
|
|
Thread.sleep(startDelay); // Time to lay down the phone |
|
|
} catch (InterruptedException e) { |
|
|
} catch (InterruptedException e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Verarbeitungsergebnis kalibrierErgebnis = verarbeiten(puffer, n); |
|
|
Verarbeitungsergebnis kalibrierErgebnis = verarbeiten(puffer, n); |
|
|
kalibierWert += kalibrierErgebnis.maxAmp; |
|
|
kalibierWert += kalibrierErgebnis.maxAmp; |
|
|
try { |
|
|
try { |
|
|
Thread.sleep(50); |
|
|
|
|
|
|
|
|
Thread.sleep(threadSleeptime); |
|
|
} catch (InterruptedException e) { |
|
|
} catch (InterruptedException e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
publishProgress(ergebnis); |
|
|
publishProgress(ergebnis); |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
Thread.sleep(10); |
|
|
|
|
|
|
|
|
Thread.sleep(threadSleeptime); |
|
|
} catch (InterruptedException e) { |
|
|
} catch (InterruptedException e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ringPuffer.hinzufuegen(max); |
|
|
ringPuffer.hinzufuegen(max); |
|
|
maxAmp = ringPuffer.maximum(); |
|
|
maxAmp = ringPuffer.maximum(); |
|
|
if (maxAmp <= Schwellwert_Alarm+kalibierWert) { |
|
|
|
|
|
|
|
|
if (maxAmp <= schwellwertAlarm + kalibierWert) { |
|
|
armed = true; |
|
|
armed = true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
super.onProgressUpdate(progress); |
|
|
super.onProgressUpdate(progress); |
|
|
float maxAmpPrint = round(20*log10(abs(progress[0].maxAmp/1.0))); |
|
|
float maxAmpPrint = round(20*log10(abs(progress[0].maxAmp/1.0))); |
|
|
float kalibierWertPrint = round(20*log10(abs(kalibierWert))); |
|
|
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; |
|
|
armed = false; |
|
|
detectionReport = new DetectionReport(true, "Audio", maxAmpPrint); |
|
|
detectionReport = new DetectionReport(true, "Audio", maxAmpPrint); |
|
|
reportViolation("Audio", maxAmpPrint); |
|
|
reportViolation("Audio", maxAmpPrint); |
|
|
|
|
|
|
|
|
this.wichtungAlterWert = 1 - this.wichtungNeuerWert; |
|
|
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) { |
|
|
float mittel(float wert) { |
|
|
if (istMittelwertGesetzt) { |
|
|
if (istMittelwertGesetzt) { |
|
|
mittelwert = wert * wichtungNeuerWert + mittelwert * wichtungAlterWert; |
|
|
mittelwert = wert * wichtungNeuerWert + mittelwert * wichtungAlterWert; |