-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternal_functions.cpp
More file actions
64 lines (54 loc) · 1.68 KB
/
external_functions.cpp
File metadata and controls
64 lines (54 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "external_functions.h"
std::string voice_transfer(std::string path)
{
// QApplication a(argc, argv);
// Widget w;
// w.show();
// return a.exec();
// 创建一个配置对象
ps_config_t *config = ps_config_init(NULL);
ps_config_set_str(config, "hmm", "..\\DirectTransfer\\external_libs\\pocketsphinx\\model\\en-us\\en-us");
ps_config_set_str(config, "lm", "..\\DirectTransfer\\external_libs\\pocketsphinx\\model\\en-us\\en-us.lm.bin");
ps_config_set_str(config, "dict", "..\\DirectTransfer\\external_libs\\pocketsphinx\\model\\en-us\\cmudict-en-us.dict");
ps_decoder_t *ps = ps_init(config);
// 初始化解码器
// if (ps_start_utt(ps) < 0)
// {
// std::cout << "Error initializing decoder." << std::endl;
// return 1;
// }
// 读取音频数据并进行解码
const char *audio_data = path.c_str(); // 替换为你的音频文件路径
FILE *audio_file = fopen(audio_data, "rb");
if (audio_file == NULL)
{
std::string str="Error opening audio file.\n";
qDebug() << str;
return str;
}
ps_start_utt(ps);
while (!feof(audio_file))
{
int16_t buf[512];
size_t nread = fread(buf, sizeof(int16_t), 512, audio_file);
ps_process_raw(ps, buf, nread, FALSE, FALSE);
}
// 结束解码
ps_end_utt(ps);
// 获取识别结果
char const *hyp = ps_get_hyp(ps, NULL);
std::string str=hyp;
if (hyp != NULL)
{
qDebug() << "result:" << hyp << "\n";
}
else
{
qDebug() << "No key word.\n" ;
}
// 释放资源
fclose(audio_file);
ps_free(ps);
ps_config_free(config);
return str;
}