Sapi 5.1 under SBS2003 IIS BUG?
am 15.01.2008 09:03:00 von EdoardoRinaldiHello,
I found many problems having SAPI running under IIS.
To do some tests i wrote a generic handler which take the phrase as query
string and uses interop services to interact with sapi.dll.
The first problem is that requesting once the generic handler under XP
results in having the ProcessRequest of the IHttpHandler being called many
times, but the wave stream is reproduced correctly.
The second problem is that requesting once the generic handler under SBS2003
results in having the ProcessRequest of the IHttpHandler being called many
times, and the wave stream gets truncated at 46 bytes.
Following is part of the IHttpHandler code used to do the tests.
public class SpeechHandler : IHttpHandler
{
private FileInfo _tempWaveFile = default(FileInfo);
private const string voiceName = "Microsoft Sam";
public void ProcessRequest(HttpContext context)
{
string phrase = context.Request["Phrase"];
if (!String.IsNullOrEmpty(phrase))
{
_tempWaveFile = new FileInfo(context.Server.MapPath("~/" +
Guid.NewGuid().ToString("N") + ".wav"));
CreateSpeechFile(phrase, _tempWaveFile.FullName, voiceName);
HttpResponse resp = context.Response;
resp.ContentType = "audio/wav";
resp.WriteFile(_tempWaveFile.FullName, true);
//resp.End();
//_tempWaveFile.Delete();
}
}
public bool IsReusable { get { return false; } }
private void CreateSpeechFile(string phrase, string filePath, string
voiceName)
{
SpFileStream spFileStream = new SpFileStream();
try
{
SpVoice speech = new SpVoice();
ISpeechObjectTokens voices = speech.GetVoices(string.Empty, string.Empty);
speech.Voice = voices.Item(VoiceIndexByName(voices, voiceName));
SpAudioFormatClass format = new SpAudioFormatClass();
format.Type = SpeechAudioFormatType.SAFT8kHz16BitMono;
spFileStream.Format = format;
spFileStream.Open(filePath, SpeechStreamFileMode.SSFMCreateForWrite,
false);
speech.AudioOutputStream = spFileStream;
speech.Rate = 0;
speech.Speak(phrase, SpeechVoiceSpeakFlags.SVSFlagsAsync |
SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
speech.WaitUntilDone(System.Threading.Timeout.Infinite);
}
catch (Exception e)
{
System.Diagnostics.Debug.Print("Exception: " + e.Message);
}
finally
{
spFileStream.Close();
}
}
private int VoiceIndexByName(ISpeechObjectTokens voices, string voiceName)
{
int i = 0;
foreach (ISpeechObjectToken voice in voices)
{
if (voiceName.Equals(voice.GetDescription(0))) return i;
i++;
}
return i;
}
}
Also, when I user System.Speech from .Net Framework 3.0 the stream is
reproduced correctly under XP, but a strange error about memory corruption is
generated under SBS2003.
Any idea on how to workaround this?
Thanks