1

我在 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 那样更改语音的音频格式? TTSApp

4

1 回答 1

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、 和 方法中AssignFormatFormatId作用WaveFormatExPtr

于 2014-04-09T22:19:25.703 回答