ROS語音交互(三)科大訊飛語音在ROS平臺下使用

以上節tts語音輸出爲例
node

下載sdk連接:http://www.xfyun.cn/sdk/dispatcher服務器

一、下載SDK,解壓;session

二、在ROS工做空間下建立一個Package;
app

  catkin_create_pkg tts_voice roscpp rospy std_msgs

三、將SDK文件夾中的頭文件,即下載的SDK文件夾中include文件夾下的 .h文件拷貝至tts_voice/include/下;函數

四、將SDK文件夾中sample/tts_sample中的xf_tts.cpp文件拷貝至tts_voice/src/下;ui

五、將SDK文件夾中libs/x64/libmsc.so文件拷貝至/user/libs/下;編碼

六、打開CMakelist,spa

    1)修改include_directories爲:即包括拷貝的頭文件所在的include目錄指針

         include_directories(
                                     ${catkin_INCLUDE_DIRS}
                                     include
                                     )code

    2)添加:

  add_executable(tts_voice_node src/xf_tts.cpp)

        target_link_libraries(tts_voice_node
   ${catkin_LIBRARIES} -lmsc -lrt -ldl -lpthread
 )

# 生成可執行文件
     # 添加連接庫,其中動態連接庫路徑按實際修改

CmakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(tts_voice)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
)
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES tts_voice
#  CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
# include_directories(include)
include_directories(
  ${catkin_INCLUDE_DIRS}
   include
)

## Declare a C++ executable
 add_executable(tts_voice_node src/xf_tts.cpp)

## Specify libraries to link a library or executable target against
 target_link_libraries(tts_voice_node
   ${catkin_LIBRARIES} -lmsc -lrt -ldl -lpthread
 )

xf_tts.cpp源文件

/*
* 語音合成(Text To Speech,TTS)技術可以自動將任意文字實時轉換爲連續的
* 天然語音,是一種可以在任什麼時候間、任何地點,向任何人提供語音信息服務的
* 高效便捷手段,很是符合信息時代海量數據、動態更新和個性化查詢的需求。
*/
#include <ros/ros.h>
#include "std_msgs/String.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "qtts.h"
#include "msp_cmn.h"
#include "msp_errors.h"

const char* FileName = "/home/cam/Music/voice.wav";
const char* PlayPath = "play /home/cam/Music/voice.wav";

/* wav音頻頭部格式 */
typedef struct _wave_pcm_hdr
{
    char            riff[4];                // = "RIFF"
    int        size_8;                 // = FileSize - 8
    char            wave[4];                // = "WAVE"
    char            fmt[4];                 // = "fmt "
    int        fmt_size;        // = 下一個結構體的大小 : 16

    short int       format_tag;             // = PCM : 1
    short int       channels;               // = 通道數 : 1
    int        samples_per_sec;        // = 採樣率 : 8000 | 6000 | 11025 | 16000
    int        avg_bytes_per_sec;      // = 每秒字節數 : samples_per_sec * bits_per_sample / 8
    short int       block_align;            // = 每採樣點字節數 : wBitsPerSample / 8
    short int       bits_per_sample;        // = 量化比特數: 8 | 16

    char            data[4];                // = "data";
    int        data_size;              // = 純數據長度 : FileSize - 44
} wave_pcm_hdr;

/* 默認wav音頻頭部數據 */
wave_pcm_hdr default_wav_hdr =
{
    { 'R', 'I', 'F', 'F' },
    0,
    {'W', 'A', 'V', 'E'},
    {'f', 'm', 't', ' '},
    16,
    1,
    1,
    16000,
    32000,
    2,
    16,
    {'d', 'a', 't', 'a'},
    0
};
/* 文本合成 */
int text_to_speech(const char* src_text, const char* des_path, const char* params)
{
    int          ret          = -1;
    FILE*        fp           = NULL;
    const char*  sessionID    = NULL;
    unsigned int audio_len    = 0;
    wave_pcm_hdr wav_hdr      = default_wav_hdr;
    int          synth_status = MSP_TTS_FLAG_STILL_HAVE_DATA;

    if (NULL == src_text || NULL == des_path)
    {
        printf("params is error!\n");
        return ret;
    }
    fp = fopen(des_path, "wb");
    if (NULL == fp)
    {
        printf("open %s error.\n", des_path);
        return ret;
    }
    /* 開始合成 */
    sessionID = QTTSSessionBegin(params, &ret);
    if (MSP_SUCCESS != ret)
    {
        printf("QTTSSessionBegin failed, error code: %d.\n", ret);
        fclose(fp);
        return ret;
    }
    ret = QTTSTextPut(sessionID, src_text, (unsigned int)strlen(src_text), NULL);
    if (MSP_SUCCESS != ret)
    {
        printf("QTTSTextPut failed, error code: %d.\n",ret);
        QTTSSessionEnd(sessionID, "TextPutError");
        fclose(fp);
        return ret;
    }
    printf("正在合成 ...\n");
    fwrite(&wav_hdr, sizeof(wav_hdr) ,1, fp); //添加wav音頻頭,使用採樣率爲16000
    while (1)
    {
        /* 獲取合成音頻 */
        const void* data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);
        if (MSP_SUCCESS != ret)
            break;
        if (NULL != data)
        {
            fwrite(data, audio_len, 1, fp);
            wav_hdr.data_size += audio_len; //計算data_size大小
        }
        if (MSP_TTS_FLAG_DATA_END == synth_status)
            break;
        printf(">");
        usleep(15*1000); //防止頻繁佔用CPU
    }//合成狀態synth_status取值請參閱《訊飛語音雲API文檔》
    printf("\n");
    if (MSP_SUCCESS != ret)
    {
        printf("QTTSAudioGet failed, error code: %d.\n",ret);
        QTTSSessionEnd(sessionID, "AudioGetError");
        fclose(fp);
        return ret;
    }
    /* 修正wav文件頭數據的大小 */
    wav_hdr.size_8 += wav_hdr.data_size + (sizeof(wav_hdr) - 8);

    /* 將修正過的數據寫回文件頭部,音頻文件爲wav格式 */
    fseek(fp, 4, 0);
    fwrite(&wav_hdr.size_8,sizeof(wav_hdr.size_8), 1, fp); //寫入size_8的值
    fseek(fp, 40, 0); //將文件指針偏移到存儲data_size值的位置
    fwrite(&wav_hdr.data_size,sizeof(wav_hdr.data_size), 1, fp); //寫入data_size的值
    fclose(fp);
    fp = NULL;
    /* 合成完畢 */
    ret = QTTSSessionEnd(sessionID, "Normal");
    if (MSP_SUCCESS != ret)
    {
        printf("QTTSSessionEnd failed, error code: %d.\n",ret);
    }

    return ret;
}

