ROS語音交互——科大訊飛語音合成TTS(二)


 

 

 

 

之前我用過科大訊飛的語音包,為了記錄一下我重新使用一下

首先注冊科大訊飛賬號及應用,以后每個下載的在線使用SDK都是以此賬戶ID登錄訊飛語音服務器。

下載科大訊飛在線合成包。

$ unzip Linux_voice_1135_57f49f64\ \(1\).zip 

 文件目錄示意圖

.
├── bin
│   ├── gm_continuous_digit.abnf
│   ├── msc
│   │   └── msc.cfg
│   ├── source.txt
│   ├── userwords.txt
│   └── wav
│       ├── iflytek01.wav
│       ├── iflytek02.wav
│       └── weather.pcm
├── doc
│   ├── Grammar Development Guidelines.chm
│   ├── iFlytek MSC Reference Manual
│   │   ├── all__0_8js.html
│   │   ├── all__1_8js.html
│   │   ├── arrowdown.png
│   │   ├── arrowright.png
│   │   ├── bc_s.png
│   │   ├── bdwn.png
│   │   ├── closed.png
│   │   ├── dir_25539194184bab781b1c7ecd67774cd8.html
│   │   ├── dir_9a65024ac0a05d9abfc2bb6c7ff8f818.html
│   │   ├── doc.png
│   │   ├── doxygen.css
│   │   ├── doxygen.png
│   │   ├── dynsections_8js.html
│   │   ├── dynsections.js
│   │   ├── files__0_8js.html
│   │   ├── files__1_8js.html
│   │   ├── files.html
│   │   ├── folderclosed.png
│   │   ├── folderopen.png
│   │   ├── functions__0_8js.html
│   │   ├── functions__1_8js.html
│   │   ├── globals_func.html
│   │   ├── globals.html
│   │   ├── globals_vars.html
│   │   ├── index.html
│   │   ├── jquery_8js.html
│   │   ├── jquery.js
│   │   ├── msp__cmn_8h.html
│   │   ├── msp__cmn_8h_source.html
│   │   ├── nav_f.png
│   │   ├── nav_g.png
│   │   ├── nav_h.png
│   │   ├── open.png
│   │   ├── qisr_8h.html
│   │   ├── qisr_8h_source.html
│   │   ├── qmfv_8h.html
│   │   ├── qmfv_8h_source.html
│   │   ├── qtts_8h.html
│   │   ├── qtts_8h_source.html
│   │   ├── search
│   │   │   ├── all_0.html
│   │   │   ├── all_0.js
│   │   │   ├── all_10.html
│   │   │   ├── all_10.js
│   │   │   ├── all_11.html
│   │   │   ├── all_11.js
│   │   │   ├── all_1.html
│   │   │   ├── all_1.js
│   │   │   ├── all_2.html
│   │   │   ├── all_2.js
│   │   │   ├── all_3.html
│   │   │   ├── all_3.js
│   │   │   ├── all_4.html
│   │   │   ├── all_4.js
│   │   │   ├── all_5.html
│   │   │   ├── all_5.js
│   │   │   ├── all_6.html
│   │   │   ├── all_6.js
│   │   │   ├── all_7.html
│   │   │   ├── all_7.js
│   │   │   ├── all_8.html
│   │   │   ├── all_8.js
│   │   │   ├── all_9.html
│   │   │   ├── all_9.js
│   │   │   ├── all_a.html
│   │   │   ├── all_a.js
│   │   │   ├── all_b.html
│   │   │   ├── all_b.js
│   │   │   ├── all_c.html
│   │   │   ├── all_c.js
│   │   │   ├── all_d.html
│   │   │   ├── all_d.js
│   │   │   ├── all_e.html
│   │   │   ├── all_e.js
│   │   │   ├── all_f.html
│   │   │   ├── all_f.js
│   │   │   ├── close.png
│   │   │   ├── files_0.html
│   │   │   ├── files_0.js
│   │   │   ├── files_1.html
│   │   │   ├── files_1.js
│   │   │   ├── files_2.html
│   │   │   ├── files_2.js
│   │   │   ├── files_3.html
│   │   │   ├── files_3.js
│   │   │   ├── files_4.html
│   │   │   ├── files_4.js
│   │   │   ├── files_5.html
│   │   │   ├── files_5.js
│   │   │   ├── files_6.html
│   │   │   ├── files_6.js
│   │   │   ├── functions_0.html
│   │   │   ├── functions_0.js
│   │   │   ├── functions_1.html
│   │   │   ├── functions_1.js
│   │   │   ├── functions_2.html
│   │   │   ├── functions_2.js
│   │   │   ├── functions_3.html
│   │   │   ├── functions_3.js
│   │   │   ├── functions_4.html
│   │   │   ├── functions_4.js
│   │   │   ├── functions_5.html
│   │   │   ├── functions_5.js
│   │   │   ├── functions_6.html
│   │   │   ├── functions_6.js
│   │   │   ├── functions_7.html
│   │   │   ├── functions_7.js
│   │   │   ├── functions_8.html
│   │   │   ├── functions_8.js
│   │   │   ├── functions_9.html
│   │   │   ├── functions_9.js
│   │   │   ├── functions_a.html
│   │   │   ├── functions_a.js
│   │   │   ├── mag_sel.png
│   │   │   ├── nomatches.html
│   │   │   ├── search.css
│   │   │   ├── searchdata.js
│   │   │   ├── search.js
│   │   │   ├── search_l.png
│   │   │   ├── search_m.png
│   │   │   ├── search_r.png
│   │   │   ├── variables_0.html
│   │   │   ├── variables_0.js
│   │   │   ├── variables_1.html
│   │   │   ├── variables_1.js
│   │   │   ├── variables_2.html
│   │   │   ├── variables_2.js
│   │   │   ├── variables_3.html
│   │   │   ├── variables_3.js
│   │   │   ├── variables_4.html
│   │   │   ├── variables_4.js
│   │   │   ├── variables_5.html
│   │   │   ├── variables_5.js
│   │   │   ├── variables_6.html
│   │   │   ├── variables_6.js
│   │   │   ├── variables_7.html
│   │   │   └── variables_7.js
│   │   ├── search_8js.html
│   │   ├── searchdata_8js.html
│   │   ├── splitbar.png
│   │   ├── sync_off.png
│   │   ├── sync_on.png
│   │   ├── tab_a.png
│   │   ├── tab_b.png
│   │   ├── tab_h.png
│   │   ├── tabs.css
│   │   └── tab_s.png
│   ├── MSC Novice Manual for Linux.pdf
│   ├── Open Semantic Platform API Documents.pdf
│   └── Recording API Documents_.doc
├── include
│   ├── msp_cmn.h
│   ├── msp_errors.h
│   ├── msp_types.h
│   ├── qisr.h
│   └── qtts.h
├── libs
│   ├── x64
│   │   └── libmsc.so
│   └── x86
│       └── libmsc.so
├── Linux_voice_1135_57f49f64 (1).zip
├── README
└── samples
    ├── asr_sample
    │   ├── 32bit_make.sh
    │   ├── 64bit_make.sh
    │   ├── asr_sample.c
    │   └── Makefile
    ├── iat_record
    │   ├── 32bit_make.sh
    │   ├── 64bit_make.sh
    │   ├── formats.h
    │   ├── iat_record.c
    │   ├── linuxrec.c
    │   ├── linuxrec.h
    │   ├── Makefile
    │   ├── speech_recognizer.c
    │   └── speech_recognizer.h
    ├── iat_sample
    │   ├── 32bit_make.sh
    │   ├── 64bit_make.sh
    │   ├── iat_sample.c
    │   └── Makefile
    ├── sch_speak
    │   ├── 32bit_make.sh
    │   ├── 64bit_make.sh
    │   ├── Makefile
    │   └── sch_speak.c
    ├── sch_text
    │   ├── 32bit_make.sh
    │   ├── 64bit_make.sh
    │   ├── Makefile
    │   └── sch_text.c
    └── tts_sample
        ├── 32bit_make.sh
        ├── 64bit_make.sh
        ├── Makefile
        └── tts_sample.c

