lukasjapan / bt-speaker

A simple Bluetooth Speaker Daemon for the Raspberry Pi 3
GNU General Public License v3.0
495 stars 95 forks source link

bt-speaker ALSA configuration with Hifiberry DAC+ #43

Open alexjaw opened 6 years ago

alexjaw commented 6 years ago

Thank's for a terrific project!!! Now I would like to improve the audio output and installed the DAC+ from Hifiberry. I dont really understand how to use the alsa config in order to allow for volume control. Maybe not right to post this here, but eventually others will find some clues for how bt-speaker can be setup with a DAC on latest Rasbian.

My setup: RPi 2 b+ with BT dongle: DIGITUS Bluetooth® 4.0 Tiny USB Adapter Rasbian 2017-11-29-raspbian-stretch-lite.zip

config.txt, observe that I'm allowing both default RPi audio and DAC+. bt-speaker cannot connect over BT if dtparam=audio=on is commented out (as noted by others)

pi@raspberrypi:~ $ tail -7 /boot/config.txt 
# Enable audio (loads snd_bcm2835)
dtparam=audio=on

# Hifiberry
dtoverlay=hifiberry-dacplus

bt-speaker conf, without ALSA - dont understand the settings

pi@raspberrypi:~ $ cat /etc/bt_speaker/config.ini 
[bt_speaker]
play_command = aplay -f cd -
connect_command = /etc/bt_speaker/hooks/connect
disconnect_command = /etc/bt_speaker/hooks/disconnect

[bluez]
device_path = /org/bluez/hci0
discoverable = yes

[alsa]
enabled = no
mixer = PCM
id = 0
cardindex = 0

sound cards

pi@raspberrypi:~ $ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 7/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: sndrpihifiberry [snd_rpi_hifiberry_dacplus], device 0: HiFiBerry DAC+ HiFi pcm512x-hifi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0

asound.conf

pi@raspberrypi:~ $ cat /etc/asound.conf 
pcm.!default {
  type hw card 1
}
ctl.!default {
  type hw card 1
}

amixer possibilies for card 1, i.e. the DAC, can this info be used somehow for the settings in the config? With alsamixer its possible to adjust the volume with "Simple mixer control 'Digital',0" (number 10 if we start at 1), presently at 100%. Its also possible to use amixer sset 'Digital' , for example

pi@raspberrypi:~ $ amixer sset 'Digital' 200
Simple mixer control 'Digital',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 207
  Mono:
  Front Left: Playback 200 [97%] [-3.50dB] [on]
  Front Right: Playback 200 [97%] [-3.50dB] [on]
pi@raspberrypi:~ $ amixer -c 1
Simple mixer control 'DSP Program',0
  Capabilities: enum
  Items: 'FIR interpolation with de-emphasis' 'Low latency IIR with de-emphasis' 'High attenuation with de-emphasis' 'Fixed process flow' 'Ringing-less low latency FIR'
  Item0: 'FIR interpolation with de-emphasis'
Simple mixer control 'Analogue',0
  Capabilities: pvolume
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 1
  Mono:
  Front Left: Playback 1 [100%] [0.00dB]
  Front Right: Playback 1 [100%] [0.00dB]
Simple mixer control 'Analogue Playback Boost',0
  Capabilities: volume
  Playback channels: Front Left - Front Right
  Capture channels: Front Left - Front Right
  Limits: 0 - 1
  Front Left: 1 [100%] [0.80dB]
  Front Right: 1 [100%] [0.80dB]
Simple mixer control 'Auto Mute',0
  Capabilities: pswitch
  Playback channels: Front Left - Front Right
  Mono:
  Front Left: Playback [on]
  Front Right: Playback [on]
Simple mixer control 'Auto Mute Mono',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
Simple mixer control 'Auto Mute Time Left',0
  Capabilities: enum
  Items: '21ms' '106ms' '213ms' '533ms' '1.07s' '2.13s' '5.33s' '10.66s'
  Item0: '21ms'
Simple mixer control 'Auto Mute Time Right',0
  Capabilities: enum
  Items: '21ms' '106ms' '213ms' '533ms' '1.07s' '2.13s' '5.33s' '10.66s'
  Item0: '21ms'
Simple mixer control 'Clock Missing Period',0
  Capabilities: enum
  Items: '1s' '2s' '3s' '4s' '5s' '6s' '7s' '8s'
  Item0: '1s'
Simple mixer control 'Deemphasis',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
Simple mixer control 'Digital',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 207
  Mono:
  Front Left: Playback 207 [100%] [0.00dB] [on]
  Front Right: Playback 206 [100%] [-0.50dB] [on]
Simple mixer control 'Max Overclock DAC',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 40
  Mono: 0 [0%]
Simple mixer control 'Max Overclock DSP',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 40
  Mono: 0 [0%]
Simple mixer control 'Max Overclock PLL',0
  Capabilities: volume volume-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: 0 - 20
  Mono: 0 [0%]
Simple mixer control 'Volume Ramp Down Emergency Rate',0
  Capabilities: enum
  Items: '1 sample/update' '2 samples/update' '4 samples/update' 'Immediate'
  Item0: 'Immediate'
Simple mixer control 'Volume Ramp Down Emergency Step',0
  Capabilities: enum
  Items: '4dB/step' '2dB/step' '1dB/step' '0.5dB/step'
  Item0: '4dB/step'
Simple mixer control 'Volume Ramp Down Rate',0
  Capabilities: enum
  Items: '1 sample/update' '2 samples/update' '4 samples/update' 'Immediate'
  Item0: '1 sample/update'
