Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize speech Robot based on Raspberry Pie

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to implement a voice robot based on raspberry pie. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Preparation: raspberry pie, audio module, stm32 single chip microcomputer, Baidu speech recognition interface, loudspeaker.

Overall thinking:

1. Because the raspberry pie has no ADC module, so here with the help of stm32's ADC module to convert the voice signal into a digital signal, and then transmit it to the raspberry pie through the serial port, and the raspberry pie sends you to assemble the data into a wave file for speech recognition.

two。 Upload the assembled voice file to Baidu speech recognition platform for recognition through http protocol. Documentation (free call)

3. The corresponding processing is made according to the recognition results.

4. When the voice needs to be played, the voice is synthesized according to Baidu speech synthesis interface and then played using mplayer. Mplayer installation reference blog

Part of the code:

Convert audio to wave files

# include # include "listen.h" / / gcc-o uart uart.c-lwiringPitypedef struct WAV_Format WAVHEADER; # define MAX_LISTEN_SIZES 1024mm70 / / define the size of received data # define bty 460800amp / serial port baud rate struct listen*listenUart () {int fd,file; char buff,buff2; struct listen*liste= (struct listen*) malloc (sizeof (struct listen)); unsigned short size; unsigned short*music,temp=0; unsigned short max=0,min=0 Char*result=NULL;// stores the final return value int index=0,i=0; char stop=1; WAVHEADER wavHead; music= (unsigned short*) malloc (MAX_LISTEN_SIZES*2); result= (char*) malloc (MAX_LISTEN_SIZES*2+sizeof (WAVHEADER)); if (wiringPiSetup ())

< 0)return NULL; if((fd = serialOpen ("/dev/ttyAMA0",bty))length/3)*4+(lsn->

Length%3) * 2room1; base= (char*) malloc (baseSize); base64_encode (lsn- > data,lsn- > length,base); / / send request free (lsn- > data); free (lsn); int code=initToken (); if (code==1) {char*token=getToken (); / / start creating json string cJSON * root = cJSON_CreateObject (); cJSON_AddItemToObject (root, "format", cJSON_CreateString ("wav")); cJSON_AddItemToObject (root, "rate", cJSON_CreateString ("16000")) CJSON_AddItemToObject (root, "channel", cJSON_CreateString ("1")); cJSON_AddItemToObject (root, "cuid", cJSON_CreateString ("34-68-95-91-77-43")); cJSON_AddItemToObject (root, "token", cJSON_CreateString (token)); cJSON_AddItemToObject (root, "dev_pid", cJSON_CreateString ("1537")); cJSON_AddItemToObject (root, "speech", cJSON_CreateString (base); cJSON_AddItemToObject (root, "len", cJSON_CreateNumber (fileLength)) Char*jsonParam=cJSON_PrintUnformatted (root); char*apiurl= "http://vop.baidu.com/server_api"; CURL* curl; CURLcode res; / / ptr = curl_easy_escape (NULL, (char*) a, asize); curl = curl_easy_init (); struct curl_slist* headers = NULL; headers = curl_slist_append (headers," Content-Type:application/json "); curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt (curl, CURLOPT_URL, apiurl) Curl_easy_setopt (curl, CURLOPT_TIMEOUT, 60); curl_easy_setopt (curl, CURLOPT_POST, 1); / / http://vop.baidu.com/server_api / / CURLOPT_POSTFIELDS,CURLOPT_POSTFIELDSIZE curl_easy_setopt (curl, CURLOPT_POSTFIELDS, jsonParam); curl_easy_setopt (curl, CURLOPT_POSTFIELDSIZE, strlen (jsonParam)); curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, listen_getData2); curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1) Res = curl_easy_perform (curl); curl_easy_cleanup (curl); cJSON_Delete (root); curl_slist_free_all (headers); free (token); free (jsonParam); if (res = = CURLE_OK) {char*chars; char*tempresult= (char*) malloc (listen_buff2- > length+1); memcpy (tempresult,listen_buff2- > buff,listen_buff2- > length); tempress [buff2- > length] = 0; cJSON* json; cJSON* item = NULL; cJSON*errCode; json=cJSON_Parse (tempresult) Item=cJSON_GetObjectItem (json, "result"); errCode=cJSON_GetObjectItem (json, "err_no"); if (errCode- > valueintents 0) {return-3;} chars=cJSON_GetArrayItem (item,0)-> valuestring; strcpy (result2,chars); free (tempresult); cJSON_Delete (json); return 0;} else {return-3;} else {return-2;} return-1;}

Main program

# include # include # include "convertText.h" # include "mp3.h" # include "led.h" # include "say.h" / / gcc-o robot robot.o mp3.o Buffer.o base64.o token.o cJSON.o listen.o convertText.o led.o say.o-lcurl-lm-lwiringPi-lmad void sayChina (char*china) {int resp=initSay (china); printf ("resp:%d\ n", resp); if (resp==1) {int tte=playData ("temp.mp3") Printf ("tte:%d\ n", tte);} int main () {char text [100] = {0}; sayChina ("Hello, I'm Xiaozhi, what can I do for you"); while (1) {printf (";"); int code= listenText (text); if (code==0) {printf ("result:%s\ n", text) If (strstr (text, "play music,")! = NULL | | strstr (text, "turn on music,") = NULL) {sayChina ("turning on music for you"); musicPlayFile ("mu.mp3");} if (strstr (text, "turn on the light,")! = NULL | | strstr (text, "turn on,")! = NULL) {sayChina ("OK"); printf ("turning on"); ledOn () } if (strstr (text, "turn off the lights,")! = NULL | | strstr (text, "off,")! = NULL | | strstr (text, "over,")! = NULL) {sayChina ("OK"); printf ("shutting down"); ledOff ();} if (strstr (text, "what's your name")! = NULL | strstr (text, "what's your name")! = NULL | | strstr (text, "name")! = NULL) {sayChina ("my name is Xiao Zhi") } if (strstr (text, "what's the weather like today")! = NULL | | strstr (text, "weather")! = NULL) {sayChina ("it's raining outside, it's a little cold");} if (strstr (text, "good afternoon")! = NULL | | strstr (text, "noon")! = NULL) {sayChina ("what's good, I haven't eaten yet") } if (strstr (text, "how old are you")! = NULL | | strstr (text, "how old are you")! = NULL | | strstr (text, "how old")! = NULL) {sayChina ("I was just born, not yet full moon");}} else {printf ("error\ n");}} return 0 } this is the end of the article on "how to implement a voice robot based on raspberry pie". I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report