我們編譯使用tts_sample目錄下的源文件

編譯方法依照訊飛文檔

cam@cam-Aspire-V3-572G:~/xunfei/TTS/samples/tts_sample$ source 64bit_make.sh 

 

將64bit_make.sh拷貝到bin目錄,執行程序時會用到動態鏈接庫

cam@cam-Aspire-V3-572G:~/xunfei/TTS/samples/tts_sample$ cp 64bit_make.sh ../../bin/

 

運行程序

cam@cam-Aspire-V3-572G:~/xunfei/TTS/bin$ ./tts_sample

會在當前目錄合成一個聲音文件。

cam@cam-Aspire-V3-572G:~/xunfei/TTS/bin$ play tts_sample.wav 

 

 

如果沒有安裝sox,執行安裝命令

sudo apt-get install sox

 

由於官方的需要選擇系統平台,我們都是確定的,就不用那么麻煩的 每次更新環境

修改makfile,刪除判斷語句

 

#common makefile header

DIR_INC = ../../include
DIR_BIN = ../../bin
DIR_LIB = ../../libs

TARGET	= tts_sample
BIN_TARGET = $(DIR_BIN)/$(TARGET)

CROSS_COMPILE = 
CFLAGS = -g -Wall -I$(DIR_INC)


LDFLAGS := -L$(DIR_LIB)/x64

