Commit 4fb97f3a authored by Oliver Adams's avatar Oliver Adams

Removed Noise, BackgroundNoise and BackgroundNoiseListener

parent eaaeb3ac
/*
Copyright (C) 2013, The Aikuma Project
AUTHORS: Oliver Adams and Florian Hanke
*/
package org.lp20.aikuma.audio.record;
import android.util.Log;
import org.lp20.aikuma.audio.Processor;
/**
* Tries to extract the level of background noise
* from the given buffers.
*
* @author Florian Hanke <florian.hanke@gmail.com>
* @author Oliver Adams <oliver.adams@gmail.com>
*/
public class Noise {
protected Processor processor = new Processor();
protected int[] averages;
protected int currentAverage = -1;
protected float currentQuality;
public Noise() {
this(20);
}
public Noise(int windowSize) {
this.averages = new int[windowSize];
this.currentQuality = -1;
}
/**
* Container class for information regarding background noise.
*/
public class Information {
private int average;
private int minimum;
private int maximum;
private float quality;
public Information(int average, int minimum, int maximum, float quality) {
this.average = average;
this.minimum = minimum;
this.maximum = maximum;
this.quality = quality;
}
/**
* One needs to speak about x times as loud as the background noise.
*/
public int getRecommendedRecordingLevel() {
return (int) Math.round(getAverage()*5);
}
public int getAverage() { return this.average; }
public int getMinimum() { return this.minimum; }
public int getMaximum() { return this.maximum; }
public float getQuality() { return this.quality; } // Negative. The closer to 0, the higher the quality.
}
/**
* @return A positive number if threshold is found, else if not.
*/
public Information getInformation(short[] buffer) {
addBuffer(buffer);
float average = 0;
int min = 0;
int max = 0;
float quality = -100;
if (isBufferFilled()) {
average = getAverage();
min = getMin();
max = getMax();
if (average > 0) {
// If there are no exceptional values, we return a threshold.
//
float minThreshold = average*0.75f;
float maxThreshold = average*1.33f;
if (minThreshold <= min && max <= maxThreshold) {
quality = 0;
} else {
quality = -(minThreshold/min)-(max/maxThreshold);
}
}
}
return new Information(Math.round(average), min, max, quality);
}
protected boolean isBufferFilled() {
for (int i = 0; i < averages.length; i++) {
if (averages[i] == 0) { return false; }
}
return true;
}
protected void addBuffer(short[] buffer) {
currentAverage++;
if (currentAverage >= averages.length) { currentAverage = 0; }
averages[currentAverage] = processor.getAverage(buffer);
}
protected float getAverage() {
float sum = 0;
for (int i = 0; i < averages.length; i++) {
float average = averages[i];
if (average == 0) {
return -1;
} else {
sum += averages[i];
}
}
return sum / averages.length;
};
protected int getMax() {
int max = Integer.MIN_VALUE;
for (int i = 0; i < averages.length; i++) {
if (averages[i] > max) {
max = averages[i];
}
}
return max;
};
protected int getMin() {
int min = Integer.MAX_VALUE;
for (int i = 0; i < averages.length; i++) {
if (averages[i] < min) {
min = averages[i];
}
}
return min;
};
}
/*
Copyright (C) 2013, The Aikuma Project
AUTHORS: Oliver Adams and Florian Hanke
*/
package org.lp20.aikuma.audio.record.analyzers;
import android.util.Log;
import org.lp20.aikuma.audio.record.Microphone;
import org.lp20.aikuma.audio.record.Microphone.MicException;
import org.lp20.aikuma.audio.record.MicrophoneListener;
import org.lp20.aikuma.audio.Processor;
import org.lp20.aikuma.audio.record.Noise;
import org.lp20.aikuma.audio.record.analyzers.BackgroundNoiseListener;
/**
* Tries to extract the level of background noise
* from the given buffers.
*
* @author Florian Hanke <florian.hanke@gmail.com>
* @author Oliver Adams <oliver.adams@gmail.com>
*/
public class BackgroundNoise {
/** The thing that extracts the level of background noise from the buffers*/
protected Noise thresholder;
/** The microphone used to gather the noise data. */
protected Microphone microphone;
/**
* Constructor
*
* @param duration The duration of the noise
* @param sampleRate The sample rate of the noise.
* @throws MicException If there is an issue creating the microphone.
*/
public BackgroundNoise(int duration, long sampleRate) throws MicException {
this.thresholder = new Noise(duration);
this.microphone = new Microphone(sampleRate);
}
/**
* Tries to find a meaningful threshold value to define speech with.
*
* @param listener The thing that will listen to the background noise.
*/
public void getThreshold(final BackgroundNoiseListener listener) {
// Try finding a stable background noise.
//
microphone.listen(new MicrophoneListener() {
public void onBufferFull(short[] buffer) {
Noise.Information information = BackgroundNoise.this.thresholder.getInformation(buffer);
listener.noiseLevelQualityUpdated(information);
if (information.getQuality() >= 0) {
try {
microphone.stop();
} catch (MicException e) {
//Not much can be done.
}
listener.noiseLevelFound(information);
}
}
});
}
/**
* Stops the microphone.
*
* @throws MicException If there is an issue stopping the microphone.
*/
public void stop() throws MicException {
microphone.stop();
}
}
/*
Copyright (C) 2013, The Aikuma Project
AUTHORS: Oliver Adams and Florian Hanke
*/
package org.lp20.aikuma.audio.record.analyzers;
import org.lp20.aikuma.audio.record.Noise;
/**
* Interface for anything that listens to background noise data.
*
* @author Oliver Adams <oliver.adams@gmail.com>
* @author Florian Hanke <florian.hanke@gmail.com>
*/
public interface BackgroundNoiseListener {
/**
* Quality will be a negative value getting closer and closer to 0 (higher is better).
*
* @param information Information about the background noise.
*/
public void noiseLevelQualityUpdated(Noise.Information information);
/**
* Level will be a positive value; This will be called once when it's found.
*
* @param information Information about the background noise.
*/
public void noiseLevelFound(Noise.Information information);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment