加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test.cpp 4.51 KB
一键复制 编辑 原始数据 按行查看 历史
爆裂键盘手 提交于 2024-02-26 02:44 . 完成本地pcm文件语音识别
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <iostream>
#include <mutex>
#include <thread>
#include <libwebsockets.h>
struct lws *websocket;
using namespace std;
void *readFileToMem(string file, size_t *read_size)
{
FILE *fp;
size_t file_size;
void *mem = 0;
fp = fopen(file.c_str(), "rb");
if (fp == NULL) {
printf("failed to open input file %s\n", file.c_str());
*read_size = 0;
return 0;
}
fseek(fp, 0L, SEEK_END);
file_size = ftell(fp);
rewind(fp);
if (file_size <= 0)
{
fclose(fp);
*read_size = 0;
return 0;
}
mem = malloc(file_size);
*read_size = fread(mem, 1, file_size, fp);
if (*read_size != file_size)
{
free(mem);
*read_size = 0;
mem = 0;
}
fclose(fp);
return mem;
}
int send_websockets(const void *data, int size, enum lws_write_protocol protocol)
{
int ret;
unsigned char *buf;
buf = (unsigned char *)malloc(LWS_PRE + size);
memcpy(&buf[LWS_PRE], data, size);
ret = lws_write(websocket, &buf[LWS_PRE], size, protocol);
free(buf);
return ret;
}
void send_start_params(void)
{
int ret; // cuid值不能有空格
static string req =
R"({
"header": {
"message_id": "05450bf69c53413f8d88aed1ee60aeae",
"task_id": "640bc797bb684bd6960185651307aeae",
"namespace": "SpeechTranscriber",
"name": "StartTranscription",
"appkey": "w4Wjnmb3YKrXkIUy"
},
"payload": {
"format": "pcm",
"sample_rate": 16000,
"enable_intermediate_result": true,
"enable_punctuation_prediction": true,
"enable_inverse_text_normalization": true
}
})";
ret = send_websockets(req.c_str(), req.size(), LWS_WRITE_TEXT);
printf("[send_start_params]: size=%d\n", ret);
}
void send_audio(void)
{
void *pcm;
size_t size;
int chunk_ms = 160; // 160ms的录音
int chunk_len = 16000 * 2 / 1000 * chunk_ms;
pcm = readFileToMem("16k-0.pcm", &size);
printf("pcm size = %d\n", size);
for(int i = 0; i < size; i += chunk_len)
{
int ret;
int send_size;
if(i + chunk_len < size)
send_size = chunk_len;
else
send_size = size - i;
ret = send_websockets((unsigned char*)pcm + i, send_size, LWS_WRITE_BINARY);
printf("[send_audio]: size=%d\n", ret);
usleep(chunk_ms * 1000);
}
}
void send_finish()
{
int ret;
static string req = R"({
"header": {
"message_id": "05450bf69c53413f8d88aed1ee60aeae",
"task_id": "640bc797bb684bd6960185651307aeae",
"namespace": "SpeechTranscriber",
"name": "StopTranscription",
"appkey": "w4Wjnmb3YKrXkIUy"
}
})";
ret = send_websockets(req.c_str(), req.size(), LWS_WRITE_TEXT);
printf("[send_finish]: size=%d\n", ret);
}
void run(void)
{
send_start_params();
usleep(100 * 1000);
send_audio();
send_finish();
}
static int callback_websocket(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
{
switch (reason) {
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
fprintf(stderr, "Error connecting to server\n");
break;
case LWS_CALLBACK_CLIENT_ESTABLISHED:
{
fprintf(stderr, "Connected to server\n");
thread th1(run);
th1.detach();
}
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
fprintf(stderr, "Received data: %s\n", (char *)in);
break;
case LWS_CALLBACK_CLOSED:
fprintf(stderr, "Disconnected from server\n");
break;
default:
break;
}
return 0;
}
struct lws_protocols protocols[] = {
{ "default", callback_websocket, 65536, 65536 },
{ NULL, NULL, 0, 0 }
};
int main(int argc, char *argv[])
{
struct lws_context_creation_info info = {};
struct lws_client_connect_info i = {};
struct lws_context *context;
info.port = CONTEXT_PORT_NO_LISTEN;
info.protocols = protocols;
info.gid = -1;
info.uid = -1;
context = lws_create_context(&info);
if (context == NULL) {
fprintf(stderr, "Creating libwebsocket context failed\n");
return 0;
}
i.port = 80;
i.path = "/ws/v1?token=4d7389a6432a4432880f9fa8ff728e08";
i.context = context;
i.address = "nls-gateway-cn-shanghai.aliyuncs.com";
i.host = i.address;
i.origin = i.address;
websocket = lws_client_connect_via_info(&i);
if (websocket == NULL) {
fprintf(stderr, "Error connecting to server\n");
return 0;
}
while(1)
{
int ret;
ret = lws_service(context, 10000); // 10ms
if(ret < 0)
break;
}
lws_context_destroy(context);
printf("out\n");
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化