package smm.dspp;
import anywheresoftware.b4a.AbsObjectWrapper;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Events;
import be.tarsos.dsp.AudioDispatcher;
import be.tarsos.dsp.AudioProcessor;
import be.tarsos.dsp.AudioEvent;
import be.tarsos.dsp.io.android.AudioDispatcherFactory;
import be.tarsos.dsp.pitch.PitchDetectionHandler;
import be.tarsos.dsp.pitch.PitchDetectionResult;
import be.tarsos.dsp.pitch.PitchProcessor;
import be.tarsos.dsp.pitch.PitchProcessor.PitchEstimationAlgorithm;
import be.tarsos.dsp.pitch.Goertzel;
import be.tarsos.dsp.pitch.Goertzel.FrequenciesDetectedHandler;
@Author("SMM")
@Version(0.02f)
@Permissions(values={"android.permission.RECORD_AUDIO"})
@ActivityObject
@ShortName("DSP")
@Events(values={"pitchdata(data as float)",
"GoertzelData(DetectedFrequencies() as Double)"})
@DependsOn(values={"TarsosDSP-Android-2.0-bin"})
public class dspba extends AbsObjectWrapper<AudioDispatcher> {
private BA ba;
private String eventName;
private AudioDispatcher obj;
public void Initialize(BA ba, String EventName) {
}
/**
* Initializes the DSP Object as a Single Pitch Frequency Detector
*EventName = Name of Object/Event that will be raised with the frequency value
*SampleRate = Sampling frequency of the audio
*AudioBufferSize = Size of the buffer where the sampled audio will be stored
*bufferOverlap = Amount of space where the buffers are allowed to overlap
*/
// public void StartPitchDetect(BA ba, String EventName, int sampleRate, int audioBufferSize, int bufferOverlap) {
// _initializepitch(ba, EventName, sampleRate, audioBufferSize, bufferOverlap);
// }
/**
* Initializes the DSP Object as a Goertzel Frequency Detector
*EventName = Name of Object/Event that will be raised with the frequency value
*SampleRate = Sampling frequency of the audio
*AudioBufferSize = Size of the buffer where the sampled audio will be stored
*bufferOverlap = Amount of space where the buffer is allowed to overlap
*DetectFrequencies = Array of frequencies at which to be detected
*/
public void StartGoertzelDetect(BA ba, String EventName, int sampleRate, int audioBufferSize, int bufferOverlap, double[] DetectFrequencies) {
_initializegoertzel(ba, EventName, sampleRate, audioBufferSize, bufferOverlap, DetectFrequencies);
}
/*
@Hide
public void _initializepitch(final BA ba, String EventName, int sampleRate, int audioBufferSize, int bufferOverlap) {
this.ba = ba;
this.eventName = EventName.toLowerCase(BA.cul);
this.obj = AudioDispatcherFactory.fromDefaultMicrophone(sampleRate, audioBufferSize, bufferOverlap);
setObject(this.obj);
((AudioDispatcher) getObject()).addAudioProcessor(new PitchProcessor(PitchEstimationAlgorithm.FFT_YIN, (float) sampleRate, audioBufferSize, new PitchDetectionHandler() {
public void handlePitch(PitchDetectionResult pitchDetectionResult, AudioEvent audioEvent) {
final float pitch = pitchDetectionResult.getPitch();
ba.activity.runOnUiThread(new Runnable() {
public void run() {
if (ba.subExists(dspba.this.eventName + "_pitchdata")) {
ba.raiseEvent2(ba, false, dspba.this.eventName + "_pitchdata", true, new Object[]{Float.valueOf(pitch)});
}
}
});
}
}));
new Thread((Runnable) getObject(), "Audio Dispatcher").start();
if (this.eventName.length() <= 0) {
}
}
*/
@Hide
public void _initializegoertzel(final BA ba, String EventName, int sampleRate, int audioBufferSize, int bufferOverlap, double [] DetectFrequencies) {
this.ba = ba;
this.eventName = EventName.toLowerCase(BA.cul);
this.obj = AudioDispatcherFactory.fromDefaultMicrophone(sampleRate, audioBufferSize, bufferOverlap);
setObject(this.obj);
((AudioDispatcher) getObject()).addAudioProcessor(goertzelAudioProcessor);
new Thread((Runnable) getObject(), "Audio Dispatcher").start();
if (this.eventName.length() <= 0) {
}
}
private final AudioProcessor goertzelAudioProcessor = new Goertzel(44100, 256, new double[]{ 1,2,3,4,5,6,7,8,9,10 }, new FrequenciesDetectedHandler() {
@Override
public void handleDetectedFrequencies(final double[] frequencies, final double[] powers, final double[] allFrequencies, final double allPowers[]) {
//ba.activity.runOnUiThread(new Runnable() {
//public void run() {
if (ba.subExists(dspba.this.eventName + "_goertzeldata")) {
ba.raiseEvent2(ba, false, dspba.this.eventName + "_goertzeldata", true, frequencies.length);
}
//}
//});
}
});
}