How to write a WAV byte array for a response using the Microsoft Speech Object Library?

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(); //////////////////////////////////////////////// 
+6
source share
3 answers

I would recommend using the SpeechSynthesizer class in the System.Speech assembly instead of the Microsoft speech object library because the assembly is included in the .NET libraries.

Post the example below to explain how to solve your problem using the SpeechSynthesizer class that is created on ASP.NET MVC. Hope this solves your problem.

 public class HomeController : Controller { public async Task<ActionResult> Index() { Task<FileContentResult> task = Task.Run(() => { using (var synth = new SpeechSynthesizer()) using (var stream = new MemoryStream()) { synth.SetOutputToWaveStream(stream); synth.Speak("test text to audio!"); byte[] bytes = stream.GetBuffer(); return File(bytes, "audio/x-wav"); } }); return await task; } } 
+3
source

I did this using ISpStream. Use the Setbasestream function for an ispstream to associate it with istream, and then set the ispvoice output to this ispstream.

Here is my solution if anyone wants to:

https://github.com/itsyash/MS-SAPI-demo

+1
source

I ended up using the microsoft translation service and this .net lib ( http://translatorservice.codeplex.com/ ) to connect to it. It works great. Code below:

  // Connect to translator service SpeechSynthesizer speech = new SpeechSynthesizer("clientID", "secretKey"); speech.AudioFormat = SpeakStreamFormat.MP3; speechStream = speech.GetSpeakStream(text, language); // Write it out to the stream Response.Clear(); Response.ContentType = "audio/mp3"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ""); speechStream.CopyTo(Response.OutputStream); Response.Flush(); 
+1
source

All Articles