Esempio n. 1
0
        private async void _addBtn_Click(object sender, RoutedEventArgs e)
        {
            MainWindow window = (MainWindow)Application.Current.MainWindow;

            try
            {
                window.Log("Creating Speaker Profile...");
                CreateProfileResponse creationResponse = await _serviceClient.CreateProfileAsync(_localeCmb.Text);

                window.Log("Speaker Profile Created.");
                window.Log("Retrieving The Created Profile...");
                Profile profile = await _serviceClient.GetProfileAsync(creationResponse.ProfileId);

                window.Log("Speaker Profile Retrieved.");
                SpeakersListPage.SpeakersList.AddSpeaker(profile);
            }
            catch (CreateProfileException ex)
            {
                window.Log("Profile Creation Error: " + ex.Message);
            }
            catch (GetProfileException ex)
            {
                window.Log("Error Retrieving The Profile: " + ex.Message);
            }
            catch (Exception ex)
            {
                window.Log("Error: " + ex.Message);
            }
        }
Esempio n. 2
0
        private void EnrollSpeaker(Stream stream)
        {
            // Reset pointer
            stream.Seek(0, SeekOrigin.Begin);

            SpeakerIdentificationServiceClient speakerIDClient = new SpeakerIdentificationServiceClient("c6b005dcf13e45b6a91485d38763277b");

            //Creating Speaker Profile...
            CreateProfileResponse creationResponse = speakerIDClient.CreateProfileAsync("en-US").Result;
            //Speaker Profile Created.
            //Retrieving The Created Profile...
            Profile profile = speakerIDClient.GetProfileAsync(creationResponse.ProfileId).Result;
            //Speaker Profile Retrieved."
            //Enrolling Speaker
            OperationLocation processPollingLocation = speakerIDClient.EnrollAsync(stream, profile.ProfileId, false).Result;

            EnrollmentOperation enrollmentResult;
            int      numOfRetries       = 10;
            TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5.0);

            while (numOfRetries > 0)
            {
                Task.Delay(timeBetweenRetries);
                enrollmentResult = speakerIDClient.CheckEnrollmentStatusAsync(processPollingLocation).Result;

                if (enrollmentResult.Status == Status.Succeeded)
                {
                    break;
                }
                else if (enrollmentResult.Status == Status.Failed)
                {
                    throw new EnrollmentException(enrollmentResult.Message);
                }
                numOfRetries--;
            }
            if (numOfRetries <= 0)
            {
                throw new EnrollmentException("Enrollment operation timeout.");
            }

            //Enrollment Done.
            // Store profile in memory cache
            ObjectCache memCache = MemoryCache.Default;
            var         profiles = memCache.Get("SpeakerProfiles") != null?memCache.Get("SpeakerProfiles") as List <Profile> : new List <Profile>();

            memCache.Remove("SpeakerProfiles");
            memCache.Add("SpeakerProfiles", profiles, DateTimeOffset.UtcNow.AddHours(2));
        }
        public async void voicetoprofile(byte[] data, string filename)
        {
            try
            {
                SpeakerIdentificationServiceClient _serviceClient = new SpeakerIdentificationServiceClient("xxxxxxxxxxxxxxxxxxxxxxx");
                CreateProfileResponse creationResponse            = await _serviceClient.CreateProfileAsync(name.Text.ToString());

                Profile profile = await _serviceClient.GetProfileAsync(creationResponse.ProfileId);

                //SpeakersListPage.SpeakersList.AddSpeaker(profile);
                OperationLocation processPollingLocation;
                using (Stream audioStream = new MemoryStream(data))
                {
                    //_selectedFile = "";
                    processPollingLocation = await _serviceClient.EnrollAsync(audioStream, profile.ProfileId);
                }

                EnrollmentOperation enrollmentResult;
                int      numOfRetries       = 10;
                TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5.0);
                while (numOfRetries > 0)
                {
                    await Task.Delay(timeBetweenRetries);

                    enrollmentResult = await _serviceClient.CheckEnrollmentStatusAsync(processPollingLocation);

                    if (enrollmentResult.Status == Status.Succeeded)
                    {
                        break;
                    }
                    else if (enrollmentResult.Status == Status.Failed)
                    {
                        throw new EnrollmentException(enrollmentResult.Message);
                    }
                    numOfRetries--;
                }
                if (numOfRetries <= 0)
                {
                    throw new EnrollmentException("Enrollment operation timeout.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //This Creates a new speaker in the database
        //INPUT:
        //OUTPUT: functionResult
        public async Task <functionResult> addSpeaker()
        {
            functionResult result = new functionResult();

            //If the _serviceClient is null, create it with the subsciption key stored
            if (_serviceClient == null)
            {
                _serviceClient = new SpeakerIdentificationServiceClient(_subscriptionKey);
            }
            try
            {
                //Create the new profile and assign it to the Profile Tag
                CreateProfileResponse creationResponse = await _serviceClient.CreateProfileAsync("en-us");

                Profile profile = await _serviceClient.GetProfileAsync(creationResponse.ProfileId);

                result.Result  = true;
                result.Message = profile.ProfileId.ToString();
            }
            catch (CreateProfileException ex)
            {
                result.Result  = false;
                result.Message = "Error Creating The Profile: " + ex.Message.ToString();
            }
            catch (GetProfileException ex)
            {
                result.Result  = false;
                result.Message = "Error Retrieving The Profile: " + ex.Message.ToString();
            }
            catch (Exception ex)
            {
                result.Result  = false;
                result.Message = "Error: " + ex.Message.ToString();
            }
            return(result);
        }
Esempio n. 5
0
        /// <summary>
        /// Adds speaker profile management dialog
        /// </summary>
        private void AddProfileDialog()
        {
            Add(Inputs.ManageProfile, new ChoicePrompt(Culture.English));

            Add(Dialogs.ManageProfileDialogName, new WaterfallStep[]
            {
                async(dc, args, next) =>
                {
                    // Prompt for action.
                    var mainOptions = new List <string>
                    {
                        ProfileMenu.ViewProfile,
                        ProfileMenu.CreateProfile,
                        ProfileMenu.DeleteProfile,
                        ProfileMenu.EnrollProfile,
                        ProfileMenu.BackToMainMenu
                    };
                    await dc.Prompt(Inputs.ManageProfile, "What do you want to do?", new ChoicePromptOptions
                    {
                        Choices             = ChoiceFactory.ToChoices(mainOptions),
                        RetryPromptActivity =
                            MessageFactory.SuggestedActions(mainOptions, "Please select an option.") as Activity
                    });
                },
                async(dc, args, next) =>
                {
                    var action = (FoundChoice)args["Value"];
                    switch (action.Value)
                    {
                    case ProfileMenu.ViewProfile:
                        await dc.Replace(Dialogs.ViewProfileDialogName);
                        break;

                    case ProfileMenu.CreateProfile:
                        await dc.Replace(Dialogs.CreateProfileDialogName);
                        break;

                    case ProfileMenu.DeleteProfile:
                        await dc.Replace(Dialogs.DeleteProfileDialogName);
                        break;

                    case ProfileMenu.EnrollProfile:
                        await dc.Replace(Dialogs.EnrollProfileDialogName);
                        break;

                    case ProfileMenu.BackToMainMenu:
                        await dc.Replace(Dialogs.MainDialogName);
                        break;
                    }
                }
            });

            Add(Dialogs.ViewProfileDialogName, new WaterfallStep[]
            {
                async(dc, args, next) =>
                {
                    var state = dc.Context.GetConversationState <ProfileState>();
                    if (string.IsNullOrWhiteSpace(state.Name))
                    {
                        await dc.Prompt(Inputs.NamePrompt, "What is your name?");
                    }
                    else
                    {
                        await next(args);
                    }
                },
                async(dc, args, next) =>
                {
                    var state = dc.Context.GetConversationState <ProfileState>();
                    if (string.IsNullOrWhiteSpace(state.Name))
                    {
                        string name = (string)args["Value"];
                        state.Name  = name;
                    }

                    if (state.ProfileId.HasValue)
                    {
                        var profile            = await _client.GetProfileAsync(state.ProfileId.Value);
                        state.EnrollmentStatus = profile.EnrollmentStatus;
                        switch (profile.EnrollmentStatus)
                        {
                        case EnrollmentStatus.Enrolling:
                            await dc.Context.SendActivity(
                                $"Welcome back {state.Name}. You are enrolling, {profile.RemainingEnrollmentSpeechSeconds}s remaining");
                            break;

                        case EnrollmentStatus.Training:
                            await dc.Context.SendActivity($"Welcome back {state.Name}. Your profile is being trained.");
                            break;

                        case EnrollmentStatus.Enrolled:
                            await dc.Context.SendActivity($"Welcome back {state.Name}. Your profile is enrolled.");
                            break;
                        }
                    }
                    else
                    {
                        await dc.Context.SendActivity($"I haven't seen you before, {state.Name}");
                    }

                    //we're done
                    await dc.Replace(Dialogs.ManageProfileDialogName);
                }
            });

            Add(Dialogs.CreateProfileDialogName, new WaterfallStep[]
            {
                async(dc, args, next) =>
                {
                    var state = dc.Context.GetConversationState <ProfileState>();
                    if (string.IsNullOrWhiteSpace(state.Name))
                    {
                        await dc.Prompt(Inputs.NamePrompt, "What is your name?");
                    }
                    else
                    {
                        await next(args);
                    }
                },
                async(dc, args, next) =>
                {
                    var state = dc.Context.GetConversationState <ProfileState>();
                    if (string.IsNullOrWhiteSpace(state.Name))
                    {
                        string name = (string)args["Value"];
                        state.Name  = name;
                    }

                    if (state.ProfileId.HasValue)
                    {
                        //exists
                        await dc.Context.SendActivity(
                            $"I know you {state.Name}. Your existing profile id is: {state.ProfileId.Value}");
                    }
                    else
                    {
                        //new
                        await dc.Context.SendActivity("Creating a new profile...");
                        var result             = await _client.CreateProfileAsync("en-US");
                        state.ProfileId        = result.ProfileId;
                        state.EnrollmentStatus = EnrollmentStatus.Enrolling;
                        await dc.Context.SendActivity($"Welcome {state.Name}. Your new profile id is: {result.ProfileId}");

                        //we're done
                        await dc.Replace(Dialogs.ManageProfileDialogName);
                    }
                }
            });

            Add(Dialogs.DeleteProfileDialogName, new WaterfallStep[]
            {
                async(dc, args, next) =>
                {
                    var state = dc.Context.GetConversationState <ProfileState>();
                    if (string.IsNullOrWhiteSpace(state.Name))
                    {
                        await dc.Prompt(Inputs.NamePrompt, "What is your name?");
                    }
                    else
                    {
                        await next(args);
                    }
                },
                async(dc, args, next) =>
                {
                    var state = dc.Context.GetConversationState <ProfileState>();
                    if (string.IsNullOrWhiteSpace(state.Name))
                    {
                        string name = (string)args["Value"];
                        state.Name  = name;
                    }

                    if (state.ProfileId.HasValue)
                    {
                        //exists
                        await dc.Context.SendActivity($"Deleting your profile");
                        await _client.DeleteProfileAsync(state.ProfileId.Value);
                        state.ProfileId = null;
                        await dc.Context.SendActivity($"Deleted your profile");
                    }
                    else
                    {
                        //new
                        await dc.Context.SendActivity("I'm sorry, you don't have a profile to delete.");
                    }

                    //we're done
                    await dc.Replace(Dialogs.ManageProfileDialogName);
                }
            });

            Add(Dialogs.EnrollProfileDialogName, new WaterfallStep[]
            {
                async(dc, args, next) =>
                {
                    var state = dc.Context.GetConversationState <ProfileState>();
                    if (string.IsNullOrWhiteSpace(state.Name))
                    {
                        await dc.Prompt(Inputs.NamePrompt, "What is your name?");
                    }
                    else
                    {
                        await next(args);
                    }
                },
                async(dc, args, next) =>
                {
                    var state = dc.Context.GetConversationState <ProfileState>();
                    if (string.IsNullOrWhiteSpace(state.Name))
                    {
                        string name = (string)args["Value"];
                        state.Name  = name;
                    }

                    if (state.ProfileId.HasValue)
                    {
                        //exists
                        await dc.Context.SendActivity($"Enrolling your profile");
                        await dc.Prompt(Inputs.EnrollProfilePrompt, "Please upload a .wav file", new PromptOptions());
                    }
                    else
                    {
                        //new
                        await dc.Context.SendActivity("I'm sorry, you don't have a profile to enroll.");
                        //we're done
                        await dc.Replace(Dialogs.ManageProfileDialogName);
                    }
                },
                async(dc, args, next) =>
                {
                    var state = dc.Context.GetConversationState <ProfileState>();
                    if (!state.ProfileId.HasValue)
                    {
                        //new
                        await dc.Context.SendActivity("I'm sorry, you don't have a profile to enroll.");
                        //we're done
                        await dc.Replace(Dialogs.ManageProfileDialogName);
                        return;
                    }

                    //Get attachment details
                    var attachment = ((List <Attachment>)args["Attachments"]).FirstOrDefault();

                    if (attachment == null)
                    {
                        await dc.Context.SendActivity("I didn't get the attachment...");
                        //we're done
                        await dc.Replace(Dialogs.ManageProfileDialogName);
                        return;
                    }

                    if (attachment.ContentType != "audio/wav" || string.IsNullOrWhiteSpace(attachment.ContentUrl))
                    {
                        await dc.Context.SendActivity($"I didn't get a .wav file attachment...");
                        //we're done
                        await dc.Replace(Dialogs.ManageProfileDialogName);
                        return;
                    }

                    string attachmentContentUrl = attachment.ContentUrl;

                    //send attachment in chunks to be analyzed
                    await dc.Context.SendActivity("Enrolling a profile with your voice...");

                    await EnrollWavFile(attachmentContentUrl, state.ProfileId.Value, dc);

                    await dc.Context.SendActivity("Enrolling of attachment is complete.");

                    //we're done
                    await dc.Replace(Dialogs.ManageProfileDialogName);
                }
            });

            Add(Inputs.EnrollProfilePrompt, new AttachmentPrompt());
        }