Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Add configuration parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
incubo4u committed Aug 4, 2023
1 parent ec0a471 commit 6991d28
Showing 1 changed file with 5 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.");
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -104,7 +100,7 @@ public void run() {
return;
int amplitude = getMaxAmplitude(buffer, bytesRead);
int value = calculateValue(amplitude);
detectSound(value);
detectSound(value,volumeThreshold);
}
}, 0, monitorInterval);
}
Expand Down

0 comments on commit 6991d28

Please sign in to comment.