Java Code Examples for be.tarsos.dsp.AudioEvent#getFloatBuffer()

The following examples show how to use be.tarsos.dsp.AudioEvent#getFloatBuffer() . 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: IIRFilter.java    From cythara with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] audioFloatBuffer = audioEvent.getFloatBuffer();
	
	for (int i = audioEvent.getOverlap(); i < audioFloatBuffer.length; i++) {
		//shift the in array
		System.arraycopy(in, 0, in, 1, in.length - 1);
		in[0] = audioFloatBuffer[i];

		//calculate y based on a and b coefficients
		//and in and out.
		float y = 0;
		for(int j = 0 ; j < a.length ; j++){
			y += a[j] * in[j];
		}			
		for(int j = 0 ; j < b.length ; j++){
			y += b[j] * out[j];
		}
		//shift the out array
		System.arraycopy(out, 0, out, 1, out.length - 1);
		out[0] = y;
		
		audioFloatBuffer[i] = y;
	} 
	return true;
}
 
Example 2
Source File: HaarWaveletCoder.java    From cythara with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {

	float[] audioBuffer = audioEvent.getFloatBuffer();
	float[] sortBuffer = new float[audioBuffer.length];
	transform.transform(audioEvent.getFloatBuffer());

	for (int i = 0; i < sortBuffer.length; i++) {
		sortBuffer[i] = Math.abs(audioBuffer[i]);
	}
	Arrays.sort(sortBuffer);

	double threshold = sortBuffer[compression];

	for (int i = 0; i < audioBuffer.length; i++) {
		if (Math.abs(audioBuffer[i]) <= threshold) {
			audioBuffer[i] = 0;
		}
	}

	return true;
}
 
Example 3
Source File: Daubechies4WaveletCoder.java    From cythara with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {

	float[] audioBuffer = audioEvent.getFloatBuffer();
	float[] sortBuffer = new float[audioBuffer.length];

	transform.forwardTrans(audioBuffer);

	for (int i = 0; i < sortBuffer.length; i++) {
		sortBuffer[i] = Math.abs(audioBuffer[i]);
	}
	Arrays.sort(sortBuffer);

	double threshold = sortBuffer[compression];

	for (int i = 0; i < audioBuffer.length; i++) {
		if (Math.abs(audioBuffer[i]) <= threshold) {
			audioBuffer[i] = 0;
		}
	}
	return true;
}
 
Example 4
Source File: DelayEffect.java    From cythara with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] audioFloatBuffer = audioEvent.getFloatBuffer();
	int overlap = audioEvent.getOverlap();
		
	for(int i = overlap ; i < audioFloatBuffer.length ; i++){
		if(position >= echoBuffer.length){
			position = 0;
		}
		
		//output is the input added with the decayed echo 		
		audioFloatBuffer[i] = audioFloatBuffer[i] + echoBuffer[position] * decay;
		//store the sample in the buffer;
		echoBuffer[position] = audioFloatBuffer[i];
		
		position++;
	}
	
	applyNewEchoLength();
	
	return true;
}
 
Example 5
Source File: RateTransposer.java    From cythara with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] src = audioEvent.getFloatBuffer();
	//Creation of float array in loop could be prevented if src.length is known beforehand...
	//Possible optimization is to instantiate it outside the loop and get a pointer to the 
	//array here, in the process method method.
	float[] out = new float[(int) (src.length * factor)];
	r.process(factor, src, 0, src.length, false, out, 0, out.length);
	//The size of the output buffer changes (according to factor). 
	audioEvent.setFloatBuffer(out);
	//Update overlap offset to match new buffer size
	audioEvent.setOverlap((int) (audioEvent.getOverlap() * factor));
	return true;
}
 
Example 6
Source File: SoundTouchRateTransposer.java    From cythara with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	int i, used;
	float[] src = audioEvent.getFloatBuffer();
	float[] dest = new float[(int) Math.round(audioEvent.getBufferSize() / rate)];
    used = 0;
    i = 0;

    // Process the last sample saved from the previous call first...
    while (slopeCount <= 1.0f) {
        dest[i] = (float)((1.0f - slopeCount) * prevSample + slopeCount * src[0]);
        i++;
        slopeCount += rate;
    }
    slopeCount -= 1.0f;
    end:
       while(true){
           while (slopeCount > 1.0f) {
               slopeCount -= 1.0f;
               used++;
               if (used >= src.length - 1) 
               	break end;
           }
           if(i < dest.length){
           	dest[i] = (float)((1.0f - slopeCount) * src[used] + slopeCount * src[used + 1]);
           }
           i++;
           slopeCount += rate;
       }
    
    //Store the last sample for the next round
    prevSample = src[src.length - 1];
    dispatcher.setStepSizeAndOverlap(dest.length, 0);
    audioEvent.setFloatBuffer(dest);
	return true;
}
 
