Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Instruction Guide Moode 8: Add bluetooth codec support for AAC, aptX & aptX-HD to bluez-alsa package
#21
I was interested enough to trace the origin of the libopenaptx package in Debian / Raspberry Pi OS. It turns out not to be the code in Arkq's  repo. Instead, it's code developed by GitHub user pali https://github.com/pali/libopenaptx (Full disclosure: I looked only at the file headers).

I guess if the library is in the general distros it's fair game for us since we would just be linking to it. Bummer that the libopenaptx package has no Debian package maintainer; he backed out over a year ago. I note that pali hasn't touched his repo in 2 years either. The rev in the Debian repo (0.2.0-5) is slightly behind the latest rev in pali's repo (0.2.1). Judging from the comment on the 0.2.1 release, there's been a kerfuffle over who's been doing what with the code. I don't think this affects us since Debian stands between.

As I said before, it's not a feature I'm likely to use, but if you decide to go forward I'm happy to try to test as best I can with the gear at my disposal.

Regards,
Kent
Reply
#22
(05-26-2023, 01:36 PM)TheOldPresbyope Wrote: I was interested enough to trace the origin of the libopenaptx package in Debian / Raspberry Pi OS. It turns out not to be the code in Arkq's  repo. Instead, it's code developed by GitHub user pali https://github.com/pali/libopenaptx (Full disclosure: I looked only at the file headers).

I guess if the library is in the general distros it's fair game for us since we would just be linking to it. Bummer that the libopenaptx package has no Debian package maintainer; he backed out over a year ago. I note that pali hasn't touched his repo in 2 years either. The rev in the Debian repo (0.2.0-5) is slightly behind the latest rev in pali's repo (0.2.1). Judging from the comment on the 0.2.1 release, there's been a kerfuffle over who's been doing what with the code. I don't think this affects us since Debian stands between.

As I said before, it's not a feature I'm likely to use, but if you decide to go forward I'm happy to try to test as best I can with the gear at my disposal.

Regards,
Kent

I've reviewed that as well.

I think I'll make a test release so we can see what works and what doesn't. 

Isn't there a bluez-alsa command that shows which codec is being used?
Enjoy the Music!
moodeaudio.org | Mastodon Feed | GitHub
Reply
#23
(05-26-2023, 01:53 PM)Tim Curtis Wrote:
(05-26-2023, 01:36 PM)TheOldPresbyope Wrote: I was interested enough to trace the origin of the libopenaptx package in Debian / Raspberry Pi OS. It turns out not to be the code in Arkq's  repo. Instead, it's code developed by GitHub user pali https://github.com/pali/libopenaptx (Full disclosure: I looked only at the file headers).
...
Regards,
Kent

I've reviewed that as well.

I think I'll make a test release so we can see what works and what doesn't. 

Isn't there a bluez-alsa command that shows which codec is being used?

@jenzd may well know a better command. Here's one I gleaned from the bluez-alsa issues long ago which queries the dbus

gdbus call --system -d org.bluealsa -o /org/bluealsa -m org.bluealsa.Manager1.GetPCMs

So, for example, with a stock moOde player paired and connected to a JBL Flip2 BT speaker, I get

Code:
pi@moode:~ $ gdbus call --system -d org.bluealsa -o /org/bluealsa -m org.bluealsa.Manager1.GetPCMs
({objectpath '/org/bluealsa/hci0/dev_00_1D_DF_AA_27_37/a2dpsrc/sink': {'Device': <objectpath '/org/bluez/hci0/dev_00_1D_DF_AA_27_37'>, 'Transport': <'A2DP-source'>, 'Mode': <'sink'>, 'Format': <uint16 33296>, 'Channels': <byte 0x02>, 'Sampling': <uint32 48000>, 'Codec': <'SBC'>, 'Delay': <uint16 150>, 'SoftVolume': <true>, 'Volume': <uint16 32639>}},)

whereas, with the same player connected as BT speaker for my iPad

Code:
pi@moode:~ $ gdbus call --system -d org.bluealsa -o /org/bluealsa -m org.bluealsa.Manager1.GetPCMs
({objectpath '/org/bluealsa/hci0/dev_D0_D2_B0_DB_13_78/a2dpsnk/source': {'Device': <objectpath '/org/bluez/hci0/dev_D0_D2_B0_DB_13_78'>, 'Transport': <'A2DP-sink'>, 'Mode': <'source'>, 'Format': <uint16 33296>, 'Channels': <byte 0x02>, 'Sampling': <uint32 44100>, 'Codec': <'SBC'>, 'Delay': <uint16 150>, 'SoftVolume': <true>, 'Volume': <uint16 1799>}},)


In both cases here the SBC codec is being used.

