jaseg / python-mpv

Python interface to the awesome mpv media player
https://git.jaseg.de/python-mpv.git
Other
531 stars 67 forks source link

[Question/Clarification request] How to play a stream and - on a button click - a short sound file simultaneously - withouc jackd? #247

Closed Wikinaut closed 1 year ago

Wikinaut commented 1 year ago

In issue #126 you describe how to run several players, basically so

import mpv
import time

p1=mpv.MPV()
p1.play("http:/.....")

time.sleep(10)
p2=mpv.MPV()
p2.play("file-beep.wav")

On my Raspberry Pi, I only get this working after jackd -R -dalsa having running, but using this approach I have to add

p1.ao="jack"
p2.ao="jack"

to the code above and (at the moment) I could not use the alsa equalizer plugin.

Question: Is there a programmatic way to run the two players to mix their signals?

Wikinaut commented 1 year ago

I found a solution using alsa, alsa equalizer and dmixer which I will publish here soon.

See

how to use multiple mpv-instances which audio signals are then mixed and finally equalised before they are sent to the DAC unit.

This is the core of my tweak:

mpvtest.py:

import mpv
options = { 'audio-output' : 'alsa/plugmixequal' } # plugmixequal as defined in .asoundrc
player1 = mpv.MPV( **options )
player2 = mpv.MPV( **options )

.asoundrc:

ctl.equal {
  type equal;
}

pcm.plugequal {
  type equal;
  slave {
    pcm "plughw:CARD=sndrpihifiberry,DEV=0";
  }
}

pcm.plugmixequal {
  type equal;
  slave {
    pcm plug:dmix;
  }
}

pcm.!default {
 type plug;
 slave {
   pcm plugequal;
 }
}

# Shared access to our audio card
pcm.dmixer {
    type dmix
    ipc_key 1234
    ipc_key_add_uid true
    ipc_perm 0666
    slave {
        pcm "hw:CARD=sndrpihifiberry,DEV=0";
    }
}
Wikinaut commented 1 year ago

self-answered my question and fixed the issue.