Compare commits

...

7 Commits

Author SHA1 Message Date
144b8eb676 Kapselung in Funktionen; 2023-06-20 16:23:42 +02:00
d3d7def833 Mikrofon Activity Ausbesserung Kalibrierung;
Speicherung der dB-Werte nur, wenn positiv + Kommentare zum Verständnis der zu speichernden Variablen
2023-06-12 13:28:45 +02:00
c61e56c3b5 Mikrofon Activity kalibrierung_do initialisiert 2023-06-07 15:17:28 +02:00
f2b85fb4e7 Mikrofon Activity commit im Nachhinein 2023-06-07 13:48:36 +02:00
32decab687 Mikrofon XML commit im Nachhinein 2023-06-07 13:47:56 +02:00
f98a3580a6 Mikrofon Manifest angepasst - App läuft 2023-06-07 13:46:53 +02:00
5b920359a1 Mikrofon als Activity 2023-06-07 13:39:57 +02:00
9 changed files with 448 additions and 6 deletions

View File

@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.greenwatch">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@ -9,6 +10,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GreenWatch">
<activity
android:name=".Mikrofon"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">

View File

@ -0,0 +1,23 @@
package com.example.greenwatch;
public 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 mittel(float wert) {
if (istMittelwertGesetzt) {
mittelwert = wert * wichtungNeuerWert + mittelwert * wichtungAlterWert;
} else {
mittelwert = wert;
istMittelwertGesetzt = true;
}
return mittelwert;
}}

View File

@ -2,7 +2,10 @@ package com.example.greenwatch;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@ -10,5 +13,13 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button)findViewById(R.id.buttonstartmicdetection)).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Mikrofon.class);
startActivity(i);
}
});
}
}

View File