As Jens said, bluealsa --help tells us which codecs are available and in this stock moOde player they are just

Code:
pi@moode:~ $ bluealsa --help

Available BT profiles:
 - a2dp-source    Advanced Audio Source (LDAC, SBC)
 - a2dp-sink    Advanced Audio Sink (SBC)
...

Since this is stock moOde, bluealsa-cli isn't present.

Regards,
Kent
Reply
#24
As a follow-up to my previous post, I followed Jens's instructions to add the extra codecs. Then I paired and connected to my Sennheiser 4.50BT headphones (which contain SBC and aptX codecs)

Code:
# check that the codecs are available
pi@moode:~ $ bluealsa-cli status
Service: org.bluealsa
Version: v4.0.0
Adapters: hci0
Profiles:
 A2DP-source : SBC AAC aptX aptX-HD LDAC
 A2DP-sink   : SBC AAC aptX aptX-HD

# check UUID of my headphones
pi@moode:~ $ bluetoothctl info
Device 00:16:94:29:7F:10 (public)
    Name: HD 4.50BTNC
    Alias: HD 4.50BTNC
    Class: 0x00240404
    Icon: audio-card
    Paired: yes
    Trusted: yes
    Blocked: no
    Connected: yes
    ...

# look to see which codec was negotiated in the pairing process (noting it's the right UUID)
pi@moode:~ $ gdbus call --system -d org.bluealsa -o /org/bluealsa -m org.bluealsa.Manager1.GetPCMs
({objectpath '/org/bluealsa/hci0/dev_00_16_94_29_7F_10/a2dpsrc/sink': {'Device': <objectpath '/org/bluez/hci0/dev_00_16_94_29_7F_10'>, 'Sequence': <uint32 0>, 'Transport': <'A2DP-source'>, 'Mode': <'sink'>, 'Format': <uint16 33296>, 'Channels': <byte 0x02>, 'Sampling': <uint32 48000>, 'Codec': <'aptX'>, 'Delay': <uint16 150>, 'SoftVolume': <true>, 'Volume': <uint16 32639>}},)

Yay. The "higher" quality aptX codec was selected during the negotiation rather than the SBC.

However, moOde seems to think it's connected to a Bluetooth source rather than sink because the webUI is overlaid with "Bluetooth Active: HD 4.50BTNC" and the Bluetooth control button?!? ETA: self-induced error. BT's working fine.

Regards,
Kent
Reply
#25
(05-26-2023, 10:07 PM)TheOldPresbyope Wrote: As a follow-up to my previous post, I followed Jens's instructions to add the extra codecs. Then I paired and connected to my Sennheiser 4.50BT headphones (which contain SBC and aptX codecs)

Code:
# check that the codecs are available
pi@moode:~ $ bluealsa-cli status
Service: org.bluealsa
Version: v4.0.0
Adapters: hci0
Profiles:
 A2DP-source : SBC AAC aptX aptX-HD LDAC
 A2DP-sink   : SBC AAC aptX aptX-HD

# check UUID of my headphones
pi@moode:~ $ bluetoothctl info
Device 00:16:94:29:7F:10 (public)
    Name: HD 4.50BTNC
    Alias: HD 4.50BTNC
    Class: 0x00240404
    Icon: audio-card
    Paired: yes
    Trusted: yes
    Blocked: no
    Connected: yes
    ...

# look to see which codec was negotiated in the pairing process (noting it's the right UUID)
pi@moode:~ $ gdbus call --system -d org.bluealsa -o /org/bluealsa -m org.bluealsa.Manager1.GetPCMs
({objectpath '/org/bluealsa/hci0/dev_00_16_94_29_7F_10/a2dpsrc/sink': {'Device': <objectpath '/org/bluez/hci0/dev_00_16_94_29_7F_10'>, 'Sequence': <uint32 0>, 'Transport': <'A2DP-source'>, 'Mode': <'sink'>, 'Format': <uint16 33296>, 'Channels': <byte 0x02>, 'Sampling': <uint32 48000>, 'Codec': <'aptX'>, 'Delay': <uint16 150>, 'SoftVolume': <true>, 'Volume': <uint16 32639>}},)

Yay. The "higher" quality aptX codec was selected during the negotiation rather than the SBC.

However, moOde seems to think it's connected to a Bluetooth source rather than sink because the webUI is overlaid with "Bluetooth Active: HD 4.50BTNC" and the Bluetooth control button?!? 


Regards,
Kent

Thats strange. Check the audio output setting in Bluetooth Config.
Enjoy the Music!
moodeaudio.org | Mastodon Feed | GitHub
Reply
#26
(05-26-2023, 10:20 PM)Tim Curtis Wrote:
(05-26-2023, 10:07 PM)TheOldPresbyope Wrote: As a follow-up to my previous post, I followed Jens's instructions to add the extra codecs. Then I paired and connected to my Sennheiser 4.50BT headphones (which contain SBC and aptX codecs)

