Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Speech] streamingRecognize for real time always hint: Shutdown has already been called #723

Closed
zhongyzh opened this issue Nov 1, 2017 · 2 comments
Assignees

Comments

@zhongyzh
Copy link

zhongyzh commented Nov 1, 2017

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;
    } 

@zhongyzh
Copy link
Author

zhongyzh commented Nov 1, 2017

At this line: streamingCall.WriteAsync(request).Wait(); will receive the exception.

@dwsupplee
Copy link
Contributor

@zhongyzh, it looks like you want to direct this issue towards https://github.com/googlecloudplatform/google-cloud-dotnet.

gcf-owl-bot bot added a commit that referenced this issue Aug 4, 2022
Source-Link: googleapis/googleapis@60a0fa7

Source-Link: googleapis/googleapis-gen@ba1df1b
Copy-Tag: eyJwIjoiQ29tcHV0ZS8uT3dsQm90LnlhbWwiLCJoIjoiYmExZGYxYmU5M2Y1YTFhYTVjNjQ3ZmMyZjE5NWQ0MWIwMDc1YWE5MyJ9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants