Skip to content

Commit

Permalink
Fixed service. Added some instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
kha84 committed Sep 3, 2022
1 parent 089036a commit 27e8e6a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 24 deletions.
67 changes: 56 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ I just made some cosmetical changes to it and swapped the funky python voice lib

This will be a part of the pet project I'm working on - [to build an ultimate entertainment / desktop replacement machine out of cheap ARM SBC](https://orange-pi-4-lts.blogspot.com/p/todo.html). So this "voice assistant" will be running as background daemon with the system startup.

## Installation on Debian Linux
But it doesn't mean you need to have the same device as me - it could run on any Linux machine.

## Installation (Debian Bullseye 11)


1. Install all dependancies
Expand All @@ -35,35 +37,78 @@ ln -s $(pwd)/sayit /usr/local/bin/sayit

4. Make sure you have /usr/local/bin in your $PATH, if not - change the profile

5. Create a service, that will be executed for multi-user.target (look inside .service file, you might need to update your path)
5. Test the voice assistant by running it stand alone:

```
python3 main.py
```

Give it 10 or so seconds to warm up, look after logs it's printing and then say out loud "Orange, who is Donald Trump?"

6. Create a service, that will be executed for multi-user.target.
Before doing that, look inside voiceassistant.service file.
You might need to update the path to main.py script, if you installed it to different than mine location.
Also make sure that xxx in "User=xxx" is actually the user, who is running PulseAudio process. To get that to know run
```
ps -ef | grep -i pulseaudio
```
In my case PulseAudio is running under pi, so the service will be also runing under pi

Install the service:
```
sudo cp $(pwd)/voiceassistant.service /lib/systemd/system/voiceassistant.service
sudo cp ./voiceassistant.service /lib/systemd/system/voiceassistant.service
sudo systemctl daemon-reload
sudo systemctl enable voiceassistant.service
sudo systemctl start voiceassistant.service
sudo systemctl status voiceassistant.service
```

## Uninstall
## Uninstall instructions

```
sudo systemctl stop voiceassistant.service
sudo systemctl disable voiceassistant.service
rm /lib/systemd/system/voiceassistant.service
systemctl daemon-reload
rm /usr/local/bin/sayit
```

This step doesn't work yet. Run it manually:
python3 main.py
## Usage notes / troubleshooting

### Where to see logs

When the voice assistant is installed as service, logs will be collected by journald, and you can see them like this:
```
journalctl -x
```
... then press [SHIFT] + [F]

If you executed the assistant from the command line, logs will be printed there.

### Audio issues

I only tested it on my Orange PI 4 LTS which is having a built-in microphone.
pyAudio / SpeechRecognition puthon packages did a great job detecting it and using it as the default input device. But you might be less lucky than me.
If you have troubles with the built-in microphone or if your SBC doesn't have it, the usual suggesting would be to buy a decent USB microphone
instead and make sure it will be detected by pyAudio / SpeechRecognition, by running it as stand-alone (not as deamon)

First make sure you can run shell **sayit** script under the non-privileged user and you hear the sound:
```
/usr/local/bin/sayit "This is the voice of Google. Testing, testing, one, two, three"
```

Also make sure you can run sayit with using sudo, but under the same user, who's owning PulseAudio (in my case it's **pi**):

```
# first become a root with clean env
sudo su -
# then attempt to run sayit under your original user
sudo -u pi sayit "Say it again"
```
I had to export XDG_RUNTIME_DIR in that script, otherwise all audio playback tools using PulseAudio didn't know which one to use.

## TODO:

0. Fix bug with service
- where the heck is log written to?
- hearing doesn't work
- playback doesn't work
## Project TODO

1. Multi-language support
https://gtts.readthedocs.io/en/latest/module.html#languages-gtts-lang
Expand Down
31 changes: 21 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,32 @@
import pyjokes
import subprocess

listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
## configuration ##
last_text = 'I did not say anything'
activation_word = 'orange'
phraseRepeat = ['repeat', 'say it again','pardon me']
phraseJoke = ['tell me a joke','say something funny','make me smile']
phraseExit = ['stop listening','terminate yourself','exit']
###################

listener = sr.Recognizer()

def talk_orig(text):
engine.say(text)
engine.runAndWait()
## disabled this as we're using external shell script
# engine = pyttsx3.init()
# voices = engine.getProperty('voices')
# engine.setProperty('voice', voices[0].id)
#
# def talk_orig(text):
# engine.say(text)
# engine.runAndWait()


def talk(text):
if not text:
print("Warning: no text was given to talk function")
return
print("Talking this text: "+text)
last_text = text;
process = subprocess.Popen('/usr/local/bin/sayit "%s"' % str(text), shell=True)
process.wait()
print("Return code is: " + str(process.returncode))
Expand Down Expand Up @@ -62,11 +72,12 @@ def run_alexa():
info = wikipedia.summary(person, 1)
except:
info = "I have no idea"
print(info)
talk(info)
elif 'joke' in command:
elif any(x in command for x in phraseRepeat):
talk(last_text)
elif any(x in command for x in phraseJoke):
talk(pyjokes.get_joke())
elif 'stop listening' in command:
elif any(x in command for x in phraseExit):
talk("Bye-bye!")
quit()
else:
Expand Down
8 changes: 5 additions & 3 deletions sayit
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ then
exit 0
fi

# implementation with using ffplay
export XDG_RUNTIME_DIR=/run/user/1000

# implementation with using ffplay
#tfile="$(mktemp /tmp/tts.XXXXXXXXX.mp3)" || exit 1
#~pi/.local/bin/gtts-cli "$1" --output $tfile
#ffplay -nodisp -autoexit -loglevel quiet $tfile
#rm $tfile

# implementation with using SoX
~/.local/bin/gtts-cli "$1" | play -t mp3 -
# implementation with using SoX play
~pi/.local/bin/gtts-cli "$1" | play -t mp3 -

0 comments on commit 27e8e6a

Please sign in to comment.