我正在尝试建立一个系统,能够处理某人吹口哨和输出笔记的记录。
有人可以推荐一个开源平台,我可以将其用作音符/音高识别和波形文件分析的基础吗?
提前致谢
答案 0 :(得分: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"));
}
}
答案 1 :(得分:4)
我认为这个开源平台适合你 http://code.google.com/p/musicg-sound-api/
答案 2 :(得分:2)
好吧,你总是可以使用fftw来执行快速傅立叶变换。这是一个非常受尊敬的框架。一旦获得信号的FFT,就可以分析得到的峰值阵列。简单的直方图样式分析应该为您提供最大音量的频率。然后你只需要将这些频率与不同音高对应的频率进行比较。
答案 3 :(得分:1)
您可能需要考虑Python(x,y)。它是基于Matlab精神的python的科学编程框架,它具有在FFT域中工作的简单功能。
答案 4 :(得分:1)
除了其他很棒的选择:
答案 5 :(得分:1)
答案 6 :(得分:0)
我是FFT的粉丝,但对于单声道和相当纯正的啸叫正弦音,零交叉检测器在以更低的处理成本确定实际频率方面做得更好。零交叉检测用于电子频率计数器,用于测量任何被测试的时钟速率。
如果要分析纯正弦波音以外的任何东西,那么FFT绝对是可行的方法。
A very simple implementation of zero cross detection in Java on GitHub