Code:
# check that the codecs are available
pi@moode:~ $ bluealsa-cli status
Service: org.bluealsa
Version: v4.0.0
Adapters: hci0
Profiles:
 A2DP-source : SBC AAC aptX aptX-HD LDAC
 A2DP-sink   : SBC AAC aptX aptX-HD

# check UUID of my headphones
pi@moode:~ $ bluetoothctl info
Device 00:16:94:29:7F:10 (public)
    Name: HD 4.50BTNC
    Alias: HD 4.50BTNC
    Class: 0x00240404
    Icon: audio-card
    Paired: yes
    Trusted: yes
    Blocked: no
    Connected: yes
    ...

# look to see which codec was negotiated in the pairing process (noting it's the right UUID)
pi@moode:~ $ gdbus call --system -d org.bluealsa -o /org/bluealsa -m org.bluealsa.Manager1.GetPCMs
({objectpath '/org/bluealsa/hci0/dev_00_16_94_29_7F_10/a2dpsrc/sink': {'Device': <objectpath '/org/bluez/hci0/dev_00_16_94_29_7F_10'>, 'Sequence': <uint32 0>, 'Transport': <'A2DP-source'>, 'Mode': <'sink'>, 'Format': <uint16 33296>, 'Channels': <byte 0x02>, 'Sampling': <uint32 48000>, 'Codec': <'aptX'>, 'Delay': <uint16 150>, 'SoftVolume': <true>, 'Volume': <uint16 32639>}},)

Yay. The "higher" quality aptX codec was selected during the negotiation rather than the SBC.

However, moOde seems to think it's connected to a Bluetooth source rather than sink because the webUI is overlaid with "Bluetooth Active: HD 4.50BTNC" and the Bluetooth control button?!? 


Regards,
Kent

Thats strange. Check the audio output setting in Bluetooth Config.

Well, that's embarrassing. Guess who forgot to switch to Bluetooth speaker output Rolleyes 

All's well. Now to jury-rig some BT sources with advanced codecs.

Regards,
Kent
Reply
#27
So I've followed up with several tests:

Let's refer to the moOde 8.3.2 player I just built on and played with yesterday as the "the receiver."

1. copied the new .deb files over to another moOde 8.3.2 player and installed. Changing the settings in bluealsa.service on this new sender to start bluealsa with various codecs available and then connecting to the receiver I can see the results in the codec negotiated. Nice.

2. using an iPhone SE I can connect to the receiver with the AAC codec.

3. using my Google Pixel 6a, I confidently expected to connect to the receiver with the SBC codec. Surprise, the connection was with the aptX-HD codec. I didn't know until this moment that recent Pixel phones support an HD Audio mode using this codec! Sweet.

Well done, @jenzd !

Regards,
Kent
Reply
#28
Hi Kent,

Thanks for your testing efforts. This coincides with the results I got with my Sony Xperia XZ1 Compact. After adding the codec support, it automatically connects and streams to Moode via aptX-HD. By selectively deactivating the available codecs I could moreover test that streaming successfully works with aptX and AAC as well. (The order by which the Sony automatically selects its codec apparently is aptX-HD > aptX > AAC > SBC.)

By the way, an alternative command to see the codec which is in use (with better readable output) is:

Code:
bluealsa-cli --verbose list-pcms

Cheers
Jens
Reply
#29
Oh my, yes.

My last test case using my Pixel 6a as source


Code:
pi@moode:~ $ bluealsa-cli --verbose list-pcms
/org/bluealsa/hci0/dev_0C_C4_13_D6_3A_36/a2dpsnk/source
Device: /org/bluez/hci0/dev_0C_C4_13_D6_3A_36
Sequence: 9
Transport: A2DP-sink
Mode: source
Format: S24_LE
Channels: 2
Sampling: 48000 Hz
Available codecs: SBC AAC aptX aptX-HD
Selected codec: aptX-HD
Delay: 15.0 ms
SoftVolume: Y
Volume: L: 127 R: 127
Muted: L: N R: N


Regards,
Kent
Reply
#30
(05-16-2023, 09:16 PM)jenzd Wrote: Moode 8 already has built-in support for the bluetooth codecs SBC (receive & playback) and LDAC (playback only, but inactive by default) which is provided by the bluez-alsa package. At the time of writing Moode version 8.3.2 includes bluez-alsa in version 4.0.0.

