/* 注明: 使用 MultiByteToWideChar 接口转换得 WCHAR,
QString inform = “各位大佬,不吝啬,点个赞!”;
WCHAR wChar[1024];
memset(wChar ,0,1024);
MultiByteToWideChar( CP_ACP , 0 , inform.toLocal8Bit(), inform.toLocal8Bit().size(), wChar , 1024);
强制转换得 LPCWSTR:
QString dateTime = QDateTime::currentDateTime().toString(“yyyy-MM-dd_hh-mm-ss”) + “.wav”;
LPCWSTR filename = (LPCWSTR)dateTime.data();
或者 LPCWSTR filename = dateTime .toStdWString().c_str();
*/
int MainWindow::TextToVideo(const int volume, const WCHAR* wChar, const LPCWSTR &filename)
{
// 初始化语音接口
ISpVoice *pVoice = nullptr;if (FAILED(::CoInitialize(nullptr)))return -1;HRESULT hr = CoCreateInstance(CLSID_SpVoice, nullptr, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);if (SUCCEEDED(hr)){// 获取当前的音量与音速long rate;pVoice->GetRate(&rate);USHORT volume;pVoice->GetVolume(&volume);qDebug()<<"volume:"<<volume<<"rate:"<<rate;pVoice->SetVolume(volume);//设置音量// 获取当前的频率配置CComPtr<ISpStreamFormat> cpStream;HRESULT hrOutputStream =pVoice->GetOutputStream(&cpStream);if (hrOutputStream ==S_OK){CSpStreamFormat Fmt;hr = Fmt.AssignFormat(cpStream);if (SUCCEEDED(hr)){SPSTREAMFORMAT eFmt = puteFormatEnum(); // 默认: SPSF_16kHz16BitMono 18qDebug()<<"format:"<<eFmt;}}// 设置当前朗读频率CComPtr<ISpAudio> m_cpOutAudio; //声音输出接口SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOOUT,&m_cpOutAudio ); //创建接口SPSTREAMFORMAT eFmt = SPSF_8kHz16BitMono;CSpStreamFormat Fmt;Fmt.AssignFormat(eFmt);if (m_cpOutAudio)hr = m_cpOutAudio->SetFormat(Fmt.FormatId(), Fmt.WaveFormatExPtr() );elsereturn -1;pVoice->SetOutput( m_cpOutAudio, FALSE );// 设置输出到.wav文件里CComPtr <ISpStream> cpWavStream;CComPtr <ISpStreamFormat> cpOldStream;pVoice->GetOutputStream(&cpOldStream);CSpStreamFormat originalFmt;originalFmt.AssignFormat(cpOldStream);hr = SPBindToFile(filename, SPFM_CREATE_ALWAYS, &cpWavStream, &originalFmt.FormatId(), originalFmt.WaveFormatExPtr());pVoice->SetOutput(cpWavStream, TRUE);pVoice->Speak(wChar, SPF_DEFAULT, nullptr);pVoice->WaitUntilDone(1000);pVoice->Release();pVoice = nullptr;}::CoUninitialize();return 0;
}@TOC
参考文档:/zd_ad/archive//01/12/2320364.html
如果觉得《文字转语音并生成音频文件》对你有帮助,请点赞、收藏,并留下你的观点哦!