@ -1,4 +1,254 @@
package com.example.greenwatch;
public class Mikrofon {
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
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.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Mikrofon extends AppCompatActivity {
private AufnahmeTask aufnahmeTask;
private TextView textViewMaxAmp;
private TextView textViewMaxAmpdb;
private TextView textViewZeitstempel;
private static final int RECHTEANFORDERUNG_MIKROFON = 1;
private boolean detection_mic = false;
private String zeit = "0";
private boolean kalibrierung_do = true;
private float threshold = 40;
private float sensitivity = 5;
private final static String KEY_WERT = "KEY_WERT_" + Mikrofon.class.getSimpleName();
private final static String KEY_KALIBRIERUNG = "KEY_KALIBRIERUNG_" + Mikrofon.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mikrofon);
textViewMaxAmp = (TextView) findViewById(R.id.textViewMaxAmp);
textViewMaxAmpdb = (TextView) findViewById(R.id.textViewMaxAmpdb);
textViewZeitstempel = (TextView) findViewById(R.id.textViewZeitstempel);
//kalibrierung_do = true;
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences p = getPreferences(Context.MODE_PRIVATE);
threshold = p.getFloat(KEY_WERT, 40);
kalibrierung_do = p.getBoolean(KEY_KALIBRIERUNG, true);
if (!istZugriffAufMikrofonErlaubt()) {
zugriffAufMikrofonAnfordern();
}
if (istZugriffAufMikrofonErlaubt()) {
aufnahmeTask = new AufnahmeTask();
aufnahmeTask.execute();
}
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences p = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = p.edit();
editor.putFloat(KEY_WERT, threshold);
if(isFinishing()==true){
editor.putBoolean(KEY_KALIBRIERUNG, true);
}else {
editor.putBoolean(KEY_KALIBRIERUNG, false);
}
editor.commit();
if(aufnahmeTask!=null) {
aufnahmeTask.cancel(true);
aufnahmeTask = null;
}
}
private boolean istZugriffAufMikrofonErlaubt() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
return false;
} else {
return true;
}
}
private void zugriffAufMikrofonAnfordern() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, RECHTEANFORDERUNG_MIKROFON);
}
public static String Zeitpunkt(){
Long tslong = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date date = new Date(tslong);
String resultdate = sdf.format(date);
return resultdate;
}
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);
private boolean erlaubt_rise = true;
private boolean erlaubt_fall = true;
AufnahmeTask() {
minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
pufferGroesseInBytes = minPufferGroesseInBytes * 2;
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, pufferGroesseInBytes);
}
//hi
public float kalibrieren(short[] puffer){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int anzahlIterationen = 100;
float sum = 0;
for(int i = 0; i < anzahlIterationen; i++){
int z = recorder.read(puffer, 0, puffer.length);
Verarbeitungsergebnis kalibrierung = verarbeiten(puffer, z);
sum += (float) kalibrierung.db;
System.out.println("Aktueller_Wert Kalibrierung" +kalibrierung.db);
}
System.out.println("Summe:"+sum);
return sum/anzahlIterationen;
}
public void Detektion(Verarbeitungsergebnis ergebnis){
if(ergebnis.db >= (threshold+sensitivity)){
detection_mic = true;
erlaubt_fall = true;
while(erlaubt_rise){
erlaubt_rise = false;
zeit = Zeitpunkt() + " - " + ergebnis.db + " - " + (threshold+sensitivity); //Überschreiben des Zeitpunkts beim Erkennen der Detektion
//nur zum Testen in zeit noch ergebnis.db und threshold ausgegeben -> muss am Ende nur Zeitpunkt() rein, Rest weg
}
} else if (ergebnis.db <= (threshold)) {
detection_mic = false;
erlaubt_rise = true;
while(erlaubt_fall){
erlaubt_fall = false;
zeit = Zeitpunkt(); //erneutes Überschreiben des Zeitpunkts beim Verschwinden der Detektion
}
}
}
@Override
protected Void doInBackground(Long... params) {
recorder.startRecording();
short[] puffer = new short[pufferGroesseInBytes / 2];
GleitenderMittelwert gleitenderMittelwertdB = new GleitenderMittelwert(0.2f);
float db = 0;
//kalibrierung
if(kalibrierung_do){
threshold = kalibrieren(puffer);
kalibrierung_do = false;
}
for (; ; ) {
if (isCancelled()) {
break;
} else {
//ergebnis ermitteln
int n = recorder.read(puffer, 0, puffer.length);
Verarbeitungsergebnis ergebnis = verarbeiten(puffer, n);
//Mittelwertberechnnung
db = (float) ergebnis.db;
db = gleitenderMittelwertdB.mittel(db);
ergebnis.db = (int) db;
//Db Wert mit Schwellwert vergleichen und Warnung setzen
Detektion(ergebnis);
publishProgress(ergebnis);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
recorder.release();
return null;
}
private Verarbeitungsergebnis verarbeiten(short[] daten, int n) {
String status;
short maxAmp = -1;
int db = 0, db_wert = 0;
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();
}
//Umwandlung Amplitudenwert in dB
db_wert = (int) (20*Math.log10(maxAmp));
if (db_wert > 0){
db = db_wert;
}
return new Verarbeitungsergebnis(status, maxAmp, db);
}
/////////////////////////////////////////////
@Override
protected void onProgressUpdate(Verarbeitungsergebnis... progress) {
super.onProgressUpdate(progress);
System.out.println(detection_mic);
System.out.println(zeit);
textViewZeitstempel.setText(zeit);
textViewMaxAmpdb.setText("" + progress[0].db);
textViewMaxAmp.setText("" + progress[0].maxAmp);
}
}
/////////////////////////////////////////////
}

View File

@ -0,0 +1,63 @@
package com.example.greenwatch;
public 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;
}*/
}

View File

@ -0,0 +1,13 @@
package com.example.greenwatch;
public class Verarbeitungsergebnis {
String status;
short maxAmp;
int db;
//
Verarbeitungsergebnis(String status, short maxAmp, int db) {
this.status = status;
this.maxAmp = maxAmp;
this.db = db;
}
}

View File

@ -6,13 +6,13 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
<Button
android:id="@+id/buttonstartmicdetection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="Starte Mikrofon Detection"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:activity="com.example.greenwatch">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="max. Amp: "
android:id="@+id/labelMaxAmp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewMaxAmp"
android:text=" "
android:layout_alignBaseline="@+id/labelMaxAmp"
android:layout_toEndOf="@+id/labelMaxAmpdb"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="max. Amp in dB: "
android:id="@+id/labelMaxAmpdb"
android:layout_below="@+id/labelMaxAmp"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewMaxAmpdb"
android:text=" "
android:layout_alignBaseline="@+id/labelMaxAmpdb"
android:layout_toEndOf="@+id/labelMaxAmpdb"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Meldung: "
android:id="@+id/labelZeitstempel"
android:layout_below="@+id/labelMaxAmpdb"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewZeitstempel"
android:text=" "
android:layout_alignBaseline="@+id/labelZeitstempel"
android:layout_toEndOf="@+id/labelMaxAmpdb"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView3"
android:layout_below="@+id/labelMaxAmpdb"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewLogger"/>
</ScrollView>
</RelativeLayout>

0
gradlew vendored Executable file → Normal file
View File