Esempio n. 1
0
        public void Speak(string msg, SynthParams p, bool toFile = false)
        {
            const int   headerSize      = 44;
            const int   formatChunkSize = 16;
            const short waveAudioFormat = 1;
            const short numChannels     = 1;
            const int   sampleRate      = 16000;
            const short bitsPerSample   = 16;
            const int   byteRate        = (numChannels * bitsPerSample * sampleRate) / 8;
            const short blockAlign      = numChannels * bitsPerSample / 8;
            var         text            = Encoding.UTF8.GetBytes(msg);
            var         message         = NativeMethods.RHVoice_new_message(engine, text, (uint)text.Length, MessageType.Text, p, IntPtr.Zero);

            using (stream = new MemoryStream())
            {
                NativeMethods.RHVoice_speak(message);
                NativeMethods.RHVoice_delete_message(message);
                var sizeInBytes = (int)stream.Length;
                using (var writer = new MemoryStream())
                {
                    writer.Write(Encoding.ASCII.GetBytes("RIFF"), 0, 4);
                    writer.Write(BitConverter.GetBytes(sizeInBytes + headerSize - 8), 0, 4);
                    writer.Write(Encoding.ASCII.GetBytes("WAVEfmt "), 0, 8);
                    writer.Write(BitConverter.GetBytes(formatChunkSize), 0, 4);
                    writer.Write(BitConverter.GetBytes(waveAudioFormat), 0, 2);
                    writer.Write(BitConverter.GetBytes(numChannels), 0, 2);
                    writer.Write(BitConverter.GetBytes(sampleRate), 0, 4);
                    writer.Write(BitConverter.GetBytes(byteRate), 0, 4);
                    writer.Write(BitConverter.GetBytes(blockAlign), 0, 2);
                    writer.Write(BitConverter.GetBytes(bitsPerSample), 0, 2);
                    writer.Write(Encoding.ASCII.GetBytes("data"), 0, 4);
                    writer.Write(BitConverter.GetBytes(sizeInBytes), 0, 4);
                    stream.Position = 0;
                    stream.CopyTo(writer);
                    var player = new SoundPlayer(writer);
                    writer.Position = 0;
                    player.Play();
                    if (toFile)
                    {
                        using (var file = File.Create("file.wav"))
                        {
                            writer.Position = 0;
                            writer.CopyTo(file);
                        }
                    }
                }
            }
        }
 public static extern IntPtr RHVoice_new_message(IntPtr engine, byte[] text, uint length, MessageType messageType, SynthParams synthParams, IntPtr user_data);