如何解决将 WasapiLoopbackCapture 与谷歌流识别相结合
我正在尝试编写一个应用,该应用将收听我的计算机音频并使用 Google 语音识别进行转录。
我已经能够使用 WasapiLoopbackCapture
录制系统声音,并且能够将谷歌流识别 api 与测试文件一起使用,但我无法将两者合并在一起。
当我将音频从 WasapiLoopbackCapture
流式传输到 google 时,它不会返回任何结果。
我的代码基于以下谷歌代码示例: https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/9588cee6d96bfe484c8e189e9ac2f6eaa3c3b002/speech/api/Recognize/InfiniteStreaming.cs#L225
private WaveInEvent StartListening()
{
var waveIn = new WaveInEvent
{
DeviceNumber = 0,WaveFormat = new WaveFormat(SampleRate,ChannelCount)
};
waveIn.DataAvailable += (sender,args) =>
_microphoneBuffer.Add(ByteString.copyFrom(args.Buffer,args.BytesRecorded));
waveIn.StartRecording();
return waveIn;
}
并调整它以使用 WasapiLoopbackCapture:
private Idisposable StartListening()
{
var waveIn = new WasapiLoopbackCapture();
//var waveIn = new WaveInEvent
//{
// DeviceNumber = 0,// WaveFormat = new WaveFormat(SampleRate,ChannelCount)
//};
SampleRate = waveIn.WaveFormat.SampleRate;
ChannelCount = waveIn.WaveFormat.Channels;
BytesPerSecond = SampleRate * ChannelCount * BytesPerSample;
Console.WriteLine(SampleRate);
Console.WriteLine(BytesPerSecond);
waveIn.DataAvailable += (sender,args.BytesRecorded));
waveIn.StartRecording();
return waveIn;
}
但它不会返回任何转录文本。
我已将输入流保存到文件中,并且播放正常 - 所以声音到达那里,我猜是从 WasapiLoopback 接收的 waveFormat 与谷歌喜欢的不兼容 - 我尝试了一些转换并且无法让它工作。
我已经查看了以下有关堆栈溢出的主题,但仍然无法使其正常工作: Resampling WasapiLoopbackCapture Naudio - Convert 32 bit wav to 16 bit wav
并尝试将它们结合起来:
private Idisposable StartListening()
{
var waveIn = new WasapiLoopbackCapture();
//var waveIn = new WaveInEvent
//{
//DeviceNumber = 0,//WaveFormat = new WaveFormat(SampleRate,ChannelCount)
//};
// SampleRate = waveIn.WaveFormat.SampleRate;
// ChannelCount = waveIn.WaveFormat.Channels;
// BytesPerSecond = waveIn.WaveFormat.AverageBytesPerSecond;// SampleRate * ChannelCount * BytesPerSample;
var target = new WaveFormat(SampleRate,16,1);
var writer = new WaveFileWriter(@"c:\temp\xx.wav",waveIn.WaveFormat);
Console.WriteLine(SampleRate);
Console.WriteLine(BytesPerSecond);
var stop = false;
waveIn.DataAvailable += (sender,args) =>
{
var a = args;
byte[] newArray16Bit = new byte[args.BytesRecorded / 2];
short two;
float value;
for (int i = 0,j = 0; i < args.BytesRecorded; i += 4,j += 2)
{
value = (BitConverter.ToSingle(args.Buffer,i));
two = (short)(value * short.MaxValue);
newArray16Bit[j] = (byte)(two & 0xFF);
newArray16Bit[j + 1] = (byte)((two >> 8) & 0xFF);
}
var resampleStream = new NAudio.Wave.Compression.AcmStream(new WaveFormat(waveIn.WaveFormat.SampleRate,waveIn.WaveFormat.Channels),target);
Buffer.Blockcopy(newArray16Bit,resampleStream.sourceBuffer,a.BytesRecorded/2);
int sourceBytesConverted = 0;
var bytes = resampleStream.Convert(a.BytesRecorded/2,out sourceBytesConverted);
var converted = new byte[bytes];
Buffer.Blockcopy(resampleStream.DestBuffer,9,converted,bytes);
a = new WaveInEventArgs(converted,bytes);
_microphoneBuffer.Add(ByteString.copyFrom(a.Buffer,a.BytesRecorded));
if (writer != null)
{
writer.Write(a.Buffer,a.BytesRecorded);
if (writer.Position > waveIn.WaveFormat.AverageBytesPerSecond * 5)
{
stop = true;
writer.dispose();
writer = null;
Console.WriteLine("Saved file");
}
}
};
waveIn.StartRecording();
return waveIn;
}
但它不起作用。
我不确定这是否是正确的路径。
非常感谢修复的代码示例
我尝试转换比特率等,但无法使其正常工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。