Foxtable(狐表)用户栏目专家坐堂 → C#代码转换


  共有5256人关注过本帖树形打印复制链接

主题:C#代码转换

帅哥哟,离线,有人找我吗?
明丰
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:558 积分:4352 威望:0 精华:2 注册:2011/4/18 8:27:00
C#代码转换  发帖心情 Post By:2013/9/4 21:49:00 [显示全部帖子]

请问下面C#的代码能转换吗?主要是回调函数的应用。

 

using System;
using System.Collections.Generic;
using System.Text;
using DotNetSpeech;
using System.Threading;
using System.IO;

namespace SpVoiceDemo
{
    class SpVoiceUtil
    {
        SpVoice voice = new DotNetSpeech.SpVoiceClass();

        public delegate void CallBack(bool b,int InputWordPosition, int InputWordLength); 

        /// <summary>
        /// 朗读文本
        /// </summary>
        /// <param name="str">要朗读的文本</param>
        /// <param name="CallBack">回调地址</param>
        /// <returns>返回bool</returns>
        public bool Speak(string str, CallBack CallBack)
        {
            int n = voice.Speak(str, SpeechVoiceSpeakFlags.SVSFlagsAsync);
            Thread thread = new Thread(new ParameterizedThreadStart(Call));
            thread.IsBackground = true;
            thread.Start((Object)CallBack);
            return !(n!=1);
        }


        /// <summary>
        /// 回调函数线程子程序
        /// </summary>
        /// <param name="callBack"></param>
        private void Call(Object callBack)
        {
            int InputWordLength = 0;    //局部_朗读长度
            int InputWordPosition = 0; //局部_朗读位置

            CallBack CallBack = (CallBack)callBack;

            while ((int)voice.Status.RunningState != 1)
            {
                if (InputWordPosition != voice.Status.InputWordPosition || InputWordLength != voice.Status.InputWordLength)
                {
                    InputWordPosition = voice.Status.InputWordPosition;
                    InputWordLength = voice.Status.InputWordLength;

                    //回调                  
                    CallBack(false, InputWordPosition, InputWordLength);
                }
            }
            CallBack(true, InputWordPosition, InputWordLength);
        }

        /// <summary>
        /// 获取语音库
        /// </summary>
        /// <returns>List<string></returns>
        public List<string> getDescription()
        {
            List<string> list = new List<string>();
            DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices();
            int count = obj.Count;//获取语音库总数
            for (int i = 0; i < count; i++)
            {
               string desc = obj.Item(i).GetDescription(); //遍历语音库
               list.Add(desc);
            }
            return list;
        }

        /// <summary>
        /// 设置当前使用语音库
        /// </summary>
        /// <returns>bool</returns>
        public bool setDescription(string name)
        {
            List<string> list = new List<string>();
            DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices();
            int count = obj.Count;//获取语音库总数
            bool result = false;
            for (int i = 0; i < count; i++)
            {
                string desc = obj.Item(i).GetDescription(); //遍历语音库
                if (desc.Equals(name))
                {
                    voice.Voice = obj.Item(i);
                    result = true;
                }
            }
            return result;
        }

        /// <summary>
        /// 设置语速
        /// </summary>
        /// <param name="n"></param>
        public void setRate(int n)
        {
            voice.Rate = n;
        }

        /// <summary>
        /// 设置声音大小
        /// </summary>
        /// <param name="n"></param>
        public void setVolume(int n)
        {
            voice.Volume = n;
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            voice.Pause();
        }

        /// <summary>
        /// 继续
        /// </summary>
        public void Resume()
        {
            voice.Resume();
        }

        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
        }


        /// <summary>
        /// 输出WAV
        /// </summary>
        /// <param name="path">保存路径</param>
        /// <param name="str">要转换的文本内容</param>
        /// <returns></returns>
        public bool WreiteToWAV(string path,string str,SpeechAudioFormatType SpAudioType)
        {
            SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
            SpFileStream SpFileStream = new SpFileStream();
            SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            SpAudioFormat SpAudio = new DotNetSpeech.SpAudioFormat();
            SpAudio.Type = SpAudioType;
            SpFileStream.Format = SpAudio;
            SpFileStream.Open(path, SpFileMode, false);
            voice.AudioOutputStream = SpFileStream;
            voice.Speak(str, SpFlags);
            voice.WaitUntilDone(Timeout.Infinite);
            SpFileStream.Close();
            return File.Exists(path);
        }
    }
}

 回到顶部
帅哥哟,离线,有人找我吗?
明丰
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:二尾狐 帖子:558 积分:4352 威望:0 精华:2 注册:2011/4/18 8:27:00
  发帖心情 Post By:2013/9/5 23:14:00 [显示全部帖子]

下面代码设置正确吗?异步朗读时如何获得和设置朗读位置、朗读长度、是否读完,请问如何设置?

 

