Exemplo n.º 1
0
int main(int argc, char* argv[]) {
    VoiceCommand vc;
    //this is a really crude and terrible hack.
    //It makes it so that the config file doesn't overwrite the command line options
    //And it allows the config file to be set to something random
    system("echo \"\" > /dev/shm/voice.log"); //lazily clear out the log file
    vc.CheckConfigParam(argc,argv);
    
    FILE *cmd = NULL;
    char message[200];
    message[0] = '\0';

    vc.GetConfig();
    //command line options after the config options
    vc.CheckCmdLineParam(argc,argv);
    //vc.CheckConfig();
    if(!vc.pid_file.empty()) {
        FILE *out;
        out = fopen(vc.pid_file.c_str(),"w");
        if(out == NULL)
            printf("Can not write to pid file: %s\n",vc.pid_file.c_str());
        else {
            fprintf(out,"%d",getpid());
            fclose(out);
            printf("Wrote pid file\n");
        }
    }
    if(vc.quiet)
        fprintf(stderr,"running in quiet mode\n");
    if(vc.ignoreOthers)
        fprintf(stderr,"Not querying for answers\n");
    if(vc.edit) {
        vc.EditConfig();
    } else if(vc.continuous && vc.forced_input.empty()) {
        fprintf(stderr,"running in continuous mode\n");
        if(vc.verify)
            fprintf(stderr,"verifying command as well\n");
        fprintf(stderr,"keyword duration is %s and duration is %s\n",vc.command_duration.c_str(),vc.duration.c_str());
        float volume = 0.0f;
        changemode(1);
        string cont_com = "curl -X POST --data-binary @/dev/shm/noise.flac --user-agent 'Mozilla/5.0' --header 'Content-Type: audio/x-flac; rate=16000;' 'https://www.google.com/speech-api/v2/recognize?output=json&lang=" + vc.lang + "&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw&client=Mozilla/5.0' | sed -e 's/[{}]/''/g' | awk -F\":\" '{print $4}' | awk -F\",\" '{print $1}' | tr -d '\\n'";

        while(vc.continuous) {
            volume = GetVolume(vc.recordHW, vc.command_duration, true);
            if(volume > vc.thresh) {
                //printf("Found volume %f above thresh %f\n",volume,vc.thresh);
                if(vc.verify) {
                    system("flac /dev/shm/noise.wav -f --best --sample-rate 16000 -o /dev/shm/noise.flac 1>>/dev/shm/voice.log 2>>/dev/shm/voice.log");
                    cmd = popen(cont_com.c_str(),"r");
                    if(cmd == NULL)
                        printf("ERROR\n");
                    fscanf(cmd,"\"%[^\"\n]\"",message);
                    fclose(cmd);
                    system("rm -fr /dev/shm/noise.*");
                    //printf("message: %s, keyword: %s\n", message, vc.keyword.c_str());
                    if(iequals(message,vc.keyword.c_str())) {
                        message[0] = '\0'; //this will clear the first bit
                        ProcessVoice(cmd,vc,message);
                    }
                } else {
                    ProcessVoice(cmd,vc,message);
                }
                message[0] = '\0'; //this will clear the first bit
            }
            if(kbhit()) {
                if(getchar() == 27) {
                    printf("Escaping\n");
                    vc.continuous = false;
                    changemode(0);
                } else if(getchar() == 'v') {
                    if(vc.verify) {
                        printf("Turning verify off\n");
                        vc.verify = false;
                    } else {
                        printf("Turning verify on\n");
                        vc.verify = true;
                    }
                }
            }
        }
    } else {
        if(vc.forced_input.empty()) {
            string command = "speech-recog.sh";
            if(vc.differentHW) {
                command += " -D ";
                command += vc.recordHW;
            }
            command += " -d ";
            command += vc.duration;
            command += " -l ";
            command += vc.lang;
            cmd = popen(command.c_str(),"r");
            fscanf(cmd,"\"%[^\"\n]\"",message);
            vc.ProcessMessage(message);
            fclose(cmd);
        } else {
            vc.ProcessMessage(vc.forced_input.c_str());
        }
    }

    return 0;
}
Exemplo n.º 2
0
int main(int argc, char* argv[]) {
    VoiceCommand vc;
    //this is a really crude and terrible hack.
    //It makes it so that the config file doesn't overwrite the command line options
    //And it allows the config file to be set to something random
    system("echo \"\" > /dev/shm/voice.log"); //lazily clear out the log file
    vc.CheckConfigParam(argc,argv);
    
    FILE *cmd = NULL;
    char message[200];
    message[0] = '\0';

    vc.GetConfig();
    //command line options after the config options
    vc.CheckCmdLineParam(argc,argv);
    //vc.CheckConfig();
    if(vc.quiet)
        fprintf(stderr,"running in quiet mode\n");
    if(vc.ignoreOthers)
        fprintf(stderr,"Not querying for answers\n");
    if(vc.edit) {
        vc.EditConfig();
    } else if(vc.continuous && vc.forced_input.empty()) {
        fprintf(stderr,"running in continuous mode\n");
        if(vc.verify)
            fprintf(stderr,"verifying command as well\n");
        fprintf(stderr,"keyword duration is %s and duration is %s\n",vc.command_duration.c_str(),vc.duration.c_str());
        float volume = 0.0f;
        changemode(1);
        string cont_com = "wget -O - -o /dev/null --post-file /dev/shm/noise.flac --header=\"Content-Type: audio/x-flac; rate=16000\" http://www.google.com/speech-api/v1/recognize?lang=" + vc.lang + " | sed -e 's/[{}]/''/g'| awk -v k=\"text\" '{n=split($0,a,\",\"); for (i=1; i<=n; i++) print a[i]; exit }' | awk -F: 'NR==3 { print $3; exit }'";
        while(vc.continuous) {
            volume = GetVolume(vc.recordHW, vc.command_duration, true);
            if(volume > vc.thresh) {
                //printf("Found volume %f above thresh %f\n",volume,vc.thresh);
                if(vc.verify) {
                    system("flac /dev/shm/noise.wav -f --best --sample-rate 16000 -o /dev/shm/noise.flac 1>>/dev/shm/voice.log 2>>/dev/shm/voice.log");
                    cmd = popen(cont_com.c_str(),"r");
                    if(cmd == NULL)
                        printf("ERROR\n");
                    fscanf(cmd,"\"%[^\"\n]\"\n",message);
                    fclose(cmd);
                    //system("rm -fr /dev/shm/noise.*");
                    //printf("message: %s, keyword: %s\n", message, vc.keyword.c_str());
                    if(contains(message,vc.keyword.c_str())) {
                        message[0] = '\0'; //this will clear the first bit
                        ProcessVoice(cmd,vc,message);
                    }
                } else {
                    ProcessVoice(cmd,vc,message);
                }
                message[0] = '\0'; //this will clear the first bit
            }
            if(kbhit()) {
                if(getchar() == 27) {
                    printf("Escaping\n");
                    vc.continuous = false;
                    changemode(0);
                } else if(getchar() == 'v') {
                    if(vc.verify) {
                        printf("Turning verify off\n");
                        vc.verify = false;
                    } else {
                        printf("Turning verify on\n");
                        vc.verify = true;
                    }
                }
            }
        }
    } else {
        if(vc.forced_input.empty()) {
            string command = "speech-recog.sh";
            if(vc.differentHW) {
                command += " -D ";
                command += vc.recordHW;
            }
            command += " -d ";
            command += vc.duration;
            command += " -l ";
            command += vc.lang;
            cmd = popen(command.c_str(),"r");
            fscanf(cmd,"\"%[^\"\n]\"\n",message);
            vc.ProcessMessage(message);
            fclose(cmd);
        } else {
            vc.ProcessMessage(vc.forced_input.c_str());
        }
    }

    return 0;
}