|
|
|
|
|
|
|
|
import static java.lang.Math.*; |
|
|
import static java.lang.Math.*; |
|
|
|
|
|
|
|
|
import android.Manifest; |
|
|
import android.Manifest; |
|
|
|
|
|
import android.annotation.SuppressLint; |
|
|
import android.app.Activity; |
|
|
import android.app.Activity; |
|
|
import android.content.Context; |
|
|
import android.content.Context; |
|
|
import android.content.pm.PackageManager; |
|
|
import android.content.pm.PackageManager; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.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 { |
|
|
public class MicrophoneDetector extends Detector { |
|
|
/** |
|
|
/** |
|
|
|
|
|
|
|
|
private AufnahmeTask aufnahmeTask; |
|
|
private AufnahmeTask aufnahmeTask; |
|
|
public boolean armed = false; |
|
|
public boolean armed = false; |
|
|
public int Schwellwert_Alarm = 100; |
|
|
public int Schwellwert_Alarm = 100; |
|
|
private Activity MainActivityForClass; |
|
|
|
|
|
|
|
|
private Context context; |
|
|
|
|
|
|
|
|
public MicrophoneDetector(Context context) { |
|
|
public MicrophoneDetector(Context context) { |
|
|
super(context); |
|
|
|
|
|
MainActivityForClass = (Activity) context; |
|
|
|
|
|
|
|
|
|
|
|
if (!istZugriffAufMikrofonErlaubt()) { |
|
|
|
|
|
zugriffAufMikrofonAnfordern(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
super(); |
|
|
|
|
|
this.context = context; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void startDetection() { |
|
|
public void startDetection() { |
|
|
|
|
|
|
|
|
if (!istZugriffAufMikrofonErlaubt()) { |
|
|
|
|
|
zugriffAufMikrofonAnfordern(); |
|
|
|
|
|
} |
|
|
|
|
|
if (istZugriffAufMikrofonErlaubt()) { |
|
|
|
|
|
aufnahmeTask = new AufnahmeTask(); |
|
|
|
|
|
aufnahmeTask.execute(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
aufnahmeTask = new AufnahmeTask(); |
|
|
|
|
|
aufnahmeTask.execute(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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> { |
|
|
class AufnahmeTask extends AsyncTask<Long, Verarbeitungsergebnis, Void> { |
|
|
private AudioRecord recorder; |
|
|
private AudioRecord recorder; |
|
|
private final int sampleRateInHz = 44100; |
|
|
private final int sampleRateInHz = 44100; |
|
|
|
|
|
|
|
|
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.Detector.DetectionReport detectionReport; |
|
|
|
|
|
|
|
|
private com.example.ueberwachungssystem.Detection.DetectionReport detectionReport; |
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("MissingPermission") |
|
|
AufnahmeTask() { |
|
|
AufnahmeTask() { |
|
|
minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); |
|
|
minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); |
|
|
pufferGroesseInBytes = minPufferGroesseInBytes * 2; |
|
|
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","Puffergroeße: "+ minPufferGroesseInBytes + " " + pufferGroesseInBytes); |
|
|
Log.d("0","Recorder (SR, CH): "+ recorder.getSampleRate() + " " + recorder.getChannelCount()); |
|
|
Log.d("0","Recorder (SR, CH): "+ recorder.getSampleRate() + " " + recorder.getChannelCount()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.d("0","Konfiguration: "+ s); |
|
|
Log.d("0","Konfiguration: "+ s); |
|
|
|
|
|
|
|
|
int pufferGroesseInAnzahlAbtastwerten = pufferGroesseInBytes / anzahlBytesProAbtastwert; |
|
|
int pufferGroesseInAnzahlAbtastwerten = pufferGroesseInBytes / anzahlBytesProAbtastwert; |
|
|
int pufferGroesseInMillisekunden = 1000 * pufferGroesseInAnzahlAbtastwerten / recorder.getSampleRate(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int n = recorder.read(puffer, 0, puffer.length); |
|
|
int n = recorder.read(puffer, 0, puffer.length); |
|
|
Verarbeitungsergebnis ergebnis = verarbeiten(puffer, n); |
|
|
Verarbeitungsergebnis ergebnis = verarbeiten(puffer, n); |
|
|
anzahlVerarbeitet += n; |
|
|
anzahlVerarbeitet += n; |
|
|
|
|
|
|
|
|
// __Part of FFT__ |
|
|
// __Part of FFT__ |
|
|
// spektrum = calculateFFT(puffer); |
|
|
// spektrum = calculateFFT(puffer); |
|
|
// LineGraphSeries<DataPoint> newseries = new LineGraphSeries<DataPoint>(new DataPoint[]{}); |
|
|
// LineGraphSeries<DataPoint> newseries = new LineGraphSeries<DataPoint>(new DataPoint[]{}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (progress[0].maxAmp >= Schwellwert_Alarm+kalibierWert && armed == true) { |
|
|
if (progress[0].maxAmp >= Schwellwert_Alarm+kalibierWert && armed == true) { |
|
|
armed = false; |
|
|
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()); |
|
|
Log.d("1",detectionReport.toString()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
final int mNumberOfFFTPoints =1024; |
|
|
final int mNumberOfFFTPoints =1024; |
|
|
double mMaxFFTSample; |
|
|
|
|
|
|
|
|
|
|
|
double temp; |
|
|
double temp; |
|
|
Complex[] y; |
|
|
Complex[] y; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
y = FFT.fft(complexSignal); |
|
|
y = FFT.fft(complexSignal); |
|
|
|
|
|
|
|
|
mMaxFFTSample = 0.0; |
|
|
|
|
|
for(int i = 0; i < (mNumberOfFFTPoints/2); i++) |
|
|
for(int i = 0; i < (mNumberOfFFTPoints/2); i++) |
|
|
{ |
|
|
{ |
|
|
absSignal[i] = y[i].abs(); |
|
|
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; |
|
|
return absSignal; |