天天看點

用JS輕應用與HaaS 600實作語音播報音箱1、播報音箱介紹2、硬體3、軟體架構4、應用開發5、物模型6、功能實作7、開發者技術支援

1、播報音箱介紹

播報音箱主要功能為語音播報,需要和雲端保持連接配接,接收雲端播報消息,裝置端按照指定規則進行播報。常見的播報場景有支付到賬資訊、動态更新的定制化音頻内容、使用者操作回應和提醒等。播報音箱方案涉及雲端、裝置端的開發,屬于端雲一體化解決方案。本文聚焦于裝置端,主要講述基于IoT JS輕應用和HaaS600硬體平台實作播報音箱方案。

用JS輕應用與HaaS 600實作語音播報音箱1、播報音箱介紹2、硬體3、軟體架構4、應用開發5、物模型6、功能實作7、開發者技術支援

2、硬體

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

3、軟體架構

用JS輕應用與HaaS 600實作語音播報音箱1、播報音箱介紹2、硬體3、軟體架構4、應用開發5、物模型6、功能實作7、開發者技術支援

4、應用開發

4.1、連接配接雲平台

使用JS輕應用的IoT API,傳入三元組資訊,即可快速建立和雲端的連接配接,示例:

var iot = require('iot');

const productkey = '';

const devicename = '';

const devicesecret = '';

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({

console.log('key volumeup pressed');           

key_volumedown.onIRQ({

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. {$+數字}:表示按照金額進行播放

  1. {N+數字}或{n+數字}:表示按照數字進行播放
  2. 其它:表示需要播放的語料的辨別 播放

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、連接配接雲平台

console.log('success connect to aliyun iot server');           
console.log('fail to connect to aliyun iot server');           

執行完成後,和雲端的連接配接通道建立。

6.2、物模型處理

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、語音拼接

數字拼接

按照普通數字發音規則進行拼接,例如手機号。

按照金額類數字發音規則進行拼接,例如收款金額。

本地語音拼接

通過音頻辨別查找本地檔案。

拼接結果:

例如,

id:123,

timestamp:"1595765968612"

拼接後:

用JS輕應用與HaaS 600實作語音播報音箱1、播報音箱介紹2、硬體3、軟體架構4、應用開發5、物模型6、功能實作7、開發者技術支援

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、開發者技術支援

如需更多技術支援,可加入釘釘開發者群,或者關注微信公衆号

用JS輕應用與HaaS 600實作語音播報音箱1、播報音箱介紹2、硬體3、軟體架構4、應用開發5、物模型6、功能實作7、開發者技術支援

更多技術與解決方案介紹,請通路阿裡雲AIoT首頁 (

https://iot.aliyun.com/

)

繼續閱讀