JorenSix / TarsosDSP

A Real-Time Audio Processing Framework in Java
http://0110.be/tag/TarsosDSP
GNU General Public License v3.0
1.97k stars 472 forks source link

calling dispatcher.stop() #200

Open ilyosbekkk opened 3 years ago

ilyosbekkk commented 3 years ago

Hi, I am implementing AudioDispatcher in my application, but I am not sure how to start and stop it properly, here is my code:


public class MyAudioFeatureRecorder { private static final String TAG = "MyAudioFeatureRecorder"; Thread thread; private boolean runThread; private final AudioDispatcher dispatcher; static int dataSourceId; static SilenceDetector silenceDetector; MyAudioProcessor mainProcessor; SharedPreferences configPrefs; Context context; public static long PREVIOUS_TIME; // endregion

public MyAudioFeatureRecorder(Context context) {

    this.context = context;
    configPrefs = context.getSharedPreferences("Configurations", Context.MODE_PRIVATE);
    if (DbMgr.getDB() == null)
        DbMgr.init(context);
    PREVIOUS_TIME = 0;
    int SAMPLING_RATE = 11025;
    int AUDIO_BUFFER_SIZE = 1024;
    dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(SAMPLING_RATE, AUDIO_BUFFER_SIZE, 512);
    double SILENCE_THRESHOLD = -65.0D;
    silenceDetector = new SilenceDetector(SILENCE_THRESHOLD, false);
    dataSourceId = configPrefs.getInt("SOUND_DATA", -1);

    Log.d(TAG, "MyAudioFeatureRecorder: Service  is  Running...");
    mainProcessor = new MyAudioProcessor(configPrefs);

    if (dispatcher != null) {
        dispatcher.addAudioProcessor(silenceDetector);
        dispatcher.addAudioProcessor(mainProcessor);
    }
}

public void start() {
    thread = new Thread(() -> {

        while (runThread) {
            Log.e(TAG, "start: waiting");
            dispatcher.run();

        }

    });
    runThread = true;
    thread.start();
}

public void stop() {
    if (runThread) {
        runThread = false;
        thread.interrupt();
        try {
            dispatcher.stop() 
            Log.e(TAG, "stop: dispatcher stop");
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.e("TAG", "stop: DeviceModel Stop");
    }
}

private static class MyAudioProcessor implements be.tarsos.dsp.AudioProcessor {
    SharedPreferences confPrefs;

    public MyAudioProcessor(SharedPreferences confPrefs) {
        this.confPrefs = confPrefs;
    }

    @Override
    public boolean process(AudioEvent audioEvent) {
        long nowTime = System.currentTimeMillis();

        if (silenceDetector.currentSPL() >= -110.0D) {
            if (dataSourceId != -1 && nowTime >= PREVIOUS_TIME + 10 * 1000) {
                Log.e(TAG, "process: SoundData  is  saving");
                DbMgr.saveMixedData(dataSourceId, nowTime, 1.0f, nowTime, silenceDetector.currentSPL(), "ENERGY");

                PREVIOUS_TIME = System.currentTimeMillis();
            }
        }
        return true;
    }

    @Override
    public void processingFinished() {
    }
}

}


In my Android application, the current SPL should be saved only once in 10 seconds, it is working correctly but when I call the stop() function (interrupts the running thread) UI is freezing and I know that UI is freezing because of improper use of dispatcher.stop(). Where exactly should I call dispatcher.stop() to properly stop the dispatcher?

Thank you

zukarusan commented 2 years ago

I might be a latecomer. But you might want to try to use a thread to stop the dispatcher