基于STM32的AI智能小音箱毕业设计

ARM 1077浏览

基于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