Example 7
Source File: PercussionOnsetDetector.java    From cythara with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] audioFloatBuffer = audioEvent.getFloatBuffer();
	this.processedSamples += audioFloatBuffer.length;
	this.processedSamples -= audioEvent.getOverlap();

	fft.forwardTransform(audioFloatBuffer);
	fft.modulus(audioFloatBuffer, currentMagnitudes);
	int binsOverThreshold = 0;
	for (int i = 0; i < currentMagnitudes.length; i++) {
		if (priorMagnitudes[i] > 0.f) {
			double diff = 10 * Math.log10(currentMagnitudes[i]
					/ priorMagnitudes[i]);
			if (diff >= threshold) {
				binsOverThreshold++;
			}
		}
		priorMagnitudes[i] = currentMagnitudes[i];
	}

	if (dfMinus2 < dfMinus1
			&& dfMinus1 >= binsOverThreshold
			&& dfMinus1 > ((100 - sensitivity) * audioFloatBuffer.length) / 200) {
		float timeStamp = processedSamples / sampleRate;
		handler.handleOnset(timeStamp,-1);
	}

	dfMinus2 = dfMinus1;
	dfMinus1 = binsOverThreshold;

	return true;
}
 
Example 8
Source File: GeneralizedGoertzel.java    From cythara with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	
	float[] x = audioEvent.getFloatBuffer();
	WindowFunction f  = new HammingWindow();
	f.apply(x);
	for (int j = 0; j < frequenciesToDetect.length; j++) {
		double pik_term = 2 * Math.PI * indvec[j]/(float) audioEvent.getBufferSize(); 
		double cos_pik_term2 = Math.cos(pik_term) * 2;
		Complex cc = new Complex(0,-1*pik_term).exp();
		double s0=0;
		double s1=0;
		double s2=0;
		
		for(int i = 0 ; i < audioEvent.getBufferSize() ; i++ ){
			s0 = x[i]+cos_pik_term2*s1-s2;
			s2=s1;
			s1=s0;
		}
		s0 = cos_pik_term2 * s1 - s2;
		calculatedComplex[j] = cc.times(new Complex(-s1,0)).plus(new Complex(s0,0));
		calculatedPowers[j] = calculatedComplex[j].mod();
	}
	
	handler.handleDetectedFrequencies(audioEvent.getTimeStamp(),frequenciesToDetect.clone(), calculatedPowers.clone(),
			frequenciesToDetect.clone(), calculatedPowers.clone());
	
	return true;
}
 
Example 9
Source File: Goertzel.java    From cythara with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] audioFloatBuffer = audioEvent.getFloatBuffer();
	double skn0, skn1, skn2;
	int numberOfDetectedFrequencies = 0;
	for (int j = 0; j < frequenciesToDetect.length; j++) {
		skn0 = skn1 = skn2 = 0;
		for (int i = 0; i < audioFloatBuffer.length; i++) {
			skn2 = skn1;
			skn1 = skn0;
			skn0 = precalculatedCosines[j] * skn1 - skn2
					+ audioFloatBuffer[i];
		}
		double wnk = precalculatedWnk[j];
		calculatedPowers[j] = 20 * Math.log10(Math.abs(skn0 - wnk * skn1));
		if (calculatedPowers[j] > POWER_THRESHOLD) {
			numberOfDetectedFrequencies++;
		}
	}

	if (numberOfDetectedFrequencies > 0) {
		double[] frequencies = new double[numberOfDetectedFrequencies];
		double[] powers = new double[numberOfDetectedFrequencies];
		int index = 0;
		for (int j = 0; j < frequenciesToDetect.length; j++) {
			if (calculatedPowers[j] > POWER_THRESHOLD) {
				frequencies[index] = frequenciesToDetect[j];
				powers[index] = calculatedPowers[j];
				index++;
			}
		}
		handler.handleDetectedFrequencies(audioEvent.getTimeStamp(),frequencies, powers,
				frequenciesToDetect.clone(), calculatedPowers.clone());
	}

	return true;
}
 
Example 10
Source File: AmplitudeLFO.java    From cythara with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] buffer = audioEvent.getFloatBuffer();
	double sampleRate = audioEvent.getSampleRate();
	double twoPiF = 2 * Math.PI * frequency;
	double time = 0;
	for(int i = 0 ; i < buffer.length ; i++){
		time = i / sampleRate;
		float gain =  (float) (scaleParameter * Math.sin(twoPiF * time + phase));
		buffer[i] = gain * buffer[i];
	}
	phase = twoPiF * buffer.length / sampleRate + phase;
	return true;
}
 
Example 11
Source File: SineGenerator.java    From cythara with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] buffer = audioEvent.getFloatBuffer();
	double sampleRate = audioEvent.getSampleRate();
	double twoPiF = 2 * Math.PI * frequency;
	double time = 0;
	for(int i = 0 ; i < buffer.length ; i++){
		time = i / sampleRate;
		buffer[i] += (float) (gain * Math.sin(twoPiF * time + phase));
	}
	phase = twoPiF * buffer.length / sampleRate + phase;
	return true;
}
 
