Install the Speechly API stubs with
dotnet add package SpeechlyApi
using Grpc.Net.Client;
using Grpc.Core;
using Speechly.Identity.V1;
using Speechly.Slu.V1;Logging in with IdentityAPI is a single RPC call with appId and deviceId.
GrpcChannel channel = GrpcChannel.ForAddress("https://api.speechly.com");
Identity.IdentityClient identityClient = new Identity.IdentityClient(channel);
var deviceId = System.Guid.NewGuid().ToString();
var loginRquest = new LoginRequest
{
AppId = appId,
DeviceId = deviceId
};
var response = await identityClient.LoginAsync(loginRquest);
var token = response.Token;Streaming SLU is a bidirectional RPC call to the API, which means that it is not no simple as the login example above. Use the token from the Identity response to create the channel for the SLUClient.
var authInterceptor = new AsyncAuthInterceptor(async (context, metadata) =>
{
metadata.Add(
new Metadata.Entry("Authorization", "Bearer " + token));
});
var metadataCredentials = CallCredentials.FromInterceptor(authInterceptor);
ChannelCredentials channelCredentials = ChannelCredentials.Create(new SslCredentials(), metadataCredentials);
Channel channel = new Channel("api.speechly.com", channelCredentials);
SLU.SLUClient sluClient = new SLU.SLUClient(channel);
AsyncDuplexStreamingCall<SLURequest, SLUResponse> call = sluClient.Stream();Before starting context need to send SLUConfig with Encoding, LanguageCode and SampleRateHertz.
SLURequest configRequest = new SLURequest
{
Config = new SLUConfig
{
Encoding = SLUConfig.Types.Encoding.Linear16,
LanguageCode = "en-US",
SampleRateHertz = 16_000,
}
};
call.RequestStream.WriteAsync(configRequest).Wait();
SLURequest startRequest = new SLURequest
{
Event = new SLUEvent { Event = SLUEvent.Types.Event.Start }
};
call.RequestStream.WriteAsync(startRequest).Wait();Audio should be sent as a ByteString. In the example audioChunk is a byte[].
SLURequest audio = new SLURequest
{
Audio = ByteString.CopyFrom(audioChunk)
};
call.RequestStream.WriteAsync(audio).Wait();SLURequest stopRequest = new SLURequest
{
Event = new SLUEvent { Event = SLUEvent.Types.Event.Stop }
};
call.RequestStream.WriteAsync(stopRequest).Wait();