Output audio to multiple sources on raspberry pi

The article will discuss how to output the audio played on the raspberry pi to several sources (players) at the same time. In particular, the audio will be simultaneously broadcast via hdmi, audio jack, bluetooth device(s).

The question is applied, but it was not possible to find a ready-made solution on the Internet, so this short post was born.

Installing packages.

pip3 install pygame==2.1.2

sudo apt-get install python3-sdl2

sudo apt install paprefs

sudo apt install pavucontrol

We reboot.

Setting up

Go to “Pulseaudio Preferences” –

and check the box by going to the “Simultaneous Output” tab –

Now open “PulseAudio Volume control” –
and make sure that a new output device has appeared among the output devices –
At the same time, it doesn’t matter at all what you have selected as the output device at the moment –
Optionally, you can connect several bluetooth devices to the raspberry –

Small code.

Now let’s write the code that will output audio to multiple devices at the same time.

import pygame._sdl2 as sdl2
from pygame import mixer
from time import sleep

mixer.init()# Initialize the mixer, this will allow the next command to work
print(sdl2.audio.get_audio_device_names(False)) # Returns playback devices, Boolean value determines whether they are Input or Output devices.
def output_devices(): #возвращает device, который включает все play devices
    for i in sdl2.audio.get_audio_device_names(False):
        if i.startswith ('Simultaneous'):
            return i

#mixer.init(devicename="Simultaneous output to Встроенное аудио Digital Stereo, Встроенное аудио Analog Stereo, JBL GO") # Initialize it with the correct device
mixer.init(devicename = a)
mixer.music.load("english.wav") # Load the mp3
mixer.music.play() # Play it

while mixer.music.get_busy():  # wait for music to finish playing

After starting, the sound should flow from all devices at the same time.
The essence of the program is that it goes through all the output devices on the raspberry, finds one that starts with the word “Simultaneous” and outputs sound to it.
The full name of this device looks something like this: “Simultaneous output to Digital Stereo Integrated Audio, Analog Stereo Integrated Audio, JBL GO”.

Program code – here
Audio for the test – here

Similar Posts


Leave a Reply

Your email address will not be published. Required fields are marked *