Node + 讯飞语音 定时播放天气预报音频

本文介绍了如何使用Node.js结合讯飞语音和聚合数据API,实现每天早上八点定时播放天气预报的音频。通过创建lib和src文件夹,设置定时任务,并利用play库自动播放音频。
摘要由CSDN通过智能技术生成

前言

最近看了几篇文章,总觉得自己没发挥树莓派的作用,于是就琢磨着,哎,灵光一闪,整一个早晨叫醒服务,于是便有了本篇水文。

功能

每天早上八点钟,定时播放音频(音频内容为当天天气预报和空气质量),播放完成之后继续等待到明天的八点钟播放。

效果如下

技术

开始本来是想加个客户端的,但是一想先先直接跑个服务就用的node试试,所以本文只需要你会用js就行

  1. node(服务)

  2. 讯飞语音(转音频)

  3. play(播放语言)

  4. 聚合api(天气预报接口)

  5. scheduleJob(定时任务)

准备工作

我们需要文字识别转成音频,讯飞是可以白嫖的

讯飞语音

请先在讯飞注册账号及 > 创建应用 > 实名认证

https://passport.xfyun.cn/

然后去控制台找到服务接口认证信息

https://console.xfyun.cn/services/tts

找到需要的 key

聚合数据

天气预报api,如果有你其他的当然也可以用其他的,这个也是白嫖

就不做重点讲了

官网 https://www.juhe.cn/

天气预报 https://www.juhe.cn/docs/api/id/73

快速上手

初始化项目

npm init -y

然后安装我们需要的依赖, 下面是依赖的版本

{
  "name": "node-jiaoxing",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "dev": "node src/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "node-schedule": "^2.0.0",
    "request": "^2.88.2",
    "websocket": "^1.0.31"
  }
}

安装依赖

npm insatll

创建lib文件夹

创建一个 lib 文件夹,放入别人封装好的讯飞的资源,其主要作用就是将文本转为音频

index.js
const fs = require("fs");
const WebSocketClient = require('websocket').client;
const { getWssInfo, textToJson } = require('./util');

const xunfeiTTS = (auth, business, text, fileName, cb) => {
  let audioData = [];

  const client = new WebSocketClient();
  client.on('connect', (con) => {
    con.on('error', error => {
      throw (error);
    });

    con.on('close', () => {
      const buffer = Buffer.concat(audioData);
      fs.writeFile(fileName, buffer, (err) => {
        if (err) {
          throw (err);
          cb(err);
        } else {
          cb(null, 'OK');
        }
      });
    })

    con.on('message', (message) => {
      if (message.type == 'utf8') {
        const ret = JSON.parse(message.utf8Data);
        audioData.push(Buffer.from(ret.data.audio, 'base64'));
        if (ret.data.status == 2) {
          con.close();
        }
      }
    });

    if (con.connected) {
      const thejson = textToJson(auth, business, text);
      con.sendUTF(thejson);
    }
  });

  client.on('connectFailed', error => {
    throw (error);
  });

  const info = getWssInfo(auth);
  client.connect(info.url);
}

module.exports = xunfeiTTS;
util.js
function btoa(text) {
  return Buffer.from(text, "utf8").toString("base64");
}

function getWssInfo(auth, path = "/v2/tts", host = "tts-api.xfyun.cn") {
  const { app_skey, app_akey } = auth;
  const date = new Date(Date.now()).toUTCString();
  const request_line = `GET ${path} HTTP/1.1`;

  const signature_origin = `host: ${host}\ndate: ${date}\n${request_line}`;

  let crypto = require("crypto");

  const signature = cry
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Node.js 中实现语音转写功能,你可以使用科大开放平台提供的语音听写接口。以下是一个示例代码,演示如何使用 Node.js 发送 HTTP 请求调用语音听写接口: ```javascript const fs = require('fs'); const crypto = require('crypto'); const request = require('request'); const appid = 'your_appid'; // 替换为你的 AppID const secretKey = 'your_secretkey'; // 替换为你的 SecretKey const audioFilePath = 'path_to_your_audio_file'; // 替换为你的语音文件路径 // 构造请求头 const curTime = Math.floor(Date.now() / 1000).toString(); const param = { engine_type: 'sms16k', aue: 'raw', speex_size: 'small', }; const paramStr = JSON.stringify(param); const checkSum = crypto .createHash('md5') .update(secretKey + curTime + paramStr) .digest('hex'); const headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'X-Appid': appid, 'X-CurTime': curTime, 'X-Param': paramStr, 'X-CheckSum': checkSum, }; // 发起请求 const options = { url: 'http://api.xfyun.cn/v1/service/v1/iat', method: 'POST', headers: headers, body: fs.readFileSync(audioFilePath), }; request(options, (error, response, body) => { if (!error && response.statusCode == 200) { console.log(body); // 输出语音转写结果 } else { console.error(error); } }); ``` 在上面的示例代码中,你需要将 `your_appid`、`your_secretkey` 和 `path_to_your_audio_file` 替换为你自己的 AppID、SecretKey 和语音文件路径。代码会读取指定路径的语音文件,并将其转发给语音听写接口,返回转写结果。 请确保已安装 `request` 模块,你可以使用以下命令进行安装: ``` npm install request ``` 需要注意的是,开放平台还有其他接口和功能可供使用,你可以根据自己的需求调用相应的接口。详细的接口文档和使用说明可以在科大开放平台官网上找到。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值