基于STM32的AI智能小音箱畢業設計

ARM 1537瀏覽

基于STM32的AI智能小音箱畢業設計,所需硬件:

PS:如果你經費充足,請買一塊樹莓派解決一切煩惱!!!

  1. STM32F4系列,也可使用STM32F103
  2. ESP8266 WIFI模塊
  3. 聲音采集模塊VS1003,一般開發板會有集成也可采用其他錄音模塊
  4. SD存儲卡或者板載flash
  5. 帶放大器的喇叭或者USB喇叭
可以實現的功能:
  1. 語音控制燈光打開關閉
  2. 語音控制播放歌曲
  3. 語音問答,例如:北京天氣怎么樣
  4. 其他功能可以自己加
本系統工作原理:STM32使用話筒PCM采樣聲音數據,通過ESP8266
發送到服務器,服務器通過語音接口識別出指令內容返回對應數據,問答接口采用圖靈機器人接口,文字轉語音使用TTS接口。這個系統
的網頁版本實現參考我的另外一篇博客文章,該系統實現的最重要部分就是語音數據如何
發送到語音接口,直接發送到百度語音接口的話會有點難以實現,所以我采用的是自己搭
服務器進行中轉,現在的服務器也很便宜,一個月才幾塊錢,因此成本也是很低的。
部分源碼:
#include
#include
#include
#include
#include
#include "ai.h"
#include "api/api.h"

#include"control.h"


int
        _START_EXIT
        = 0;

int
        _OPEN_VOICE
        = 1;

int
        _BD_TTS_TIME
        = 0;

int
        _TL_RCV_TIME
        = 0;

int
find_cmd_local(char
               *cmdstr);

int
process_cmd(int
            local, char
            **result);


int
        LIGHT_PIN
        = 3;


char
        *cmdlist[] = {"001退出系統", "002退出程序", "003關閉程序", "004確定", "005關閉語音",

                      "006不要回復", "007關閉回復", "008打開語音", "009開啟語音", "010開啟回復", "011是的",

                      "012打開電燈", "013開燈", "014開一下燈", "015點亮電燈", "016關閉電燈", "017關燈", "018關一下燈",

                      "019熄滅電燈", "020開電燈", "021關電燈"
};


int
speech_record(char
              *buffer, int
              count) {

    int
            retnum
            = 0;

    err_log("錄音完成,進入識別階段!n");

    set_bdtts_light_state(1);

    if
            (count != 0
             &&
             (buffer != NULL
              || !strcmp(buffer, ""))) {

        char
                *result = (char
        *) calloc(1, sizeof(char));

        size_t
                resultCount
                = 0;

//百度語音識別

        int
                ret
                = bd_voice_recognition(buffer, count, &result);

        if
                (ret <</span > 0)


        {

            err_log("語音識別錯誤!錯誤:%dn", ret);

        }

        else
        if
                (result == NULL
                 || !strcmp(result, "")) {

            err_log("未能識別到任何內容!n");

        } else {

            err_log("語音識別內容:%sn", result);

//先查找本地指令

            int
                    ret
                    = find_cmd_local(result);

            if
                    (ret == 0) {

//未找到,那么使用圖靈機器人應答

                ret
                        = tl_reboot(result, &result, &resultCount);

                if
                        (ret <</span > 0)


                {

                    err_log("圖靈機器人智能應答獲取失敗!n");

                    free(result);

                    retnum
                            = -1;

                    goto
                            exit;

                }

//如果前一個指令是退出程序,但是現在的指令不是確認指令,重置準備退出狀態為0

                if
                        (_START_EXIT) {

                    _START_EXIT
                            = 0;

                }

                err_log("圖靈機器人應答:%sn", result);

            } else {

                process_cmd(ret, &result);

                err_log("命令執行應答:%sn", result);

                if
                        (!_OPEN_VOICE) { //當關閉回復之后,回復命令執行語音

                    ret
                            = bd_voice_tts(result, &result);

                    if
                            (ret <</span > 0)


                    {

                        err_log("文字轉語音失敗!n");

                        free(result);

                        retnum
                                = -2;

                        goto
                                exit;

                    }

                }

            }

            if
                    (_OPEN_VOICE) {

                ret
                        = bd_voice_tts(result, &result);

                if
                        (ret <</span > 0)


                {

                    err_log("文字轉語音失敗!n");

                    free(result);

                    retnum
                            = -3;

                    goto
                            exit;

                }

            }

        }

        free(result);

    } else {

        err_log("音頻數據為空!n");

        retnum
                = -4;

        goto
                exit;

    }

    exit:
    set_bdtts_light_state(0);

    return
            retnum;

}


int
process_cmd(int
            local, char
            **result) {

    char
            *back;

    if
            (local == 0
             || *result == NULL) {

        return
                -1;

    }

//正對退出指令

    if
            (_START_EXIT && (local == 4
                             || local == 11)) {

//關掉識別狀態指示燈

        set_bdtts_light_state(0);

        err_log("程序已退出!n");

        exit(0);

    } else if
            (_START_EXIT && (local != 1
                             || local != 2
                             || local != 3)) {

        _START_EXIT
                = 0;

    }

    switch
            (local) {

        case
            1: //退出程序

        case
            2:

        case
            3:

            _START_EXIT
                    = 1;

            back
                    = "確定退出語音識別程序嗎?";

            break;

        case
            5: //關閉語音回復

        case
            6:

        case
            7:

            _OPEN_VOICE
                    = 0;

            back
                    = "已經為您關閉語音回復!";

            break;

        case
            8: //開啟語音回復

        case
            9:

        case
            10:

            _OPEN_VOICE
                    = 1;

            back
                    = "已經為您開啟語音回復!";

            break;

        case
            12: //開燈

        case
            13:

        case
            14:

        case
            15:

        case
            20:

            set_output_pin_state(LIGHT_PIN, 1);

            back
                    = "已經為您打開電燈!";

            break;

        case
            16: //關燈

        case
            17:

        case
            18:

        case
            19:

        case
            21:

            set_output_pin_state(LIGHT_PIN, 0);

            back
                    = "已經為您關閉電燈!";

            break;

        default:
//未識別到內容

            back
                    = "本機沒法識別你提交的參數所表達的意思!";

            break;

    }

    *result = (char
    *) calloc(strlen(back) + 1, sizeof(char));

    strcpy(*result, back);

    return
            0;

}


int
find_cmd_local(char
               *cmdstr) {

    if
            (cmdstr == NULL
             || !strcmp(cmdstr, "")) {

        return
                0;

    }

    int
            cmdCount
            = sizeof(cmdlist) / sizeof(cmdlist[1]);

    for
            (int
                     i = 0; i <</span > cmdCount;
    i++)


    {

        char
                *tempCmd = (char
        *) calloc(strlen(cmdlist[i] + 1), sizeof(char));

        strcpy(tempCmd, cmdlist[i] + 3); //拷貝指令,原指令指針位置加3

        for
                (int
                         j = 0; j <</span > strlen(cmdstr);
        j++)


        {

            if
                    (strlen(cmdstr) - j <</span > strlen(tempCmd))


            {

                break;

            }

            else

            {

                int
                        ret
                        = strncmp(&cmdstr[j], tempCmd, strlen(tempCmd));

                if
                        (ret == 0) {

                    int
                            back
                            = i + 1;

                    i = cmdCount;

                    free(tempCmd);

                    return
                            back;

                }

            }

        }

        free(tempCmd);

    }

    return
            0;

}
參考資料:
https://blog.csdn.net/ownWell/article/details/8114121
https://github.com/zyfworks/STM32_AudioRecorder
七星彩走势图2元网官网