From 6991d2867e00dce5f29591e2d3cfdbceb881c348 Mon Sep 17 00:00:00 2001 From: Mikolaj Date: Fri, 4 Aug 2023 12:57:29 +0200 Subject: [PATCH] Add configuration parameters --- .../rtc/media/SoundDetection.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/MembraneRTC/src/main/java/org/membraneframework/rtc/media/SoundDetection.java b/MembraneRTC/src/main/java/org/membraneframework/rtc/media/SoundDetection.java index 86a296f..30096f3 100644 --- a/MembraneRTC/src/main/java/org/membraneframework/rtc/media/SoundDetection.java +++ b/MembraneRTC/src/main/java/org/membraneframework/rtc/media/SoundDetection.java @@ -21,10 +21,10 @@ public class SoundDetection { private final int samplingRate = 22050; public void start() throws SecurityException { - start(monitorInterval, samplingRate); + start(monitorInterval, samplingRate, volumeThreshold); } - public void start(int monitorInterval, int samplingRate) throws SecurityException { + public void start(int monitorInterval, int samplingRate, int volumeThreshold) throws SecurityException { if (isRecording) return; bufferSize = AudioRecord.getMinBufferSize(samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); @@ -33,7 +33,7 @@ public void start(int monitorInterval, int samplingRate) throws SecurityExceptio if (audioRecord.getState() == AudioRecord.STATE_INITIALIZED) { audioRecord.startRecording(); isRecording = true; - startTimer(monitorInterval); + startTimer(monitorInterval,volumeThreshold); } else { Timber.e("COULDNT_PREPARE_RECORDING AudioRecord couldn't be initialized."); } @@ -66,10 +66,6 @@ public void setIsSoundDetected(boolean newValue) { } } - private void detectSound(int volumeValue) { - detectSound(volumeThreshold, volumeValue); - } - private void detectSound(int volumeThreshold, int volumeValue) { setIsSoundDetected(volumeValue > volumeThreshold); } @@ -93,7 +89,7 @@ private int readAudioData(short[] buffer) { return audioRecord.read(buffer, 0, bufferSize); } - private void startTimer(int monitorInterval) { + private void startTimer(int monitorInterval, int volumeThreshold) { timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override @@ -104,7 +100,7 @@ public void run() { return; int amplitude = getMaxAmplitude(buffer, bytesRead); int value = calculateValue(amplitude); - detectSound(value); + detectSound(value,volumeThreshold); } }, 0, monitorInterval); }