//Functionality that plays the passed stream back to the user. //This will "freeze" the UI untill playback is completed. //INPUT: memoryStream //OUTPUT: Function result public functionResult onPlayback(ref Stream ms) { functionResult result = new functionResult(); try { //Go to position 0 of the audio stream ms.Position = 0; //Conver the stream to a wavefile and create the required heades using (WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new WaveFileReader(ms)))) { using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback())) { //Start playing waveOut.Init(blockAlignedStream); waveOut.Play(); //While we are playing, prevent anything from happenign on the UI. Might be worth expanding this to have a "Stop" button while (waveOut.PlaybackState == PlaybackState.Playing) { System.Threading.Thread.Sleep(100); } } } result.Result = true; result.Message = "onPlayback - OK"; } catch (Exception ex) { result.Result = false; result.Message = "ERROR - onPlayback - " + ex.ToString(); } return(result); }
//Once we stop recording, clean up and store recording end date/time //INPUT: sender and StoppedEventArgs //OUTPUT: public void OnRecordingStopped(object sender, StoppedEventArgs e) { functionResult result = new functionResult(); try { //Clean up everytyhing used within the recording if (waveIn != null) { waveIn.Dispose(); waveIn = null; } if (writer != null) { writer.Close(); writer = null; } //Store the date/time the recording ended to be used for calculating the sample length recordingEnded = DateTime.Now; } catch (Exception ex) { result.Result = false; result.Message = "ERROR - OnDataAvailable - " + ex.ToString(); } }
//This function will take the passed recording and string to compare againas and do a speect to text //INPUT: stream with recording, expected text //OUPUT: functionResult public functionResult convertToSpeechWithString(Stream ms, string requestString) { functionResult f = new functionResult(); //Create a list of choices of phrases we expect Choices c = new Choices(); //Add the exprected phrase to the choices c.Add(requestString); f = convertToSpeechwithChoices(ms, c); return(f); }
//As data becomes vailable, add it to the writer. //INPUT: sender and waveEventArgs //OUTPUT: private void OnDataAvailable(object sender, WaveInEventArgs e) { functionResult result = new functionResult(); try { writer.Write(e.Buffer, 0, e.BytesRecorded); } catch (Exception ex) { result.Result = false; result.Message = "ERROR - OnDataAvailable - " + ex.ToString(); } }
//This function remove the passed UserID //INPUT: userId //OUTPUT: functionResult public functionResult removeUser(string userID) { functionResult r = new functionResult(); string SQL = @"DELETE FROM users WHERE ID = '" + userID + "'"; string output = executeNonQueryV2(SQL); if (output.ToUpper().Contains("ERROR")) { r.Result = false; r.Message = output; } else { r.Result = true; r.Message = "removeUser - OK"; } return(r); }
//This function will update the passed UserID with a new Name and ProfileID //INPUT: UserID to update, new Name and ProfileID //OUTPUT: functionResult public functionResult updateUser(string userID, string name, string ProfileID) { functionResult r = new functionResult(); string SQL = @"UPDATE users SET NAME = '" + name + "', ProfileID = '" + ProfileID + "' WHERE ID = '" + userID + "'"; string output = executeNonQueryV2(SQL); if (output.Contains("Error")) { r.Result = false; r.Message = output; } else { r.Result = true; r.Message = "updateUser - OK"; } return(r); }
//This function will remove the passed GUID from the database //INPUT: speaker GUID //OUTPUT: functionResult public async Task <functionResult> removeSpeaker(Guid speakerID) { functionResult result = new functionResult(); Console.WriteLine(speakerID.ToString()); try { //Remove the speaker from the database await _serviceClient.DeleteProfileAsync(speakerID); result.Result = true; result.Message = "The selected profile has been remove"; } catch (CreateProfileException ex) { result.Result = false; result.Message = "Profile Creation Error: " + ex.Message; } catch (DeleteProfileException ex) { //If the error contains "speakerInvalid" we can assume the speakerID doesn't exist. So return can be true to continue removing from the database if (ex.Message.ToUpper().Contains("SPEAKERINVALID")) { result.Result = true; result.Message = "Missing Profile: " + ex.ToString(); } else { result.Result = false; result.Message = "Error deleting The Profile: " + ex.ToString(); } } catch (Exception ex) { result.Result = false; result.Message = "Error: " + ex.Message + " - " + ex.GetType(); } return(result); }
//This function will create the audio into the passed stream. //We pass the required audio stream to to record to. //INPUT: the memoryStream to store the recorded audio in //OUTPUT: runctionResult public functionResult onRecord(ref Stream memoryStream) { functionResult result = new functionResult(); if (waveIn == null) { try { waveIn = new WaveIn(); //Need to configure to 16kz and Mono for the library waveIn.WaveFormat = new WaveFormat(16000, 1); //Create a new memory stream memoryStream = new MemoryStream(); //Allocate the writer to the wavefilre writer. Need to ifnoredisposestream as if we don't it caused some //issues within the Project Oxford items writer = new WaveFileWriter(new IgnoreDisposeStream(memoryStream), waveIn.WaveFormat); waveIn.DataAvailable += new EventHandler <WaveInEventArgs>(OnDataAvailable); waveIn.RecordingStopped += new EventHandler <StoppedEventArgs>(OnRecordingStopped); //Start recording from the input device waveIn.StartRecording(); //store the date/time the recoriding started to be used for calculating the length recordingStarted = DateTime.Now; result.Result = true; result.Message = ("onRecord - Sucess"); } catch (Exception ex) { result.Result = false; result.Message = ("ERROR - onRecord - " + ex.ToString()); OnRecordingStopped(null, null); } } return(result); }
//This function will remove the passed AccountID //INPUT: AccountID //OUTPUT: functionResult public functionResult RemoveAccountLogin(int AccountID) { functionResult result = new functionResult(); EncryptionDecrypt edde = new EncryptionDecrypt(); //Create the SQL string. Encrypt the password we are storing string SQL = "DELETE FROM Logons WHERE AccountID ='" + AccountID + "'"; string output = executeNonQueryV2(SQL); if (output.ToUpper().Contains("ERROR")) { result.Result = false; result.Message = "RemoveAccountLogin: "******"RemoveAccountLogin - OK"; } return(result); }
//This function will uppdate the passed userlogin //INPUT: UserLogin //OUTPUT: functionResult public functionResult UpdateAccountLogin(UserLogin u) { functionResult result = new functionResult(); EncryptionDecrypt edde = new EncryptionDecrypt(); //Create the SQL string. Encrypt the password we are storing string SQL = "UPDATE Logons SET SiteKey = '" + u.SiteKey + "' , username = '******', password = '******', website = '" + u.website + "' WHERE AccountID ='" + u.AccountID + "'"; string output = executeNonQueryV2(SQL); if (output.ToUpper().Contains("ERROR")) { result.Result = false; result.Message = "UpdateAccountLogin: "******"UpdateAccountLogin - OK"; } return(result); }
//This function add a new login //INPUT: UserLogin (contains all details) //OUTPUT: functionResult public functionResult AddAccountLogin(UserLogin u) { functionResult result = new functionResult(); EncryptionDecrypt edde = new EncryptionDecrypt(); //Create the SQL string. Encrypt the password we are storing string SQL = "INSERT INTO Logons (UserID, SiteKey, username, password, website) VALUES ('" + u.UserID + "','" + u.SiteKey + "', '" + u.username + "','" + edde.EncryptString(u.password) + "','" + u.website + "')"; string output = executeNonQueryV2(SQL); if (output.ToUpper().Contains("ERROR")) { result.Result = false; result.Message = "AddAccountLogin: "******"AddAccountLogin - OK"; } return(result); }
//This function will add a new user //INPUT: New user name //OUTPUT: functionResult public functionResult addNewUser(string userName) { functionResult result = new functionResult(); string SQL = "INSERT INTO users (NAME) VALUES ('" + userName + "')"; string output = executeNonQueryV2(SQL); if (output.ToUpper().Contains("ERROR")) { result.Result = false; result.Message = "addNewUser: "******"addNewUser - OK"; } if (result.Result == true) { SQL = "SELECT MAX(ID) FROM users"; string output2 = executeQueryV2(SQL); if (output2.ToUpper().Contains("ERROR") || output2.ToUpper().Contains("EMPTY")) { result.Result = false; result.Message = "addNewUser (get max): " + output; } else { result.Result = true; string temp = output2.Substring(output2.IndexOf(":") + 1); temp = temp.Replace("\"", ""); temp = temp.Replace("}", ""); temp = temp.Replace("]", ""); result.Message = temp; } } return(result); }
//This function converst the passed memory stream and compares it to the passed Choices //INPUT: stream with recording, Text choices to look for //OUPUT: FunctionResult //This does the speech recognition against the stream and coices public functionResult convertToSpeechwithChoices(Stream ms, Choices c) { functionResult f = new functionResult(); SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")); //Create a dictionary Grammar and assign the coices to it Grammar dictationGrammar = new DictationGrammar(); var gb = new GrammarBuilder(c); var g = new Grammar(gb); //Load the grammar for the speech recognition engine recognizer.LoadGrammar(g); try { //Go to the beginning of the stream ms.Position = 0; //Conver the stream into a wave stream WaveStream pcmStream = new WaveFileReader(ms); recognizer.SetInputToAudioStream(pcmStream, new SpeechAudioFormatInfo(16000, AudioBitsPerSample.Sixteen, AudioChannel.Mono)); //Do the speech recognition RecognitionResult result = recognizer.Recognize(); f.Result = true; f.recognitionResult = result; } catch (Exception ex) { f.Message = "ERROR: " + ex.Message; f.Result = false; } finally { recognizer.UnloadAllGrammars(); } return(f); }
//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); }
//This function will save the passed audio stream to the file name specified. This wills save in the same directory as the application //INPUT: memory stream, Filename //OUPUT: FunctionResult public functionResult onSaveFile(ref Stream ms, string filename) { functionResult result = new functionResult(); try { //Reset the audio stream position to 0, and save it to a new file. ms.Position = 0; using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(new WaveFileReader(ms))) { //step 3: write wave data into file with WaveFileWriter. WaveFileWriter.CreateWaveFile(filename, pcmStream); } result.Result = true; result.Message = "onSaveFile - ok"; } catch (Exception ex) { result.Result = false; result.Message = "ERROR - onSaveFile - " + ex.ToString(); } return(result); }
//This functionality will used the passed audio stream to enroll a speaker with the passed GUID //INPUT: enrollment audio sample, Speaker ID, Sample Length //OUTPUT: FunctionResult public async Task <functionResult> enrollSpeaker(Stream ms, Guid speakerID, double sampleLength) { functionResult result = new functionResult(); //Ensure the stream isnot null if (ms != null) { //Ensure we have a sample lenght of more than 5 seconds to enroll if (sampleLength > 5) { try { { OperationLocation processPollingLocation; using (Stream audioStream = ms) { audioStream.Position = 0; processPollingLocation = await _serviceClient.EnrollAsync(audioStream, speakerID, true); } 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) { result.Result = false; result.Message = ("Enrollment Error: Enrollment operation timeout."); } result.Result = true; result.Message = ("User has been enrolled successfully"); } } catch (EnrollmentException ex) { result.Result = false; result.Message = ("Enrollment Error: " + ex.Message); } catch (Exception ex) { result.Result = false; result.Message = ("Error: " + ex.Message); } } else { result.Result = false; result.Message = ("Enroll: Audio sample need to be > 5 seconds"); } } else { result.Result = false; result.Message = ("Enroll: No valid sample recorded"); } return(result); }