我在 Windows 上搞乱 SAPI,我注意到声音的音频质量相当平庸。通过比较一个简单测试程序的音频质量和eSpeak提供的各种质量,我得出的结论是,默认质量在16kHz 16 Bit Mono
.
简单的 SAPI 测试程序
#include <string>
#include <iostream>
#include <Windows.h>
#include <sapi.h>
#define _CHECK_HR(hr, debug_str) \
if(FAILED(hr)) { \
std::cout << debug_str << ": " << std::hex << "0x" << hr << std::dec << std::endl; \
goto check_failure; \
}
#define CHECK_HR(expr, debug_str) \
_CHECK_HR(expr, debug_str);
#define SAFE_RELEASE(obj) \
if(obj != NULL) { \
obj->Release(); \
obj = NULL; \
}
int main()
{
ISpVoice* voice = NULL;
CHECK_HR(CoInitialize(NULL), "CoInitialize");
CHECK_HR(CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (LPVOID*)&voice), "voice = CoCreateInstance");
CHECK_HR(voice->Speak(TEXT("This is a simple test."), 0, NULL), "voice->Speak");
std::cout << "No errors!" << std::endl;
check_failure:
SAFE_RELEASE(voice);
CoUninitialize();
}
当然,我尝试过查阅SAPI 文档,但还没有找到如何更改格式。ISpVoice没有设置格式的方法,但它有一个SetOuput方法,该方法采用:
输出音频设备的流、音频设备或对象令牌
我的下一步是创建一个IAudioClient,其格式由SpConvertStreamFormatEnum提供,并将其IAudioRenderClient设置为语音输出。尝试失败,因为我无法初始化 IAudioClient。
尝试将音频流设置为语音的输出
#include <string>
#include <iostream>
#include <Windows.h>
#include <Mmdeviceapi.h>
#include <Audioclient.h>
#include <audiopolicy.h>
#include <sapi.h>
#include <sphelper.h>
#define _CHECK_HR(hr, debug_str) \
if(FAILED(hr)) { \
std::cout << debug_str << ": " << std::hex << "0x" << hr << std::dec << std::endl; \
goto check_failure; \
}
#define CHECK_HR(expr, debug_str) \
_CHECK_HR(expr, debug_str);
#define SAFE_RELEASE(obj) \
if(obj != NULL) { \
obj->Release(); \
obj = NULL; \
}
#define SAFE_FREE(obj) \
if(obj != NULL) { \
CoTaskMemFree(obj); \
obj = NULL; \
}
int main()
{
ISpVoice* voice = NULL;
IMMDeviceEnumerator* device_enumerator = NULL;
IMMDevice* audio_device = NULL;
WAVEFORMATEX *audio_format = NULL;
GUID format_guid;
IAudioClient* audio_client = NULL;
IAudioRenderClient* audio_render_client = NULL;
CHECK_HR(CoInitialize(NULL), "CoInitialize");
CHECK_HR(CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (LPVOID*)&voice), "CoCreateInstance");
CHECK_HR(CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), reinterpret_cast<void**>(&device_enumerator)), "CoCreateInstance");
CHECK_HR(device_enumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &audio_device), "device_enumerator->GetDefaultAudioEndpoint");
CHECK_HR(audio_device->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, reinterpret_cast<void**>(&audio_client)), "audio_device->Activate");
CHECK_HR(SpConvertStreamFormatEnum(SPSF_48kHz16BitStereo, &format_guid, &audio_format), "SpConvertStreamFormatEnum");
CHECK_HR(audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST | AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE, 0, 0, audio_format, NULL), "audio_client->Initialize");
CHECK_HR(audio_client->Start(), "audio_client->Start");
CHECK_HR(audio_client->GetService(__uuidof(IAudioRenderClient), reinterpret_cast<void**>(&audio_render_client)), "audio_client->GetService");
CHECK_HR(voice->SetOutput(audio_render_client, FALSE), "voice->SetOutput");
CHECK_HR(voice->Speak(TEXT("This is a test."), 0, NULL), "voice->Speak");
std::cout << "No errors!" << std::endl;
check_failure:
SAFE_RELEASE(device_enumerator);
SAFE_RELEASE(audio_device);
SAFE_FREE(audio_format);
SAFE_RELEASE(audio_client);
SAFE_RELEASE(audio_render_client);
CoUninitialize();
}
除此之外,我还浏览了SAPI Audio Interfaces,发现了许多其他接口和实现,但似乎没有一个对这项任务特别有用。我觉得我在这里绕圈子。
问题:如何像 eSpeak 的 TTSApp 那样更改语音的音频格式?