继我之前的问题后,我的目标是从C#中检测WAV文件中的DTMF音调。然而,我真的很难理解如何完成这个任务。
我知道DTMF使用一组频率,并且可以使用Goertzel算法来实现...某种方式。我拿了一个Goertzel代码片段并尝试将.WAV文件塞进去(使用NAudio读取文件,该文件是8KHz单声道16位PCM WAV):
using (WaveFileReader reader = new WaveFileReader(@"dtmftest_w.wav"))
{
byte[] buffer = new byte[reader.Length];
int read = reader.Read(buffer, 0, buffer.Length);
short[] sampleBuffer = new short[read/2];
Buffer.BlockCopy(buffer, 0, sampleBuffer, 0, read/2);
Console.WriteLine(CalculateGoertzel(sampleBuffer,8000,16));
}
public static double CalculateGoertzel(short[] sample, double frequency, int samplerate)
{
double Skn, Skn1, Skn2;
Skn = Skn1 = Skn2 = 0;
for (int i = 0; i < sample.Length; i++)
{
Skn2 = Skn1;
Skn1 = Skn;
Skn = 2 * Math.Cos(2 * Math.PI * frequency / samplerate) * Skn1 - Skn2 + sample[i];
}
double WNk = Math.Exp(-2 * Math.PI * frequency / samplerate);
return 20 * Math.Log10(Math.Abs((Skn - WNk * Skn1)));
}
我知道我的做法是错误的:我认为应该遍历缓冲区,每次只对少量数据计算Goertzel值-这样做正确吗?
其次,我不太理解Goertzel方法的输出告诉我什么:返回一个double(例如:
210.985812
),但我不知道如何将其与音频文件中DTMF音调的存在和值相对应。我已经到处搜索答案,包括此答案中提到的库;不幸的是,这里的代码似乎不起作用(正如网站上的评论中所述)。TAPIEx提供了一款商业库;我已经尝试过他们的评估库,它正好满足我的需求-但他们没有回复邮件,这让我对实际购买他们的产品感到怀疑。
我非常清楚地知道自己在寻找答案时可能并不知道确切的问题,但最终我只需要一种在.WAV文件中查找DTMF音调的方法。我走对了吗?如果不是,有没有人可以指点我正确的方向?
编辑:使用@Abbondanza的代码为基础,并在(可能根本是错误的)假设需要一点一点地滴入音频文件的情况下,我现在有了这个(非常粗糙的概念验证)代码:
const short sampleSize = 160;
using (WaveFileReader reader = new WaveFileReader(@"\\mac\home\dtmftest.wav"))
{
byte[] buffer = new byte[reader.Length];
reader.Read(buffer, 0, buffer.Length);
int bufferPos = 0;
while (bufferPos < buffer.Length-(sampleSize*2))
{
short[] sampleBuffer = new short[sampleSize];
Buffer.BlockCopy(buffer, bufferPos, sampleBuffer, 0, sampleSize*2);
var frequencies = new[] {697.0, 770.0, 852.0, 941.0, 1209.0, 1336.0, 1477.0};
var powers = frequencies.Select(f => new
{
Frequency = f,
Power = CalculateGoertzel(sampleBuffer, f, 8000)
});
const double AdjustmentFactor = 1.05;
var adjustedMeanPower = AdjustmentFactor*powers.Average(result => result.Power);
var sortedPowers = powers.OrderByDescending(result => result.Power);
var highestPowers = sortedPowers.Take(2).ToList();
float seconds = bufferPos / (float)16000;
if (highestPowers.All(result => result.Power > adjustedMeanPower))
{
// Use highestPowers[0].Frequency and highestPowers[1].Frequency to
// classify the detected DTMF tone.
switch (Convert.ToInt32(highestPowers[0].Frequency))
{
case 1209:
switch (Convert.ToInt32(highestPowers[1].Frequency))
{
case 697:
Console.WriteLine("1 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 770:
Console.WriteLine("4 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 852:
Console.WriteLine("7 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 941:
Console.WriteLine("* pressed at " + bufferPos);
break;
}
break;
case 1336:
switch (Convert.ToInt32(highestPowers[1].Frequency))
{
case 697:
Console.WriteLine("2 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 770:
Console.WriteLine("5 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 852:
Console.WriteLine("8 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 941:
Console.WriteLine("0 pressed at " + bufferPos + " (" + seconds + "s)");
break;
}
break;
case 1477:
switch (Convert.ToInt32(highestPowers[1].Frequency))
{
case 697:
Console.WriteLine("3 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 770:
Console.WriteLine("6 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 852:
Console.WriteLine("9 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 941:
Console.WriteLine("# pressed at " + bufferPos + " (" + seconds + "s)");
break;
}
break;
}
}
else
{
Console.WriteLine("No DTMF at " + bufferPos + " (" + seconds + "s)");
}
bufferPos = bufferPos + (sampleSize*2);
}
这是在Audacity中查看的示例文件;我添加了已按下的DTMF按键-
![enter image description here](https://istack.dev59.com/xGPEn.webp)
9 pressed at 1920 (0.12s)
1 pressed at 2880 (0.18s)
* pressed at 3200
1 pressed at 5120 (0.32s)
1 pressed at 5440 (0.34s)
7 pressed at 5760 (0.36s)
7 pressed at 6080 (0.38s)
7 pressed at 6720 (0.42s)
5 pressed at 7040 (0.44s)
7 pressed at 7360 (0.46s)
7 pressed at 7680 (0.48s)
1 pressed at 8000 (0.5s)
7 pressed at 8320 (0.52s)
直到3秒钟之后,它才开始稳定下来给出正确的答案:即按下了1
键。
7 pressed at 40000 (2.5s)
# pressed at 43840 (2.74s)
No DTMF at 44800 (2.8s)
1 pressed at 45120 (2.82s)
1 pressed at 45440 (2.84s)
1 pressed at 46080 (2.88s)
1 pressed at 46720 (2.92s)
4 pressed at 47040 (2.94s)
1 pressed at 47360 (2.96s)
1 pressed at 47680 (2.98s)
1 pressed at 48000 (3s)
1 pressed at 48960 (3.06s)
4 pressed at 49600 (3.1s)
1 pressed at 49920 (3.12s)
1 pressed at 50560 (3.16s)
1 pressed at 51520 (3.22s)
1 pressed at 52160 (3.26s)
4 pressed at 52480 (3.28s)
如果我将
AdjustmentFactor
提高到1.2以上,几乎无法检测出任何东西。我感觉我就快成功了,但有人能看出我漏掉了什么吗?
编辑2:上面的测试文件可以在此处下载:here。上面示例中的
adjustedMeanPower
是 47.6660450354638
,功率如下:
powers
的内容和adjustedMeanPower
的值添加到你的调试输出中吗?另外,出于好奇,我想稍微调整一下那个问题。你有办法让我能够访问你的测试WAV文件吗? - Good Night Nerd Pride