StevenHickson / PiAUISuite

Raspberry PI AUI Suite
Other
695 stars 210 forks source link

Experimental Feature - turn on/off ACT light when recording or not recording #60

Open irsx02 opened 8 years ago

irsx02 commented 8 years ago

A little background; My guess is that VoiceCommand (VC) works by recording X duration seconds, sends it to Google API, gets Google's response, and then parses it to see if your keyword is present. So, in my situation, my Rasp Pi records 2 seconds, uploads it up to google, reads what Google returns, and then compare with my keyword. If I say "Computer" (my keyword) it gets recorded, sent up to google and the result is parsed by VC.

One annoying thing I have discovered is that I must say "Computer" during those two seconds; and only during two seconds. If you speak the keyword during the upload period, while waiting for google period, or the parsing time frame, you are out of luck.

I've found a way using my Raspberry Pi 3 (Raspbian Jessie) to take control of the green LED (ACT) and turn on when recording, turn off when doing other processes.

What I need is a few brave testers to try this feature out. One thing; I do not have any other versions of Raspberry Pi - just the Rasp Pi 3 (and running on raspbian jessie) Who wants to be a QA tester?

First, lets see if you are able to control the ACT led light (Instructions for RaspPi 3, your results may vary).
STAGE 1 - Can you access the LED from the OS Step 1. cd /sys/class/leds and see if there's a directory called led0 (may be led1 for some) Step 2. run echo gpio | sudo tee /sys/class/leds/led0/trigger You now have control of the LED light. (I assume its led0, may be different)
Step 3. turn on the LED with echo 1 | sudo tee /sys/class/leds/led0/brightness Light should turn on. Step 4. turn off the LED with echo 0 | sudo tee /sys/class/leds/led0/brightness Light should turn off.

irsx02 commented 8 years ago

STAGE 2 - lets edit the header and cpp file Step 1. Find the /PiAUISuite/voicecommand.h file and open it in your favorite editor.

Step 2. find #define COM_DURATION_DEFAULT "2" add extern std::string LED_LOCATION; underneath.

Step 3. find voicecommand.cpp

Step 4. Find the line static const char *optString = "I:l:d:D:psb::c::v::ei::q::t:k:r:f:h?"; and add std::string LED_LOCATION = " /sys/class/leds/led0"; // rasp pi location of LED

Step 5. Find the line system("echo \"\" > /dev/shm/voice.log"); //lazily clear out the log file and add underneath system("echo gpio | sudo tee /sys/class/leds/led0/trigger"); // takes control of LED

Step 6. Now do the following; top line is the original line of code. You will then replace that line with the code on the bottom. original: string run = "arecord -D "; replace with: string run = "echo 1 | sudo tee "+LED_LOCATION+"/brightness | arecord -D ";

Step 7. Like step 5, search for top line, and replace with bottom line code. find this original: 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'";

replace with: string cont_com = "echo 0 |sudo tee "+LED_LOCATION+"/brightness | 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'";

irsx02 commented 8 years ago

STAGE 3 - update the speech-recog.sh, compile and update.

step 1. find speech-recog.sh

step 2. find the line that starts with arecord -D $hardware -f (there's much more after the -f. but get that complete one line of code. Now replace with echo 1 | sudo tee /sys/class/leds/led0/brightness | arecord -D $hardware -f S16_LE -d $duration -r 16000 |flac - -f --best --sample-rate 16000 -o /dev/shm/out.flac 1>/dev/shm/voice.log 2>/dev/shm/voice.log; echo 0 | sudo tee /sys/class/leds/led0/brightness | curl -X POST --data-binary @/dev/shm/out.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=$lang&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw&client=Mozilla/5.0" | sed -e 's/[{}]/''/g' | awk -F":" '{print $4}' | awk -F"," '{print $1}' | tr -d '\n'

important note. I'm using /sys/class/leds/led0 and it appears twice. Your Pi may differ.

step 3. do a quick test to make sure everything is typed correctly. Run ./speech-recog.sh and the ACT LED should turn on when it is recording. It will turn itself off when done, and then sends it to google.

step 4 Compile. Assuming you have g++-4.8 installed. Run make then go to \PiAUISuite\Install folder and sudo ./InstallAUISuite.sh

step 5. Run voicecommand -c and the LED should turn on when recording, off when its processing other tasks.

irsx02 commented 8 years ago

So, why am I putting my code here and no in Pull? a) because this change is limited to the Raspberry Pi (and version 3 model B. I don't have other versions of Rasp Pi to test this code) And steven seemed to write VoiceCommand for all linux systems. My changes would lock this down to Rasp Pi. I would like to help from a few daring pioneers to first test. b) because I also dont know how to check out/check in code.

Try this out - and tell me if it is working for you.

irsx02 commented 8 years ago

Info on stage 1 - Controlling PWR/ACT on Raspberry Pi .This can help you out to see if you can control the built in LEDs