Java源码示例:be.tarsos.dsp.pitch.PitchProcessor

示例1
private void startDispatch() {
    dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050, 1024, 0);
    uiThread = new Handler();
    PitchDetectionHandler pdh = (PitchDetectionResult result, AudioEvent audioEven) -> uiThread.post(() -> {
        final float pitchInHz = result.getPitch();
        int pitch =  pitchInHz > 0 ? (int) pitchInHz : 1;

        if(pitch > 1 && mConnected) {
            if((pitch - lastPitch) >= sensitive * 10) {
                Random random = new Random();
                byte[] rgb = getLedBytes(random.nextInt(600000000) + 50000);
                controlLed(rgb);
            }

            if(minPitch > pitch)
                minPitch = pitch;
        }

        lastPitch = pitch;
    });

    processor = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
    dispatcher.addAudioProcessor(processor);
    listeningThread = new Thread(dispatcher);
    listeningThread.start();
}
 
示例2
@Override
protected Void doInBackground(Void... params) {
    PitchDetectionHandler pitchDetectionHandler = (pitchDetectionResult, audioEvent) -> {

        if (isCancelled()) {
            stopAudioDispatcher();
            return;
        }

        if (!IS_RECORDING) {
            IS_RECORDING = true;
            publishProgress();
        }

        float pitch = pitchDetectionResult.getPitch();

        if (pitch != -1) {
            PitchDifference pitchDifference = PitchComparator.retrieveNote(pitch);

            pitchDifferences.add(pitchDifference);

            if (pitchDifferences.size() >= MIN_ITEMS_COUNT) {
                PitchDifference average =
                        Sampler.calculateAverageDifference(pitchDifferences);

                publishProgress(average);

                pitchDifferences.clear();
            }
        }
    };

    PitchProcessor pitchProcessor = new PitchProcessor(PitchEstimationAlgorithm.FFT_YIN,
            SAMPLE_RATE,
            BUFFER_SIZE, pitchDetectionHandler);

    audioDispatcher = AudioDispatcherFactory.fromDefaultMicrophone(SAMPLE_RATE,
            BUFFER_SIZE, OVERLAP);

    audioDispatcher.addAudioProcessor(pitchProcessor);

    audioDispatcher.run();

    return null;
}
 
示例3
public void startPitchDetection()
{
       Log.d(TAG, "startPitchDetection");

	//algorithm, sampleRate, bufferSize, handler
	dispatcher.addAudioProcessor(new PitchProcessor(PitchEstimationAlgorithm.FFT_YIN, 16000, 1024, new PitchDetectionHandler() {
		
		@Override
		public void handlePitch(PitchDetectionResult pitchDetectionResult, AudioEvent audioEvent) {
			
			//-1 means no sound 
			final float pitchInHz = pitchDetectionResult.getPitch();
			//Log.i("Pitch", String.valueOf(pitchInHz));
			
			if(pitchInHz == -1)
	    		sendResult("Silent");
	    	else
	    		sendResult("Speaking");
			
			
			//call showPitchOnUI(pitchInHz) 
			/*runOnUiThread(new Runnable() {
			     @Override
			     public void run() {
			    	
			    	 
			    	 
			    	if(pitchInHz == -1)
			    		uiMessage = "Silent";
			    	else
			    		uiMessage = "Speaking";
			    }
			});
			
			*/
			
		}
	}));
	

	
}