I am trying to convert text to audio in C # using the Microsoft Speech Object Library. I successfully accomplished this while saving the sound directly to the wav file, but my main goal is to save the audio to an array of bytes, which I can then write back to asp.net (so that the end user can load it on their machine).
When I try to open a wav file recorded in a response that is being downloaded, nothing is played, and an error message appears when Windows Media Player cannot open the file.
The code below shows what I work and what not.
Does anyone have any ideas on what I might lose in the second part when I just try to write an array of bytes in response as wav?
//////////////////////////////////////////////// // THIS WORKS //SpVoice my_Voice = new SpVoice(); //declaring and initializing SpVoice Class //SpeechVoiceSpeakFlags my_Spflag = SpeechVoiceSpeakFlags.SVSFlagsAsync; // declaring and initializing Speech Voice Flags //SpFileStream spFileStream = new SpFileStream(); //declaring and Initializing fileStream obj //SpeechStreamFileMode spFileMode = SpeechStreamFileMode.SSFMCreateForWrite; //declaring fileStreamMode as to Create or Write //spFileStream.Open("C:\\temp\\hellosample.wav", spFileMode, false); //my_Voice.AudioOutputStream = spFileStream; //my_Voice.Speak("test text to audio in asp.net", my_Spflag); //my_Voice.WaitUntilDone(-1); //spFileStream.Close(); //////////////////////////////////////////////// //////////////////////////////////////////////// // THIS DOES NOT WORK SpVoice my_Voice = new SpVoice(); //declaring and initializing SpVoice Class SpeechVoiceSpeakFlags my_Spflag = SpeechVoiceSpeakFlags.SVSFlagsAsync; // declaring and initializing Speech Voice Flags SpMemoryStream spMemStream = new SpMemoryStream(); spMemStream.Format.Type = SpeechAudioFormatType.SAFT11kHz8BitMono; object buf = new object(); my_Voice.AudioOutputStream = spMemStream; my_Voice.Speak("test text to audio!", my_Spflag); my_Voice.WaitUntilDone(-1); spMemStream.Seek(0, SpeechStreamSeekPositionType.SSSPTRelativeToStart); buf = spMemStream.GetData(); byte[] byteArray = (byte[])buf; Response.Clear(); Response.ContentType = "audio/wav"; Response.AppendHeader("Content-Disposition", "attachment; filename=mergedoutput.wav"); Response.BinaryWrite(byteArray); Response.Flush(); ////////////////////////////////////////////////
source share