'Namespace SpVoiceDemo
 Class SpVoiceUtil
  Private voice As New DotNetSpeech.SpVoiceClass() '私有变量,声明的变量只在它自身的一个模块中通行

  Public Delegate Sub CallBack(b As Boolean, InputWordPosition As Integer, InputWordLength As Integer)

  ''' <summary>
  ''' 朗读文本
  ''' </summary>
  ''' <param name="str">要朗读的文本</param>
  ''' <param name="CallBack">回调地址</param>
  ''' <returns>返回bool</returns>
  Public Function Speak(str As String, CallBack As CallBack) As Boolean
   Dim n As Integer = voice.Speak(str, DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync)
   Dim thread As New System.Threading.Thread(AddressOf [call])'AddressOf运算符 用于获得过程参数
   thread.IsBackground = True
   thread.Start(DirectCast(CallBack, [Object]))
   Return Not (n <> 1)
  End Function


  ''' <summary>
  ''' 回调函数线程子程序
  ''' </summary>
  ''' <param name="callBack"></param>
  Private Sub [Call](callBack__1 As [Object])
   Dim InputWordLength As Integer = 0
   '局部_朗读长度
   Dim InputWordPosition As Integer = 0
   '局部_朗读位置
   Dim CallBack__2 As CallBack = DirectCast(callBack__1, CallBack)

   While CInt(voice.Status.RunningState) <> 1
    If InputWordPosition <> voice.Status.InputWordPosition OrElse InputWordLength <> voice.Status.InputWordLength Then
     InputWordPosition = voice.Status.InputWordPosition
     InputWordLength = voice.Status.InputWordLength

     '回调                 
     CallBack__2(False, InputWordPosition, InputWordLength)
    End If
   End While
   CallBack__2(True, InputWordPosition, InputWordLength)
  End Sub

  ''' <summary>
  ''' 获取语音库
  ''' </summary>
  ''' <returns>List<string></returns>
  Public Function getDescription() As List(Of String)
   Dim List As New List(Of String)()
   Dim obj As DotNetSpeech.ISpeechObjectTokens = voice.GetVoices()
   Dim count As Integer = obj.Count
   '获取语音库总数
   For i As Integer = 0 To count - 1
    Dim desc As String = obj.Item(i).GetDescription()
    '遍历语音库
    List.Add(desc)
   Next
   Return List
  End Function

  ''' <summary>
  ''' 设置当前使用语音库
  ''' </summary>
  ''' <returns>bool</returns>
  Public Function setDescription(name As String) As Boolean
   Dim List As New List(Of String)()
   Dim obj As DotNetSpeech.ISpeechObjectTokens = voice.GetVoices()
   Dim count As Integer = obj.Count
   '获取语音库总数
   Dim result As Boolean = False
   For i As Integer = 0 To count - 1
    Dim desc As String = obj.Item(i).GetDescription()
    '遍历语音库
    If desc.Equals(name) Then
     voice.Voice = obj.Item(i)
     result = True
    End If
   Next
   Return result
  End Function

  ''' <summary>
  ''' 设置语速
  ''' </summary>
  ''' <param name="n"></param>
  Public Sub setRate(n As Integer)
   voice.Rate = n
  End Sub

  ''' <summary>
  ''' 设置声音大小
  ''' </summary>
  ''' <param name="n"></param>
  Public Sub setVolume(n As Integer)
   voice.Volume = n
  End Sub

  ''' <summary>
  ''' 暂停
  ''' </summary>
  Public Sub Pause()
   voice.Pause()
  End Sub

  ''' <summary>
  ''' 继续
  ''' </summary>
  Public Sub [Resume]()
   voice.[Resume]()
  End Sub

  ''' <summary>
  ''' 停止
  ''' </summary>
  Public Sub [Stop]()
   voice.Speak(String.Empty, DotNetSpeech.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak)
  End Sub


  ''' <summary>
  ''' 输出WAV
  ''' </summary>
  ''' <param name="path">保存路径</param>
  ''' <param name="str">要转换的文本内容</param>
  ''' <returns></returns>
  Public Function WreiteToWAV(path As String, str As String, SpAudioType As DotNetSpeech.SpeechAudioFormatType) As Boolean
   Dim SpFileMode As DotNetSpeech.SpeechStreamFileMode = DotNetSpeech.SpeechStreamFileMode.SSFMCreateForWrite
   Dim SpFileStream As New DotNetSpeech.SpFileStream()
   Dim SpFlags As DotNetSpeech.SpeechVoiceSpeakFlags = DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync
   Dim SpAudio As DotNetSpeech.SpAudioFormat = New DotNetSpeech.SpAudioFormat()
   SpAudio.Type = SpAudioType
   SpFileStream.Format = SpAudio
   SpFileStream.Open(path, SpFileMode, False)
   voice.AudioOutputStream = SpFileStream
   voice.Speak(str, SpFlags)

   voice.WaitUntilDone(System.Threading.Timeout.Infinite)
    'Timeout.Infinite 用于指定无限长等待时间的常数.此字段为常数-1
   'voice.WaitUntilDone(-1)
   SpFileStream.Close()
   Return System.IO.File.Exists(path)
  End Function
 End Class
'End Namespace


 回到顶部