我目前正在尝试编写一个Win 10应用程序,它使用chromaprint识别一首歌并从acoustid.org获取数据。
但我的ExtractPCM-Methods主题返回错误的值。 它返回许多价值观的第一个问题。我需要246秒对237的数据。第二个值是fpcalc.exe返回。
第二个问题是我的ExtractPCM实现返回的完全不同于我在开源项目中找到的工作实现。我不太了解音频,但我认为我的价值观是错误的。
我用来测试我的代码的参考实现是AresRpg。它使用BASS提取PCM数据。
Data BASS返回以10.054零开始,然后继续:
-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -1 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 -1 -1 0
两秒标记结束时的最后一个值如下所示:
612 627 635 647 655 656 662 662 663 658
我的实现开始于10.528后跟零:
-15194 11946 21344 12111 25732 12414 -28715 12748 6098 12973 -31198 13046 28784 12844 21248 12797 1592 13165 22544 13294 -20448
以
结尾13636 -17580 -18770 -17639 -29613 -17604 10168 -17608 20472 -17618
我的ExtractPCM看起来像
public static async Task<PcmInfo> ExtractPcm(IStorageFile file)
{
int sampleRate = 0;
int chanels = 0;
double seconds = 0;
short[] erg = null;
var graphResult = await Windows.Media.Audio.AudioGraph.CreateAsync(new Windows.Media.Audio.AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.Media));
using (var graph = graphResult.Graph)
{
var encodingProperties = Windows.Media.MediaProperties.AudioEncodingProperties.CreatePcm(22050, 1, 16);
var fileResult = await graph.CreateFileInputNodeAsync(file);
var fileInput = fileResult.FileInputNode;
{
var output = graph.CreateFrameOutputNode(encodingProperties);
{
fileInput.AddOutgoingConnection(output);
var duratio = fileInput.Duration.TotalSeconds;
var taskSource = new TaskCompletionSource<object>();
fileInput.FileCompleted += (source, e) =>
{
graph.Stop();
output.Stop();
taskSource.TrySetResult(null);
};
graph.Start();
await taskSource.Task;
var audioFrame = output.GetFrame();
using (var lockedBuffer = audioFrame.LockBuffer(Windows.Media.AudioBufferAccessMode.ReadWrite))
{
using (var refference = lockedBuffer.CreateReference())
{
await Task.Run(() =>
{
unsafe
{
var memoryByteAccess = refference as IMemoryBufferByteAccess;
byte* p;
uint capacity;
memoryByteAccess.GetBuffer(out p, out capacity);
chanels = (int)output.EncodingProperties.ChannelCount;
sampleRate = (int)output.EncodingProperties.SampleRate;
int length = (int)(capacity / sizeof(Int16));
Int16* b = (Int16*)(p);
erg = new short[length];
for (int i = 0; i < erg.Length; i++)
erg[i] = b[i];
seconds = length / (double)output.EncodingProperties.SampleRate / chanels;
}
});
}
}
}
}
var sb = new StringBuilder();
foreach (var item in erg.SkipWhile(y => y == 0).Take(sampleRate * 2))
{
sb.Append($"{item} ");
}
System.Diagnostics.Debug.WriteLine($"Leading zeros {erg.TakeWhile(y => y == 0).Count()} ");
System.Diagnostics.Debug.WriteLine(sb);
return new PcmInfo() { Data = erg, Seconds = seconds, SampleRate = sampleRate, Chanels = chanels };
}
}
AresRPG使用以下代码来读取数据:
public static System.Int16[] ExtractPcm(String file, out double seconds)
{
seconds = 0;
int handle = Bass.BASS_StreamCreateFile(file, 0, 0, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_MONO | BASSFlag.BASS_STREAM_PRESCAN);
if (handle == 0)
{
BASSError error = Bass.BASS_ErrorGetCode();
// System.Console.WriteLine("ERROR: " + error);
return null;
}
long length = Bass.BASS_ChannelGetLength(handle);
seconds = Bass.BASS_ChannelBytes2Seconds(handle, length);
int mixHandle = Un4seen.Bass.AddOn.Mix.BassMix.BASS_Mixer_StreamCreate(22050, 1, BASSFlag.BASS_STREAM_DECODE);
if (mixHandle == 0)
{
BASSError error = Bass.BASS_ErrorGetCode();
// System.Console.WriteLine("ERROR: " + error);
return null;
}
if (!Un4seen.Bass.AddOn.Mix.BassMix.BASS_Mixer_StreamAddChannel(mixHandle, handle, BASSFlag.BASS_DEFAULT))
{
BASSError error = Bass.BASS_ErrorGetCode();
// System.Console.WriteLine("ERROR: " + error);
return null;
}
List<System.Int16> data = new List<System.Int16>();
while (true)
{
Int16[] buffer = new Int16[512];
int num = Bass.BASS_ChannelGetData(mixHandle, buffer, buffer.Length * 2);
if (num == -1)
{
BASSError error = Bass.BASS_ErrorGetCode();
Bass.BASS_StreamFree(handle);
// System.Console.WriteLine("ERROR: " + error);
return null;
}
for (int i = 0; i < num / 2; ++i)
{
if (i < buffer.Length)
data.Add(buffer[i]);
else
throw new ApplicationException();
}
if (num < buffer.Length * 2)
break;
}
Bass.BASS_StreamFree(handle);
try
{
return data.ToArray();
}
catch (System.OutOfMemoryException)
{
System.GC.Collect();
return null;
}
}
修改
我将不安全的Block更改为以下内容:
await Task.Run(() =>
{
unsafe
{
var memoryByteAccess = refference as IMemoryBufferByteAccess;
byte* p;
uint capacity;
memoryByteAccess.GetBuffer(out p, out capacity);
chanels = (int)output.EncodingProperties.ChannelCount;
sampleRate = (int)output.EncodingProperties.SampleRate;
int length = Math.Min((int)(sampleRate * duratio) * chanels, (int)(capacity / sizeof(float)));
float* b = (float*)(p);
erg = new short[length];
for (int i = 0; i < erg.Length; i++)
erg[i] = (Int16)(b[i] * Int16.MaxValue);
seconds = length / (double)output.EncodingProperties.SampleRate / chanels;
}
});
那是因为即使子类型不是浮点数,用于存储数据的格式也是浮点数。
现在我得到的数据我认为是歌曲(我作为一个人),但仍然有些东西:(
我的提取时间略长,而且响亮。因此,在同一时间播放两个版本的开始语音同步,但在听到两个音轨以不同的速度播放后不久。最后它差异大约一秒钟。我不认为音量会影响指纹,但解码速度较慢可能会产生差异。
我以前测试的样品是来自Adele的Rolling Deep,将由acousticid.org识别。但我想我不允许发布已编辑的版本。
所以我使用了这个cc Song并创建了一个wav-file,它在左侧chanel中进行了解码,在右侧chanel中进行了Ares解码。
不幸的是,这首歌不在acusticId数据库中,至少我找不到指纹。