record audio in java and determine real time if a tone of x frequency was played if so do something

后端 未结 2 817
一向
一向 2021-01-23 04:17

I want to be able to detect a tone of a predetermined frequency using java. What I am doing is playing a tone (the frequency of the tone is variable by user input) and I am try

2条回答
  •  旧巷少年郎
    2021-01-23 04:30

    Im actually working on a similar project with pitch detection, in Java as well. If you want to use FFT, you could do it with these steps. Java has a lot of libraries that can make this process easy for you.

    First, you need to read in the sound file. This can be done using Java Sound. It's a built in library with functions that make it easy to record sound. Examples can be found here. The default sample rate is 44,100 KHz (CD quality). These examples can get you from playing the actual tone to a double array of bytes representing the tone.

    Second, you should take the FFT with JTransforms. Here is an example of FFT being taken on a collection of samples.

    FFT gives you an array twice the length of the array of samples you passed it. You need to go through the FFT array by two's, since each part of this array is represented as an imaginary and a real piece. Compute the magnitude of each part of this array with sqrt(im^2 + re^2). Then, find which magnitude is the largest. The index of that magnitude corresponds to the frequency you're looking for.

    Keep in mind, you don't take FFT on the entire portion of sound. You break the sound up into chunks, and FFT each one. The chunks can overlap for higher accuracy, but that shouldn't be a problem, since you're just looking for a predetermined note. If you want to improve performance, you can also window each chunk before doing this.

    Once you have all the FFTs, they should confirm a certain frequency, and you can check that against the note you want.

    If you want to try and visualize this, I'd suggest using JFreeChart. It's another library that makes it easy to graph things.

提交回复
热议问题