[JAVA] Build Amazon Alexa cheaply with Raspberry Pi 3B + Bluetooth speaker (1. Make a sound first)

Great purpose

When I finally bought and tried Amazon Echo, I felt a technological innovation. After all, a 28-year-old daughter said, "Let's take Alexa," without looking at the instructions, and the conversation was established. Well, I want to buy two, but I haven't received an invitation from Amazon. I have a Raspberry Pi at hand.

There is a hand in such a case. The hand to make Raspberry Pi Amazon Echo. Amazon distributes AVS (Amazon Voice Service) for free to developers, and it is said that Alexa (voice of Amazon Echo) is included in it. I want to move it somehow.

From now on, I will buy the necessary hardware at the lowest possible price from the situation that only Raspberry Pi 3B is available, and aim to start Alexa equivalent to Echo.

Below is an article about the pioneers of making Echo on the Raspberry Pi.

https://qiita.com/bwtakacy/items/32226ed2406b5ebe1984

This chapter: First of all, the speaker does not sound. .. .. I managed to connect with Bluetooth.

The Raspberry Pi has an analog output for the speakers, so it's pretty good to get sound out of it. (Apt-get install mpg321 to play mp3) This time I want to make some sound from the wireless speaker. Otherwise you won't be able to hear Alexa's voice or song. (Song: http://www.itmedia.co.jp/news/articles/1712/19/news097.html)

Purchased item

--Bluetooth wireless speaker 1400 yen

It seems that you can use it in the bath.

image.png

Existing product

First preparation

Raspberry Pi up to date

# sudo apt-get dist-upgrade
# sudo rpi-update

Make a sound from Raspberry Pi

It seems that a daemon called PulseAudio needs to be running to make sounds from the Raspberry Pi.

sudo apt-get install pulseaudio pavucontrol
pulseaudio -D

You should now hear sound from the analog output first. For example, I have an mp3 file at hand

mpg321 Chicken.mp3

If so, For example, Chicken Attack will also be playable.

Connect wireless speakers to Raspberry Pi

Activate the wireless speaker. First, pair as Bluetooth. Details are described in the following article.

https://qiita.com/Sam/items/5169d9f060aa31080b77

sudo apt-get install pi-bluetooth blueman pulseaudio-module-bluetooth
pi@raspberrypi:~ $ bluetoothctl
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# agent on
Agent registered
[bluetooth]# scan on
Discovery started
[NEW] Device 30:21:18:71:23:51 Q50
[bluetooth]# pair 30:21:18:71:23:51
[CHG] Device 30:21:18:71:23:51 Paired: yes
Pairing successful
[bluetooth]# trust 30:21:18:71:23:51
[CHG] Device 30:21:18:71:23:51 Trusted: yes
[bluetooth]# connect 30:21:18:71:23:51
Attempting to connect to 30:21:18:71:23:51
[CHG] Device 30:21:18:71:23:51 Connected: yes
Connection successful

The service creates the following file and

[Unit]
Description=Pulse Audio

[Service]
Type=simple
ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disable-shm

[Install]
WantedBy=multi-user.target

Start the daemon with the following command, and set it to start automatically.

$ sudo systemctl start pulseaudio.service
$ sudo systemctl status pulseaudio.service
$ sudo systemctl enable pulseaudio.service

Make sound from wireless speakers

According to the article below, it is necessary to set bluetooth as the output destination of the pulseaudio daemon.

http://zokibayashi.hatenablog.com/entry/2017/10/10/020902

Add the following to the end.

/etc/pulse/system.pa


<Omission>

### Automatically load driver modules for Bluetooth hardware
.ifexists module-bluetooth-policy.so
load-module module-bluetooth-policy
.endif

.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
.endif

Set the transfer destination in /etc/dbus-1/system.d/pulseaudio-bluetooth.conf. Bluez is an implementation of the Bluetooth transfer protocol.

http://www.silex.jp/blog/wireless/2012/12/bluetooth4.html

/etc/dbus-1/system.d/pulseaudio-bluetooth.conf


  <policy user="pulse">
    <!--Comment out here
    <allow own="org.pulseaudio.Server"/>
End comment out-->
    <allow send_destination="org.bluez"/>
  </policy>

Reboot the system.

sudo reboot

If there is no sound, you may not have access to transfer with pulse. Give access rights below.

sudo usermod -a -G pulse-access,audio pi(This is the user name)

(2018/1/3 postscript) Java setting to make Alexa speak properly from the bluetooth speaker later

Later, I run the Alexa sample app, but for some reason it only produces sound from the analog output. For this reason, as a result of various investigations, I narrowed down to the setting related to Java that the sound does not sound only from the Java application (other mpg321 and ʻaplay` sound), and I found the following article and managed to succeed I understand how to make a sound. https://stackoverflow.com/questions/45847635/java-audio-clip-cannot-be-closed-when-using-linux-pulseaudio

It's a bit confusing, but it seems that you have to transfer via the route Alsa Userland → PulseAudio → Bluetooth speaker. Now, for this, edit the following file with sudo and add the following 4 lines.

/usr/lib/jvm/java-8-oracle/jre/lib/sound.properties


javax.sound.sampled.Clip=com.sun.media.sound.DirectAudioDeviceProvider
javax.sound.sampled.Port=com.sun.media.sound.PortMixerProvider
javax.sound.sampled.SourceDataLine=com.sun.media.sound.DirectAudioDeviceProvider
javax.sound.sampled.TargetDataLine=com.sun.media.sound.DirectAudioDeviceProvider

Next

Now that the speaker is producing sound, I will attach a microphone so that it can be controlled by voice.

Recommended Posts

Build Amazon Alexa cheaply with Raspberry Pi 3B + Bluetooth speaker (1. Make a sound first)
Build Amazon Alexa cheaply with Raspberry Pi 3B + Bluetooth speaker (3 Alexa Voice Service installation)
I couldn't install docker with raspberry pi2 b +.
Play down with Raspberry PI4 as a server. Part 2