<commit_before>package edu.berkeley.cs160.DeansOfDesign.cookease;

import java.text.DecimalFormat;
import java.util.ArrayList;

import weka.classifiers.Classifier;
import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.SerializationHelper;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.util.Log;
import be.hogent.tarsos.dsp.AudioEvent;
import edu.berkeley.cs160.DeansOfDesign.cookease.AudioProc.OnAudioEventListener;

public class BoilingWaterDetector implements OnAudioEventListener {
	
	public interface OnBoilingEventListener {
		public void processBoilingEvent();
	}
	
	private static final String TAG = "AudioProc";
	private static final int mModelResourceID = R.raw.boiling_water_clf;
	public static final int SAMPLE_RATE = 8000;
	private AudioProc mAudioProc;
	private boolean mUseMic;
	private AudioFeaturizer mAudioFeaturizer;
	private Classifier mModel;
	private boolean mModelLoaded = false;
	private OnBoilingEventListener mOnBoilingEventListener;
	private double mSensitivity;
	private ArrayList<Double> mDetectionHistory;
	private static final double HISTORY_SIZE = 100d;
		
	public BoilingWaterDetector(Context context, double sensitivity) {
		mUseMic = true;
		mAudioProc = new AudioProc(SAMPLE_RATE, context);
		mAudioProc.setOnAudioEventListener(this);
		mAudioFeaturizer = new AudioFeaturizer();
		loadModel(context);
		mSensitivity = sensitivity;
		mDetectionHistory = new ArrayList<Double>();
	}

	public void stopDetection() {
		if (mAudioProc.isRecording()) {
			mAudioProc.stop();
		}
	}
	
	public void startDetection() {
		mAudioProc.useMic = mUseMic;
		mAudioProc.listen();
		Log.d(TAG, "listening!");
	}
	
	@Override
	public void processAudioProcEvent(AudioEvent ae) {
		//Log.v(TAG, "Got a sample!");
		if (!mModelLoaded) return;
		
		// featurize the sample
		Instance feature_vector = mAudioFeaturizer.run(ae);
		//Log.v(TAG, "Got a feature vector!");
		//Log.v(TAG, feature_vector.toString());
		
		// run the model on the sample
		String className;
		try {
			int predictedClassIndex = (int) mModel.classifyInstance(feature_vector);
			className = feature_vector.classAttribute().value(predictedClassIndex);
			//Log.v(TAG, "predicted the audio class: " + className);<commit_msg>Stop restarting the microphone every time the display rotates.<commit_after>package edu.berkeley.cs160.DeansOfDesign.cookease;

import java.text.DecimalFormat;
import java.util.ArrayList;

import weka.classifiers.Classifier;
import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.SerializationHelper;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.util.Log;
import be.hogent.tarsos.dsp.AudioEvent;
import edu.berkeley.cs160.DeansOfDesign.cookease.AudioProc.OnAudioEventListener;

public class BoilingWaterDetector implements OnAudioEventListener {
	
	public interface OnBoilingEventListener {
		public void processBoilingEvent();
	}
	
	private static final String TAG = "AudioProc";
	private static final int mModelResourceID = R.raw.boiling_water_clf;
	public static final int SAMPLE_RATE = 8000;
	private AudioProc mAudioProc;
	private boolean mUseMic;
	private AudioFeaturizer mAudioFeaturizer;
	private Classifier mModel;
	private boolean mModelLoaded = false;
	private OnBoilingEventListener mOnBoilingEventListener;
	private double mSensitivity;
	private ArrayList<Double> mDetectionHistory;
	private static final double HISTORY_SIZE = 100d;
		
	public BoilingWaterDetector(Context context, double sensitivity) {
		mUseMic = true;
		mAudioProc = new AudioProc(SAMPLE_RATE, context);
		mAudioProc.setOnAudioEventListener(this);
		mAudioFeaturizer = new AudioFeaturizer();
		loadModel(context);
		mSensitivity = sensitivity;
		mDetectionHistory = new ArrayList<Double>();
	}

	public void stopDetection() {
		if (mAudioProc.isRecording()) {
			mAudioProc.stop();
		}
	}
	
	public void startDetection() {
		if (!mAudioProc.isRecording()) {
			mAudioProc.useMic = mUseMic;
			mAudioProc.listen();
			Log.d(TAG, "listening!");
		}
	}
	
	@Override
	public void processAudioProcEvent(AudioEvent ae) {
		//Log.v(TAG, "Got a sample!");
		if (!mModelLoaded) return;
		
		// featurize the sample
		Instance feature_vector = mAudioFeaturizer.run(ae);
		//Log.v(TAG, "Got a feature vector!");
		//Log.v(TAG, feature_vector.toString());
		
		// run the model on the sample
		String className;
		try {
			int predictedClassIndex = (int) mModel.classifyInstance(feature_vector);
			className = feature_vector.classAttribute().value(predictedClassIndex);
			//Log.v(TAG, "predicted the audio class: " + className);
{'fraction_non_alphanumeric': 0.09576465679115377, 'fraction_numerical': 0.005424577508867098, 'mean_word_length': 5.358090185676392, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2896155', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1451, 'n_words': 347}