plectrum

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 3f621c703dfc51c65e9a7ef5a1772570039fc704
parent d76fc23d78b0b713989e833f3585ac3ca957bff2
Author: Taco <SkytkRSfan3895@gmail.com>
Date:   Sat,  2 Feb 2019 15:48:38 -0500

(1/4) update TarsosDSP library to [c26e500](https://github.com/JorenSix/TarsosDSP/tree/c26e5004e203ee79be1ec25c2603b1f11b69d276)

Signed-off-by: Taco <SkytkRSfan3895@gmail.com>

Diffstat:
Mapp/src/main/java/be/tarsos/dsp/AudioEvent.java | 6+++---
Mapp/src/main/java/be/tarsos/dsp/AudioGenerator.java | 16++++++++--------
Mapp/src/main/java/be/tarsos/dsp/AutoCorrelation.java | 4++--
Mapp/src/main/java/be/tarsos/dsp/ConstantQ.java | 10++++++----
Mapp/src/main/java/be/tarsos/dsp/FadeOut.java | 6+++++-
Mapp/src/main/java/be/tarsos/dsp/SilenceDetector.java | 10+++++++---
Mapp/src/main/java/be/tarsos/dsp/SpectralPeakProcessor.java | 126++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mapp/src/main/java/be/tarsos/dsp/WaveformSimilarityBasedOverlapAdd.java | 15++++++++-------
Mapp/src/main/java/be/tarsos/dsp/beatroot/Agent.java | 16++++++++--------
Mapp/src/main/java/be/tarsos/dsp/beatroot/AgentList.java | 8++++----
Mapp/src/main/java/be/tarsos/dsp/beatroot/BeatRootOnsetEventHandler.java | 8++++----
Mapp/src/main/java/be/tarsos/dsp/io/PipeDecoder.java | 3++-
Mapp/src/main/java/be/tarsos/dsp/pitch/McLeodPitchMethod.java | 6+++---
Mapp/src/main/java/be/tarsos/dsp/resample/RateTransposer.java | 2++
Mapp/src/main/java/be/tarsos/dsp/util/AudioResourceUtils.java | 3++-
Mapp/src/main/java/be/tarsos/dsp/util/ConcurrencyUtils.java | 51++++++++++++++++++++++++++-------------------------
Mapp/src/main/java/be/tarsos/dsp/util/CubicSplineFast.java | 15++++++++-------
Mapp/src/main/java/be/tarsos/dsp/util/FFMPEGDownloader.java | 10+++++-----
Mapp/src/main/java/be/tarsos/dsp/util/PeakPicker.java | 26+++++++++++++++-----------
Mapp/src/main/java/be/tarsos/dsp/util/PitchConverter.java | 8++++----
Mapp/src/main/java/be/tarsos/dsp/util/fft/FFT.java | 2+-
Mapp/src/main/java/be/tarsos/dsp/wavelet/HaarWaveletDecoder.java | 2++
Mapp/src/main/java/be/tarsos/dsp/wavelet/HaarWaveletTransform.java | 5++++-
Mapp/src/main/java/be/tarsos/dsp/writer/WaveHeader.java | 7+++++--
Mapp/src/main/java/be/tarsos/dsp/writer/WriterProcessor.java | 1+
25 files changed, 199 insertions(+), 167 deletions(-)

diff --git a/app/src/main/java/be/tarsos/dsp/AudioEvent.java b/app/src/main/java/be/tarsos/dsp/AudioEvent.java @@ -184,10 +184,10 @@ public class AudioEvent { */ public static double calculateRMS(float[] floatBuffer){ double rms = 0.0; - for (float aFloatBuffer : floatBuffer) { - rms += aFloatBuffer * aFloatBuffer; + for (int i = 0; i < floatBuffer.length; i++) { + rms += floatBuffer[i] * floatBuffer[i]; } - rms = rms / (double) floatBuffer.length; + rms = rms / Double.valueOf(floatBuffer.length); rms = Math.sqrt(rms); return rms; } diff --git a/app/src/main/java/be/tarsos/dsp/AudioGenerator.java b/app/src/main/java/be/tarsos/dsp/AudioGenerator.java @@ -99,8 +99,8 @@ public class AudioGenerator implements Runnable { } public AudioGenerator(final int audioBufferSize, final int bufferOverlap,final int samplerate){ - - audioProcessors = new CopyOnWriteArrayList<>(); + + audioProcessors = new CopyOnWriteArrayList<AudioProcessor>(); format = getTargetAudioFormat(samplerate); @@ -126,12 +126,12 @@ public class AudioGenerator implements Runnable { * @return The audio format after conversion. */ private TarsosDSPAudioFormat getTargetAudioFormat(int targetSampleRate) { - TarsosDSPAudioFormat audioFormat = new TarsosDSPAudioFormat(TarsosDSPAudioFormat.Encoding.PCM_SIGNED, - targetSampleRate, - 2 * 8, - 1, - 2, - targetSampleRate, + TarsosDSPAudioFormat audioFormat = new TarsosDSPAudioFormat(TarsosDSPAudioFormat.Encoding.PCM_SIGNED, + targetSampleRate, + 2 * 8, + 1, + 2 * 1, + targetSampleRate, ByteOrder.BIG_ENDIAN.equals(ByteOrder.nativeOrder())); return audioFormat; } diff --git a/app/src/main/java/be/tarsos/dsp/AutoCorrelation.java b/app/src/main/java/be/tarsos/dsp/AutoCorrelation.java @@ -36,8 +36,8 @@ public class AutoCorrelation implements AudioProcessor { result = 0; - for (float anAudioFloatbuffer : audioFloatbuffer) { - result += Math.abs(anAudioFloatbuffer) / (float) audioFloatbuffer.length; + for (int i=0; i<audioFloatbuffer.length; i++){ + result += ((float)Math.abs(audioFloatbuffer[i]))/(float)audioFloatbuffer.length; } return true; } diff --git a/app/src/main/java/be/tarsos/dsp/ConstantQ.java b/app/src/main/java/be/tarsos/dsp/ConstantQ.java @@ -214,8 +214,8 @@ public class ConstantQ implements AudioProcessor { int len = (int)Math.min(Math.ceil( q * sampleRate / frequencies[i]), fftLength); for (int j = 0; j < len; j++) { - - double window = -.5 * Math.cos(2. * Math.PI * (double) j / (double) len) + .5;// Hanning Window + + double window = -.5*Math.cos(2.*Math.PI*(double)j/(double)len)+.5;; // Hanning Window // double window = -.46*Math.cos(2.*Math.PI*(double)j/(double)len)+.54; // Hamming Window window /= len; @@ -252,8 +252,10 @@ public class ConstantQ implements AudioProcessor { sKernel = new float[k * 2]; int[] indexes = new int[k]; - System.arraycopy(cKernel, 0, sKernel, 0, k * 2); - System.arraycopy(cindexes, 0, indexes, 0, k); + for (int j = 0; j < k * 2; j++) + sKernel[j] = cKernel[j]; + for (int j = 0; j < k; j++) + indexes[j] = cindexes[j]; // Normalize fft output for (int j = 0; j < sKernel.length; j++) diff --git a/app/src/main/java/be/tarsos/dsp/FadeOut.java b/app/src/main/java/be/tarsos/dsp/FadeOut.java @@ -1,5 +1,9 @@ package be.tarsos.dsp; +import be.tarsos.dsp.AudioEvent; +import be.tarsos.dsp.AudioProcessor; +import be.tarsos.dsp.GainProcessor; + public class FadeOut implements AudioProcessor { // VARIABLES @@ -27,7 +31,7 @@ public class FadeOut implements AudioProcessor public boolean process(AudioEvent audioEvent) { // Don't do anything before the beginning of Fade Out - if (isFadeOut) + if(isFadeOut==true) { if(firstTime==-1) firstTime=audioEvent.getTimeStamp(); diff --git a/app/src/main/java/be/tarsos/dsp/SilenceDetector.java b/app/src/main/java/be/tarsos/dsp/SilenceDetector.java @@ -124,9 +124,13 @@ public class SilenceDetector implements AudioProcessor { public boolean process(AudioEvent audioEvent) { boolean isSilence = isSilence(audioEvent.getFloatBuffer()); //break processing chain on silence? - //break if silent - return !breakProcessingQueueOnSilence || !isSilence; -//never break the chain + if(breakProcessingQueueOnSilence){ + //break if silent + return !isSilence; + }else{ + //never break the chain + return true; + } } diff --git a/app/src/main/java/be/tarsos/dsp/SpectralPeakProcessor.java b/app/src/main/java/be/tarsos/dsp/SpectralPeakProcessor.java @@ -124,11 +124,11 @@ public class SpectralPeakProcessor implements AudioProcessor { frequencyEstimates = new float[bufferSize / 2]; dt = (bufferSize - overlap) / (double) sampleRate; - cbin = dt * sampleRate / (double) bufferSize; + cbin = (double) (dt * sampleRate / (double) bufferSize); - inv_2pi = 1.0 / (2.0 * Math.PI); - inv_deltat = 1.0 / dt; - inv_2pideltat = inv_deltat * inv_2pi; + inv_2pi = (double) (1.0 / (2.0 * Math.PI)); + inv_deltat = (double) (1.0 / dt); + inv_2pideltat = (double) (inv_deltat * inv_2pi); this.sampleRate = sampleRate; @@ -140,49 +140,18 @@ public class SpectralPeakProcessor implements AudioProcessor { // Extract the power and phase data fft.powerPhaseFFT(fftData, magnitudes, currentPhaseOffsets); } - - /** - * Calculate a noise floor for an array of magnitudes. - * - * @param magnitudes The magnitudes of the current frame. - * @param medianFilterLength The length of the median filter used to determine the noise floor. - * @param noiseFloorFactor The noise floor is multiplied with this factor to determine if the - * information is either noise or an interesting spectral peak. - * @return a float array representing the noise floor. - */ - public static float[] calculateNoiseFloor(float[] magnitudes, int medianFilterLength, float noiseFloorFactor) { - double[] noiseFloorBuffer; - float[] noisefloor = new float[magnitudes.length]; - - float median = (float) median(magnitudes.clone()); - - // Naive median filter implementation. - // For each element take a median of surrounding values (noiseFloorBuffer) - // Store the median as the noise floor. - for (int i = 0; i < magnitudes.length; i++) { - noiseFloorBuffer = new double[medianFilterLength]; - int index = 0; - for (int j = i - medianFilterLength / 2; j <= i + medianFilterLength / 2 && index < noiseFloorBuffer.length; j++) { - if (j >= 0 && j < magnitudes.length) { - noiseFloorBuffer[index] = magnitudes[j]; - } else { - noiseFloorBuffer[index] = median; - } - index++; - } - // calculate the noise floor value. - noisefloor[i] = median(noiseFloorBuffer) * (noiseFloorFactor); - } - - float rampLength = 12.0f; - for (int i = 0; i <= rampLength; i++) { - //ramp - float ramp = 1.0f; - ramp = (float) (-1 * (Math.log(i / rampLength))) + 1.0f; - noisefloor[i] = ramp * noisefloor[i]; - } - - return noisefloor; + + private void normalizeMagintudes(){ + float maxMagnitude = (float) -1e6; + for(int i = 0;i<magnitudes.length;i++){ + maxMagnitude = Math.max(maxMagnitude, magnitudes[i]); + } + + //log10 of the normalized value + //adding 75 makes sure the value is above zero, a bit ugly though... + for(int i = 1;i<magnitudes.length;i++){ + magnitudes[i] = (float) (10 * Math.log10(magnitudes[i]/maxMagnitude)) + 75; + } } @Override @@ -254,21 +223,51 @@ public class SpectralPeakProcessor implements AudioProcessor { frequencyInHertz = (float) (inv_2pideltat * phaseDelta + inv_deltat * k); } else { frequencyInHertz = (float) fft.binToHz(binIndex, sampleRate); - } - return frequencyInHertz; - } - - private void normalizeMagintudes() { - float maxMagnitude = (float) -1e6; - for (float magnitude : magnitudes) { - maxMagnitude = Math.max(maxMagnitude, magnitude); - } - - //log10 of the normalized value - //adding 75 makes sure the value is above zero, a bit ugly though... - for (int i = 1; i < magnitudes.length; i++) { - magnitudes[i] = (float) (10 * Math.log10(magnitudes[i]/maxMagnitude)) + 75; } + return frequencyInHertz; + } + + /** + * Calculate a noise floor for an array of magnitudes. + * @param magnitudes The magnitudes of the current frame. + * @param medianFilterLength The length of the median filter used to determine the noise floor. + * @param noiseFloorFactor The noise floor is multiplied with this factor to determine if the + * information is either noise or an interesting spectral peak. + * @return a float array representing the noise floor. + */ + public static float[] calculateNoiseFloor(float[] magnitudes, int medianFilterLength, float noiseFloorFactor) { + double[] noiseFloorBuffer; + float[] noisefloor = new float[magnitudes.length]; + + float median = (float) median(magnitudes.clone()); + + // Naive median filter implementation. + // For each element take a median of surrounding values (noiseFloorBuffer) + // Store the median as the noise floor. + for (int i = 0; i < magnitudes.length; i++) { + noiseFloorBuffer = new double[medianFilterLength]; + int index = 0; + for (int j = i - medianFilterLength/2; j <= i + medianFilterLength/2 && index < noiseFloorBuffer.length; j++) { + if(j >= 0 && j < magnitudes.length){ + noiseFloorBuffer[index] = magnitudes[j]; + } else{ + noiseFloorBuffer[index] = median; + } + index++; + } + // calculate the noise floor value. + noisefloor[i] = (float) (median(noiseFloorBuffer) * (noiseFloorFactor)) ; + } + + float rampLength = 12.0f; + for(int i = 0 ; i <= rampLength ; i++){ + //ramp + float ramp = 1.0f; + ramp = (float) (-1 * (Math.log(i/rampLength))) + 1.0f; + noisefloor[i] = ramp * noisefloor[i]; + } + + return noisefloor; } /** @@ -420,7 +419,8 @@ public class SpectralPeakProcessor implements AudioProcessor { return (m[middle-1] + m[middle]) / 2.0; } } - + + public static class SpectralPeak{ private final float frequencyInHertz; private final float magnitude; @@ -473,4 +473,6 @@ public class SpectralPeakProcessor implements AudioProcessor { return bin; } } + + } diff --git a/app/src/main/java/be/tarsos/dsp/WaveformSimilarityBasedOverlapAdd.java b/app/src/main/java/be/tarsos/dsp/WaveformSimilarityBasedOverlapAdd.java @@ -136,10 +136,10 @@ public class WaveformSimilarityBasedOverlapAdd implements AudioProcessor { * @param output The output buffer. * @param input The input buffer. */ - private void overlap(final float[] output, float[] input, int inputOffset) { + private void overlap(final float[] output, int outputOffset, float[] input,int inputOffset){ for(int i = 0 ; i < overlapLength ; i++){ int itemp = overlapLength - i; - output[i + 0] = (input[i + inputOffset] * i + pMidBuffer[i] * itemp) / overlapLength; + output[i + outputOffset] = (input[i + inputOffset] * i + pMidBuffer[i] * itemp ) / overlapLength; } } @@ -152,9 +152,10 @@ public class WaveformSimilarityBasedOverlapAdd implements AudioProcessor { * cross-correlation value over the overlapping period * * @param inputBuffer The input buffer + * @param postion The position where to start the seek operation, in the input buffer. * @return The best position. */ - private int seekBestOverlapPosition(float[] inputBuffer) { + private int seekBestOverlapPosition(float[] inputBuffer, int postion) { int bestOffset; double bestCorrelation, currentCorrelation; int tempOffset; @@ -172,12 +173,12 @@ public class WaveformSimilarityBasedOverlapAdd implements AudioProcessor { // over the permitted range. for (tempOffset = 0; tempOffset < seekLength; tempOffset++) { - comparePosition = tempOffset; + comparePosition = postion + tempOffset; // Calculates correlation value for the mixing position // corresponding // to 'tempOffset' - currentCorrelation = calcCrossCorr(pRefMidBuffer, inputBuffer, comparePosition); + currentCorrelation = (double) calcCrossCorr(pRefMidBuffer, inputBuffer,comparePosition); // heuristic rule to slightly favor values close to mid of the // range double tmp = (double) (2 * tempOffset - seekLength) / seekLength; @@ -228,13 +229,13 @@ public class WaveformSimilarityBasedOverlapAdd implements AudioProcessor { assert audioFloatBuffer.length == getInputBufferSize(); //Search for the best overlapping position. - int offset = seekBestOverlapPosition(audioFloatBuffer); + int offset = seekBestOverlapPosition(audioFloatBuffer,0); // Mix the samples in the 'inputBuffer' at position of 'offset' with the // samples in 'midBuffer' using sliding overlapping // ... first partially overlap with the end of the previous sequence // (that's in 'midBuffer') - overlap(outputFloatBuffer, audioFloatBuffer, offset); + overlap(outputFloatBuffer,0,audioFloatBuffer,offset); //copy sequence samples from input to output int sequenceLength = seekWindowLength - 2 * overlapLength; diff --git a/app/src/main/java/be/tarsos/dsp/beatroot/Agent.java b/app/src/main/java/be/tarsos/dsp/beatroot/Agent.java @@ -61,18 +61,18 @@ public class Agent { /** The maximum amount by which a beat can be earlier than the predicted beat time, * expressed as a fraction of the beat period. */ public static double PRE_MARGIN_FACTOR = 0.15; - - /** The default value of innerMargin, which is the maximum time (in seconds) that a + + /** The default value of innerMargin, which is the maximum time (in seconds) that a * beat can deviate from the predicted beat time without a fork occurring. */ public static final double INNER_MARGIN = 0.040; - - /** The maximum allowed deviation from the initial tempo, expressed as a fraction of the initial beat period. */ + + /** The maximum allowed deviation from the initial tempo, expressed as a fraction of the initial beat period. */ public static double MAX_CHANGE = 0.2; - - /** The slope of the penalty function for onsets which do not coincide precisely with predicted beat times. */ + + /** The slope of the penalty function for onsets which do not coincide precisely with predicted beat times. */ public static double CONF_FACTOR = 0.5; - - /** The reactiveness/inertia balance, i.e. degree of change in the tempo, is controlled by the correctionFactor + + /** The reactiveness/inertia balance, i.e. degree of change in the tempo, is controlled by the correctionFactor * variable. This constant defines its default value, which currently is not subsequently changed. The * beat period is updated by the reciprocal of the correctionFactor multiplied by the difference between the * predicted beat time and matching onset. */ diff --git a/app/src/main/java/be/tarsos/dsp/beatroot/AgentList.java b/app/src/main/java/be/tarsos/dsp/beatroot/AgentList.java @@ -53,11 +53,11 @@ public class AgentList { /** Flag for choice between sum and average beat salience values for Agent scores. * The use of summed saliences favours faster tempi or lower metrical levels. */ public static boolean useAverageSalience = false; - - /** Flag for printing debugging output. */ + + /** Flag for printing debugging output. */ public static boolean debug = false; - - /** For the purpose of removing duplicate agents, the default JND of IBI */ + + /** For the purpose of removing duplicate agents, the default JND of IBI */ public static final double DEFAULT_BI = 0.02; /** For the purpose of removing duplicate agents, the default JND of phase */ diff --git a/app/src/main/java/be/tarsos/dsp/beatroot/BeatRootOnsetEventHandler.java b/app/src/main/java/be/tarsos/dsp/beatroot/BeatRootOnsetEventHandler.java @@ -47,9 +47,9 @@ public class BeatRootOnsetEventHandler implements OnsetHandler { e.salience = salience; onsetList.add(e); } - - - /** + + + /** * Creates a new Event object representing an onset or beat. * * @param time @@ -70,7 +70,7 @@ public class BeatRootOnsetEventHandler implements OnsetHandler { * the beat is not calculated: -1 is returned. */ public void trackBeats(OnsetHandler beatHandler){ - AgentList agents = null; + AgentList agents = null; // tempo not given; use tempo induction agents = Induction.beatInduction(onsetList); agents.beatTrack(onsetList, -1); diff --git a/app/src/main/java/be/tarsos/dsp/io/PipeDecoder.java b/app/src/main/java/be/tarsos/dsp/io/PipeDecoder.java @@ -210,7 +210,8 @@ public class PipeDecoder { public double getDuration(final String resource) { double duration = -1; try { - String command = "ffmpeg -i '%resource%'"; + //use " for windows compatibility! + String command = "ffmpeg -i \"%resource%\""; command = command.replace("%resource%", resource); diff --git a/app/src/main/java/be/tarsos/dsp/pitch/McLeodPitchMethod.java b/app/src/main/java/be/tarsos/dsp/pitch/McLeodPitchMethod.java @@ -33,7 +33,7 @@ import java.util.List; * <p> * Implementation of The McLeod Pitch Method (MPM). It is described in the * article <a href= - * "http://miracle.otago.ac.nz/postgrads/tartini/papers/A_Smarter_Way_to_Find_Pitch.pdf" + * "http://miracle.otago.ac.nz/tartini/papers/A_Smarter_Way_to_Find_Pitch.pdf" * >A Smarter Way to Find Pitch</a>. According to the article: * </p> * <blockquote> <bufferCount> @@ -223,7 +223,7 @@ public final class McLeodPitchMethod implements PitchDetector { if (nsdf[tau] > SMALL_CUTOFF) { // calculates turningPointX and Y - prabolicInterpolation(tau); + parabolicInterpolation(tau); // store the turning points ampEstimates.add(turningPointY); periodEstimates.add(turningPointX); @@ -300,7 +300,7 @@ public final class McLeodPitchMethod implements PitchDetector { * @param tau * The delay tau, b value in the drawing is the tau value. */ - private void prabolicInterpolation(final int tau) { + private void parabolicInterpolation(final int tau) { final float nsdfa = nsdf[tau - 1]; final float nsdfb = nsdf[tau]; final float nsdfc = nsdf[tau + 1]; diff --git a/app/src/main/java/be/tarsos/dsp/resample/RateTransposer.java b/app/src/main/java/be/tarsos/dsp/resample/RateTransposer.java @@ -72,6 +72,8 @@ public class RateTransposer implements AudioProcessor { 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; } diff --git a/app/src/main/java/be/tarsos/dsp/util/AudioResourceUtils.java b/app/src/main/java/be/tarsos/dsp/util/AudioResourceUtils.java @@ -152,7 +152,7 @@ public class AudioResourceUtils { * @return The contents of the file. */ public static String readTextFromUrl(URL url) { - StringBuilder fubber = new StringBuilder(); + StringBuffer fubber = new StringBuffer(); try { BufferedReader in = new BufferedReader(new InputStreamReader( url.openStream())); @@ -166,4 +166,5 @@ public class AudioResourceUtils { } return fubber.toString(); } + } diff --git a/app/src/main/java/be/tarsos/dsp/util/ConcurrencyUtils.java b/app/src/main/java/be/tarsos/dsp/util/ConcurrencyUtils.java @@ -57,8 +57,6 @@ * ***** END LICENSE BLOCK ***** */ package be.tarsos.dsp.util; -import android.support.annotation.NonNull; - import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -97,15 +95,21 @@ public class ConcurrencyUtils { } - /** - * Checks if x is a power-of-two number. - * - * @param x - * @return true if x is a power-of-two number - */ - public static boolean isPowerOf2(int x) { - return x > 0 && (x & (x - 1)) == 0; - } + private static class CustomThreadFactory implements ThreadFactory { + private static final ThreadFactory defaultFactory = Executors.defaultThreadFactory(); + + private final Thread.UncaughtExceptionHandler handler; + + CustomThreadFactory(Thread.UncaughtExceptionHandler handler) { + this.handler = handler; + } + + public Thread newThread(Runnable r) { + Thread t = defaultFactory.newThread(r); + t.setUncaughtExceptionHandler(handler); + return t; + } + }; /** * Returns the number of available processors. @@ -269,20 +273,17 @@ public class ConcurrencyUtils { return (int) Math.pow(2, Math.floor(Math.log(x) / Math.log(2))); } - private static class CustomThreadFactory implements ThreadFactory { - private static final ThreadFactory defaultFactory = Executors.defaultThreadFactory(); - - private final Thread.UncaughtExceptionHandler handler; - - CustomThreadFactory(Thread.UncaughtExceptionHandler handler) { - this.handler = handler; - } - - public Thread newThread(@NonNull Runnable r) { - Thread t = defaultFactory.newThread(r); - t.setUncaughtExceptionHandler(handler); - return t; - } + /** + * Checks if x is a power-of-two number. + * + * @param x + * @return true if x is a power-of-two number + */ + public static boolean isPowerOf2(int x) { + if (x <= 0) + return false; + else + return (x & (x - 1)) == 0; } /** diff --git a/app/src/main/java/be/tarsos/dsp/util/CubicSplineFast.java b/app/src/main/java/be/tarsos/dsp/util/CubicSplineFast.java @@ -69,9 +69,9 @@ package be.tarsos.dsp.util; public class CubicSplineFast{ private int nPoints = 0; // no. of tabulated points - private double[] y; // y=f(x) tabulated function - private double[] x; // x in tabulated function f(x) - private double[] d2ydx2; // second derivatives of y + private double[] y = null; // y=f(x) tabulated function + private double[] x = null; // x in tabulated function f(x) + private double[] d2ydx2 = null; // second derivatives of y // Constructors // Constructor with data arrays initialised to arrays x and y @@ -110,7 +110,8 @@ public class CubicSplineFast{ // Primarily for use in BiCubicSplineFast public static CubicSplineFast zero(int n){ if(n<3)throw new IllegalArgumentException("A minimum of three data points is needed"); - return new CubicSplineFast(n); + CubicSplineFast aa = new CubicSplineFast(n); + return aa; } // Create a one dimensional array of cubic spline objects of length n each of array length m @@ -128,7 +129,7 @@ public class CubicSplineFast{ // for use by the cubic spline interpolation method (.interpolate) // This method follows the procedure in Numerical Methods C language procedure for calculating second derivatives public void calcDeriv(){ - double p, qn, sig, un; + double p=0.0D,qn=0.0D,sig=0.0D,un=0.0D; double[] u = new double[nPoints]; d2ydx2[0]=u[0]=0.0; @@ -154,8 +155,8 @@ public class CubicSplineFast{ // then stored for use on all subsequent calls public double interpolate(double xx){ - double h, b, a, yy; - int k; + double h=0.0D,b=0.0D,a=0.0D, yy=0.0D; + int k=0; int klo=0; int khi=this.nPoints-1; while (khi-klo > 1){ diff --git a/app/src/main/java/be/tarsos/dsp/util/FFMPEGDownloader.java b/app/src/main/java/be/tarsos/dsp/util/FFMPEGDownloader.java @@ -24,7 +24,7 @@ import java.util.logging.Logger; */ public class FFMPEGDownloader { - private static String url = "http://0110.be/releases/TarsosDSP/TarsosDSP-static-ffmpeg/"; + private static String url = "https://0110.be/releases/TarsosDSP/TarsosDSP-static-ffmpeg/"; private final String ffmpegBinary; @@ -84,9 +84,9 @@ public class FFMPEGDownloader { String operatingSystem = System.getProperty("os.name").toLowerCase(); if(operatingSystem.indexOf("indows") > 0 ){ name = "windows"; - } else if (operatingSystem.contains("nux")) { + }else if(operatingSystem.indexOf("nux") >= 0){ name="linux"; - } else if (operatingSystem.contains("mac")) { + }else if(operatingSystem.indexOf("mac") >= 0){ name="mac_os_x"; }else{ name = null; @@ -95,11 +95,11 @@ public class FFMPEGDownloader { } private String processorArchitecture(){ - boolean is64bit; + boolean is64bit = false; if (System.getProperty("os.name").contains("Windows")) { is64bit = (System.getenv("ProgramFiles(x86)") != null); } else { - is64bit = (System.getProperty("os.arch").contains("64")); + is64bit = (System.getProperty("os.arch").indexOf("64") != -1); } if(is64bit){ return "64_bits"; diff --git a/app/src/main/java/be/tarsos/dsp/util/PeakPicker.java b/app/src/main/java/be/tarsos/dsp/util/PeakPicker.java @@ -99,8 +99,8 @@ public class PeakPicker { * @return True if a peak is detected, false otherwise. **/ public boolean pickPeak(float onset) { - float mean; - float median; + float mean = 0.f; + float median = 0.f; int length = win_post + win_pre + 1; @@ -116,7 +116,7 @@ public class PeakPicker { onset_proc[length-1] = onset; /* filter onset_proc */ - /* \bug filtfilt calculated post+pre times, should be only once !? */ + /** \bug filtfilt calculated post+pre times, should be only once !? */ biquad.doFiltering(onset_proc,scratch); /* calculate mean and median for onset_proc */ @@ -129,14 +129,16 @@ public class PeakPicker { } Arrays.sort(scratch); median = scratch[scratch.length/2]; - mean = sum / (float) length; + mean = sum/Float.valueOf(length); /* shift peek array */ - System.arraycopy(onset_peek, 1, onset_peek, 0, 3 - 1); + for (int j=0;j<3-1;j++){ + onset_peek[j] = onset_peek[j+1]; + } /* calculate new peek value */ onset_peek[2] = (float) (onset_proc[win_post] - median - mean * threshold); - - boolean isPeak = isPeak(); + + boolean isPeak = isPeak(1); lastPeekValue = onset; return isPeak; @@ -153,11 +155,13 @@ public class PeakPicker { /** * Returns true if the onset is a peak. * + * @param index + * the index in onset_peak to check. * @return True if the onset is a peak, false otherwise. */ - private boolean isPeak() { - return (onset_peek[1] > onset_peek[0] && - onset_peek[1] > onset_peek[1 + 1] && - onset_peek[1] > 0.); + private boolean isPeak(int index) { + return ( onset_peek[index] > onset_peek[index - 1] && + onset_peek[index] > onset_peek[index + 1] && + onset_peek[index] > 0.); } } diff --git a/app/src/main/java/be/tarsos/dsp/util/PitchConverter.java b/app/src/main/java/be/tarsos/dsp/util/PitchConverter.java @@ -109,10 +109,10 @@ public final class PitchConverter { return absoluteCentValue % 1200.0; } - /* - This method is not really practical. Maybe I will need it someday. - - @param relativeCent + /** + * This method is not really practical. Maybe I will need it someday. + * + * @param relativeCent * @return public static double relativeCentToHertz(double relativeCent){ if * (relativeCent < 0 || relativeCent >= 1200) throw new * IllegalArgumentException diff --git a/app/src/main/java/be/tarsos/dsp/util/fft/FFT.java b/app/src/main/java/be/tarsos/dsp/util/fft/FFT.java @@ -32,7 +32,7 @@ package be.tarsos.dsp.util.fft; * * @author Joren Six */ -public final class FFT { +public class FFT { /** * Forward FFT. diff --git a/app/src/main/java/be/tarsos/dsp/wavelet/HaarWaveletDecoder.java b/app/src/main/java/be/tarsos/dsp/wavelet/HaarWaveletDecoder.java @@ -43,5 +43,7 @@ public class HaarWaveletDecoder implements AudioProcessor{ @Override public void processingFinished() { + } + } diff --git a/app/src/main/java/be/tarsos/dsp/wavelet/HaarWaveletTransform.java b/app/src/main/java/be/tarsos/dsp/wavelet/HaarWaveletTransform.java @@ -111,7 +111,10 @@ public class HaarWaveletTransform { if (number <= 0) { throw new IllegalArgumentException("number: " + number); } - return (number & -number) == number; + if ((number & -number) == number) { + return true; + } + return false; } /** diff --git a/app/src/main/java/be/tarsos/dsp/writer/WaveHeader.java b/app/src/main/java/be/tarsos/dsp/writer/WaveHeader.java @@ -5,6 +5,9 @@ import java.io.InputStream; import java.io.OutputStream; /** + * this source code is copied from : https://android.googlesource.com/platform/frameworks/base.git/+/android-4.3_r2/core/java/android/speech/srec/WaveHeader.java + */ +/** * This class represents the header of a WAVE format audio file, which usually * have a .wav suffix. The following integer valued fields are contained: * <ul> @@ -237,14 +240,14 @@ public class WaveHeader { } private static void writeInt(OutputStream out, int val) throws IOException { - out.write(val); + out.write(val >> 0); out.write(val >> 8); out.write(val >> 16); out.write(val >> 24); } private static void writeShort(OutputStream out, short val) throws IOException { - out.write(val); + out.write(val >> 0); out.write(val >> 8); } diff --git a/app/src/main/java/be/tarsos/dsp/writer/WriterProcessor.java b/app/src/main/java/be/tarsos/dsp/writer/WriterProcessor.java @@ -1,5 +1,6 @@ package be.tarsos.dsp.writer; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.RandomAccessFile;