如何使用ISpLexicon :: GetWords获取单词列表?

时间:2019-03-25 09:25:57

标签: c++ text-to-speech sapi speech-synthesis

我正在使用Microsoft SAPI开发文本语音转换应用程序。我发现可以在字典中添加单词的自定义发音(如果我错了,请纠正我)。我实现了一个允许在此词典中添加单词的功能。这是我的代码:

int addPrononciation( const char* addPron, const char* phon )
{
   hr = cpLexicon.CoCreateInstance( CLSID_SpLexicon );
   hr = cpContainerLexicon.CoCreateInstance( CLSID_SpLexicon );

   hr = SpEnumTokens( SPCAT_VOICES, NULL, NULL, &cpEnum );
   cpEnum->Item( saveVoice, &cpVoiceToken ); //get saveVoice token defined at line 136
   cpVoice->SetVoice( cpVoiceToken ); //Initialization of the voice

   hr = cpContainerLexicon->AddLexicon( cpLexicon, eLEXTYPE_APP );
   langId = MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US );
   hr = SpCreatePhoneConverter( langId, NULL, NULL, &cpPhoneConv );

   int wchars_num = MultiByteToWideChar( CP_ACP, 0, addPron, -1, NULL, 0 );
   wchar_t* pronWstr = new wchar_t[ wchars_num ];
   MultiByteToWideChar( CP_ACP, 0, addPron, -1, pronWstr, wchars_num );

   int phonWchars_num = MultiByteToWideChar( CP_ACP, 0, phon, -1, NULL, 0 );
   wchar_t* phonWstr = new wchar_t[ phonWchars_num ];
   MultiByteToWideChar( CP_ACP, 0, phon, -1, phonWstr, phonWchars_num );

   if(SUCCEEDED( hr ))
   {
      hr = cpPhoneConv->PhoneToId( phonWstr, wszId );
      hr = cpVoice->Speak( phonWstr, SPF_DEFAULT, NULL );
      hr = cpLexicon->AddPronunciation( pronWstr, langId, SPPS_Noun, wszId );
      hr = cpVoice->Speak( pronWstr, SPF_DEFAULT, NULL );
      if( SUCCEEDED( hr ) )
      {
         printf( "Success\n" );
      }
      else
      {
         printf( "Failed\n" );
      }
   }

   cpEnum.Release();
   cpVoiceToken.Release();
   cpContainerLexicon.Release();
   cpLexicon.Release();
   cpPhoneConv.Release();
   delete new wchar_t[ wchars_num ];
   delete new wchar_t[ phonWchars_num ];

   return true;
}

现在,我想使用 ISpLexicon :: GetWords 列出这些单词。

我已经阅读了Microsoft网站上的documentation,并尝试实现该功能,但是我不知道如何初始化变量spWordList

这是我的代码:

 ZeroMemory( &spWordList, sizeof( spWordList ) );
 if( SUCCEEDED( hr ) )
 {
     hr = cpLexicon->GetWords( eLEXTYPE_APP, &dwGeneration, &dwCookie, &spWordList );
     printf( "Words: %ls\n", spWordList ); //print words but the output is null
 }
 CoTaskMemFree( spWordList.pvBuffer );

我正在尝试打印单词,但输出为null。我认为spWordList变量未初始化。这是变量值的屏幕截图。

Variable values

如何初始化它?

1 个答案:

答案 0 :(得分:0)

我发现了如何初始化<td>。您必须将spWordList替换为eLEXTYPE_APP。但是,您可以像我一样保留它们两个。在下面,您将找到一个有关如何列出单词的示例。

eLEXTYPE_USER

在代码中,我遍历整个词典。请注意,列出的单词是随机显示的。如果找到有关 ISpLexicon :: GetWords

的其他重要信息,我将更新我的答案。