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