如何从用C+编写的外部dll中获取常量字符*+;变成C# 我将CuxPythSimx与Unity集成在一起,通过在VisualStudio 2010中编译自己的C++项目作为DLL,我从Unity PRO中的C脚本调用。我知道dll是可以工作的,因为我用同样的代码制作了另一个exe项目,并编译了它,它作为一个独立的程序完美地工作。我使用的是pocketsphinx_连续项目示例,它获取麦克风输入并将文本输出到控制台。我已经定制了从Unity内部调用的代码,它应该以字符串形式输出回我的C代码,而不是输出到控制台。我觉得我几乎可以让它工作了,但是const char*不能作为字符串返回。如果使用此声明,我将最终获得访问冲突错误:
专用静态外部字符串从_麦克风()识别_ 所以,我试着用这个: 专用静态外部IntPtr从_麦克风()识别_ 然后,我使用这行代码尝试打印该函数的输出: 打印(“您刚才说”+Marshal.PtrToStringAnsi(从麦克风()识别) 但是,我只得到“你刚才说的”作为回报。如果我这样做的话,我可以设法找回一个内存地址:打印(“你刚才说“+从麦克风中识别出”);所以,我知道有些东西正在被退回如何从用C+编写的外部dll中获取常量字符*+;变成C# 我将CuxPythSimx与Unity集成在一起,通过在VisualStudio 2010中编译自己的C++项目作为DLL,我从Unity PRO中的C脚本调用。我知道dll是可以工作的,因为我用同样的代码制作了另一个exe项目,并编译了它,它作为一个独立的程序完美地工作。我使用的是pocketsphinx_连续项目示例,它获取麦克风输入并将文本输出到控制台。我已经定制了从Unity内部调用的代码,它应该以字符串形式输出回我的C代码,而不是输出到控制台。我觉得我几乎可以让它工作了,但是const char*不能作为字符串返回。如果使用此声明,我将最终获得访问冲突错误:,c#,c++,dll,sphinx,unity3d,C#,C++,Dll,Sphinx,Unity3d,专用静态外部字符串从_麦克风()识别_ 所以,我试着用这个: 专用静态外部IntPtr从_麦克风()识别_ 然后,我使用这行代码尝试打印该函数的输出: 打印(“您刚才说”+Marshal.PtrToStringAnsi(从麦克风()识别) 但是,我只得到“你刚才说的”作为回报。如果我这样做的话,我可以设法找回一个内存地址:打印(“你刚才说“+从麦克风中识别出”);所以,我知道有些东西正在被退回 这是我的C++代码(大部分是以C编写的,作为PokSpHythx的示例代码): char*MakeSt
这是我的C++代码(大部分是以C编写的,作为PokSpHythx的示例代码):
char*MakeStringCopy(const char*str)
{
if(str==NULL)返回NULL;
char*res=(char*)malloc(strlen(str)+1);
strcpy(res,str);
返回res;
}
外部数据规格(dllexport)常量字符*从麦克风识别
{
//这是main()中代码的几乎完全复制
字符常量*cfg;
config=cmd\u ln\u init(NULL,ps\u args(),TRUE,
“-hmm”,MODELDIR“\\hmm\\en\u US\\hub4wsj\u sc\u 8k”,
“-lm”,MODELDIR“\\lm\\en\\turtle.DMP”,
“-dict”,MODELDIR“\\lm\\en\\turtle.dic”,
无效);
if(config==NULL)
{
返回“配置为空”;
}
ps=ps_init(配置);
如果(ps==NULL)
{
返回“ps为空”;
}
广告记录*广告;
int16 adbuf[4096];
int32k,ts,rem;
字符常量*hyp;
字符常量*uttid;
续;
字符字[256];
字符字[1024]=“”;
//char temp[]=“假设”;
//hyp=温度;
如果((ad=ad_open_dev)(cmd_ln_str_r(config,“-adcdev”),
(int)cmd_ln_float32_r(配置,“-samprate”))==NULL)
E_致命(“无法打开音频设备”);
/*初始化连续监听模块*/
if((cont=cont_ad_init(ad,ad_read))==NULL)
E_致命(“未能初始化语音活动检测\n”);
if(ad_start_rec(ad)<0)
E_致命(“无法开始录制”);
如果(继续校准)(继续)<0
E_致命(“未能校准语音活动检测\n”);
对于(;;){
/*表示倾听下一句话*/
//printf(“就绪…”\n);
fflush(stdout);
fflush(stderr);
/*等待数据,等待下一次发言*/
而((k=cont_ad_read(cont,adbuf,4096))==0)
睡眠时间(100毫秒);
if(k<0)
E_致命(“读取音频失败”);
/*
*接收到的数据量不为零;开始识别新的话语。
*uttproc_begin_utt=>自动生成话语id的空参数。
*/
if(ps_start_utt(ps,NULL)<0)
E_FATAL(“无法开始说话”);
ps_过程_原始(ps、adbuf、k、FALSE、FALSE);
//printf(“侦听…\n”);
fflush(stdout);
/*注意第一个数据块的时间戳*/
ts=继续->读取;
/*解码话语直到结束(以“长”沉默为标志,>1秒)*/
对于(;;){
/*从持续收听模块读取非静音音频数据(如有)*/
如果((k=cont_ad_read(cont,adbuf,4096))<0)
E_致命(“读取音频失败”);
如果(k==0){
/*
*没有可用的语音数据;请用最近的语音检查当前时间戳
*演讲时间是否超过1秒。如果超过1秒,演讲结束。
*/
if((cont->read\u ts-ts)>默认的每秒钟采样数)
打破
}
否则{
/*接收到新语音数据;注意当前时间戳*/
ts=继续->读取;
}
/*
*解码上面读取的任何数据。
*/
rem=ps_过程_原始(ps、adbuf、k、FALSE、FALSE);
/*如果没有工作要做,就睡一会儿*/
如果((rem==0)和&(k==0))
睡眠时间(20毫秒);
}
/*
*发声结束;刷新任何累积的未处理的A/D数据并停止
*倾听直到当前话语完全解码
*/
自动停止(ad);
而(ad_read(ad,adbuf,4096)>=0);
控制和复位(续);
fflush(stdout);
/*完成解码,获取并打印结果*/
ps_end_utt(ps);
hyp=ps_get_hyp(ps,NULL,&uttid);
fflush(stdout);
/*如果说的第一句话是再见,就退出*/
//事实上,为了团结,如果说了任何话就退出!这将避免无限的厄运循环!
如果(hyp){
/*sscanf(hyp,“%s”,字);
if(strcmp(字,“再见”)==0)*/
打破
}
其他的
返回“无返回”;
/*恢复A/D录制以备下次发言*/
if(ad_start_rec(ad)<0)
E_致命(“无法开始录制”);
}
继续并关闭(续);
ad_close(ad);
无聚苯乙烯(ps);
常量字符*临时=新字符[1024];
temp=MakeStringCopy(hyp);
返回温度;}
如果改变返回温度;返回“此处的某些字符串”;然后我看到文本出现在Unity中。不过,这并没有什么帮助,因为我不需要硬编码文本,我需要语音识别代码的输出,最终存储在hyp变量中
谁能帮我找出我做错了什么?谢谢问题是,你不应该在C++中分配原始内存并用C方式消耗它,这样你就可以去掉函数中分配的内存。
char* MakeStringCopy (const char* str)
{
if (str == NULL) return NULL;
char* res = (char*)malloc(strlen(str) + 1);
strcpy(res, str);
return res;
}
extern __declspec(dllexport) const char * recognize_from_microphone()
{
//this is a near complete duplication of the code from main()
char const *cfg;
config = cmd_ln_init(NULL, ps_args(), TRUE,
"-hmm", MODELDIR "\\hmm\\en_US\\hub4wsj_sc_8k",
"-lm", MODELDIR "\\lm\\en\\turtle.DMP",
"-dict", MODELDIR "\\lm\\en\\turtle.dic",
NULL);
if (config == NULL)
{
return "config is null";
}
ps = ps_init(config);
if (ps == NULL)
{
return "ps is null";
}
ad_rec_t *ad;
int16 adbuf[4096];
int32 k, ts, rem;
char const *hyp;
char const *uttid;
cont_ad_t *cont;
char word[256];
char words[1024] = "";
//char temp[] = "hypothesis";
//hyp = temp;
if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),
(int)cmd_ln_float32_r(config, "-samprate"))) == NULL)
E_FATAL("Failed to open audio device\n");
/* Initialize continuous listening module */
if ((cont = cont_ad_init(ad, ad_read)) == NULL)
E_FATAL("Failed to initialize voice activity detection\n");
if (ad_start_rec(ad) < 0)
E_FATAL("Failed to start recording\n");
if (cont_ad_calib(cont) < 0)
E_FATAL("Failed to calibrate voice activity detection\n");
for (;;) {
/* Indicate listening for next utterance */
//printf("READY....\n");
fflush(stdout);
fflush(stderr);
/* Wait data for next utterance */
while ((k = cont_ad_read(cont, adbuf, 4096)) == 0)
sleep_msec(100);
if (k < 0)
E_FATAL("Failed to read audio\n");
/*
* Non-zero amount of data received; start recognition of new utterance.
* NULL argument to uttproc_begin_utt => automatic generation of utterance-id.
*/
if (ps_start_utt(ps, NULL) < 0)
E_FATAL("Failed to start utterance\n");
ps_process_raw(ps, adbuf, k, FALSE, FALSE);
//printf("Listening...\n");
fflush(stdout);
/* Note timestamp for this first block of data */
ts = cont->read_ts;
/* Decode utterance until end (marked by a "long" silence, >1sec) */
for (;;) {
/* Read non-silence audio data, if any, from continuous listening module */
if ((k = cont_ad_read(cont, adbuf, 4096)) < 0)
E_FATAL("Failed to read audio\n");
if (k == 0) {
/*
* No speech data available; check current timestamp with most recent
* speech to see if more than 1 sec elapsed. If so, end of utterance.
*/
if ((cont->read_ts - ts) > DEFAULT_SAMPLES_PER_SEC)
break;
}
else {
/* New speech data received; note current timestamp */
ts = cont->read_ts;
}
/*
* Decode whatever data was read above.
*/
rem = ps_process_raw(ps, adbuf, k, FALSE, FALSE);
/* If no work to be done, sleep a bit */
if ((rem == 0) && (k == 0))
sleep_msec(20);
}
/*
* Utterance ended; flush any accumulated, unprocessed A/D data and stop
* listening until current utterance completely decoded
*/
ad_stop_rec(ad);
while (ad_read(ad, adbuf, 4096) >= 0);
cont_ad_reset(cont);
fflush(stdout);
/* Finish decoding, obtain and print result */
ps_end_utt(ps);
hyp = ps_get_hyp(ps, NULL, &uttid);
fflush(stdout);
/* Exit if the first word spoken was GOODBYE */
//actually, for unity, exit if any word was spoken at all! this will avoid an infinite loop of doom!
if (hyp) {
/*sscanf(hyp, "%s", words);
if (strcmp(word, "goodbye") == 0)*/
break;
}
else
return "nothing returned";
/* Resume A/D recording for next utterance */
if (ad_start_rec(ad) < 0)
E_FATAL("Failed to start recording\n");
}
cont_ad_close(cont);
ad_close(ad);
ps_free(ps);
const char *temp = new char[1024];
temp = MakeStringCopy(hyp);
return temp;}
[DllImport("MyLibrary.dll")]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string GetStringValue();
[DllImport ("pocketsphinx_unity",CallingConvention=CallingConvention.Cdecl,CharSet = CharSet.Ansi)]
private static extern void recognize_from_microphone(StringBuilder str);StringBuilder mytext= new StringBuilder(1000);
recognize_from_microphone(mytext);
print("you just said " + mytext.ToString());
extern __declspec(dllexport) void recognize_from_microphone(char * fromUnity){
static ps_decoder_t *ps;
static cmd_ln_t *config;
config = cmd_ln_init(NULL, ps_args(), TRUE,
"-hmm", MODELDIR "\\hmm\\en_US\\hub4wsj_sc_8k",
"-lm", MODELDIR "\\lm\\en\\turtle.DMP",
"-dict", MODELDIR "\\lm\\en\\turtle.dic",
NULL);
if (config == NULL)
{
//return "config is null";
}
ps = ps_init(config);
if (ps == NULL)
{
//return "ps is null";
}
ad_rec_t *ad;
int16 adbuf[4096];
int32 k, ts, rem;
char const *hyp;
char const *uttid;
cont_ad_t *cont;
//char word[256];
char * temp;
if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),
(int)cmd_ln_float32_r(config, "-samprate"))) == NULL)
printf("Failed to open audio device\n");
/* Initialize continuous listening module */
if ((cont = cont_ad_init(ad, ad_read)) == NULL)
printf("Failed to initialize voice activity detection\n");
if (ad_start_rec(ad) < 0)
printf("Failed to start recording\n");
if (cont_ad_calib(cont) < 0)
printf("Failed to calibrate voice activity detection\n");
for (;;) {
/* Indicate listening for next utterance */
//printf("READY....\n");
fflush(stdout);
fflush(stderr);
/* Wait data for next utterance */
while ((k = cont_ad_read(cont, adbuf, 4096)) == 0)
sleep_msec(100);
if (k < 0)
printf("Failed to read audio\n");
/*
* Non-zero amount of data received; start recognition of new utterance.
* NULL argument to uttproc_begin_utt => automatic generation of utterance-id.
*/
if (ps_start_utt(ps, NULL) < 0)
printf("Failed to start utterance\n");
ps_process_raw(ps, adbuf, k, FALSE, FALSE);
//printf("Listening...\n");
fflush(stdout);
/* Note timestamp for this first block of data */
ts = cont->read_ts;
/* Decode utterance until end (marked by a "long" silence, >1sec) */
for (;;) {
/* Read non-silence audio data, if any, from continuous listening module */
if ((k = cont_ad_read(cont, adbuf, 4096)) < 0)
printf("Failed to read audio 2nd\n");
if (k == 0) {
/*
* No speech data available; check current timestamp with most recent
* speech to see if more than 1 sec elapsed. If so, end of utterance.
*/
if ((cont->read_ts - ts) > DEFAULT_SAMPLES_PER_SEC)
break;
}
else {
/* New speech data received; note current timestamp */
ts = cont->read_ts;
}
/*
* Decode whatever data was read above.
*/
rem = ps_process_raw(ps, adbuf, k, FALSE, FALSE);
/* If no work to be done, sleep a bit */
if ((rem == 0) && (k == 0))
sleep_msec(20);
}
/*
* Utterance ended; flush any accumulated, unprocessed A/D data and stop
* listening until current utterance completely decoded
*/
ad_stop_rec(ad);
while (ad_read(ad, adbuf, 4096) >= 0);
cont_ad_reset(cont);
fflush(stdout);
/* Finish decoding, obtain and print result */
ps_end_utt(ps);
hyp = ps_get_hyp(ps, NULL, &uttid);
fflush(stdout);
/* Exit if the first word spoken was GOODBYE */
//actually, for unity, exit if any word was spoken at all! this will avoid an infinite loop of doom!
if (hyp) {
strcpy(fromUnity,hyp);
break;
}
else
//return "nothing returned";
/* Resume A/D recording for next utterance */
if (ad_start_rec(ad) < 0)
printf("Failed to start recording\n");
}
cont_ad_close(cont);
ad_close(ad);
ps_free(ps);
}