<manifest xmlns:android="http://schemas.android.com/apk/res/android" | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||||
xmlns:tools="http://schemas.android.com/tools"> | xmlns:tools="http://schemas.android.com/tools"> | ||||
<uses-permission android:name="android.permission.RECORD_AUDIO"/> | |||||
<uses-permission android:name="android.permission.CAMERA"/> | |||||
<!--<uses-permission android:name="android.permission.BLUETOOTH"/>--> | |||||
<!--<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>--> | |||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | |||||
<uses-permission android:name="android.permission.INTERNET" /> | |||||
<application | <application | ||||
android:allowBackup="true" | android:allowBackup="true" | ||||
android:dataExtractionRules="@xml/data_extraction_rules" | android:dataExtractionRules="@xml/data_extraction_rules" |
package com.example.ueberwachungssystem; | |||||
import android.util.Log; | |||||
import java.util.Calendar; | |||||
/** Detection Report Class */ | |||||
public class DetectionReport { | |||||
public String timeStamp; | |||||
public String detectionType; | |||||
public float detectedValue; | |||||
public String detectorID; | |||||
public DetectionReport(String detectorID, String detectionType, float detectedAmplitude) { | |||||
this.timeStamp = String.valueOf(Calendar.getInstance().getTime()); | |||||
this.detectionType = detectionType; | |||||
this.detectedValue = detectedAmplitude; | |||||
this.detectorID = detectorID; | |||||
} | |||||
/** Get Detection Report in String format */ | |||||
public String toString() { | |||||
String time = "Time: " + "[" + this.timeStamp + "]"; | |||||
String type = "Type: " + "[" + this.detectionType + "]"; | |||||
String value = "Value: " + "[" + this.detectedValue + "]"; | |||||
String id = "ID: " + "[" + this.detectorID + "]"; | |||||
return String.join("\t", time, type, value, id); | |||||
} | |||||
/** Debug Report */ | |||||
public void log(String tag) { | |||||
Log.d(tag, this.toString()); | |||||
} | |||||
} |
package com.example.ueberwachungssystem; | |||||
import android.app.Activity; | |||||
import android.content.Context; | |||||
import androidx.annotation.NonNull; | |||||
abstract public class Detector { | |||||
private OnDetectionListener listener; | |||||
/** Constructor - takes context of current activity */ | |||||
public Detector(Context context) {}; | |||||
/** On Detection Listener - runs when violation is reported */ | |||||
public interface OnDetectionListener { | |||||
void onDetection(@NonNull DetectionReport detectionReport); | |||||
} | |||||
public void setOnDetectionListener(@NonNull OnDetectionListener listener) { | |||||
this.listener = listener; | |||||
} | |||||
/** Triggers onDetectionListener - call this to trigger violation/alarm */ | |||||
private void reportViolation(String detectorID, String detectionType, float amplitude) { | |||||
if (listener != null) { | |||||
DetectionReport detectionReport = new DetectionReport(detectorID, detectionType, amplitude); | |||||
listener.onDetection(detectionReport); | |||||
} else { | |||||
throw new IllegalStateException("No listener set for violation reporting"); | |||||
} | |||||
} | |||||
/** Starts Detection (abstract method: needs to be overridden in child class) */ | |||||
public abstract void startDetection(); | |||||
/** Stops Detection (abstract method: needs to be overridden in child class) */ | |||||
public abstract void stopDetection(); | |||||
} |
package com.example.ueberwachungssystem; | package com.example.ueberwachungssystem; | ||||
import androidx.appcompat.app.AppCompatActivity; | import androidx.appcompat.app.AppCompatActivity; | ||||
import androidx.core.app.ActivityCompat; | |||||
import android.Manifest; | |||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.view.View; | |||||
import android.widget.Button; | |||||
import android.widget.Switch; | |||||
import android.widget.TextView; | |||||
import android.widget.ToggleButton; | |||||
import com.example.ueberwachungssystem.Mikrofon; | |||||
import com.example.ueberwachungssystem.logger.Logger; | |||||
public class MainActivity extends AppCompatActivity { | public class MainActivity extends AppCompatActivity { | ||||
Logger logger; | |||||
private TextView tv_log; | |||||
Mikrofon Mikrofon_1= new Mikrofon(); | |||||
MicrophoneDetector Mic; | |||||
private Switch TglBtn_Mic; | |||||
@Override | @Override | ||||
protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
setContentView(R.layout.activity_main); | setContentView(R.layout.activity_main); | ||||
TextView textView = new TextView(this); | |||||
tv_log = (TextView) findViewById(R.id.tv_log); //Set textview for showing logged content | |||||
logger = new Logger(this.getClass().getSimpleName(), tv_log, ""); | |||||
logger.log(this.getClass().getSimpleName() + ".onCreate"); | |||||
Mic = new MicrophoneDetector(this, logger); | |||||
setupMic(); | |||||
} | |||||
@Override | |||||
public void onResume() { | |||||
super.onResume(); | |||||
Mikrofon_1.onResume(); | |||||
logger.log(this.getClass().getSimpleName() + ".onResume"); | |||||
} | |||||
@Override | |||||
public void onPause() { | |||||
super.onPause(); | |||||
Mikrofon_1.onPause(); | |||||
logger.log(this.getClass().getSimpleName() + ".onPause"); | |||||
} | |||||
private void setupMic() { | |||||
Mikrofon_1.onCreate(this, logger); | |||||
TglBtn_Mic = (Switch) findViewById(R.id.TglBtn_Mic); | |||||
TglBtn_Mic.setOnClickListener(new View.OnClickListener() { | |||||
@Override | |||||
public void onClick(View v) { | |||||
if (v == TglBtn_Mic) { | |||||
logger.log("onClick toggleButtonThread " + TglBtn_Mic.isChecked()); | |||||
if (TglBtn_Mic.isChecked()) { | |||||
Mikrofon_1.onResume(); | |||||
} else { | |||||
Mikrofon_1.onPause(); | |||||
} | |||||
} | |||||
} | |||||
}); | |||||
} | } | ||||
} | } |
package com.example.ueberwachungssystem; | |||||
import android.Manifest; | |||||
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 androidx.core.app.ActivityCompat; | |||||
import androidx.core.content.ContextCompat; | |||||
import com.example.ueberwachungssystem.logger.Logger; | |||||
public class MicrophoneDetector extends Detector{ | |||||
/** | |||||
* Constructor - takes context of current activity | |||||
* | |||||
* @param context | |||||
*/ | |||||
private static final int RECHTEANFORDERUNG_MIKROFON = 1; | |||||
private AufnahmeTask aufnahmeTask; | |||||
public boolean Alarm = false; | |||||
public int Schwellwert_Alarm = 500; | |||||
Logger logger; | |||||
private Activity MainActivityForClass; | |||||
public MicrophoneDetector(Context context, Logger MainLogger) { | |||||
super(context); | |||||
MainActivityForClass = (Activity) context; | |||||
logger = MainLogger; //Class uses the same logger as the MainActivity | |||||
logger.log(this.getClass().getSimpleName() + ".onCreate"); | |||||
if (!istZugriffAufMikrofonErlaubt()) { | |||||
zugriffAufMikrofonAnfordern(); | |||||
} | |||||
} | |||||
@Override | |||||
public void startDetection() { | |||||
logger.log(this.getClass().getSimpleName() + ".startDetection"); | |||||
if (!istZugriffAufMikrofonErlaubt()) { | |||||
zugriffAufMikrofonAnfordern(); | |||||
} | |||||
if (istZugriffAufMikrofonErlaubt()) { | |||||
aufnahmeTask = new AufnahmeTask(); | |||||
aufnahmeTask.execute(); | |||||
} | |||||
} | |||||
@Override | |||||
public void stopDetection() { | |||||
logger.log(this.getClass().getSimpleName() + ".stopDetection"); | |||||
if(aufnahmeTask!=null) { | |||||
aufnahmeTask.cancel(true); | |||||
// aufnahmeTask = null; // if aufnahmeTask = null, break in for loop would not work (Nullpointer Exception) | |||||
} | |||||
} | |||||
private boolean istZugriffAufMikrofonErlaubt() { | |||||
if (ContextCompat.checkSelfPermission(MainActivityForClass, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { | |||||
logger.log("Zugriff auf Mikrofon ist verboten."); | |||||
return false; | |||||
} else { | |||||
logger.log("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; | |||||
private final int channelConfig = AudioFormat.CHANNEL_IN_MONO; | |||||
private final int audioFormat = AudioFormat.ENCODING_PCM_16BIT; | |||||
private int minPufferGroesseInBytes; | |||||
private int pufferGroesseInBytes; | |||||
private RingPuffer ringPuffer = new RingPuffer(10); | |||||
AufnahmeTask() { | |||||
minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); | |||||
pufferGroesseInBytes = minPufferGroesseInBytes * 2; | |||||
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, pufferGroesseInBytes); | |||||
// textViewMinPufferGroesseInBytes.setText("" + minPufferGroesseInBytes); | |||||
// textViewPufferGroesseInBytes.setText("" + pufferGroesseInBytes); | |||||
// textViewAbtastrate.setText("" + recorder.getSampleRate()); | |||||
// textViewAnzahlKanaele.setText("" + recorder.getChannelCount()); | |||||
logger.log("Puffergroeße: "+ minPufferGroesseInBytes + " " + pufferGroesseInBytes); | |||||
logger.log("Recorder (SR, CH): "+ recorder.getSampleRate() + " " + recorder.getChannelCount()); | |||||
int anzahlBytesProAbtastwert; | |||||
String s; | |||||
switch (recorder.getAudioFormat()) { | |||||
case AudioFormat.ENCODING_PCM_8BIT: | |||||
s = "8 Bit PCM "; | |||||
anzahlBytesProAbtastwert = 1; | |||||
break; | |||||
case AudioFormat.ENCODING_PCM_16BIT: | |||||
s = "16 Bit PCM"; | |||||
anzahlBytesProAbtastwert = 2; | |||||
break; | |||||
case AudioFormat.ENCODING_PCM_FLOAT: | |||||
s = "Float PCM"; | |||||
anzahlBytesProAbtastwert = 4; | |||||
break; | |||||
default: | |||||
throw new IllegalArgumentException(); | |||||
} | |||||
// textViewAudioFormat.setText(s); | |||||
switch (recorder.getChannelConfiguration()) { | |||||
case AudioFormat.CHANNEL_IN_MONO: | |||||
s = "Mono"; | |||||
break; | |||||
case AudioFormat.CHANNEL_IN_STEREO: | |||||
s = "Stereo"; | |||||
anzahlBytesProAbtastwert *= 2; | |||||
break; | |||||
case AudioFormat.CHANNEL_INVALID: | |||||
s = "ungültig"; | |||||
break; | |||||
default: | |||||
throw new IllegalArgumentException(); | |||||
} | |||||
// textViewKanalKonfiguration.setText(s); | |||||
logger.log("Konfiguration: "+ s); | |||||
int pufferGroesseInAnzahlAbtastwerten = pufferGroesseInBytes / anzahlBytesProAbtastwert; | |||||
int pufferGroesseInMillisekunden = 1000 * pufferGroesseInAnzahlAbtastwerten / recorder.getSampleRate(); | |||||
// textViewPufferGroesseInAnzahlAbtastwerte.setText("" + pufferGroesseInAnzahlAbtastwerten); | |||||
// textViewPufferGroesseInMillisekunden.setText("" + pufferGroesseInMillisekunden); | |||||
} | |||||
@Override | |||||
protected Void doInBackground(Long... params) { | |||||
recorder.startRecording(); | |||||
short[] puffer = new short[pufferGroesseInBytes / 2]; | |||||
long lastTime = System.currentTimeMillis(); | |||||
float verarbeitungsrate = 0; | |||||
final int maxZaehlerZeitMessung = 10; | |||||
int zaehlerZeitMessung = 0; | |||||
int anzahlVerarbeitet = 0; | |||||
GleitenderMittelwert gleitenderMittelwert = new GleitenderMittelwert(0.3f); | |||||
for (; ; ) { | |||||
if (aufnahmeTask.isCancelled()) { | |||||
break; | |||||
} else { | |||||
int n = recorder.read(puffer, 0, puffer.length); | |||||
Verarbeitungsergebnis ergebnis = verarbeiten(puffer, n); | |||||
anzahlVerarbeitet += n; | |||||
zaehlerZeitMessung++; | |||||
if (zaehlerZeitMessung == maxZaehlerZeitMessung) { | |||||
long time = System.currentTimeMillis(); | |||||
long deltaTime = time - lastTime; | |||||
verarbeitungsrate = 1000.0f * anzahlVerarbeitet / deltaTime; | |||||
verarbeitungsrate = gleitenderMittelwert.mittel(verarbeitungsrate); | |||||
zaehlerZeitMessung = 0; | |||||
anzahlVerarbeitet = 0; | |||||
lastTime = time; | |||||
} | |||||
float noiseLevel = gleitenderMittelwert.MittelwertPuffer(puffer); | |||||
// logger.log("Noise Level:" + noiseLevel); | |||||
ergebnis.noiseLevel = noiseLevel; | |||||
ergebnis.verarbeitungsrate = (int) verarbeitungsrate; | |||||
publishProgress(ergebnis); | |||||
try { | |||||
Thread.sleep(100); | |||||
} catch (InterruptedException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
} | |||||
recorder.release(); | |||||
return null; | |||||
} | |||||
private Verarbeitungsergebnis verarbeiten(short[] daten, int n) { | |||||
String status; | |||||
short maxAmp = -1; | |||||
if (n == AudioRecord.ERROR_INVALID_OPERATION) { | |||||
status = "ERROR_INVALID_OPERATION"; | |||||
} else if (n == AudioRecord.ERROR_BAD_VALUE) { | |||||
status = "ERROR_BAD_VALUE"; | |||||
} else { | |||||
status = "OK"; | |||||
short max = 0; | |||||
for (int i = 0; i < n; i++) { | |||||
if (daten[i] > max) { | |||||
max = daten[i]; | |||||
} | |||||
} | |||||
ringPuffer.hinzufuegen(max); | |||||
maxAmp = ringPuffer.maximum(); | |||||
} | |||||
return new Verarbeitungsergebnis(status, maxAmp, 0, 0); | |||||
} | |||||
@Override | |||||
protected void onProgressUpdate(Verarbeitungsergebnis... progress) { | |||||
super.onProgressUpdate(progress); | |||||
// textViewMaxAmp.setText("" + progress[0].maxAmp); | |||||
// textViewVerarbeitungsrate.setText("" + progress[0].verarbeitungsrate); | |||||
logger.overwriteLastlog("VR, Max, NL:" + progress[0].verarbeitungsrate + ", " + progress[0].maxAmp | |||||
+ ", " + progress[0].noiseLevel); | |||||
if (progress[0].maxAmp >= Schwellwert_Alarm) { | |||||
Alarm = true; | |||||
} | |||||
} | |||||
} | |||||
class Verarbeitungsergebnis { | |||||
String status; | |||||
short maxAmp; | |||||
int verarbeitungsrate; | |||||
float noiseLevel; | |||||
Verarbeitungsergebnis(String status, short maxAmp, int verarbeitungsrate, float noiseLevel) { | |||||
this.status = status; | |||||
this.maxAmp = maxAmp; | |||||
this.verarbeitungsrate = verarbeitungsrate; | |||||
this.noiseLevel = noiseLevel; | |||||
} | |||||
} | |||||
class RingPuffer { | |||||
private short[] puffer; | |||||
private final int laenge; | |||||
private int anzahlEnthaltenerDaten; | |||||
private int position; | |||||
public RingPuffer(int n) { | |||||
laenge = n; | |||||
anzahlEnthaltenerDaten = 0; | |||||
position = 0; | |||||
puffer = new short[laenge]; | |||||
} | |||||
public void hinzufuegen(short wert) { | |||||
puffer[position] = wert; | |||||
position++; | |||||
if (position >= laenge) { | |||||
position = 0; | |||||
} | |||||
if (anzahlEnthaltenerDaten < laenge) { | |||||
anzahlEnthaltenerDaten++; | |||||
} | |||||
} | |||||
public void hinzufuegen(short[] daten) { | |||||
for (short d : daten) { | |||||
puffer[position] = d; | |||||
position++; | |||||
if (position >= laenge) { | |||||
position = 0; | |||||
} | |||||
} | |||||
if (anzahlEnthaltenerDaten < laenge) { | |||||
anzahlEnthaltenerDaten += daten.length; | |||||
if (anzahlEnthaltenerDaten >= laenge) { | |||||
anzahlEnthaltenerDaten = laenge; | |||||
} | |||||
} | |||||
} | |||||
public short maximum() { | |||||
short max = 0; | |||||
for (int i = 0; i < anzahlEnthaltenerDaten; i++) { | |||||
if (puffer[i] > max) { | |||||
max = puffer[i]; | |||||
} | |||||
} | |||||
return max; | |||||
} | |||||
public float mittelwert() { | |||||
float summe = 0; | |||||
for (int i = 0; i < anzahlEnthaltenerDaten; i++) { | |||||
summe += puffer[i]; | |||||
} | |||||
return summe / anzahlEnthaltenerDaten; | |||||
} | |||||
} | |||||
class GleitenderMittelwert { | |||||
private final float wichtungNeuerWert; | |||||
private final float wichtungAlterWert; | |||||
private float mittelwert = 0; | |||||
private boolean istMittelwertGesetzt = false; | |||||
GleitenderMittelwert(float wichtungNeuerWert) { | |||||
this.wichtungNeuerWert = 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) { | |||||
if (istMittelwertGesetzt) { | |||||
mittelwert = wert * wichtungNeuerWert + mittelwert * wichtungAlterWert; | |||||
} else { | |||||
mittelwert = wert; | |||||
istMittelwertGesetzt = true; | |||||
} | |||||
return mittelwert; | |||||
} | |||||
} | |||||
} |
package com.example.ueberwachungssystem; | package com.example.ueberwachungssystem; | ||||
import android.Manifest; | |||||
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.os.Bundle; | |||||
import android.os.Environment; | |||||
import android.widget.TextView; | |||||
import androidx.core.app.ActivityCompat; | |||||
import androidx.core.content.ContextCompat; | |||||
import com.example.ueberwachungssystem.logger.Logger; | |||||
import java.io.File; | |||||
public class Mikrofon { | public class Mikrofon { | ||||
Logger logger; | |||||
private static final int RECHTEANFORDERUNG_MIKROFON = 1; | |||||
private Activity MainActivityForClass; | |||||
private AufnahmeTask aufnahmeTask; | |||||
public boolean Alarm = false; | |||||
public int Schwellwert_Alarm = 500; | |||||
public void onCreate(Activity MainActivity, Logger MainLogger) { | |||||
MainActivityForClass = MainActivity; | |||||
logger = MainLogger; //Class uses the same logger as the MainActivity | |||||
logger.log(this.getClass().getSimpleName() + ".onCreate"); | |||||
if (!istZugriffAufMikrofonErlaubt()) { | |||||
zugriffAufMikrofonAnfordern(); | |||||
} | |||||
} | |||||
public void onResume() { | |||||
logger.log(this.getClass().getSimpleName() + ".onResume"); | |||||
if (!istZugriffAufMikrofonErlaubt()) { | |||||
zugriffAufMikrofonAnfordern(); | |||||
} | |||||
if (istZugriffAufMikrofonErlaubt()) { | |||||
aufnahmeTask = new AufnahmeTask(); | |||||
aufnahmeTask.execute(); | |||||
} | |||||
} | |||||
public void onPause() { | |||||
logger.log(this.getClass().getSimpleName() + ".onPause"); | |||||
if(aufnahmeTask!=null) { | |||||
aufnahmeTask.cancel(true); | |||||
// aufnahmeTask = null; // if aufnahmeTask = null, break in for loop would not work (Nullpointer Exception) | |||||
} | |||||
} | |||||
private boolean istZugriffAufMikrofonErlaubt() { | |||||
if (ContextCompat.checkSelfPermission(MainActivityForClass, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { | |||||
logger.log("Zugriff auf Mikrofon ist verboten."); | |||||
return false; | |||||
} else { | |||||
logger.log("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; | |||||
private final int channelConfig = AudioFormat.CHANNEL_IN_MONO; | |||||
private final int audioFormat = AudioFormat.ENCODING_PCM_16BIT; | |||||
private int minPufferGroesseInBytes; | |||||
private int pufferGroesseInBytes; | |||||
private RingPuffer ringPuffer = new RingPuffer(10); | |||||
AufnahmeTask() { | |||||
minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); | |||||
pufferGroesseInBytes = minPufferGroesseInBytes * 2; | |||||
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, pufferGroesseInBytes); | |||||
// textViewMinPufferGroesseInBytes.setText("" + minPufferGroesseInBytes); | |||||
// textViewPufferGroesseInBytes.setText("" + pufferGroesseInBytes); | |||||
// textViewAbtastrate.setText("" + recorder.getSampleRate()); | |||||
// textViewAnzahlKanaele.setText("" + recorder.getChannelCount()); | |||||
logger.log("Puffergroeße: "+ minPufferGroesseInBytes + " " + pufferGroesseInBytes); | |||||
logger.log("Recorder (SR, CH): "+ recorder.getSampleRate() + " " + recorder.getChannelCount()); | |||||
int anzahlBytesProAbtastwert; | |||||
String s; | |||||
switch (recorder.getAudioFormat()) { | |||||
case AudioFormat.ENCODING_PCM_8BIT: | |||||
s = "8 Bit PCM "; | |||||
anzahlBytesProAbtastwert = 1; | |||||
break; | |||||
case AudioFormat.ENCODING_PCM_16BIT: | |||||
s = "16 Bit PCM"; | |||||
anzahlBytesProAbtastwert = 2; | |||||
break; | |||||
case AudioFormat.ENCODING_PCM_FLOAT: | |||||
s = "Float PCM"; | |||||
anzahlBytesProAbtastwert = 4; | |||||
break; | |||||
default: | |||||
throw new IllegalArgumentException(); | |||||
} | |||||
// textViewAudioFormat.setText(s); | |||||
switch (recorder.getChannelConfiguration()) { | |||||
case AudioFormat.CHANNEL_IN_MONO: | |||||
s = "Mono"; | |||||
break; | |||||
case AudioFormat.CHANNEL_IN_STEREO: | |||||
s = "Stereo"; | |||||
anzahlBytesProAbtastwert *= 2; | |||||
break; | |||||
case AudioFormat.CHANNEL_INVALID: | |||||
s = "ungültig"; | |||||
break; | |||||
default: | |||||
throw new IllegalArgumentException(); | |||||
} | |||||
// textViewKanalKonfiguration.setText(s); | |||||
logger.log("Konfiguration: "+ s); | |||||
int pufferGroesseInAnzahlAbtastwerten = pufferGroesseInBytes / anzahlBytesProAbtastwert; | |||||
int pufferGroesseInMillisekunden = 1000 * pufferGroesseInAnzahlAbtastwerten / recorder.getSampleRate(); | |||||
// textViewPufferGroesseInAnzahlAbtastwerte.setText("" + pufferGroesseInAnzahlAbtastwerten); | |||||
// textViewPufferGroesseInMillisekunden.setText("" + pufferGroesseInMillisekunden); | |||||
} | |||||
@Override | |||||
protected Void doInBackground(Long... params) { | |||||
recorder.startRecording(); | |||||
short[] puffer = new short[pufferGroesseInBytes / 2]; | |||||
long lastTime = System.currentTimeMillis(); | |||||
float verarbeitungsrate = 0; | |||||
final int maxZaehlerZeitMessung = 10; | |||||
int zaehlerZeitMessung = 0; | |||||
int anzahlVerarbeitet = 0; | |||||
GleitenderMittelwert gleitenderMittelwert = new GleitenderMittelwert(0.3f); | |||||
for (; ; ) { | |||||
if (aufnahmeTask.isCancelled()) { | |||||
break; | |||||
} else { | |||||
int n = recorder.read(puffer, 0, puffer.length); | |||||
Verarbeitungsergebnis ergebnis = verarbeiten(puffer, n); | |||||
anzahlVerarbeitet += n; | |||||
zaehlerZeitMessung++; | |||||
if (zaehlerZeitMessung == maxZaehlerZeitMessung) { | |||||
long time = System.currentTimeMillis(); | |||||
long deltaTime = time - lastTime; | |||||
verarbeitungsrate = 1000.0f * anzahlVerarbeitet / deltaTime; | |||||
verarbeitungsrate = gleitenderMittelwert.mittel(verarbeitungsrate); | |||||
zaehlerZeitMessung = 0; | |||||
anzahlVerarbeitet = 0; | |||||
lastTime = time; | |||||
} | |||||
float noiseLevel = gleitenderMittelwert.MittelwertPuffer(puffer); | |||||
// logger.log("Noise Level:" + noiseLevel); | |||||
ergebnis.noiseLevel = noiseLevel; | |||||
ergebnis.verarbeitungsrate = (int) verarbeitungsrate; | |||||
publishProgress(ergebnis); | |||||
try { | |||||
Thread.sleep(100); | |||||
} catch (InterruptedException e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
} | |||||
recorder.release(); | |||||
return null; | |||||
} | |||||
private Verarbeitungsergebnis verarbeiten(short[] daten, int n) { | |||||
String status; | |||||
short maxAmp = -1; | |||||
if (n == AudioRecord.ERROR_INVALID_OPERATION) { | |||||
status = "ERROR_INVALID_OPERATION"; | |||||
} else if (n == AudioRecord.ERROR_BAD_VALUE) { | |||||
status = "ERROR_BAD_VALUE"; | |||||
} else { | |||||
status = "OK"; | |||||
short max = 0; | |||||
for (int i = 0; i < n; i++) { | |||||
if (daten[i] > max) { | |||||
max = daten[i]; | |||||
} | |||||
} | |||||
ringPuffer.hinzufuegen(max); | |||||
maxAmp = ringPuffer.maximum(); | |||||
} | |||||
return new Verarbeitungsergebnis(status, maxAmp, 0, 0); | |||||
} | |||||
@Override | |||||
protected void onProgressUpdate(Verarbeitungsergebnis... progress) { | |||||
super.onProgressUpdate(progress); | |||||
// textViewMaxAmp.setText("" + progress[0].maxAmp); | |||||
// textViewVerarbeitungsrate.setText("" + progress[0].verarbeitungsrate); | |||||
logger.overwriteLastlog("VR, Max, NL:" + progress[0].verarbeitungsrate + ", " + progress[0].maxAmp | |||||
+ ", " + progress[0].noiseLevel); | |||||
if (progress[0].maxAmp >= Schwellwert_Alarm) { | |||||
Alarm = true; | |||||
} | |||||
} | |||||
} | |||||
class Verarbeitungsergebnis { | |||||
String status; | |||||
short maxAmp; | |||||
int verarbeitungsrate; | |||||
float noiseLevel; | |||||
Verarbeitungsergebnis(String status, short maxAmp, int verarbeitungsrate, float noiseLevel) { | |||||
this.status = status; | |||||
this.maxAmp = maxAmp; | |||||
this.verarbeitungsrate = verarbeitungsrate; | |||||
this.noiseLevel = noiseLevel; | |||||
} | |||||
} | |||||
class RingPuffer { | |||||
private short[] puffer; | |||||
private final int laenge; | |||||
private int anzahlEnthaltenerDaten; | |||||
private int position; | |||||
public RingPuffer(int n) { | |||||
laenge = n; | |||||
anzahlEnthaltenerDaten = 0; | |||||
position = 0; | |||||
puffer = new short[laenge]; | |||||
} | |||||
public void hinzufuegen(short wert) { | |||||
puffer[position] = wert; | |||||
position++; | |||||
if (position >= laenge) { | |||||
position = 0; | |||||
} | |||||
if (anzahlEnthaltenerDaten < laenge) { | |||||
anzahlEnthaltenerDaten++; | |||||
} | |||||
} | |||||
public void hinzufuegen(short[] daten) { | |||||
for (short d : daten) { | |||||
puffer[position] = d; | |||||
position++; | |||||
if (position >= laenge) { | |||||
position = 0; | |||||
} | |||||
} | |||||
if (anzahlEnthaltenerDaten < laenge) { | |||||
anzahlEnthaltenerDaten += daten.length; | |||||
if (anzahlEnthaltenerDaten >= laenge) { | |||||
anzahlEnthaltenerDaten = laenge; | |||||
} | |||||
} | |||||
} | |||||
public short maximum() { | |||||
short max = 0; | |||||
for (int i = 0; i < anzahlEnthaltenerDaten; i++) { | |||||
if (puffer[i] > max) { | |||||
max = puffer[i]; | |||||
} | |||||
} | |||||
return max; | |||||
} | |||||
public float mittelwert() { | |||||
float summe = 0; | |||||
for (int i = 0; i < anzahlEnthaltenerDaten; i++) { | |||||
summe += puffer[i]; | |||||
} | |||||
return summe / anzahlEnthaltenerDaten; | |||||
} | |||||
} | |||||
class GleitenderMittelwert { | |||||
private final float wichtungNeuerWert; | |||||
private final float wichtungAlterWert; | |||||
private float mittelwert = 0; | |||||
private boolean istMittelwertGesetzt = false; | |||||
GleitenderMittelwert(float wichtungNeuerWert) { | |||||
this.wichtungNeuerWert = 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) { | |||||
if (istMittelwertGesetzt) { | |||||
mittelwert = wert * wichtungNeuerWert + mittelwert * wichtungAlterWert; | |||||
} else { | |||||
mittelwert = wert; | |||||
istMittelwertGesetzt = true; | |||||
} | |||||
return mittelwert; | |||||
} | |||||
} | |||||
} | } |
package com.example.ueberwachungssystem.logger; | |||||
import android.util.Log; | |||||
import android.widget.TextView; | |||||
import java.io.PrintWriter; | |||||
import java.io.StringWriter; | |||||
public class Logger { | |||||
private TextView textView; | |||||
private StringBuffer sb = new StringBuffer(); | |||||
private String tag; | |||||
private int lengthOfLastLog = 0; | |||||
private boolean overwrite = false; | |||||
public Logger(String tag, TextView textView, String logInitText) { | |||||
this.tag = tag; | |||||
this.textView = textView; | |||||
sb.append(logInitText); | |||||
} | |||||
public void log(String s) { | |||||
overwrite = false; | |||||
Log.d(tag, s); | |||||
sb.append(s).append("\n"); | |||||
if (textView != null) { | |||||
textView.setText(sb.toString()); | |||||
} | |||||
} | |||||
public void overwriteLastlog(String s) { | |||||
Log.d(tag, s); | |||||
lengthOfLastLog = s.length(); | |||||
if (overwrite) | |||||
{ | |||||
sb.setLength(sb.length() - (lengthOfLastLog + 1)); | |||||
} | |||||
sb.append(s).append("\n"); | |||||
overwrite = true; | |||||
if (textView != null) { | |||||
textView.setText(sb.toString()); | |||||
} | |||||
} | |||||
public void log(Exception e) { | |||||
StringWriter sw = new StringWriter(); | |||||
e.printStackTrace(new PrintWriter(sw)); | |||||
log(sw.toString()); | |||||
} | |||||
public void clearLog() { | |||||
sb.setLength(0); | |||||
if (textView != null) { | |||||
textView.setText(""); | |||||
} | |||||
} | |||||
public String getLoggedText() { | |||||
return sb.toString(); | |||||
} | |||||
} |
tools:context=".MainActivity"> | tools:context=".MainActivity"> | ||||
<TextView | <TextView | ||||
android:id="@+id/tv_log" | |||||
android:layout_width="wrap_content" | android:layout_width="wrap_content" | ||||
android:layout_height="wrap_content" | android:layout_height="wrap_content" | ||||
android:text="Hello World!" | |||||
app:layout_constraintBottom_toBottomOf="parent" | |||||
android:text="Log" | |||||
app:layout_constraintEnd_toEndOf="parent" | app:layout_constraintEnd_toEndOf="parent" | ||||
app:layout_constraintStart_toStartOf="parent" | app:layout_constraintStart_toStartOf="parent" | ||||
app:layout_constraintTop_toTopOf="parent" /> | app:layout_constraintTop_toTopOf="parent" /> | ||||
<Switch | |||||
android:id="@+id/TglBtn_Mic" | |||||
android:layout_width="wrap_content" | |||||
android:layout_height="wrap_content" | |||||
android:text="Mic" | |||||
app:layout_constraintEnd_toStartOf="@+id/tv_log" | |||||
app:layout_constraintHorizontal_bias="0.0" | |||||
app:layout_constraintStart_toStartOf="parent" | |||||
app:layout_constraintTop_toTopOf="parent" /> | |||||
</androidx.constraintlayout.widget.ConstraintLayout> | </androidx.constraintlayout.widget.ConstraintLayout> |