我在Windows上搞乱了SAPI,我注意到声音的音质非常低。通过比较简单测试程序的音频质量和eSpeak提供的各种质量,我得出结论,默认质量大约为16kHz 16 Bit Mono
。
#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 documentation,但还没有找到如何更改格式。 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那样更改语音的音频格式?
答案 0 :(得分:2)
尝试:
ATL::CComPtr<ISpVoice> voice;
voice.CoCreateInstance(CLSID_SpVoice);
CSpStreamFormat format;
format.AssignFormat(SPSF_44kHz16BitMono);
ATL::CComPtr<ISpAudio> audio;
SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOOUT, &audio);
audio->SetFormat(format.FormatId(), format.WaveFormatExPtr());
voice->SetOutput(audio, FALSE);
注意:这不包括任何错误处理,因此您的代码需要检查HRESULT返回代码和对象/指针的有效性。
另请注意,eSpeak的原生输出格式为16位22050Hz单声道。
对于C版本,您需要自己处理COM对象的生命周期,并查看CSpStreamFormat
,AssignFormat
和FormatId
方法中WaveFormatExPtr
正在执行的操作