Skip to content

Commit

Permalink
Merge pull request sonic-pi-net#3347 from porras/pipewire
Browse files Browse the repository at this point in the history
Use pipewire on Linux if available (like on raspberry)
  • Loading branch information
samaaron authored Oct 30, 2023
2 parents 41a6728 + 9c161d4 commit 68edde6
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions app/server/ruby/bin/daemon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ def self.os
raise "Unsupported platform #{RUBY_PLATFORM}"
end
end

def self.pipewire?
`which pw-link`
$?.success?
end
end

class KillSwitch
Expand Down Expand Up @@ -1218,8 +1223,9 @@ def read_log
def run_pre_start_commands
case Util.os
when :linux, :raspberry
#Start Jack if not already running
if `jack_wait -c`.include? 'not running'
if Util.pipewire?
Util.log 'No need to start Jackd, using pipewire instead'
elsif `jack_wait -c`.include? 'not running'
#Jack not running - start a new instance
Util.log "Jackd not running on system. Starting..."
@jack_booter = JackBooter.new
Expand All @@ -1231,27 +1237,29 @@ def run_pre_start_commands

def run_post_start_commands
case Util.os
#modify case if you want linux as well as raspberry pi to use pipewire
when :raspberry #,:linux
Thread.new do
Kernel.sleep 5
hdmiL=`/usr/bin/pw-link -iI |grep -P '(hdmi).*(playback_FL)'|awk '{ print $1 }'`
hdmiR=`/usr/bin/pw-link -iI |grep -P '(hdmi).*(playback_FR)'|awk '{ print $1 }'`

sco1=`/usr/bin/pw-link -oI |grep -P '(SuperCollider:out_1)' |awk '{ print $1 }'`
sco2=`/usr/bin/pw-link -oI |grep -P '(SuperCollider:out_2)' |awk '{ print $1 }'`

system("pw-link #{sco1.strip} #{hdmiL.strip}")
system("pw-link #{sco2.strip} #{hdmiR.strip}")
end
#comment out this when section if you want linux to use pulseaudio as raspberry-pi above
when :linux
when :raspberry, :linux
Thread.new do
Kernel.sleep 5
# Note:
# need to modify this to take account for @num_inputs and @num_outputs.
# These might not always be set to two channels each.
if @jack_booter
if Util.pipewire?
port_type = if Util.os == :raspberry
'hdmi'
else
'alsa_output'
end
inputs = `pw-link -iI`.lines
left_id = inputs.grep(/#{port_type}.*playback_FL$/).first.to_i
right_id = inputs.grep(/#{port_type}.*playback_FR$/).first.to_i

outputs = `pw-link -oI`.lines
sco1 = outputs.grep(/SuperCollider:out_1$/).first.to_i
sco2 = outputs.grep(/SuperCollider:out_2$/).first.to_i

system("pw-link #{sco1} #{left_id}")
system("pw-link #{sco2} #{right_id}")
elsif @jackbooter
# Note:
# need to modify this to take account for @num_inputs and @num_outputs.
# These might not always be set to two channels each.
#First clear up any pulseaudio remains of module-loopback source=jack_in
`pactl list short modules |grep source=jack_in| cut -f1 | xargs -L1 pactl unload-module`
`pactl load-module module-jack-source channels=2 connect=0 client_name=JACK_to_PulseAudio`
Expand All @@ -1271,7 +1279,6 @@ def run_post_start_commands
end
end


def unify_toml_opts_hash(toml_opts_hash)
opts = {}

Expand Down

0 comments on commit 68edde6

Please sign in to comment.