HSP付属のサンプルとやり方が違いますが、同じことができるので。
ついでに、音量変更と速度変更機能も付けておきました。
#module SAPI_Speech_Voices
#define global ctype SUCCEEDED(%1) ((%1) >= 0)
/*
#define global SafeRelease(%1) if ((varuse@(%1)) && (vartype@(%1) == 6)){ \
delcom@ %1: \
%1 = 0 \
}
*/
#define IID_ISpVoice "{6C44DF74-72B9-4992-A1EC-EF996E0422D4}"
#define IID_IEnumSpObjectTokens "{06B64F9E-7FDA-11D2-B4F2-00C04F797396}"
#define IID_ISpObjectToken "{14056589-E16C-11D2-BB90-00C04F8EE6C0}"
#define IID_ISpObjectTokenCategory "{2D3D3845-39AF-4850-BBF9-40B49780011D}"
#define CLSID_SpVoice "{96749377-3391-11D2-9EE3-00C04F797396}"
#define CLSID_SpMMAudioEnum "{AB1890A0-E91F-11D2-BB91-00C04F8EE6C0}"
#define CLSID_SpObjectToken "{EF411752-3736-4CB4-9C8C-8EF4CCB58EFE}"
#define CLSID_SpObjectTokenCategory "{A910187F-0C7A-45AC-92CC-59EDAFB77B53}"
#define SPCAT_VOICES "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices"
#usecom ISpVoice IID_ISpVoice CLSID_SpVoice
// IUnknown(3)->ISpNotifySource(7)->ISpEventSource(3)->ISpVoice
#comfunc ISpVoice_Pause 16
#comfunc ISpVoice_Resume 17
#comfunc ISpVoice_SetVoice 18 int
#comfunc ISpVoice_GetVoice 19 var
#comfunc ISpVoice_Speak 20 wstr, int, int
#comfunc ISpVoice_SetRate 28 int
#comfunc ISpVoice_GetRate 29 var
#comfunc ISpVoice_SetVolume 30 int
#comfunc ISpVoice_GetVolume 31 var
#usecom ISpObjectTokenCategory IID_ISpObjectTokenCategory CLSID_SpObjectTokenCategory
//IUnknown(3)->ISpDataKey(12)->ISpObjectTokenCategory
#comfunc ISpObjectTokenCategory_SetId 15 wstr, int
#comfunc ISpObjectTokenCategory_EnumTokens 18 wstr, wstr, sptr
#usecom IEnumSpObjectTokens IID_IEnumSpObjectTokens CLSID_SpMMAudioEnum
// IUnknown(3)->IEnumSpObjectTokens
#comfunc IEnumSpObjectTokens_Next 3 int, sptr, sptr
#comfunc IEnumSpObjectTokens_Reset 5
#comfunc IEnumSpObjectTokens_Clone 6 sptr
#comfunc IEnumSpObjectTokens_Item 7 int, sptr
#comfunc IEnumSpObjectTokens_GetCount 8 var
#usecom ISpObjectToken IID_ISpObjectToken CLSID_SpObjectToken
// IUnknown(3)->ISpDataKey(12)->ISpObjectToken
#comfunc ISpObjectToken_GetStringValue 6 wstr, sptr
#deffunc SafeRelease var p1
if ((varuse(p1)) && (vartype(p1) == 6)){
delcom p1
p1 = 0
}
return
#deffunc CreateVoice var pSpVoice
newcom pSpVoice, ISpVoice
return
#deffunc GetVoicesList var list
sdim list, 1024
// 話者一覧取得
newcom pSpOTC, ISpObjectTokenCategory
if varuse(pSpOTC){
ISpObjectTokenCategory_SetId pSpOTC, SPCAT_VOICES, 0 : hr = stat
if SUCCEEDED(hr){
ptr = 0
ISpObjectTokenCategory_EnumTokens pSpOTC, "", "", varptr(ptr) : hr = stat
if SUCCEEDED(hr) && ptr != 0{
newcom pSpOTs, IEnumSpObjectTokens, -1, ptr
if varuse(pSpOTs){
Num = 0
IEnumSpObjectTokens_GetCount pSpOTs, Num : hr = stat
if SUCCEEDED(hr){
repeat Num
ptr = 0
IEnumSpObjectTokens_Item pSpOTs, cnt, varptr(ptr) : hr = stat
if SUCCEEDED(hr) && ptr != 0{
newcom pSpT, ISpObjectToken, -1, ptr
ISpObjectToken_GetStringValue pSpT, "", varptr(ptr) : hr = stat
if SUCCEEDED(hr){
dupptr wStr, ptr, 1024, 2
list += cnvwtos(wStr) + "\n"
}
SafeRelease pSpT
}
loop
}
SafeRelease pSpOTs
}
}
}
SafeRelease pSpOTC
}
return hr
#deffunc SetVoice var pSpVoice, int idx
newcom pSpOTC, ISpObjectTokenCategory
if varuse(pSpOTC){
ISpObjectTokenCategory_SetId pSpOTC, SPCAT_VOICES, 0 : hr = stat
if SUCCEEDED(hr){
ptr = 0
ISpObjectTokenCategory_EnumTokens pSpOTC, "", "", varptr(ptr) : hr = stat
if SUCCEEDED(hr) && ptr != 0{
newcom pSpOTs, IEnumSpObjectTokens, -1, ptr
if varuse(pSpOTs){
ptr = 0
IEnumSpObjectTokens_Item pSpOTs, idx, varptr(ptr) : hr = stat
if SUCCEEDED(hr){
ISpVoice_SetVoice pSpVoice, ptr
}
SafeRelease pSpOTs
}
}
}
SafeRelease pSpOTC
}
return hr
#deffunc VoiceSpeek var pSpVoice, str s, int f
ISpVoice_Speak pSpVoice, s, f, 0
return stat
#deffunc DestoryVoice var pSpVoice
SafeRelease pSpVoice
return stat
#defcfunc GetVoiceVolume var pSpVoice
Volume = 0
ISpVoice_GetVolume pSpVoice, Volume
return Volume
#deffunc SetVoiceVolume var pSpVoice, int vol
ISpVoice_SetVolume pSpVoice, vol
return stat
#defcfunc GetVoiceRate var pSpVoice, local Rate
Rate = 0
ISpVoice_GetRate pSpVoice, Rate
return Rate
#deffunc SetVoiceRate var pSpVoice, int rate
ISpVoice_SetRate pSpVoice, rate
return stat
#deffunc VoicePause var pSpVoice
ISpVoice_Pause pSpVoice
return stat
#deffunc VoiceResume var pSpVoice
ISpVoice_Resume pSpVoice
return stat
#global
// 以下サンプル
pSpVoice = 0
// ComObj作成
CreateVoice pSpVoice
if varuse(pSpVoice){
onexit gosub *exit
// 一覧取得
GetVoicesList list
idx = 0
objsize 420, 20
combox idx, 100, list
mes "▼ 文章"
sdim s, 1024 : s = "Hello this is H.S.P. for windows."
input s
mes "▼ 音量(0〜100)"
Volume = 100
input Volume
mes "▼ 速度(-10〜10)"
Rate = 0
input Rate
chkbox "再生終了まで待たない", Async
button gosub "再生", *speak
}else{
dialog "呼び出し失敗"
}
stop
*speak
// 音量設定(0〜100)
SetVoiceVolume pSpVoice, Volume
// 再生速度設定(-10〜10)
SetVoiceRate pSpVoice, Rate
title "音量:"+GetVoiceVolume(pSpVoice)+", 速度"+GetVoiceRate(pSpVoice)
// 音声選択
SetVoice pSpVoice, idx
if stat{
dialog "音声選択に失敗しました"
}
// 同期再生(再生終了まで待つ)
// 1にすると非同期
VoiceSpeek pSpVoice, s, Async
if stat{
dialog "再生失敗しました"
}
return
*exit
// 終了時にでも破棄する
DestoryVoice pSpVoice
end : end
return