Export (0) Print
Expand All

TextFragment Class

Contains text and speech attribute information for consumption by a speech synthsizer engine.

System.Object
  System.Speech.Synthesis.TtsEngine.TextFragment

Namespace:  System.Speech.Synthesis.TtsEngine
Assembly:  System.Speech (in System.Speech.dll)

public class TextFragment

The TextFragment type exposes the following members.

  NameDescription
Public methodTextFragmentConstructs a new instance of TextFragment.
Top

  NameDescription
Public propertyStateGets or sets speech attribute information for a TextFragment.
Public propertyTextLengthGets or sets the length of the speech text in the fragment.
Public propertyTextOffsetGets or sets the starting location of the text in the fragment.
Public propertyTextToSpeakSets or gets the speech text of the fragment.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The Speech platform infrastructure unpacks the XML based structure of the SSML input and constructs TextFragment objects.

Speech content is available through the TextLength, TextOffset, and TextToSpeak properties of a TextFragment instance.

Speech attribute information, such as emphasis, pitch, and rate, are obtained from the FragmentState object returned by the TextFragment State property.

The example below is part of a custom speech synthesis implementation inheriting from TtsEngineSsml, and using the use of TextFragment, SpeechEventInfo, FragmentState, and TtsEventID.

The implementation of Speak

  1. Receives an array of TextFragment instances and creates a new array of TextFragment instances to be passed to the Speak method on an underlying synthesis engine.

    Particular care is used to respect the TextOffset, TextLength on the original TextFragment when creating the TextToSpeak on the new TextFragment instances.

  2. If the TtsEngineAction enumeration value by found from the Action property on the FragmentState returned by the State property of each TextFragment instance is Speak, the implementation

    • Translates Americanism to Britishisms in the text to be spoken.

    • If the EventInterest property on the ITtsEngineSite interfaces provided to the implementation support the WordBoundary event type, a SpeechEventInfo instance is used to create an event to drive a synthesizer progress meter is created.

  3. A speech rendering engine is then called with the modified TextFragment array.

private const int WordBoundaryFlag = 1 << (int)TtsEventId.WordBoundary;
private readonly char[] spaces = new char[] { ' ', '\t', '\r', '\n' };
internal struct UsVsUk
{
  internal string UK;
  internal string US;
}

override public void Speak (TextFragment [] frags, IntPtr wfx, ITtsEngineSite site)
{
  TextFragment [] newFrags=new TextFragment[frags.Length];
  
  for (int i=0;i<frags.Length;i++){
    newFrags[i].State=frags[i].State;
    //truncate
    newFrags[i].TextToSpeak = frags[i].TextToSpeak.Substring(frags[i].TextOffset,
                               frags[i].TextLength);
    newFrags[i].TextLength = newFrags[i].TextToSpeak.Length;
    newFrags[i].TextOffset = 0;
    if (newFrags[i].State.Action == TtsEngineAction.Speak) {
      //Us to UK conversion
      foreach (UsVsUk term in TransList) {
      newFrags[i].TextToSpeak.Replace(term.US, term.UK);
      }
      //Generate progress meter events if supported
      if ((site.EventInterest & WordBoundaryFlag) != 0) {
      string[] subs = newFrags[i].TextToSpeak.Split(spaces);
        
      foreach (string s in subs) {
        int offset = newFrags[i].TextOffset;
        SpeechEventInfo spEvent = new SpeechEventInfo((Int16)TtsEventId.WordBoundary, 
                (Int16)EventParameterType.Undefined, 
                 s.Length, new IntPtr(offset));
        offset += s.Length;
        if (s.Trim().Length > 0) {
          SpeechEventInfo[] events = new SpeechEventInfo[1];
          events[0] = spEvent;
          site.AddEvents(events, 1);
        }
      }
      }
    }
  }
  
  _baseSynthesize.Speak(newFrags, wfx, site);
  
}

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft