mikebrady / shairport-sync

AirPlay and AirPlay 2 audio player
Other
7.17k stars 569 forks source link

Output to ALSA Bluetooth #200

Closed trickartt closed 4 years ago

trickartt commented 8 years ago

Hello Mike,

I wonder if you could help with a little question I've hit a brick wall on. I'm trying to use my Raspberry Pi as something of an Airplay to Bluetooth bridge - I'm an Airplay household, but I have a Bluetooth speaker I'd like to add to the multi-room setup.

I've got Shairport-Sync running beautifully (thank you for your work on it!) on my Raspberry Pi via a cable, and have Bluetooth audio working via ALSA on the Pi too, but I can't seem to join the two ends together.

I can play music files from the Pi using the command mplayer -ao alsa:device=bluetooth audiofile.mp3, and to allow that to happen, my .asoundrc file looks as so:

pcm.!default {
        type hw
        card 0
}

pcm.bluetooth {
        type bluetooth
        device 00:xx:xx:xx:xx:xx // my actual mac address is here, but you know
        profile "auto"
}

ctl.!default {
        type hw
        card 0
}

I figured that changing the output_device to "bluetooth" and uncommenting the line in the shairport-sync.conf file should make it work, but I get nothing.

Is there anything I'm doing wrong for this to not work? I feel like I'm missing something really obvious (I admit I'm a complete newbie at this!) that's stopping me from getting anywhere.

Thanks for looking, if you have any ideas they would be appreciated!

mikebrady commented 8 years ago

Thanks for the interesting question. I have not tried Bluetooth and I'm away from my machines for another 10 days or so.

Is there anything in the log file? It might help to turn the log verbosity to 1.

trickartt commented 8 years ago

No worries, thank you for replying!

I turned the log verbosity to 1. outputting via the default (as in over a cable) works and doesn't log much. Setting the output_device to bluetooth fills up the log file with a lot of

Jan 12 16:55:16 raspberrypi shairport-sync[3095]: Lost sync with source for 4 consecutive packets -- flushing and resyncing. Error: -5078.

The error number changes on each line (apparently randomly), between approx. 5000-5100 and 5400-5500.

cre8dojo commented 8 years ago

-----------------------------

#

bluetooth speaker setup

#

-----------------------------

sudo apt-get install libdbus-1-dev sudo apt-get install libglib2.0-dev

apt-cache search libasound sudo apt-get install libasound2-dev

wget http://www.kernel.org/pub/linux/bluetooth/bluez-4.101.tar.xz tar -xvf bluez-4.101.tar.xz cd bluez-4.101 ./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --libexecdir=/lib make sudo make install

sudo cp /home/pi/bluez-4.101/test/simple-agent /usr/bin/bluez-simple-agent sudo cp /home/pi/bluez-4.101/test/test-device /usr/bin/bluez-test-device sudo cp /home/pi/bluez-4.101/test/test-audio /usr/bin/bluez-test-audio sudo cp /home/pi/bluez-4.101/audio/audio.conf /etc/bluetooth/

sudo apt-get install python-dbus sudo apt-get install python-gobject sudo cp /usr/lib/alsa-lib/libasound_module_pcm_bluetooth.so /usr/lib/arm-linux-gnueabihf/alsa-lib/

sudo nano /etc/bluetooth/audio.conf

add in [General] section:

Enable=Sink,Socket

uncomment [A2DP] section header and add:

Maxconnections=4

sudo systemctl enable bluetooth

sudo reboot

hcitool scan

example devices found from scan

FC:58:FA:56:4C:AC Jam Classic

FC:58:FA:07:9B:F7 CANZ 808

sudo nano /etc/asound.conf

add:

pcm.bt_canz { type plug slave { pcm { type bluetooth device XX:XX:XX:XX:XX:XX profile "auto" } } hint { show on description "BT CANZ Speaker" } } ctl.bt_canz { type bluetooth }

sudo bluez-simple-agent ctrl+c

sudo bluez-simple-agent hci0 XX:XX:XX:XX:XX:XX sudo bluez-test-audio connect XX:XX:XX:XX:XX:XX

aplay -L speaker-test -c 2 -D bt_canz

sudo bluez-test-audio disconnect XX:XX:XX:XX:XX:XX

sudo reboot

in /etc/shairport-sync.conf specify output_device = bt_canz from example above.

I had to reduce ALSA audio_backend_buffer_desired_length = 3192 in /etc/shairport-sync.conf

trickartt commented 8 years ago

That's amazing @cre8dojo, it works! Thank you! I'd love to know what specifically in your code causes it to work, as most of it seemed like what I had done myself?

Now I'm trying to adjust it so it stays in sync.

I've adjusted audio_backend_latency_offset, but once I bring it beyond -2000 it really struggles to even play music. If I keep it below -2000 it'll take a few seconds to figure itself out, then it'll play consistently, but it'll drift pretty quickly. Is there another setting in the config I should be adjusting to make this more robust, or is this drifting just a reminder of why I always preferred AirPlay over bluetooth?

Thank you both for your work on this!

yfgeek commented 8 years ago

@cre8dojo It's amazing. Thank you!

biogerm commented 8 years ago

@cre8dojo It seems rPi Jessie preinstalled Pi 3 with Bluez 5. I tried to reproduce your steps to over write bluez 5 with bluez 4 files. But it cause shairport to report

ALSA lib dlmisc.c:252:(snd1_dlobj_cache_get) Cannot open shared library /usr/lib/arm-linux-gnueabihf/alsa-lib/libasound_module_pcm_plut.so

Don't know what else I can do. Somehow Alsa on my Pi 3 doesn't have this plut.so file...

francoismartin commented 7 years ago

Hi @biogerm

Do you have found a issue to your problem ? Because I have the same problem.

Thanks

biogerm commented 7 years ago

@francoismartin Unfortunately not. I am considering downgrade the FW to a lower version than Jessie where ALSA support was still there. I think it might work but since there might not have BT/Wifi support, you might see problem on that side.

francoismartin commented 7 years ago

@biogerm Thanks, so you have installed wheezy with bluez4 ?

biogerm commented 7 years ago

I wanted to but haven't got time to make it happen. francoismartin notifications@github.com于2016年10月26日 周三下午1:49写道:

@biogerm https://github.com/biogerm Thanks, so you have installed wheezy with bluez4 ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mikebrady/shairport-sync/issues/200#issuecomment-256324296, or mute the thread https://github.com/notifications/unsubscribe-auth/AFUfFKQC_D-VQ52z9J9l6BnKYjYEWTBNks5q3z4rgaJpZM4HDIEC .

DimaVIII commented 7 years ago

@biogerm && @francoismartin It's working with the RPi 3 and Raspbian Jessie, just installed as @cre8dojo described! (Biiiiiig THX).

I bought the Bluesound Pulse Flex and it was really almost useless with app from this company...

@cre8dojo Would this setup also work with a dongle like this? http://www.avantree.com/product-bluetooth-usb-audio-adapter-avantree-dg50-828.html

The dongle has Linux support (http://support.avantree.com/hc/en-us/articles/115000119103-How-to-use-Leaf-Bluetooth-audio-transmitter-with-Linux-) And as I understand this dongle will appear as a sound device under "aplay -L" and I can just add it as output_device in /etc/shairport-sync.conf?

My speaker support AptX and you can really hear the difference between bluetooth and AptX.

My Config with Raspberry Pi 3 and Jessie (May help someone)

sudo apt-get install build-essential git xmltoman

sudo apt-get install autoconf automake libtool libdaemon-dev libasound2-dev libpopt-dev libconfig-dev

sudo apt-get install avahi-daemon libavahi-client-dev

sudo apt-get install libssl-dev libsoxr-dev

Apple ALAC decoder

https://github.com/mikebrady/alac

cd src/ git clone https://github.com/mikebrady/alac.git cd alac

autoreconf -fi

./configure

make

sudo make install

sudo ldconfig -v

pkg-config --list-all

pkg-config alac

Shairport (AirPlay)

git clone https://github.com/mikebrady/shairport-sync.git

cd shairport-sync/

autoreconf -i -f

./configure --sysconfdir=/etc --with-alsa --with-soxr --with-stdout --with-pipe --with-avahi --with-ssl=openssl --with-metadata --with-systemd --with-apple-alac

make

sudo make install

getent group shairport-sync &>/dev/null || sudo groupadd -r shairport-sync >/dev/null

getent passwd shairport-sync &> /dev/null || sudo useradd -r -M -g shairport-sync -s /usr/bin/nologin -G audio shairport-sync >/dev/null

sudo usermod -a -G lp shairport-sync

sudo make install

sudo systemctl enable shairport-sync

Start

sudo systemctl start shairport-sync sudo systemctl status shairport-sync sudo systemctl restart shairport-sync

Install Bluetooth Speaker

sudo apt-get install libdbus-1-dev

sudo apt-get install libglib2.0-dev

apt-cache search libasound

sudo apt-get install libasound2-dev

sudo apt-get install python-dbus

sudo apt-get install python-gobject

cd src/

wget http://www.kernel.org/pub/linux/bluetooth/bluez-4.101.tar.xz

tar -xvf bluez-4.101.tar.xz

cd bluez-4.101

./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --libexecdir=/lib

make

sudo make install

sudo cp /home/pi/src/bluez-4.101/test/simple-agent /usr/bin/bluez-simple-agent

sudo cp /home/pi/src/bluez-4.101/test/test-device /usr/bin/bluez-test-device

sudo cp /home/pi/src/bluez-4.101/test/test-audio /usr/bin/bluez-test-audio

sudo cp /home/pi/src/bluez-4.101/audio/audio.conf /etc/bluetooth/

sudo cp /usr/lib/alsa-lib/libasound_module_pcm_bluetooth.so /usr/lib/arm-linux-gnueabihf/alsa-lib/

sudo nano /etc/bluetooth/audio.conf //ADD: Enable=Sink,Socket

// #uncomment [A2DP] section header and add: Maxconnections=4

sudo systemctl enable bluetooth

sudo reboot

Service

sudo systemctl restart bluetooth sudo systemctl status bluetooth

hcitool scan (XX:XX:XX:XX:XX:XX = Speaker)

sudo nano /etc/asound.conf // ADD:

pcm.bt_bluesound {
        type plug
        slave {
                pcm {
                        type bluetooth
                        device XX:XX:XX:XX:XX:XX
                        profile "auto"
                }
        }
        hint {
                show on
                description "BT Bluesound Speaker"
        }
}
ctl.bt_bluesound {
        type bluetooth
}

sudo bluez-simple-agent exit> ctrl+c

sudo bluez-simple-agent hci0 XX:XX:XX:XX:XX:XX

sudo bluez-test-audio connect XX:XX:XX:XX:XX:XX

aplay -L

speaker-test -c 2 -D bt_bluesound

sudo bluez-test-audio disconnect XX:XX:XX:XX:XX:XX

sudo reboot

sudo nano /etc/shairport-sync.conf

// Change output_device = "bt_bluesound";

rfkill blocked Bluetooth issue

rfkill list

// IF 1: hci0: Bluetooth Soft blocked: yes Hard blocked: no // Run sudo rfkill unblock 1:

biogerm commented 7 years ago

@DimaVIII Thanks a lot for the command list. I tried it with a fresh installation of Jessie and it works on some of my devices. I have a range of BT devices. So far, only one of them works with bluez 4... which is a in-ear headset. The speaker I want to use doesn't work fully, lagging a lot while playing music. I tried profile a2dp and auto but it doesn't seem to have difference.

DimaVIII commented 7 years ago

@biogerm May your problem is the Range? Try to put them very close together. I also made the experience, that other BT devices in range interrupting each other (like my apple mouse) may try to turn off all other devices.

biogerm commented 7 years ago

@DimaVIII shouldn't be the range, they are very close to each other. besides, it works fine in X-windows

wrumble commented 7 years ago

@DimaVIII , i just used your instructions to get the bluetooth working with my google aiy voice kit because i was getting this error: ALSA lib dlmisc.c:252:(snd1_dlobj_cache_get) Cannot open shared library /usr/lib/arm-linux-gnueabihf/alsa-lib/libasound_module_pcm_bluetooth.so it seems to have fixed it but disabled the mic that was attached giving me this error: ALSA lib pcm_dsnoop.c:618:(snd_pcm_dsnoop_open) unable to open slave arecord: main:722: audio open error: No such file or directory [2017-05-22 05:05:03,111] ERROR:audio:Microphone recorder died unexpectedly, aborting... i dont suppose you know how i could fix this? Sorry for hijacking the comments.

wrumble commented 7 years ago

Hi @DimaVIII, me again. When i run this command speaker-test -c 2 -D bt_bluesound i get the following error ` speaker-test 1.0.28

Playback device is btspeaker Stream parameters are 48000Hz, S16_LE, 2 channels Using 16 octaves of pink noise bt_audio_service_open: connect() failed: Connection refused (111) Playback open error: -111,Connection refused` Any ideas why?

oscar-c commented 6 years ago

I found a much simpler way so it will work on my pi. First run sudo bluetoothctl, then follow the tutorial to pair your bluetooth speaker.

Then add following to /etc/asound.conf

pcm.bt_speaker {
        type plug
        slave {
                pcm {
                        type bluealsa
                        device [MAC address of your BT device]
                        profile "a2dp"
                }
        }
        hint {
                show on
                description "BT  Speaker"
        }
}

Then go to /etc/shairport-sync.conf and change the value of output_device to bt_speaker. Finally do sudo systemctl restart shairport-sync to apply the change. That's it. No reboot, no extra package needed.

P/s: You can change the name of the airplay server to "%H → [BT Speaker Name]", So on iOS it will look like this: Airplay Name

biogerm commented 6 years ago

@oscar-c I tried the steps you mentioned but it doesn't seem to work. Here are the steps that I tried:

  1. Flashed the latest image to an out-of-box p3b+
  2. In GUI, connected to wifi, paired to a bluetooth headset
  3. Enabled SSH, changed password. Rebooted to CLI and connected to the headset using bluetoothctl command "connect"
  4. cloned shareport-sync repo, ran "autoreconf -i -f"
  5. ran config command "./configure --with-alsa --with-avahi --with-ssl=openssl --with-systemd --with-metadata --sysconfdir=/etc"
  6. make and sudo make install
  7. sudo systemctl enable shairport-sync
  8. create the asound.conf file with content you provided
  9. modify "shairport-sync.conf" file, uncomment the output line and change it to "bt_speaker" with quotation marks
  10. sudo systemctl restart shairport-sync

No sound is coming from the bt headset. Please let me know if I'm missing anything here.

oscar-c commented 6 years ago

@biogerm You can first play some file using aplay command, just to make sure the BT part is working. You can do aplay -D [device name] audiofile.wav

biogerm commented 6 years ago

@oscar-c aplay works as it always did...

davidhq commented 6 years ago

Hi,

I managed to get the sound out of shairport-sync to default device (= bluetooth).

Steps on raspbian stretch with GUI (easier for now with just a few clicks, will try the same later in lite):

This is how the system defined our default device:

$ pwd
/home/pi
$ cat ~/.asoundrc
pcm.!default {
    type plug
    slave.pcm {
        type bluealsa
        device "00:6A:8E:16:CA:26"
        profile "a2dp"
    }
}

ctl.!default {
    type bluealsa
}

But the problem is that in /usr/share/alsa/alsa.conf:

# pre-load the configuration files

@hooks [
        {
                func load
                files [
                        {
                                @func concat
                                strings [
                                        { @func datadir }
                                        "/alsa.conf.d/"
                                ]
                        }
                        "/etc/asound.conf"
                        "~/.asoundrc"
                ]
                errors false
        }

The file is referenced by ~ and since shairport-sync is normally not run by the pi user, underlying alsa stack won't find this configuration file.

I tried running it manually under pi user and it worked - later I'll try putting the absolute reference ("/home/pi/.asoundrc") and rebooting, now I can't because of longer compile process.

In any case, it's possible to do it simply, at least in GUI version of Raspbian. I now see @oscar-c answer how to do it with cli and a bit different setup, I'll test it later. I think there are benefits of leaving shairport on default output device though... hopefully it works when bt pairing changes back and forth.

This is my update and contribution, now for a question:

Audio is stuttering badly, this is in the log:

Player: packets out of sequence: expected: 43526, got: 43536, with ab_read: 43537 and ab_write: 43800.
Player: packets out of sequence: expected: 43572, got: 43582, with ab_read: 43583 and ab_write: 43846.
Player: packets out of sequence: expected: 43618, got: 43628, with ab_read: 43629 and ab_write: 43892.
Player: packets out of sequence: expected: 43664, got: 43674, with ab_read: 43675 and ab_write: 43938.
Player: packets out of sequence: expected: 43710, got: 43720, with ab_read: 43721 and ab_write: 43984.
Player: packets out of sequence: expected: 43756, got: 43766, with ab_read: 43767 and ab_write: 44030.
Player: packets out of sequence: expected: 43802, got: 43812, with ab_read: 43813 and ab_write: 44076.
Player: packets out of sequence: expected: 43848, got: 43858, with ab_read: 43859 and ab_write: 44122.
Player: packets out of sequence: expected: 43894, got: 43904, with ab_read: 43905 and ab_write: 44168.
Player: packets out of sequence: expected: 43940, got: 43950, with ab_read: 43951 and ab_write: 44213.
Player: packets out of sequence: expected: 43986, got: 43996, with ab_read: 43997 and ab_write: 44260.
Player: packets out of sequence: expected: 44032, got: 44042, with ab_read: 44043 and ab_write: 44306.
Player: packets out of sequence: expected: 44078, got: 44088, with ab_read: 44089 and ab_write: 44352.
Player: packets out of sequence: expected: 44124, got: 44134, with ab_read: 44135 and ab_write: 44398.
Player: packets out of sequence: expected: 44170, got: 44180, with ab_read: 44181 and ab_write: 44444.
Player: packets out of sequence: expected: 44216, got: 44226, with ab_read: 44227 and ab_write: 44490.
Player: packets out of sequence: expected: 44262, got: 44272, with ab_read: 44273 and ab_write: 44536.
Player: packets out of sequence: expected: 44308, got: 44318, with ab_read: 44319 and ab_write: 44582.
Player: packets out of sequence: expected: 44354, got: 44364, with ab_read: 44365 and ab_write: 44628.
Player: packets out of sequence: expected: 44400, got: 44410, with ab_read: 44411 and ab_write: 44674.
Player: packets out of sequence: expected: 44446, got: 44456, with ab_read: 44457 and ab_write: 44720.
Player: packets out of sequence: expected: 44492, got: 44502, with ab_read: 44503 and ab_write: 44766.
Player: packets out of sequence: expected: 44538, got: 44548, with ab_read: 44549 and ab_write: 44812.
Player: packets out of sequence: expected: 44584, got: 44594, with ab_read: 44595 and ab_write: 44858.
Player: packets out of sequence: expected: 44630, got: 44640, with ab_read: 44641 and ab_write: 44904.

With system sound it works flawlessly...

Not sure if there is any setting in shairport conf I can adjust? I don't see audio_backend_buffer_desired_length option as @cre8dojo mentions. I'm not sure that would help either...

thank you for help

oscar-c commented 6 years ago

@biogerm If you run systemctl status shairport-sync, does it say anything about not being able to play?

sealustsailing commented 5 years ago

Is this still working for an iPhone -> pi -> bluetooth setup?

vagend commented 5 years ago

Did anyone figure out how to do this? It appears that each suggestion here has been met with issues.

bedrin commented 5 years ago

It works for me: iPhone -> WiFi -> Shairport-sync on Raspberry PI (3B+) -> Bluetooth

I'm running Raspbian GNU/Linux 10 (buster)

Prerequisites

Steps to install (based on history output - hopefully I didn't remove too much from it :) )

Install shairport-sync

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential git xmltoman autoconf automake libtool     libpopt-dev libconfig-dev libasound2-dev avahi-daemon libavahi-client-dev libssl-dev libsoxr-dev
git clone https://github.com/mikebrady/shairport-sync.git
cd shairport-sync/
autoreconf -fi
./configure --sysconfdir=/etc --with-alsa --with-soxr --with-avahi --with-ssl=openssl --with-systemd
make
sudo make install

Install bluealsa

sudo apt install bluealsa

Configure bluetooth

Allow users pi and shairport-sync to use bluetooth by adding them to appropriate group

sudo adduser pi bluetooth
sudo adduser shairport-sync bluetooth
sudo reboot

I also did some steps more with bluetooth configuration from this article but I'm not really sure if they're necessary - most likely no. https://www.sigmdel.ca/michel/ha/rpi/bluetooth_n_buster_01_en.html Basically what I did is that I edited following files as described in that article:

Pair, trust and connect to your bluetooth speaker:

bluetoothctl
agent on
scan on
info <your mac address>
pair <your mac address>
trust <your mac address>
connect <your mac address>
exit

Now you should be able to play some wav file to your bluetooth speaker

wget https://file-examples.com/wp-content/uploads/2017/11/file_example_WAV_1MG.wav
aplay -D bluealsa:HCI=hci0,DEV=<your mac address>,PROFILE=a2dp file_example_WAV_1MG.wav

Create an Alsa configuration for your BT speaker

In my case it is called marshall - sudo nano /etc/asound.conf

pcm.marshall {
 type plug
  slave {
    pcm {
      type bluealsa
      device <your mac address>
      profile "a2dp"
    }
  }
  hint {
    show on
    description "Marshall speaker"
  }
}

Now you should be able to reference your speaker by alias using aplay:

aplay -D marshall file_example_WAV_1MG.wav

Configure shairport to use bluetooth speaker

sudo nano /etc/shairport-sync.conf:

general =
{
    name = "Marshall";
    volume_range_db = 60 ;
};

sessioncontrol =
{
    allow_session_interruption = "yes";
};

alsa =
{
    output_device = "marshall";
};

diagnostics =
{
    log_verbosity = 2;
};

Enable shairport-sync service

sudo systemctl enable shairport-sync
sudo systemctl start shairport-sync
sudo systemctl restart shairport-sync

Disable WiFi power management

For current session

sudo iwconfig wlan0 power off

To keep it permanent I've added one line (make sure it is before exit 0 line) to /etc/rc.local file:

iwconfig wlan0 power off

Hope it helps! Happy hacking!

mikebrady commented 4 years ago

Thanks for this useful contribution. Please open a new issue if necessary.

amitkeret commented 3 years ago

Allow users pi and shairport-sync to use bluetooth by adding them to appropriate group

sudo adduser pi bluetooth
sudo adduser shairport-sync bluetooth
sudo reboot

I realise this is an old and closed issue, but for any Googlers still searching... I had the same problem and this was the fix. shairport-sync doesn't have access to the bluetooth device unless it's added to the bluetooth group. Thank you so much @bedrin !

heapxor commented 3 years ago

Hi, I use roon-bridge on rpi4; i plugged bluetooth transmitter (BT); so when I connect my bose speaker via bluetooth to rpi4 and i stream playback to roon-bridge all works great; then I installed shairport-sync ... and when I am doing exactly same but instead streaming via airplay to shairport-sync that should forward all the sound to BT ->(via Bluetooth to) speaker; sound is chopping so basically i cant listen it...

is there any fix for that? https://github.com/mikebrady/shairport-sync/issues/1185

thanks

Muppet1856 commented 2 years ago

It works for me: iPhone -> WiFi -> Shairport-sync on Raspberry PI (3B+) -> Bluetooth

I'm running Raspbian GNU/Linux 10 (buster)

Prerequisites

* Make sure you have proper power supply for your Raspberry (in case of 3B+ it should 2.5a) - otherwise it would throttle. I had issues with iPad adapter (2a)

* There're some reports that built-in WiFi and Bluetooth in PI interfere with each other sometimes. If possible use 5GHz router and make sure your Pi uses 5GHz. Country code in wpa_supplicant.conf matters! US would work

Steps to install (based on history output - hopefully I didn't remove too much from it :) )

Install shairport-sync

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential git xmltoman autoconf automake libtool     libpopt-dev libconfig-dev libasound2-dev avahi-daemon libavahi-client-dev libssl-dev libsoxr-dev
git clone https://github.com/mikebrady/shairport-sync.git
cd shairport-sync/
autoreconf -fi
./configure --sysconfdir=/etc --with-alsa --with-soxr --with-avahi --with-ssl=openssl --with-systemd
make
sudo make install

Install bluealsa

sudo apt install bluealsa

Configure bluetooth

Allow users pi and shairport-sync to use bluetooth by adding them to appropriate group

sudo adduser pi bluetooth
sudo adduser shairport-sync bluetooth
sudo reboot

I also did some steps more with bluetooth configuration from this article but I'm not really sure if they're necessary - most likely no. https://www.sigmdel.ca/michel/ha/rpi/bluetooth_n_buster_01_en.html Basically what I did is that I edited following files as described in that article:

* /lib/systemd/system/bluealsa.service

* /lib/systemd/system/bluetooth.service

* /lib/systemd/system/bthelper@.service

Pair, trust and connect to your bluetooth speaker:

bluetoothctl
agent on
scan on
info <your mac address>
pair <your mac address>
trust <your mac address>
connect <your mac address>
exit

Now you should be able to play some wav file to your bluetooth speaker

wget https://file-examples.com/wp-content/uploads/2017/11/file_example_WAV_1MG.wav
aplay -D bluealsa:HCI=hci0,DEV=<your mac address>,PROFILE=a2dp file_example_WAV_1MG.wav

Create an Alsa configuration for your BT speaker

In my case it is called marshall - sudo nano /etc/asound.conf

pcm.marshall {
 type plug
  slave {
    pcm {
      type bluealsa
      device <your mac address>
      profile "a2dp"
    }
  }
  hint {
    show on
    description "Marshall speaker"
  }
}

Now you should be able to reference your speaker by alias using aplay:

aplay -D marshall file_example_WAV_1MG.wav

Configure shairport to use bluetooth speaker

sudo nano /etc/shairport-sync.conf:

general =
{
  name = "Marshall";
  volume_range_db = 60 ;
};

sessioncontrol =
{
  allow_session_interruption = "yes";
};

alsa =
{
  output_device = "marshall";
};

diagnostics =
{
  log_verbosity = 2;
};

Enable shairport-sync service

sudo systemctl enable shairport-sync
sudo systemctl start shairport-sync
sudo systemctl restart shairport-sync

Disable WiFi power management

For current session

sudo iwconfig wlan0 power off

To keep it permanent I've added one line (make sure it is before exit 0 line) to /etc/rc.local file:

iwconfig wlan0 power off

Hope it helps! Happy hacking!

Don't forget to add the current user to the audio group to stop errors on the aplay command (test) sudo adduser pi audio

dkonstrkt commented 2 years ago

Maybe I've made a mistake somewhere along the line, but it seems the new Rapsberry Pi OS doesn't play well with bluealsa (see: https://forums.raspberrypi.com/viewtopic.php?t=293486). I wonder if anyone has really made this work recently on a Raspberry Pi 4B.

Muppet1856 commented 2 years ago

I have not done it on a Pi4, but I did do this on a pi zero w this last week. I know its not answering you directly, but it may help focus where you are trouble shooting.

Maybe I've made a mistake somewhere along the line, but it seems the new Rapsberry Pi OS doesn't play well with bluealsa (see: https://forums.raspberrypi.com/viewtopic.php?t=293486). I wonder if anyone has really made this work recently on a Raspberry Pi 4B.

dkonstrkt commented 2 years ago

@Muppet1856 Thanks for your reply. Are you running Raspberry Pi OS?

Muppet1856 commented 2 years ago

@Muppet1856 Thanks for your reply. Are you running Raspberry Pi OS?

Yep.