aha*_*ans 10
正如许多人已经说过的那样,FFT就是这里的方式.我使用来自http://www.cs.princeton.edu/introcs/97data/的 FFT代码在Java中编写了一个小例子.要运行它,您还需要该页面中的Complex类(请参阅确切URL的源代码).
代码读入一个文件,在窗口上方,并在每个窗口上进行FFT.对于每个FFT,它寻找最大系数并输出相应的频率.这对于像正弦波这样的干净信号非常有效,但对于实际的哨声,您可能需要添加更多信号.我已经测试了几个带有吹口哨的文件我自己创建(使用我的笔记本电脑的集成麦克风),代码确实知道发生了什么,但为了获得实际笔记,还需要做更多工作.
1)您可能需要一些更智能的窗口技术.我的代码现在使用的是一个简单的矩形窗口.由于FFT假设输入信号可以周期性地继续,因此当窗口中的第一个和最后一个样本不匹配时,检测到附加频率.这被称为频谱泄漏(http://en.wikipedia.org/wiki/Spectral_leakage),通常使用一个窗口,在窗口的开头和结尾对样本进行缩减(http://en.wikipedia. org/wiki/Window_function).虽然泄漏不应该导致错误的频率被检测为最大值,但使用窗口会增加检测质量.
2)为了使频率与实际音符相匹配,您可以使用包含频率的数组(如a'为440 Hz),然后查找最接近已识别频率的频率.但是,如果吹口哨没有标准调音,这将不再起作用.鉴于吹口哨仍然是正确的,但只是调整不同(如吉他或其他乐器可以调整不同,仍然听起来"好",只要对所有琴弦进行一致的调音),你仍然可以通过寻找找到音符在所识别的频率的比率.您可以阅读http://en.wikipedia.org/wiki/Pitch_%28music%29作为起点.这也很有趣:http://en.wikipedia.org/wiki/Piano_key_frequencies
3)此外,检测每个音调开始和停止的时间点可能会很有趣.这可以作为预处理步骤添加.然后,您可以为每个单独的音符执行FFT.但是,如果吹口哨没有停止但只是在音符之间弯曲,那就不那么容易了.
绝对看看其他人建议的库.我不知道它们中的任何一个,但也许它们已经包含了我上面所描述的功能.
现在到了代码.请让我知道什么对你有用,我觉得这个话题非常有趣.
编辑:我更新了代码以包含重叠和从频率到笔记的简单映射器.如上所述,它仅适用于"调谐"的哨子.
package de.ahans.playground;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
public class FftMaxFrequency {
// taken from http://www.cs.princeton.edu/introcs/97data/FFT.java.html
// (first hit in Google for "java fft"
// needs Complex class from http://www.cs.princeton.edu/introcs/97data/Complex.java
public static Complex[] fft(Complex[] x) {
int N = x.length;
// base case
if (N == 1) return new Complex[] { x[0] };
// radix 2 Cooley-Tukey FFT
if (N % 2 != 0) { throw new RuntimeException("N is not a power of 2"); }
// fft of even terms
Complex[] even = new Complex[N/2];
for (int k = 0; k < N/2; k++) {
even[k] = x[2*k];
}
Complex[] q = fft(even);
// fft of odd terms
Complex[] odd = even; // reuse the array
for (int k = 0; k < N/2; k++) {
odd[k] = x[2*k + 1];
}
Complex[] r = fft(odd);
// combine
Complex[] y = new Complex[N];
for (int k = 0; k < N/2; k++) {
double kth = -2 * k * Math.PI / N;
Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
y[k] = q[k].plus(wk.times(r[k]));
y[k + N/2] = q[k].minus(wk.times(r[k]));
}
return y;
}
static class AudioReader {
private AudioFormat audioFormat;
public AudioReader() {}
public double[] readAudioData(File file) throws UnsupportedAudioFileException, IOException {
AudioInputStream in = AudioSystem.getAudioInputStream(file);
audioFormat = in.getFormat();
int depth = audioFormat.getSampleSizeInBits();
long length = in.getFrameLength();
if (audioFormat.isBigEndian()) {
throw new UnsupportedAudioFileException("big endian not supported");
}
if (audioFormat.getChannels() != 1) {
throw new UnsupportedAudioFileException("only 1 channel supported");
}
byte[] tmp = new byte[(int) length];
byte[] samples = null;
int bytesPerSample = depth/8;
int bytesRead;
while (-1 != (bytesRead = in.read(tmp))) {
if (samples == null) {
samples = Arrays.copyOf(tmp, bytesRead);
} else {
int oldLen = samples.length;
samples = Arrays.copyOf(samples, oldLen + bytesRead);
for (int i = 0; i < bytesRead; i++) samples[oldLen+i] = tmp[i];
}
}
double[] data = new double[samples.length/bytesPerSample];
for (int i = 0; i < samples.length-bytesPerSample; i += bytesPerSample) {
int sample = 0;
for (int j = 0; j < bytesPerSample; j++) sample += samples[i+j] << j*8;
data[i/bytesPerSample] = (double) sample / Math.pow(2, depth);
}
return data;
}
public AudioFormat getAudioFormat() {
return audioFormat;
}
}
public class FrequencyNoteMapper {
private final String[] NOTE_NAMES = new String[] {
"A", "Bb", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"
};
private final double[] FREQUENCIES;
private final double a = 440;
private final int TOTAL_OCTAVES = 6;
private final int START_OCTAVE = -1; // relative to A
public FrequencyNoteMapper() {
FREQUENCIES = new double[TOTAL_OCTAVES*12];
int j = 0;
for (int octave = START_OCTAVE; octave < START_OCTAVE+TOTAL_OCTAVES; octave++) {
for (int note = 0; note < 12; note++) {
int i = octave*12+note;
FREQUENCIES[j++] = a * Math.pow(2, (double)i / 12.0);
}
}
}
public String findMatch(double frequency) {
if (frequency == 0)
return "none";
double minDistance = Double.MAX_VALUE;
int bestIdx = -1;
for (int i = 0; i < FREQUENCIES.length; i++) {
if (Math.abs(FREQUENCIES[i] - frequency) < minDistance) {
minDistance = Math.abs(FREQUENCIES[i] - frequency);
bestIdx = i;
}
}
int octave = bestIdx / 12;
int note = bestIdx % 12;
return NOTE_NAMES[note] + octave;
}
}
public void run (File file) throws UnsupportedAudioFileException, IOException {
FrequencyNoteMapper mapper = new FrequencyNoteMapper();
// size of window for FFT
int N = 4096;
int overlap = 1024;
AudioReader reader = new AudioReader();
double[] data = reader.readAudioData(file);
// sample rate is needed to calculate actual frequencies
float rate = reader.getAudioFormat().getSampleRate();
// go over the samples window-wise
for (int offset = 0; offset < data.length-N; offset += (N-overlap)) {
// for each window calculate the FFT
Complex[] x = new Complex[N];
for (int i = 0; i < N; i++) x[i] = new Complex(data[offset+i], 0);
Complex[] result = fft(x);
// find index of maximum coefficient
double max = -1;
int maxIdx = 0;
for (int i = result.length/2; i >= 0; i--) {
if (result[i].abs() > max) {
max = result[i].abs();
maxIdx = i;
}
}
// calculate the frequency of that coefficient
double peakFrequency = (double)maxIdx*rate/(double)N;
// and get the time of the start and end position of the current window
double windowBegin = offset/rate;
double windowEnd = (offset+(N-overlap))/rate;
System.out.printf("%f s to %f s:\t%f Hz -- %s\n", windowBegin, windowEnd, peakFrequency, mapper.findMatch(peakFrequency));
}
}
public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
new FftMaxFrequency().run(new File("/home/axr/tmp/entchen.wav"));
}
}
Run Code Online (Sandbox Code Playgroud)