Simple mixer control 'Volume Ramp Down Step',0
  Capabilities: enum
  Items: '4dB/step' '2dB/step' '1dB/step' '0.5dB/step'
  Item0: '1dB/step'
Simple mixer control 'Volume Ramp Up Rate',0
  Capabilities: enum
  Items: '1 sample/update' '2 samples/update' '4 samples/update' 'Immediate'
  Item0: '1 sample/update'
Simple mixer control 'Volume Ramp Up Step',0
  Capabilities: enum
  Items: '4dB/step' '2dB/step' '1dB/step' '0.5dB/step'
  Item0: '1dB/step'
mIwav commented 6 years ago

Hello @alexjaw,

I experienced a similar problem. For me changing the config.ini in /etc/bt_speaker/ to

[bt_speaker]
play_command = aplay -f cd -
connect_command = /etc/bt_speaker/hooks/connect
disconnect_command = /etc/bt_speaker/hooks/disconnect

[bluez]
device_path = /org/bluez/hci0
discoverable = yes

[alsa]
enabled = yes
mixer = Digital
id = 0
cardindex = 0

fixed some of the more obvious playback issues. You might have to change the cardindex to 1 in addition.

sikaiser commented 6 years ago

I had moderate success with specifying the usb device in the "play_command" of config.ini (I am using a HRT microStreamer DAC) and adjusting the alsa cardindex.

[bt_speaker]
play_command = aplay -D default:CARD=microStreamer -f cd -
connect_command = /etc/bt_speaker/hooks/connect
disconnect_command = /etc/bt_speaker/hooks/disconnect

[bluez]
device_path = /org/bluez/hci0
discoverable = yes

[alsa]
enabled = yes
mixer = PCM
id = 0
cardindex = 1

The device name I found using aplay -L (see https://die-antwort.eu/techblog/2017-12-raspberry-pi-usb-audio-interface-command-line/ for a helpful guide).

However playback stutters every few seconds and the Raspberry Pi Zero W that I am running this on freezes completely after about thirty seconds.

ddekanski commented 5 years ago

I'd like to share my config as after long hours of trying out different setups, I got to a point where bt_speaker works flawlessly with my DAC soundcard, so I can connect to my Pi via Bluetooth, listen to music from my smartphones, control Pi volume with them, and hear connect/disconnect sounds.

My hardware:

My OS: Raspbian Stretch Lite 4.14.79+ (2018-11-13-raspbian-stretch-lite)

/boot/config.txt:

aplay -l returns the following:

**** List of PLAYBACK Hardware Devices ****
card 0: sndrpihifiberry [snd_rpi_hifiberry_dac], device 0: HifiBerry DAC HiFi pcm5102a-hifi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0

ALSA config (usr/share/alsa/alsa.conf):

pcm.hifiberry {
  type hw
  card 0
  device 0
}
pcm.dmixer {
  type dmix
  ipc_key 1024
  ipc_perm 0666
  slave.pcm "hifiberry"
  slave {
    period_time 0
    period_size 1024
    buffer_size 8192
    rate 44100
  }
  bindings {
    0 0
    1 1
  }
}
ctl.dmixer {
  type hw
  card 0
}
pcm.softvol {
  type softvol
  slave.pcm "dmixer"
  control {
    name "Master"
    card 0
  }
}
pcm.!default {
  type plug
  slave.pcm "softvol"
}
pcm.front pcm.default

amixer returns the following:

pi@raspberrypi:~ $ amixer
Simple mixer control 'Master',0
  Capabilities: volume
  Playback channels: Front Left - Front Right
  Capture channels: Front Left - Front Right
  Limits: 0 - 255
  Front Left: 255 [100%]
  Front Right: 255 [100%]

bt_speaker config (etc/bt_speaker/config.ini):

[bt_speaker]
play_command = aplay -f cd -
connect_command = /etc/bt_speaker/hooks/connect
disconnect_command = /etc/bt_speaker/hooks/disconnect

[bluez]
device_path = /org/bluez/hci0
discoverable = yes

[alsa]
enabled = yes
mixer = Master
id = 0
cardindex = 0

I hope it helps!

aking7 commented 4 years ago

This is great everyone! I'm using the HiFiBerry as a DSP processor and have a similar configuration. If you're curious, I have created a Loopback device in ALSA using sudo modprobe snd-aloop, such that I can use a dsnoop PCM to listen to the Loopback card in my DSP program and then route it to the output PCM.

My asound.conf is:

pcm.!default {
  type plug
  slave.pcm "input"
}

ctl.!default {
  type hw
  card sndrpihifiberry
}

pcm.input {
  type plug
  slave.pcm "hw:Loopback,0,0"
}

pcm.snoop {
  type plug
  slave.pcm {
    type dsnoop
    ipc_key 1024
    slave.pcm "hw:Loopback,1,0"
  }
}

pcm.output {
  type plug
  slave.pcm "master_volume"
}

pcm.master_volume {
  type softvol
  slave.pcm "dmix:sndrpihifiberry"
  control.name "Master"
  control.card sndrpihifiberry
}

The bt_speaker service then plays the audio to the input PCM. The etc/bt_speaker/config.ini has:

[bt_speaker]
play_command = aplay -D input -f cd -
connect_command = /etc/bt_speaker/hooks/connect
disconnect_command = /etc/bt_speaker/hooks/disconnect

[bluez]
device_path = /org/bluez/hci0
discoverable = yes

[alsa]
enabled = yes
mixer = Master
id = 0
cardindex = 1

Note that Loopback is now card 0 and sndrpihifiberry is card 1.