Example #1
0
int main(int argc, char* argv[]) {
    FILE *cmd = NULL;
    char message[200];

    VoiceCommand vc;
    vc.GetConfig();
    //vc.CheckConfig();
    bool continuous = false;
    if(argc == 2 && strcmp(argv[1],"-c") == 0) {
        printf("running in continuous mode\n");
        continuous = true;
        float volume = 0.0f;
        changemode(1);
        while(continuous) {
            system("arecord -D plughw:1,0 -f cd -t wav -d 2 -r 16000 /dev/shm/noise.wav 1>/dev/null 2>/dev/null");
            cmd = popen("sox /dev/shm/noise.wav -n stats -s 16 2>&1 | awk '/^Max\\ level/ {print $3}'","r");
            fscanf(cmd,"%f",&volume);
            fclose(cmd);
            if(volume > 0.7f) {
                /*system("flac /dev/shm/noise.wav -f --best --sample-rate 16000 -o /dev/shm/noise.flac 1>/dev/null 2>/dev/null");
                cmd = popen("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=en | 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 }'","r");
                if(cmd == NULL)
                    printf("ERROR\n");
                fscanf(cmd,"\"%[^\"\n]\"\n",message);
                fclose(cmd);
                if(strcmp(message,"pi") == 0) {*/
                    printf("Found audio\n");
                    system("espeak -v en-uk \"FILLER FILL FILL Ready?\" 2>/dev/null 1>/dev/null");
                    cmd = popen("speech-recog.sh","r");
                    fscanf(cmd,"\"%[^\"\n]\"\n",message);
                    vc.ProcessMessage(message);
                    fclose(cmd);
                //}
                message[0] = '\0'; //this will clear the first bit
            }
            if(kbhit()) {
                if(getchar() == 27) {
                    printf("Escaping\n");
                    continuous = false;
                    changemode(0);
                }
            }
        }
    } else if(argc == 2 && strcmp(argv[1],"-e") == 0) {
        //Edit the config file
        vc.EditConfig();
    } else {
        //system("espeak -v en-uk \"FILLER FILL FILL Ready?\" 2>/dev/null 1>/dev/null");
        cmd = popen("speech-recog.sh","r");
        fscanf(cmd,"\"%[^\"\n]\"\n",message);
        vc.ProcessMessage(message);
        fclose(cmd);
    }

    return 0;
}
inline void ProcessVoice(FILE *cmd, VoiceCommand &vc, char *message) {
    printf("Found audio\n");
    vc.Speak(vc.response);
    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);
}
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;
}
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;
}