我在C#中使用Google的Cloud Speech API来传输来自麦克风和输出文本的输入。它运行良好,直到它在65秒崩溃,错误说有65分钟的请求限制。这个网站https://cloud.google.com/speech-to-text/quotas说我的限制应该是大约5分钟。我使用的函数如下,而seconds参数是请求流的持续时间。我使用旧方法还是因为我在API密钥的免费试用版上?
我已经看到其他与此类似的Stack Overflow问题,并且他们链接到同一个网站,说流媒体限制应该是大约一分钟,但该网站在过去一年半内更新但我的限制是还有一分钟。
public static async Task<object> StreamingMicRecognizeAsync(int seconds)
{
if (NAudio.Wave.WaveIn.DeviceCount < 1)
{
Console.WriteLine("No microphone!");
return -1;
}
var speech = SpeechClient.Create();
var streamingCall = speech.StreamingRecognize();
// Write the initial request with the config.
await streamingCall.WriteAsync(
new StreamingRecognizeRequest()
{
StreamingConfig = new StreamingRecognitionConfig()
{
Config = new RecognitionConfig()
{
Encoding =
RecognitionConfig.Types.AudioEncoding.Linear16,
SampleRateHertz = 16000,
LanguageCode = "en",
},
InterimResults = true,
}
});
// Print responses as they arrive.
Task printResponses = Task.Run(async () =>
{
while (await streamingCall.ResponseStream.MoveNext(
default(CancellationToken)))
{
foreach (var result in streamingCall.ResponseStream
.Current.Results)
{
foreach (var alternative in result.Alternatives)
{
if (alternative.Confidence != 0)
{
Console.WriteLine(alternative.Transcript);
}
}
}
}
});
// Read from the microphone and stream to API.
object writeLock = new object();
bool writeMore = true;
var waveIn = new NAudio.Wave.WaveInEvent();
waveIn.DeviceNumber = 0;
waveIn.WaveFormat = new NAudio.Wave.WaveFormat(16000, 1); //(hertz, channels)
waveIn.DataAvailable +=
(object sender, NAudio.Wave.WaveInEventArgs args) =>
{
lock (writeLock)
{
if (!writeMore) return;
streamingCall.WriteAsync(
new StreamingRecognizeRequest()
{
AudioContent = Google.Protobuf.ByteString
.CopyFrom(args.Buffer, 0, args.BytesRecorded)
}).Wait();
}
};
waveIn.StartRecording();
Console.WriteLine("Speak now.");
await Task.Delay(TimeSpan.FromSeconds(seconds));
waveIn.StopRecording();
lock (writeLock) writeMore = false;
await streamingCall.WriteCompleteAsync();
await printResponses;
return 0;
}
编辑:Google的产品经理在Google讨论页面上回复了我的帖子。他说,宣传的5分钟是一个错误,但他们正计划尽快扩大流媒体限制。是否有一种干净的方式可以使多个流媒体请求在65秒的限制范围内,但也不会错过用户的说话或在请求之间减少一半的话?