LDFLAGS += -lmsc -lrt -ldl -lpthread

OBJECTS := $(patsubst %.c,%.o,$(wildcard *.c))

$(BIN_TARGET) : $(OBJECTS)
	$(CROSS_COMPILE)gcc $(CFLAGS) $^ -o $@ $(LDFLAGS)

%.o : %.c
	$(CROSS_COMPILE)gcc -c $(CFLAGS) $< -o $@
clean:
	@rm -f *.o $(BIN_TARGET)

.PHONY:clean

#common makefile foot

 將

├── libs │

├── x64

│   │   └── libmsc.so

libmsc.so拷貝到/user/libs下    即可。

以后直接make

 

源文件

/*
* 語音合成(Text To Speech,TTS)技術能夠自動將任意文字實時轉換為連續的
* 自然語音,是一種能夠在任何時間、任何地點,向任何人提供語音信息服務的
* 高效便捷手段,非常符合信息時代海量數據、動態更新和個性化查詢的需求。
*/

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

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

/* 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(150*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 main(int argc, char* argv[])
{
    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###########################################################################\n");
    printf("## 語音合成(Text To Speech,TTS)技術能夠自動將任意文字實時轉換為連續的 ##\n");
    printf("## 自然語音,是一種能夠在任何時間、任何地點,向任何人提供語音信息服務的  ##\n");
    printf("## 高效便捷手段,非常符合信息時代海量數據、動態更新和個性化查詢的需求。  ##\n");
    printf("###########################################################################\n\n");
    /* 文本合成 */
    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:
    printf("按任意鍵退出 ...\n");
    getchar();
    MSPLogout(); //退出登錄

    return 0;
}
const char* session_begin_params = "voice_name = xiaowanzi, text_encoding = utf8, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 0";

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
};

 

 

偏移地址

字節數

數據類型

內容

 

 

 

 

 

 

文件頭

00H

4

char

“RIFF”;   RIFF標志

04H

4

long int

0x00 01 06 0A(注意數據存儲順序);   文件長度

08H

4

char

“WAVE”;   WAVE標志

0CH

4

char

“fmt ”;   fmt標志,最后一位為空

10H

4

long int

0x12;   sizeof(PCMWAVEFORMAT)

14H

2

int

1WAVE_FORMAT_PCM格式類別,1表示為PCM形式的聲音數據

16H

2

int

2;  通道數,單聲道為1,雙聲道為2

18H

2

int

44100;  采樣頻率(每秒樣本數)

1CH

4

long int

0x10B10000;   每秒數據量;其值為通道數×每秒數據位數×每樣本的數據位數/8。播放軟件利用此值可以估計緩沖區的大小。

20H

2

int

數據塊的調整數(按字節算的),其值為通道數×每樣本的數據位值/8。播放軟件需要一次處理多個該值大小的字節數據,以便將其值用於緩沖區的調整。

22H

2

 

每樣本的數據位數,表示每個聲道中各個樣本的數據位數。如果有多個聲道,對每個聲道而言,樣本大小都一樣。

50H

4

char

“data”;   數據標記符

54H

4

long int

0x00 01 05 D8;   語音數據大小

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM