Enabling Equalizer on Raspberry Pi using ALSA equal Plugin


This post shows how to setup a system wide equalizer on Raspberry Pi using ALSA equal plugin. Below are my setup.

  • Raspberry Pi3 B+ with Raspbian Stretch (2018 April version)
  • 3.5 mm plug Speaker

 

Contents
– Assumptions
– Steps
1. Equalizer Plugin Installation
2. ALSA Configuration
3. Play audio
4. Verify Equalizer
– Notes
– Reference

 

Steps
1. Equalizer Plugin Installation

sudo apt-get install libasound2-plugin-equal -y

 

2. ALSA Configuration
2-1. Create .asoundrc.

nano ~/.asoundrc

2-2. Add the lines below, save and close the file.

pcm.!default {
 type plug
 slave.pcm plugequal;
}
 
ctl.equal {
 type equal
}
 
pcm.plugequal {
 type equal
 slave.pcm "plughw:ALSA,0"
}
 
pcm.equal {
 type plug
 slave.pcm plugequal
}

2-3. Reload .asoundrc.

alsactl kill rescan

 

3. Play audio
Play a sample sound file in a loop for testing.

while [ 1 ];do aplay /usr/share/sounds/alsa/Front_Center.wav;done

Note: Press Ctrl+C to exit.

 

4. Verify Equalizer
Adjust the equalizer by using alsamixer command.

alsamixer -D equal

The sound from Raspberry Pi’s speaker should change as you change the equalizer values.

 

Notes
Since Raspbian Stretch, Bluetooth audio is handled by ALSA using bluez-alsa [2]. So if A2DP sink is enabled, this equalizer can be also applied on Bluetooth audio.

 

References
[1] Raspbian: the great Equalizer
[2] Raspbian Stretch has arrived – Raspberry Pi Official Blog

 

 

Streaming Bluetooth Audio from Phone to Raspberry Pi using ALSA


Update (Jan 8, 2020):
Step 1 has been updated to fix codebay and elba’s problems based on peacekunov’s feedback (Thanks!). Please see the comments for details.


Update (May 22, 2019):
Steps to manually enable A2DP sink role has been added based on Wolfgang and John’s feedback. (Step 1).


This post shows steps to setup Bluetooth audio sink on Raspberry Pi using bluez-alsa. Bluetooth audio sink (A2DP sink) enables Raspberry Pi to receive audio from other Bluetooth devices (e.g. smartphone) and play it through the connected speaker. Below are my setup.

 

Steps
1. Enabling Audio Profile Sink Role
1-1. Open the configuration file for bluealsa service

sudo nano /lib/systemd/system/bluealsa.service

1-2. Search the line starts with “ExecStart” and add a profile option with a2dp-sink as below.

ExecStart=/usr/bin/bluealsa -p a2dp-sink

1-3. Reboot.

sudo reboot

 

2. Bluetooth Pairing and Connection
2-1. Launch BlueZ command line interface.

bluetoothctl

2-2. Setup a pairing agent.

default-agent

2-3. Make the Raspberry Pi discoverable.

discoverable on

2-4. On your phone, search and select your Raspberry Pi from Bluetooth menu.

2-5. Confirm the pairing on both your phone and Raspberry Pi.

Request confirmation
[agent] Confirm passkey 847261 (yes/no): yes

2-6. Authorize A2DP service (first 32 bits: 0000110d). [2]

Authorize service
[agent] Authorize service 0000110d-0000-1000-8000-00805f9b34fb (yes/no): yes

Note: You may be asked about other services (such as HFP: 0000111E) depending on what services your phone supports. Just answer ‘yes’ to all the services if you are not sure.

2-7. Trust the phone so that Raspberry Pi will automatically accept connections from the phone from the next time.

trust XX:XX:XX:XX:XX:XX

Note: Replace “XX:XX:XX:XX:XX:XX” with your phone’s Bluetooth device address.

2-8. Exit from BlueZ command line interface.

exit

 

3. Audio Routing
3-1. Forward audio from the phone to Raspberry Pi’s output.

bluealsa-aplay 00:00:00:00:00:00

 

4. Verify
4-1. Launch a media player and play some music on your phone. You should be able to hear the music from Raspberry Pi’s speaker.

 

Troubleshoot : Sound Cutting Out
Raspberry Pi’s (3 / 3+ / Zero W) on-board Bluetooth+WiFi combo chip has an issue and the Bluetooth sound cuts out when WiFi is enabled [3]. If you don’t need WiFi, you can just disable the WiFi interface to avoid the sound cutting issue.

sudo ip link set wlan0 down

If you need WiFi, one of the workarounds is to disable the on-board Bluetooth and use a Bluetooth dongle (e.g. Plugable BLE Adapter, Panda Bluetooth 4.0 Adapter) instead. Below is the steps to do that.

1. Open “/etc/modprobe.d/raspi-blacklist.conf”.

sudo nano /etc/modprobe.d/raspi-blacklist.conf

2. Add lines below and save.

blacklist btbcm
blacklist hci_uart

3. Connect Bluetooth dongle on Raspberry Pi’s USB port.

4. Restart the Raspberry Pi.

sudo reboot

 

What’s Next?
If you are interested in controlling Bluetooth audio and showing the song information, please check this out.

 

References
[1] bluealsa: Couldn’t initialize controller thread: Bad file descriptor #149
[2] Service Discovery – Bluetooth SIG
[3] Pi3 bluetooth audio stutters with Wifi enabled

 

 

Enabling Equalizer on Handsfree Sending Audio with ALSA equal plugin


This post shows steps to enable equalizer on Bluetooth handsfree (HFP) sending audio by using ALSA equal plugin.

 

Contents
– Prerequisites
– Steps
1. Equalizer Plugin Installation
2. ALSA Configuration
3. PulseAudio Configuration
4. Connect your phone and make a call
5. Adjust equalizer
– Reference

 

Prerequisites
Bluetooth Handsfree profile needs to be enabled first. If it’s not done yet, please refer this post.

 

Steps
1. Equalizer Plugin Installation

sudo apt-get install libasound2-plugin-equal -y

 

2. ALSA Configuration
2-1. Load the kernel module of ALSA loopback device.

sudo modprobe snd-aloop

The following command enables the module at boot time,

echo "snd-aloop" | sudo tee -a /etc/modules

2-2. Create .asoundrc.

nano ~/.asoundrc

2-3. Add the lines below, save and close the file.

ctl.equal {
  type equal
}

pcm.plugequal {
  type equal
  slave.pcm "plughw:Loopback,0,0"
}

pcm.equal {
  type plug
  slave.pcm plugequal
}

1-4. Reload .asoundrc by entering the command below:

alsactl kill rescan

 

3. PulseAudio Configuration
3-1. Launch PulseAudio if it’s not running yet.

pulseaudio --start

3-2. Add modules and set default source as below:

pactl load-module module-alsa-sink device=equal
pactl load-module module-loopback sink=alsa_output.equal
pactl load-module module-alsa-source device=plughw:Loopback,1,0
pactl set-default-source alsa_input.plughw_Loopback_1_0

 

4. Connect your phone and make a call
4-1. Connect your phone via bluetooth by using bluetoothctl.

4-2. Make a call by using your phone, or oFono.

 

5. Adjust equalizer
Now adjust the equalizer by using alsamixer command and listen the audio on the far-end.

alsamixer -D equal

 

Reference
– Redirect an audio stream with aloop
https://blog.getreu.net/projects/snd-aloop-device/body.html

 

 

Accessing SD Card from Linux VirtualBox Guest on Windows Host

This post shows steps to enable SD card access from Linux VirtualBox guest on Windows host. In this post, I use Windows 10 as the host and Ubuntu 18.04 as the guest.

 

Steps
1. Check Device ID for SD Card
1-1. Open Command Prompt (or Windows PowerShell) as administrator (e.g. “Windows” key + “x” and select “Command prompt (admin)”).

1-2. List disc drives using wmic command before inserting SD card to PC.

wmic diskdrive list brief

The output should be like this:

Caption                     DeviceID            Model                       Partitions  Size
SAMSUNG MZFLV256HCHP-000MV  \\.\PHYSICALDRIVE0  SAMSUNG MZFLV256HCHP-000MV  3           256052966400

1-3. Insert an SD card to the PC and run the same command. You should be able to find an additional line, and that shows DeviceID for the SD card. In this example, it’s “\\.\PHYSICALDRIVE1“.

Caption                               DeviceID            Model                                 Partitions  Size
Generic- USB3.0 CRW   -SD USB Device  \\.\PHYSICALDRIVE1  Generic- USB3.0 CRW   -SD USB Device  2           31914086400
SAMSUNG MZFLV256HCHP-000MV            \\.\PHYSICALDRIVE0  SAMSUNG MZFLV256HCHP-000MV            3           256052966400

 

2. Create Virtual Machine Disk file (VMDK) for SD Card
2-1. Move folder to VirtualBox installation folder. In this example, it’s “C:\Program Files\Oracle\VirtualBox\”.

cd "C:\Program Files\Oracle\VirtualBox\"

2-2. Create VMDK file for the SD card device with “VBoxManage.exe”. The last argument is the DeviceID for SD card. (See Step 1-3).

.\VBoxManage.exe internalcommands createrawvmdk -filename C:\sdcard.vmdk -rawdisk \\.\PHYSICALDRIVE1

If everything is fine, the output will be like this, and “sdcard.vmdk” will be created in root folder of C drive.

RAW host disk access VMDK file C:\sdcard.vmdk created successfully.

 

3. Attach SD card VMDK to VM
3-1. Launch VirtualBox as administrator.

3-2. Navigate to “Settint” > “Strage”.

3-3. Click on “Controller: SATA”.

3-4. Check “Use Host I/O Cache” check box.

3-5. Then, click on “Adds hard disk” icon.

3-6. Select “Choose Existing Disk”.

3-7. Navigate to C drive and select “sdcard.vmdk” file which is created in Step 2-2.

 

4. Verify
4-1. Launch the VM and verify. In my case it appeared as “/dev/sdb” as below (Raspbian OS image in 32GB SD card).

$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 29.8 GiB, 32010928128 bytes, 62521344 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xb1d773dc

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        8192    96453    88262 43.1M  c W95 FAT32 (LBA)
/dev/sdb2       98304 31116287 31017984 14.8G 83 Linux

 

 

Troubleshoot
I encountered several errors while trying to use SD card. Here are some troubleshooting.

Troubleshoot A
Error : I/O cache encountered an error while updating data in medium “ahci-0-1” (rc=VERR_ACCESS_DENIED)

A-1. If you got the error above during/after you made changes on the SD card, please go back to Step 2 and make sure “Use Host I/O Cache” check box is checked.

 

Troubleshoot B
Error: UUID of the medium does not match the value stored in the media registry.
[1]

B-1. Make sure you are in VirtualBox installation folder and run the command below.

.\VBoxManage.exe list hdds

You will be able to find the UUID in the error details.

UUID:           5b951504-358d-4a6e-bb8f-61be3ec32367
Parent UUID:    base
State:          inaccessible
Type:           normal (base)
Location:       C:\Users\max\Desktop\sdcard.vmdk
Storage format: VMDK
Capacity:       0 MBytes
Encryption:     disabled

B-2. Remove the disk with command below by specifying the UUID.

.\VBoxManage.exe closemedium disk 5b951504-358d-4a6e-bb8f-61be3ec32367

 

 

References
[1] Virtual Box UUID {07c3…} does not match the value {2c1b…} stored in the media registry – stack overflow

 

 

Using Your Phone as Touch Screen Display for Raspberry Pi

Got a Raspberry Pi but don’t have a display, keyboard, or mouse? You can go with headless setup, or use your phone as a touch screen display (with onscreen keyboard).

This post shows steps to connect Raspberry Pi with your phone wirelessly and use it as a touch screen display by using Remote Desktop Protocol [1].

Here is the list of contents of this post.

Contents
– Assumptions
– Steps
1. Remote Desktop Server Setup on Raspberry Pi
2. Remote Desktop Client Setup on Your Phone
– Reference

 

 

Assumptions
Here are some assumptions before start. In parentheses, it shows my environment for reference.

  • Raspbian Desktop runs on Raspberry Pi (Raspbian 2018 March on Raspberry Pi 3+)
  • iOS or Android device (Android 7.0)
  • Raspberry Pi and phone is connected to same network (private Wi-Fi network: 192.168.0.0/24)
  • SSH enabled on Raspberry Pi

 

 

Steps
1. Remote Desktop Server Setup on Raspberry Pi
xrdp [2] is a remote desktop server which is capable of accepting connections from Microsoft Remote Desktop.

1-1. Update the package list.

sudo apt-get update

1-2. Install xrdp.

sudo apt-get install xrdp -y

 

2. Remote Desktop Client Setup on Your Phone
For client side, we’ll use Microsoft Remote Desktop app which is available for both iPhone and Android. I used Android for below example but the steps are very similar for iPhone.

2-1. Install Microsoft Remote Desktop from the app store.

2-2. Launch the app, then read and accept the terms of the agreements.

2-3. Touch “+” button, then select “Desktop”.

(* I changed the display orientation to portrait mode.)

2-4. Enter Raspberry Pi’s IP address, user name and password.

IP address can be found by using ip command on Raspberry Pi.

ip address show wlan0

The result should look like this:

$ ip address show wlan0
...
inet 192.168.0.1/24 brd 192.168.0.255 scope global wlan0
...

 

2-5. Tap on newly created Desktop.

 

2-6. If everything is fine, Raspberry Pi’s desktop will be displayed on your phone!

 

Microsoft Remote Desktop app provides an onscreen keyboard.

 

Also, you can select “Touch” or “Mouse Pointer” in setting menu.

 

 

References
[1] Remote Desktop Protocol – Wikipedia
[2] xrdp: Open Source Remote Desktop Protocol Server
[3] How to Use an Android Tablet as a Screen for Raspberry Pi