- 1 year ago
Windows Speech SAPI: How to list ALL the voices installed
To list all the voices installed in the Windows Speech API (SAPI), you can use the ISpObjectTokenCategory
interface along with the SpEnumTokens
method. Here's an example in C++:
cpp#include <sapi.h>
#include <iostream>
int main() {
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr)) {
std::cerr << "Failed to initialize COM" << std::endl;
return -1;
}
ISpObjectTokenCategory* pCategory = NULL;
hr = CoCreateInstance(CLSID_SpObjectTokenCategory, NULL, CLSCTX_ALL, IID_ISpObjectTokenCategory, (void**)&pCategory);
if (FAILED(hr)) {
std::cerr << "Failed to create instance of ISpObjectTokenCategory" << std::endl;
CoUninitialize();
return -1;
}
hr = pCategory->SetId(SPCAT_VOICES, FALSE);
if (FAILED(hr)) {
std::cerr << "Failed to set category ID" << std::endl;
pCategory->Release();
CoUninitialize();
return -1;
}
IEnumSpObjectTokens* pEnum = NULL;
hr = pCategory->EnumTokens(NULL, NULL, &pEnum);
if (FAILED(hr)) {
std::cerr << "Failed to enumerate tokens" << std::endl;
pCategory->Release();
CoUninitialize();
return -1;
}
ISpObjectToken* pToken = NULL;
ULONG count = 0;
while (pEnum->Next(1, &pToken, &count) == S_OK) {
WCHAR* displayName;
hr = pToken->GetStringValue(NULL, &displayName);
if (SUCCEEDED(hr)) {
std::wcout << displayName << std::endl;
CoTaskMemFree(displayName);
}
pToken->Release();
}
pEnum->Release();
pCategory->Release();
CoUninitialize();
return 0;
}
In this example, we use the SAPI interfaces and functions to enumerate and print the display names of all the installed voices. The voices are categorized under the SPCAT_VOICES
category.
Make sure to include the sapi.h
header and link against the sapi.lib
library when compiling the code.
Running this program will output the names of all the installed voices to the console. Each voice has a display name associated with it, which represents its name as it would be displayed to the user.
Note that this code is specific to C++ and the Windows platform. To use the Windows Speech API in other languages, such as C# or Python, you would need to utilize the appropriate language bindings or wrappers for the SAPI API.