/**
*   將文本發送到訊飛服務器,獲取語音文件文件
**/
int makeTextToWav(const char* text, const char* filename)
{
    int         ret                  = MSP_SUCCESS;
    const char* login_params         = "appid = 57f49f64, work_dir = .";//登陸參數,appid與msc庫綁定,請勿隨意改動
    /*
    * rdn:           合成音頻數字發音方式
    * volume:        合成音頻的音量
    * pitch:         合成音頻的音調
    * speed:         合成音頻對應的語速
    * voice_name:    合成發音人
    * sample_rate:   合成音頻採樣率
    * text_encoding: 合成文本編碼格式
    *
    * 詳細參數說明請參閱《訊飛語音雲MSC--API文檔》
    */
    const char* session_begin_params = "voice_name = xiaowanzi, text_encoding = utf8, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 0";
//    const char* filename             = "tts_sample.wav"; //合成的語音文件名稱
//    const char* text                 = "我是小丸子,今年126歲了,個人車牌號是12365,今年是2016年12月20號"; //合成文本

    /* 用戶登陸 */
    ret = MSPLogin(NULL, NULL, login_params);//第一個參數是用戶名,第二個參數是密碼,第三個參數是登陸參數,用戶名和密碼可在http://open.voicecloud.cn註冊獲取
    if (MSP_SUCCESS != ret)
    {
        printf("MSPLogin failed, error code: %d.\n", ret);
        goto exit ;//登陸失敗,退出登陸
    }
    /* 文本合成 */
    printf("開始合成 ...\n");
    ret = text_to_speech(text, filename, session_begin_params);
    if (MSP_SUCCESS != ret)
    {
        printf("text_to_speech failed, error code: %d.\n", ret);
    }
    printf("合成完畢\n");

exit:

    MSPLogout(); //退出登陸
    return 0;
}

/**
* play the wav file
**/
void PlayWav()
{
    system(PlayPath);
}
/**
 *接受/voice/xf_tts_topic話題的字符串的回調函數
 */
void TTSCallback(const std_msgs::String::ConstPtr &msg)
{
    std::cout<<"get topic text: " << msg->data.c_str();
    makeTextToWav(msg->data.c_str(), FileName);
    PlayWav();
}

int main(int argc, char* argv[])
{
    const char* start = "寶寶啓動成功了";
    makeTextToWav(start,FileName);
    PlayWav();

    ros::init(argc, argv,"xf_tts_node");
    ros::NodeHandle n;
    ros::Subscriber sub = n.subscribe("/voice/xf_tts_topic",3,TTSCallback);

    ros::spin();

    return 0;
}

到~/catkin_ws編譯

cam@cam-Aspire-V3-572G:~/catkin_ws$ catkin_make -j4

運行

¥ roscore
$ rosrun tts_voice tts_voice_node

啓動播放  "寶寶啓動成功了"

經過話題播放

 rostopic pub  /voice/xf_tts_topic std_msgs/String " 韓聯社預計,鑑於黃教堅持現有外交政策,「薩德」部署相關工做可能提速。本月早些時候,一些政府消息人士透露,
因爲總統朴槿惠遭彈劾,國內政 局不肯定性增長,韓國國防部決定加快「薩德」的相關部署工做,最快可能在明年5月完成部署。根據消息人士的說法,爲了能儘早完成部署工做,
韓國國防部甚至考慮省略掉環境影響評估的環節。"

 

使用時須要把

const char* login_params         = "appid = 57f49f64, work_dir = .";//登陸參數,appid與msc庫綁定,請勿隨意改動

 appid改成本身的賬號

若是play播放音頻出錯,是由於沒有安裝sox

執行安裝便可

sudo apt-get install sox
相關文章
相關標籤/搜索