The following guide shows how to additionally include support for the bluetooth codecs AAC, aptX and aptX-HD (each for receive & playback) by building a modified version of the included bluez-alsa package. Note that this guide is based on section 5 "Build packages" of Moode's "Master development guide". It follows a slightly different route than the previous guide by scblock which showed how to build & install everything directly from the source code without building a package (see https://moodeaudio.org/forum/showthread.php?tid=3683). In the current approach, the modification is finally installed as a normal package instead. There should be no side effects on future in-place updates of Moode other than that the modified package might be upgraded to a newer (unmodified) bluez-alsa version.

The build process can be performed directly in Moode using ssh access, e.g. on a fresh installation.


1) After logging into Moode via ssh, we start with installing some additional building tools and dependencies:

Code:
# update the package repository
sudo apt update

# install build tools for deb packages
sudo apt install build-essential fakeroot devscripts

# install missing build dependencies for the bluez-alsa package
sudo apt install dh-exec libasound2-dev libbluetooth-dev libsbc-dev pandoc python3-docutils

# install codec libraries and development files for LDAC, AAC and aptx
sudo apt install libldacbt-abr2 libldacbt-abr-dev libldacbt-enc2 libldacbt-enc-dev libfdk-aac2 libfdk-aac-dev libopenaptx0 libopenaptx-dev


2) Then we download Moode's source package for bluez-alsa to the current directory and change into its configuration directory:

Code:
# download bluez-alsa source package
apt source bluez-alsa

# change to its configuration directory
cd bluez-alsa-4.0.0/debian


3) Two files need to be modified here (e.g. using "nano <filename>"):
  • File "rules":

    Add flags for the additional codecs in the CONFIGURE_FLAGS section, so that it reads

Code:
CONFIGURE_FLAGS := \
       --enable-ldac \
       --enable-ofono \
       --enable-manpages \
       --enable-debug \
       --enable-cli \
       --enable-aac \
       --enable-aptx \
       --enable-aptx-hd \
       --with-libopenaptx


instead of the default parameters


Code:
CONFIGURE_FLAGS := \
       --enable-ldac \
       --enable-ofono \
       --enable-manpages \
       --enable-debug \
       --enable-cli

  • File "changelog":

    Edit the version of the package in the first line of the file to distinguish it from the original version provided by moode, e.g. by adding "+codecs" at the end of the version string, resulting in "4.0.0-2moode1+codecs" instead of the default "4.0.0-2moode1".

3) Build the modified package:

Code:
# change back to the directory bluez-alsa-4.0.0/
cd ..

# build the modified package
dpkg-buildpackage -us -uc

# change back to the parent directory
cd ..

If the build process was successful, the parent directory of "bluez-alsa-4.0.0" should contain a set of modified deb packages.


4) Install the modified deb packages to replace the preinstalled versions of "bluez-alsa-utils" and "libasound2-plugin-bluez":

Code:
# install modified packages from local source
sudo apt install ./bluez-alsa-utils_4.0.0-2moode1+codecs_arm64.deb ./libasound2-plugin-bluez_4.0.0-2moode1+codecs_arm64.deb


5) Activate aptX, aptX-HD and LDAC in the config file of the bluealsa service (AAC is already activated by default):

Code:
# Edit the config file of the bluealsa service
sudo nano /etc/systemd/system/bluealsa.service

to change the line

Code:
ExecStart=/usr/bin/bluealsa -p a2dp-source -p a2dp-sink

to

Code:
ExecStart=/usr/bin/bluealsa -p a2dp-source -p a2dp-sink -c aptx -c aptx-hd -c ldac

Note: The config file /etc/systemd/system/bluealsa.service belongs to the package moode-player, so it will be overwritten with each in-place update of Moode, This means that step 5 unfortunately needs to be repeated after each Moode update.


6) Finally reboot:

Code:
sudo reboot


The additional codecs should now be used automatically when a supported bluetooth device is connected.


Final note: In case you want to remove the modification again, just undo the changes to the file /etc/systemd/system/bluealsa.service and reinstall the unmodified versions provided by Moode from the package repository (currently version 4.0.0-2moode1).

Code:
sudo apt install bluez-alsa-utils=4.0.0-2moode1 libasound2-plugin-bluez=4.0.0-2moode1

Starting with Moode 8.3.3, aptX and aptX-HD support is now officially built into the included bluez-alsa package 4.1.0-2moode2 (and activated by default). There is no need to follow the above guide for these codecs any longer.

Support for AAC (e.g. commonly used by Apple devices) is still missing, however, probably because the underlying library libfdk-aac2 is part of the non-free section of Debian's package repository. So the guide still (but only) makes sense in case you want to build a bluez-alsa package with additional AAC support.

This post conserves the original guide for Moode 8.3.2. I will update the first page to reflect the new situation for Moode 8.3.3.

Regards
Jens
Reply


Forum Jump: