# aardio 范例: 离线朗读 · SAPI 与 OneCore



```aardio
//SAPI 文本转语音
import win.ui;
/*DSG{{*/
var winform = win.form(text="离线朗读 · SAPI 与 OneCore";right=825;bottom=532)
winform.add(
button={cls="button";text="朗读";left=657;top=429;right=780;bottom=468;anchors={right=1;bottom=1};z=1};
cbVoice={cls="combobox";left=186;top=436;right=639;bottom=462;anchors={left=1;right=1;bottom=1};edge=1;items={};mode="dropdownlist";z=2};
edit={cls="edit";left=20;top=9;right=794;bottom=426;anchors={left=1;top=1;right=1;bottom=1};edge=1;hscroll=1;multiline=1;vscroll=1;z=8};
formatBox={cls="combobox";left=20;top=436;right=171;bottom=462;anchors={left=1;bottom=1};edge=1;items={"纯文本","SSML","经典 SAPI XML"};mode="dropdownlist";z=9};
lbRate={cls="static";text="语速:";left=53;top=498;right=120;bottom=520;align="right";anchors={left=1;bottom=1};transparent=1;z=6};
lbVolume={cls="static";text="音量:";left=372;top=498;right=434;bottom=520;align="right";anchors={left=1;right=1;bottom=1};transparent=1;z=5};
plusAudioLevel={cls="plus";left=795;top=9;right=813;bottom=423;anchors={top=1;right=1;bottom=1};bgcolor=0xA4A0A0;z=3};
tbRate={cls="trackbar";left=149;top=487;right=375;bottom=517;anchors={left=1;bottom=1};max=100;min=0;z=7};
tbVolume={cls="trackbar";left=467;top=487;right=693;bottom=517;anchors={right=1;bottom=1};max=100;min=0;z=4}
)
/*}}*/

import com.sapi.voice;
var voice = com.sapi.voice(); // 跟随用户区域，无需手动查找 OneCore
var voices = voice.getVoicesEx();
var currentId = #voices ? voice.Voice.Id;

winform.cbVoice.items = table.map(voices,lambda(v)v.name ++ "  ·  " ++ v.source)
winform.cbVoice.selIndex = table.find(voices,lambda (v)v.id == currentId);

var samples = {
	zh = ["听，雨停了。我们慢慢走回去吧。";
		`<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="zh-CN">
  听，雨停了。<break time="450ms"/>
  <prosody rate="-5">不用赶路。</prosody>
  <prosody duration="2s">我们慢慢走回去吧。</prosody>
</speak>`];
	en = ["Listen. The rain has stopped. Let's walk home.";
		`<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="en-US">
  Listen. The rain has stopped.<break time="450ms"/>
  <prosody rate="slow">There's no rush.</prosody>
  <prosody duration="2s">Let's walk home.</prosody>
</speak>`];
};

/*
经典 SAPI XML 仿真人语气样稿：
- 每一句话至少有一个 emphasis 。
- 词组之间轻轻留白。
- 转折稍停，解释连贯。
- 重点略慢，情绪轻微变化
- 保留标点帮助收尾：句号和感叹号仍交给引擎处理基本语调，显式静音只做补充。

注意 SAPI XML 和 W3C SSML 是不同方言。
*/
var classic = /*
<sapi>
  <emphasis>听</emphasis>，<silence msec="90"/>
  <pitch middle="1">雨<silence msec="10"/>停了。</pitch>
  <silence msec="160"/>
  <emphasis>我</emphasis>们<silence msec="7"/>
  <rate speed="-1">  <emphasis>慢</emphasis>慢</rate>走<pitch middle="-1">回去</pitch>吧。
</sapi>
*/

// 独立保存草稿；切换 Mark 和 Zira 时可对比同一段英文。
var drafts,previousKey = {},null;
var showDraft = function(){
	if(previousKey) drafts[previousKey] = winform.edit.text;
	var v = voices[[winform.cbVoice.selIndex]];
	var id = tonumber((string.match(v[["language"]] : "","^\x+")),16) : 0;
	var lang = (id & 0x3ff) == 4 ? "zh" : "en";
	var mode = winform.formatBox.selIndex;
	
	if(!mode) return;
	var key = mode == 3 ? "classic" : lang ++ tostring(mode);
	var initial = mode == 3 ? classic : samples[lang][mode];
	
	winform.edit.text = drafts[key] : string.crlf(initial,'\r\n');
	previousKey = key;
};

winform.formatBox.selIndex = 2;
winform.formatBox.onSelChange = showDraft;
winform.cbVoice.onSelChange = showDraft;
showDraft();

winform.plusAudioLevel.foreground = 0xFFE5F897;
winform.plusAudioLevel.setProgressRange(0,100);
voice.event.AudioLevel = function(stream,pos,level){ winform.plusAudioLevel.progressPos = level; };
voice.event.Word = function(stream,pos,offset,length){
	if(winform.formatBox.selIndex == 1) winform.edit.setSel(offset+1,offset+length);
};

winform.button.oncommand = function(){
	var text = winform.edit.text;
	if(!#text) return;
	var ok,err = voice.setVoiceByIndex(winform.cbVoice.selIndex);
	
	if(!ok){ winform.msgboxErr(err); return; }
	voice.rate = winform.tbRate.pos;
	voice.volume = winform.tbVolume.pos;
	
	winform.button.disabledText = "正在朗读…";
	winform.cbVoice.disabled = true;
	winform.formatBox.disabled = true;
	var mode = winform.formatBox.selIndex;
	
	try {
		//if(mode == 1) voice.speakText(text,true);
		//elseif(mode == 2) voice.speakSsml(text,true);
		//else voice.speakXml(text);
		
		voice.speakAuto(text)
		
		// 等待时仍处理窗口消息和音量事件
		voice.waitAny(); 
		
		var hr = voice.Status.LastHResult;
		if(hr < 0) error(string.format("SAPI HRESULT: 0x%08X",hr));
	}
	catch(e){ winform.msgboxErr(tostring(e),"朗读失败，请检查音色和文本格式"); }
	
	winform.formatBox.disabled = false;
	winform.cbVoice.disabled = false;
	winform.button.disabledText = null;
	winform.plusAudioLevel.progressPos = 0;
};

if(!#voices) winform.button.disabledText = "未安装语音";

//语速滑块
winform.tbRate.setRange(-10,10);
winform.tbRate.pos = voice.rate;

//音量滑块
winform.tbVolume.setRange(0,100);
winform.tbVolume.pos = voice.volume;

winform.show();
win.loopMessage();
```

