1、播報音箱介紹
播報音箱主要功能為語音播報,需要和云端保持連接,接收云端播報訊息,設備端按照指定規則進行播報,常見的播報場景有支付到賬資訊、動態更新的定制化音頻內容、用戶操作回應和提醒等,播報音箱方案涉及云端、設備端的開發,屬于端云一體化解決方案,本文聚焦于設備端,主要講述基于IoT JS輕應用和HaaS600硬體平臺實作播報音箱方案,

2、硬體
HaaS600是基于移遠EC100Y-CN通信模組的LTE Cat 1開發板,專為M2M 和IoT應用而設計,可應用于共享控制、金融支付、智能語音、泛工業等場景的智能硬體產品開發(詳情可參考HaaS600平臺介紹)
3、軟體框架

4、應用開發
4.1、連接云平臺
使用JS輕應用的IoT API,傳入三元組資訊,即可快速建立和云端的連接,示例:
var iot = require('iot');
const productkey = '<product-key>';
const devicename = '<device-name>';
const devicesecret = '<device-secret>';
var iotdev = iot.device({
productKey: productkey,
deviceName: devicename,
deviceSecret: devicesecret,
success: function() {
console.log('success connect to aliyun iot server');
},
fail: function() {
console.log('fail to connect to aliyun iot server');
}
});
接收云端播報訊息(型別為service),示例:
iotdev.on('service', function(serviceid, request) {
console.log('received cloud serviceid is ' + serviceid + '\r\n');
console.log('received cloud request is ' + request + '\r\n');
});
4.2、播報語音
IoT輕應用提供音頻播放組件audioplayer,使用相關API可實作本地和在線音頻檔案的播放及控制,示例:
var audioplayer = require('audioplayer');
var source = "/test.mp3"
audioplayer.play(source);
var sourceList = ["/test1.mp3", "/test2.mp3", "/test3.mp3"];
audioplayer.listPlay(sourceList);
-
單個檔案的播報,例如廣告、TTS合成語音等,可通過audioplayer.play()介面,傳入音頻檔案地址(支持本地檔案以及http、https網路音頻)
-
多個檔案拼接組合播報,例如金額的拼接,將需要拼接播放的音頻檔案存放在陣列中,通過audioplayer.listPlay()介面,將音頻檔案進行拼接和播放,
4.3、按鍵處理
var gpio = require('gpio');
var led_network = gpio.open({
id: 'led_network'
});
var key_function = gpio.open({
id: 'key_function'
});
var key_volumeup = gpio.open({
id: 'key_volumeup'
});
var key_volumedown = gpio.open({
id: 'key_volumedown'
});
key_function.onIRQ({
trigger: 'rising',
cb: function() {
console.log('key function pressed');
}
});
key_volumeup.onIRQ({
trigger: 'rising',
cb: function() {
console.log('key volumeup pressed');
}
});
key_volumedown.onIRQ({
trigger: 'rising',
cb: function() {
console.log('key volumedown pressed');
}
});
4.4、低功耗
當系統空閑時自動進入低功耗狀態,
var pm = require('pm');
pm.setAutosleepMode(1)
5、物模型
5.1、播報金額
物模型:
{
speechs:["alipay","{$100}","yuan"],
id:"123",
timestamp:"1595765968612"
}
定義:
id: 訊息id,用于判斷是否是重復推送
timestamp:交易時間
speechs:表示需要拼接的內容,有3類
1. {$+數字}:表示按照金額進行播放
2. {N+數字}或{n+數字}:表示按照數字進行播放
3. 其它:表示需要播放的語料的標識 播放
5.2、播放音頻鏈接
物模型:
{
url:"http://*********",
id:"123",
}
定義:
url:音頻內容url,設備端收到后通過該url下載并播放
id: 編號,用于判斷是否是重復推送
5.3、本地語料更新(SpeechPost)
{
speechs:[{"id":"test","url":"http://********"}],
jobcode:"123"
}
欄位:
speechs:需要更新的語料串列,每個元素包括id和url,其中id表示語料標識、url是語料下載地址
jobcode:表示語料更新任務id,用于云端和設備同步語料更新任務執行狀態
6、功能實作
6.1、連接云平臺
var iot = require('iot');
var iotdev = iot.device({
productKey: productkey,
deviceName: devicename,
deviceSecret: devicesecret,
success: function() {
console.log('success connect to aliyun iot server');
},
fail: function() {
console.log('fail to connect to aliyun iot server');
}
});
執行完成后,和云端的連接通道建立,
6.2、物模型處理
iotdev.on('service', function(serviceid, request) {
console.log('received cloud serviceid is ' + serviceid + '\r\n');
console.log('received cloud request is ' + request + '\r\n');
if (serviceid.indexOf("AudioPlayback") != -1) {
voiceboxPlayContent(request);
} else if (serviceid.indexOf("SpeechBroadcast") != -1) {
voiceboxPlayReceipt(request);
} else if (serviceid.indexOf("SpeechPost") != -1) {
voiceboxResUpdate(request);
}
});
6.3、語音拼接
-
數字拼接
按照普通數字發音規則進行拼接,例如手機號,
按照金額類數字發音規則進行拼接,例如收款金額,
-
本地語音拼接
通過音頻標識查找本地檔案,
拼接結果:
例如,
{
speechs:["alipay","{$100}","yuan"],
id:123,
timestamp:"1595765968612"
}
拼接后:

6.4、語料更新
下載并更新本地語料檔案
function voiceboxResUpdate(resource)
{
var resource = eval('(' + resource + ')');
var speechArray = resource.speechs;
for (var speechIndex = 0; speechIndex < speechArray.length; speechIndex++) {
var speech = speechArray[speechIndex];
console.log('update local speech id: ' + speech.id + ', url: ' + speech.url);
var resourcePath = toneDir + speech.id + tonenameSuffix;
http.download({
url: speech.url,
filepath: resourcePath,
method: 'GET',
headers: {
'Accept':'*/*'
},
success: function (data) {
if(data === defaultMessage) {
console.log('http: [success] http.download');
}
}
});
}
}
7、開發者技術支持
如需更多技術支持,可加入釘釘開發者群

更多技術與解決方案介紹,請訪問阿里云AIoT首頁https://iot.aliyun.com/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/229298.html
標籤:其他
上一篇:基于STM32的簡單電子表的實作
