Search in sources :

Example 1 with AudioProcessor

use of be.tarsos.dsp.AudioProcessor in project PICKSARI by HyunJunYANG.

the class PitchDetect method detect.

// 음역대를 측정하는 함수
public void detect() {
    AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050, 1024, 0);
    // 0은 초기 1은 성공 2는 실패
    pitchSuccess = 0;
    Log.d(LOG_TAG, "start");
    // PitchDetect Thread
    PitchDetectionHandler pdh = new PitchDetectionHandler() {

        @Override
        public void handlePitch(PitchDetectionResult result, AudioEvent e) {
            final float pitchInHz = result.getPitch();
            runOnUiThread(new Runnable() {

                @SuppressLint("ResourceType")
                @Override
                public void run() {
                    // 음이 기준보다 높으면 이미지바꾸고 sleep
                    if (!thread.isInterrupted()) {
                        if (pitchInHz > hz[hzIndex]) {
                            Log.d(LOG_TAG, String.valueOf(hz[hzIndex]));
                            pitchText.setText(scale[scaleIndex]);
                            pitch = pitchInHz;
                            // 인덱스를 늘린다
                            hzIndex = hzIndex + 1;
                            scaleIndex = scaleIndex + 1;
                            pitchSuccess = 1;
                            // 타이머를 죽인다. 성공하면 타이머 리셋해야하기 때문
                            timer.cancel();
                            manPicture.setImageResource(R.drawable.man_3);
                            // PostDelay 그림을 바꾸기 위한 딜레이
                            try {
                                Log.d(LOG_TAG, "ThreadSleepStart");
                                // thread.sleep(1000);
                                handler.postDelayed(runnable, 1000);
                                // 타이머 리셋
                                detectTime();
                                // thread.interrupt();
                                // Log.d(LOG_TAG, "ThreadSleepInterrupted");
                                Log.d(LOG_TAG, "ThreadSleepRestart");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            // 음이 기준보다 낮으면
                            // Log.d(LOG_TAG, "ThreadSleep Pitch is low.");
                            pitchSuccess = 2;
                        }
                    }
                }
            });
        }
    };
    // 선언
    AudioProcessor p = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
    dispatcher.addAudioProcessor(p);
    thread = new Thread(dispatcher, "Audio Dispatcher");
    thread.start();
}
Also used : PitchDetectionHandler(be.tarsos.dsp.pitch.PitchDetectionHandler) AudioProcessor(be.tarsos.dsp.AudioProcessor) PitchProcessor(be.tarsos.dsp.pitch.PitchProcessor) AudioEvent(be.tarsos.dsp.AudioEvent) SuppressLint(android.annotation.SuppressLint) AudioDispatcher(be.tarsos.dsp.AudioDispatcher) PitchDetectionResult(be.tarsos.dsp.pitch.PitchDetectionResult)

Example 2 with AudioProcessor

use of be.tarsos.dsp.AudioProcessor in project PICKSARI by HyunJunYANG.

the class TarsosDSP method detect.

public boolean detect() {
    AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050, 1024, 0);
    Log.d("StartaudioDetect", "start");
    PitchDetectionHandler pdh = new PitchDetectionHandler() {

        @Override
        public void handlePitch(PitchDetectionResult result, AudioEvent e) {
            final float pitchInHz = result.getPitch();
            thread = new Thread(new Runnable() {

                @Override
                public void run() {
                    if (pitchInHz > pitch) {
                        Log.d("StartAudioDetect", "start2");
                        pitch = pitchInHz;
                        bool = true;
                    // sendMessage(pitch);
                    /*
                            text = (TextView) findViewById(R.id.textView1);
                            text.setText("" + pitchInHz);
                            pitchChange();
                            */
                    } else {
                        bool = false;
                    }
                }
            });
        }
    };
    AudioProcessor p = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
    dispatcher.addAudioProcessor(p);
    thread = new Thread(dispatcher, "Audio Dispatcher");
    thread.start();
    return bool;
}
Also used : PitchDetectionHandler(be.tarsos.dsp.pitch.PitchDetectionHandler) AudioProcessor(be.tarsos.dsp.AudioProcessor) PitchProcessor(be.tarsos.dsp.pitch.PitchProcessor) AudioEvent(be.tarsos.dsp.AudioEvent) AudioDispatcher(be.tarsos.dsp.AudioDispatcher) PitchDetectionResult(be.tarsos.dsp.pitch.PitchDetectionResult)

Aggregations

AudioDispatcher (be.tarsos.dsp.AudioDispatcher)2 AudioEvent (be.tarsos.dsp.AudioEvent)2 AudioProcessor (be.tarsos.dsp.AudioProcessor)2 PitchDetectionHandler (be.tarsos.dsp.pitch.PitchDetectionHandler)2 PitchDetectionResult (be.tarsos.dsp.pitch.PitchDetectionResult)2 PitchProcessor (be.tarsos.dsp.pitch.PitchProcessor)2 SuppressLint (android.annotation.SuppressLint)1