JorenSix / TarsosDSP

A Real-Time Audio Processing Framework in Java
http://0110.be/tag/TarsosDSP
GNU General Public License v3.0
1.96k stars 471 forks source link

OnsetDetector (complex & beatroot) causes OutOfBoundsException on Android! #132

Open pyrbin opened 7 years ago

pyrbin commented 7 years ago

This is my code:

                final File file = new File(Environment.getExternalStoragePublicDirectory
                                                     (Environment.DIRECTORY_MUSIC), "stars.mp3");
                AudioDispatcher adp;
                adp = AudioDispatcherFactory.fromPipe(file.getAbsolutePath(),2048, 2048-441,0);
                adp.setZeroPadFirstBuffer(true);
                ComplexOnsetDetector b = new ComplexOnsetDetector(2048,441);
                b.setHandler(new OnsetHandler(){
                    int i = 0;
                    @Override
                    public void handleOnset(double actualTime, double salience) {
                        onsets.add(actualTime);
                    }
                });
                adp.addAudioProcessor(b);
                adp.run();

This is my error:

 java.lang.ArrayIndexOutOfBoundsException: length=1607; index=2046
              at be.tarsos.dsp.util.fft.FloatFFT.cftf1st(Unknown Source)
              at be.tarsos.dsp.util.fft.FloatFFT.cftfsub(Unknown Source)
              at be.tarsos.dsp.util.fft.FloatFFT.realForward(Unknown Source)
              at be.tarsos.dsp.util.fft.FloatFFT.realForward(Unknown Source)
              at be.tarsos.dsp.util.fft.FFT.powerPhaseFFT(Unknown Source)
              at be.tarsos.dsp.onsets.ComplexOnsetDetector.onsetDetection(Unknown Source)
              at be.tarsos.dsp.onsets.ComplexOnsetDetector.process(Unknown Source)
              at be.tarsos.dsp.AudioDispatcher.run(Unknown Source)
              at bergstrom.frans.onset_test.MainActivity$1.run(MainActivity.java:69)
              at java.lang.Thread.run(Thread.java:818)

Am I not using the Onset Detector correctly? I've tried the BeatRoot and Complex version both produces the same error. My audiofile is a standard mp3 mono stored on the sdcard (external storage)

kkirjala commented 6 years ago

I can confirm this bug also on standard Java platform.

It seems that the bug is related to the for-loop inside onsetDetection method, row 148:

" for(int j = 0 ; j < power.length ; j++){ "

It's obviously trying to access dev1, theta1 and theta2 array indexes beyond their sizes. In my example (FFT size 512) this is what the situation during runtime is:

power.length: 512 dev1.length: 257 theta1.length: 257 theta2.length: 257

I'm not familiar enough with the algorithm to say what and where to fix.