Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use minim library in a java application to detect the beat of an audio source?

Hi everyone!

I am totally new to audio programming and would like to build a Java FX application that interact with audio from my microphone or line out.

I searched google for libraries and found minim which seems pretty popular. It is written for processing but you are able to use it in Java as well. The problem is that I did not find that good documentation about how to do this. (The reason that I do not want to use processing is that I want to build a pretty advance gui, and I think it is easier to do in JavaFX).

As a first step I am trying to build a library that reacts on every beat in a song. My code looks like this:

public class Main extends Application {

    /* Used to trick minim constructor (needs an object with those methods) */
    class MinimInput {
        String sketchPath( String fileName ) {
            return "";
        }
        InputStream createInput(String fileName) {
            return new InputStream() {
                @Override
                public int read() throws IOException {
                    return 0;
                }
            };
        };
    }

    @Override
    public void start(Stage stage) throws Exception {

        ...
            //Some gui logic here!
        ...

        stage.setScene(scene);
        stage.show();

        /* Beat detect thread */
        Thread th = new Thread(new Runnable() {

            @Override
            public void run() {
                Minim minim = new Minim(new MinimInput());

                AudioInput input = minim.getLineIn(Minim.STEREO, 1024); //Tried differen values here
                BeatDetect beatDetect = new BeatDetect();
                beatDetect.setSensitivity(1000); //Tried different values here

                int i=0;

                while (true) {
                    beatDetect.detect(input.mix);

                    if(beatDetect.isHat()) {
                        System.out.print("HAT");
                    }

                    if(beatDetect.isSnare()) {
                        System.out.print("SNARE");
                    }

                    if (beatDetect.isKick()) {
                        System.out.print("KICK");
                    }
                }
            }
        });

        th.start();
    }
}

I suspect that it could be something with the while(true)-loop and that my soundbuffer gets to small, but I have no idea how I should do this. Can anyone point me in the right direction?

I would also be very thankfull for good resources about audio visualisation programming in general and more specific information on how to do it in java and minim (or tips and examples on how to do this with other libaries if they are easier to use). This is a completely new ground for me, help me break it! :)

like image 555
David Berg Avatar asked Oct 19 '22 01:10

David Berg


1 Answers

I found a solution. Providing BeatDetect with timeSize and sampleRate on init did the trick:

  • timeSize: the size of the buffer
  • sampleRate: the sample rate of the samples in the buffer

So... this is what I got:

BeatDetect beatDetect = new BeatDetect(1024, 44100.0f);

1024 is the same as you specify when you get the AudioInput from mime. The sample rate could be found using debug and look at the input value under:

input > stream > format > sampleRate

like image 166
David Berg Avatar answered Oct 21 '22 19:10

David Berg