Example 12
Source File: NoiseGenerator.java    From cythara with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] buffer = audioEvent.getFloatBuffer();
	for(int i = 0 ; i < buffer.length ; i++){
		buffer[i] += (float) (Math.random() * gain);
	}
	return true;
}
 
Example 13
Source File: FlangerEffect.java    From cythara with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] audioFloatBuffer = audioEvent.getFloatBuffer();
	int overlap = audioEvent.getOverlap();

	// Divide f by two, to counter rectifier below, which effectively
	// doubles the frequency
	double twoPIf = 2 * Math.PI * lfoFrequency / 2.0;
	double time = audioEvent.getTimeStamp();
	double timeStep = 1.0 / sampleRate;

	for (int i = overlap; i < audioFloatBuffer.length; i++) {

		// Calculate the LFO delay value with a sine wave:
		//fix by hans bickel
		double lfoValue = (flangerBuffer.length - 1) * Math.sin(twoPIf * time);
		// add a time step, each iteration
		time += timeStep;

		// Make the delay a positive integer
		int delay = (int) (Math.round(Math.abs(lfoValue)));
		
		// store the current sample in the delay buffer;
		if (writePosition >= flangerBuffer.length) {
			writePosition = 0;
		}
		flangerBuffer[writePosition] = audioFloatBuffer[i];

		// find out the position to read the delayed sample:
		int readPosition = writePosition - delay;
		if (readPosition < 0) {
			readPosition += flangerBuffer.length;
		}

		//increment the write position
		writePosition++;

		// Output is the input summed with the value at the delayed flanger
		// buffer
		audioFloatBuffer[i] = dry * audioFloatBuffer[i] + wet * flangerBuffer[readPosition];
	}
	return true;
}
 
Example 14
Source File: HaarWaveletDecoder.java    From cythara with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] audioBuffer = audioEvent.getFloatBuffer();
	transform.inverseTransform(audioBuffer);
	return true;
}
 
Example 15
Source File: Daubechies4WaveletDecoder.java    From cythara with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] audioBuffer = audioEvent.getFloatBuffer();
	transform.inverseTrans(audioBuffer);
	return true;
}
 
Example 16
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 17
Source File: QIFFTEventPointProcessor.java    From Panako with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	//clone since the buffer is reused to slide
	float[] buffer = audioEvent.getFloatBuffer();
	float[] zeroPaddedData = new float[zeropaddingFactor*512];
	int offset = (buffer.length * zeropaddingFactor - buffer.length)/2;
	for(int i = offset ; i < offset + buffer.length ;i ++){
		zeroPaddedData[i] = buffer[i-offset];
	}
	
	//calculate the fft
	fft.forwardTransform(zeroPaddedData);
	
	//store the magnitudes (moduli) in magnitudes
	fft.modulus(zeroPaddedData, magnitudes[magnitudesIndex]);
			
	//calculate the natural logarithm
	//It is not really needed, and skipped since it is very computationally expensive
	//log();
	
	//run a maximum filter on the frame
	maxFilterVertical.filter(magnitudes[magnitudesIndex]);
	previousMaxMagnitudes.put(analysisFrameIndex,maxFilterVertical.getMaxVal());

	//run a minimum filter on the frame
	minFilterVertical.filter(magnitudes[magnitudesIndex]);
	previousMinMagnitudes.put(analysisFrameIndex,minFilterVertical.getMinVal());
	
			
	//store the frame magnitudes
	previousMagintudes.put(analysisFrameIndex, magnitudes[magnitudesIndex]);
	
	//find the horziontal minima and maxima
	if(previousMaxMagnitudes.size()==longestFilterWindowSize){
		horizontalFilter();
		//Remove analysis frames that are not needed any more:
		//previousMaxFrames.removeFirst();
		previousMaxMagnitudes.remove(analysisFrameIndex-longestFilterWindowSize+1);
		previousMinMagnitudes.remove(analysisFrameIndex-longestFilterWindowSize+1);
		previousMagintudes.remove(analysisFrameIndex-longestFilterWindowSize+1);
	}
			
	//magnitude index counter
	magnitudesIndex++;
	if(magnitudesIndex == magnitudes.length){
		magnitudesIndex=0;
	}
	
	//Increment analysis frame counter
	analysisFrameIndex++;
	
	return true;
}
 
Example 18
Source File: PitchProcessor.java    From cythara with GNU General Public License v3.0 3 votes vote down vote up
@Override
public boolean process(AudioEvent audioEvent) {
	float[] audioFloatBuffer = audioEvent.getFloatBuffer();
	
	PitchDetectionResult result = detector.getPitch(audioFloatBuffer);
	
	
	handler.handlePitch(result,audioEvent);
	return true;
}