In this project, we will use Raspberry Pi 4B, ReSpeaker 4-mic Array for Raspberry Pi (You can use other microphones but you will need other codes to test them), and USB Speaker to design our AI Speaker.
First, you have to install Raspberry Pi os (Raspberry Pi 64-bit recommended).
- Prepare your Micro SD card and follow the tutorial on YouTube https://www.youtube.com/watch?v=ntaXWS8Lk34Download
- Either work directly on Raspberry Pi or use SSH (Secure Shell) to program it
- If you want to use SSH, you have to enable SSH and configure your wireless LAN before installing the os (If you have not, you need to reinstall the operating system)
Once you are ready, you can now access your Raspberry Pi
- Open the terminal and write the code below:
ssh <USER_NAME>@<IP_ADDRESS>
Replace <USER_NAME> and <IP_ADDRESS> with your user name and IP address that you specified in the settings
- Enter the password
- Congrats! Now you are remotely connected to your Raspberry Pi
(You can also use text editors if Remote-SSH extension is installed)
If you are connected to your Raspberry Pi, you are now ready to install the driver for our ReSpeaker
- First, we will get the Seeed voice card source code:
sudo apt-get update
git clone https://github.com/HinTak/seeed-voicecard.git
cd seeed-voicecard
sudo ./install.sh
sudo reboot
- Select audio output on Raspberry Pi:
sudo raspi-config
# Select 1 System options
# Select S2 Audio
# Select your preferred Audio output device (USB if you are connecting speaker with usb cable)
# Select Finish
- Check that the sound card name looks like this:
pi@raspberrypi:~ $ arecord -L
null
Discard all samples (playback) or generate zero samples (capture)
jack
JACK Audio Connection Kit
pulse
PulseAudio Sound Server
default
playback
ac108
sysdefault:CARD=seeed4micvoicec
seeed-4mic-voicecard,
Default Audio Device
dmix:CARD=seeed4micvoicec,DEV=0
seeed-4mic-voicecard,
Direct sample mixing device
dsnoop:CARD=seeed4micvoicec,DEV=0
seeed-4mic-voicecard,
Direct sample snooping device
hw:CARD=seeed4micvoicec,DEV=0
seeed-4mic-voicecard,
Direct hardware device without any conversions
plughw:CARD=seeed4micvoicec,DEV=0
seeed-4mic-voicecard,
Hardware device with all software conversions
usbstream:CARD=seeed4micvoicec
seeed-4mic-voicecard
USB Stream Output
usbstream:CARD=ALSA
bcm2835 ALSA
USB Stream Output
If your results are the same as mine, you are ready for the next step!
We will check if our microphone and speaker are working
- Clone https://github.com/respeaker/4mics_hat.git repository to your Raspberry Pi:
git clone https://github.com/respeaker/4mics_hat.git
- Install the necessary dependencies from mic_hat repository folder
sudo apt-get install portaudio19-dev libatlas-base-dev
cd 4mics_hat/ # Do not forget to change to the correct directory
pip3 install -r requirements.txt
- We use PyAudio python library to record sound with Python
python3 recording_examples/get_device_index.py
- You will see the device ID as below.
Input Device id 2 - seeed-4mic-voicecard: - (hw:1,0)
- To record the sound, open recording_examples/record.py file with nano or other text editor and change RESPEAKER_INDEX = 2 to index number of ReSpeaker on your system. Then run python script record.py to make a recording:
python3 recording_examples/record.py
- To play the recorded samples you can use aplay:
aplay output.wav # Recorded voice will be saved in the output.wav file
If the sound does not play, check if both microphone and speaker are connected properly and the right audio output is selected
(You can check using raspi-config)
We will use Olney1's Repository to set-up our AI Speaker
- Clone the repository
https://github.com/Olney1/ChatGPT-OpenAI-Smart-Speaker.git
- Install and update essential packages
pip install openai gTTS pyaudio SpeechRecognition playsound python-dotenv pyobject
sudo apt update
sudo apt install python3-gpiozero
- Get your API key from OpenAPI (You will probably need to add your payment method if you are not new to OpenAI)
- Set up the environment variable for your Open API Key. To do this create a
.env filein the same directory and add your API Key to the file like this:OPENAI_API_KEY = "API KEY GOES HERE" - There is an initial code inside
smart_speaker.pythat does not work well, therefore we have to replace it with our new code:
- First, install the required package using:
pip install pydub - Initial code looks like this:
def play_audio_file():
# play the audio file and wake speaking LEDs
pixels.speak()
# os.system("mpg321 response.mp3")
playsound("response.mp3", block=False) # There’s an optional second argument, block, which is set to True by default. Setting it to False makes the function run asynchronously.
- Replace the initial code above with our new code below:
from pydub import AudioSegment
from pydub.playback import play
def play_audio_file():
song = AudioSegment.from_mp3("response.mp3")
play(song)
- You are now ready to use AI Speaker. Simply move to
ChatGPT-OpenAI-Smart-Speakerdirectory and executesmart_speaker.py - Say the wake word
Lilyand ask the question when it is ready. (You can exit the code usingctr + Z) - If you cannot hear well, you may need to adjust the volume using the code below:
alsamixer
- Exit the interface after the adjustment