be.tarsos.dsp.pitch.PitchDetectionResult Java Examples

The following examples show how to use be.tarsos.dsp.pitch.PitchDetectionResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From MagicLight-Controller with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #2
Source File: Pitch.java    From ssj with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void transform(Stream[] stream_in, Stream stream_out) throws SSJFatalException
{
    float[] data = stream_in[0].ptrF();
    float[] out = stream_out.ptrF();

    PitchDetectionResult result = _detector.getPitch(data);

    float pitch = result.getPitch();
    if (pitch > options.maxPitch.get() || pitch < options.minPitch.get())
    {
        pitch = -1;
    }

    int dim = 0;

    if (options.computePitch.get())
    {
        out[dim++] = pitch;
    }

    if (options.computePitchEnvelope.get()) {
        if (pitch < 0) {
            out[dim++] = _lastPitch;
        } else {
            out[dim++] = pitch;
            _lastPitch = pitch;
        }
    }

    if (options.computeVoicedProb.get())
    {
        out[dim++] = result.getProbability();
    }

    if (options.computePitchedState.get())
    {
        out[dim++] = (result.isPitched() && pitch > 0) ? 1.0f : 0.0f;
    }
}
 
Example #3
Source File: PitchResyntheziser.java    From cythara with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void handlePitch(PitchDetectionResult pitchDetectionResult,
		AudioEvent audioEvent) {
	double frequency = pitchDetectionResult.getPitch();
	
	if(frequency==-1){
		frequency=prevFrequency;
	}else{
		if(previousFrequencies.length!=0){
			//median filter
			//store and adjust pointer
			previousFrequencies[previousFrequencyIndex] = frequency;
			previousFrequencyIndex++;
			previousFrequencyIndex %= previousFrequencies.length;
			//sort to get median frequency
			double[] frequenciesCopy = previousFrequencies.clone();
			Arrays.sort(frequenciesCopy);
			//use the median as frequency
			frequency = frequenciesCopy[frequenciesCopy.length/2];
		}
		
		prevFrequency = frequency;
	}
	
	

	final double twoPiF = 2 * Math.PI * frequency;
	float[] audioBuffer = audioEvent.getFloatBuffer();
	float[] envelope = null;
	if(followEnvelope){
		envelope = audioBuffer.clone();
		envelopeFollower.calculateEnvelope(envelope);
	}
	
	for (int sample = 0; sample < audioBuffer.length; sample++) {
		double time =   sample / samplerate;
		double wave =  Math.sin(twoPiF * time + phase);
		if(!usePureSine){
			wave += 0.05 * Math.sin(twoPiF * 4 * time + phaseFirst);
			wave += 0.01 * Math.sin(twoPiF * 8 * time + phaseSecond);
		}			
		audioBuffer[sample] = (float) wave;
		if(followEnvelope){
			audioBuffer[sample] = audioBuffer[sample] * envelope[sample];
		}
	}
	
	double timefactor = twoPiF * audioBuffer.length / samplerate; 
	phase =  timefactor + phase;
	if(!usePureSine){
		phaseFirst = 4 * timefactor + phaseFirst;
		phaseSecond = 8 * timefactor + phaseSecond;
	}
}
 
Example #4
Source File: RecordingMfccService.java    From android-speaker-audioanalysis with MIT License 2 votes vote down vote up
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";
			    }
			});
			
			*/
			
		}
	}));
	

	
}