You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use SpeechApi to translate real time voice to text, and I uncounted the GRPC error, can give some suggestion:
static async Task StreamingMicRecognizeAsync(int seconds)
{
//if (NAudio.Wave.WaveIn.DeviceCount < 1)
//{
// Console.WriteLine("No microphone!");
// return -1;
//}
var speech = SpeechClient.Create();
var streamingCall = speech.StreamingRecognize(null, new BidirectionalStreamingSettings(10240));
// Write the initial request with the config.
try
{
await streamingCall.WriteAsync(
new StreamingRecognizeRequest()
{
StreamingConfig = new StreamingRecognitionConfig()
{
Config = new RecognitionConfig()
{
Encoding =
RecognitionConfig.Types.AudioEncoding.Linear16,
SampleRateHertz = 16000,
LanguageCode = "en",
},
InterimResults = true,
}
});
}
catch (Exception ex)
{
Console.WriteLine("WriteAsync 0.error:{0:x}", ex.HResult);
}
// 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)
{
Console.WriteLine(alternative.Transcript);
}
}
}
});
// Read from the microphone and stream to API.
object writeLock = new object();
bool writeMore = true;
var waveIn = new NAudio.Wave.WasapiCaptureRT();
//waveIn.DeviceNumber = 0;
waveIn.WaveFormat = new NAudio.Wave.WaveFormat(48000, 2);
waveIn.DataAvailable +=
(object sender, NAudio.Wave.WaveInEventArgs args) =>
{
lock (writeLock)
{
if (!writeMore)
return;
Console.WriteLine("recording:{0:d}", args.BytesRecorded);
byte[] newBuffer = new byte[args.BytesRecorded / 6];
for (int i = 0; i < args.BytesRecorded / 6; i +=6)
{
UInt16 first = (UInt16)((args.Buffer[i * 2 + 1] << 8) | args.Buffer[i * 2]);
UInt16 second = (UInt16)((args.Buffer[i * 2 + 5] << 8) | args.Buffer[i * 2+4]);
UInt16 third = (UInt16)((args.Buffer[i * 2 + 9] << 8) | args.Buffer[i * 2+8]);
UInt16 newOne = (UInt16)((UInt32)(first + second + third) / 3);
newBuffer[i] = (byte)(newOne & 0X08);
newBuffer[i+1] = (byte)(newOne >> 8 );
}
StreamingRecognizeRequest request = new StreamingRecognizeRequest()
{
AudioContent = Google.Protobuf.ByteString.CopyFrom(newBuffer, 0, args.BytesRecorded / 6)
};
try
{
streamingCall.WriteAsync(request).Wait();
}
catch (Exception ex)
{
Console.WriteLine("WriteAsync.error:{0:x}",ex.HResult);
}
}
};
//waveIn.RecordingStopped += (object sender, StoppedEventArgs stoppedEventArgs) =>
//{
//};
waveIn.StartRecording();
Console.WriteLine("Speak now.");
await Task.Delay(TimeSpan.FromSeconds(seconds));
// Stop recording and shut down.
waveIn.StopRecording();
lock (writeLock) writeMore = false;
await streamingCall.WriteCompleteAsync();
await printResponses;
return 0;
}
The text was updated successfully, but these errors were encountered:
I use SpeechApi to translate real time voice to text, and I uncounted the GRPC error, can give some suggestion:
static async Task StreamingMicRecognizeAsync(int seconds)
{
//if (NAudio.Wave.WaveIn.DeviceCount < 1)
//{
// Console.WriteLine("No microphone!");
// return -1;
//}
var speech = SpeechClient.Create();
var streamingCall = speech.StreamingRecognize(null, new BidirectionalStreamingSettings(10240));
// Write the initial request with the config.
try
{
await streamingCall.WriteAsync(
new StreamingRecognizeRequest()
{
StreamingConfig = new StreamingRecognitionConfig()
{
Config = new RecognitionConfig()
{
Encoding =
RecognitionConfig.Types.AudioEncoding.Linear16,
SampleRateHertz = 16000,
LanguageCode = "en",
},
InterimResults = true,
}
});
}
catch (Exception ex)
{
The text was updated successfully, but these errors were encountered: