| 1 |
|
| 2 |
|
| 3 |
require 'shellwords' |
| 4 |
require 'open3' |
| 5 |
|
| 6 |
Plugin.create(:pulseaudio) do |
| 7 |
defsound :pulseaudio, 'PulseAudio' do |filename| |
| 8 |
SerialThread.new do |
| 9 |
if FileTest.exist?(filename) |
| 10 |
pacmd.puts "play-file #{Shellwords.escape(filename)} #{default_sink}" |
| 11 |
end |
| 12 |
rescue => err |
| 13 |
error 'Error occured. Force close connection.' |
| 14 |
error err |
| 15 |
pacmd.close |
| 16 |
pacmd.puts "play-file #{filename} #{default_sink}" |
| 17 |
end |
| 18 |
end |
| 19 |
|
| 20 |
IOを返す。既に書き込み用IOが開いているならそちらを返す。 |
| 21 |
なら新しく接続しなおして返す。 |
| 22 |
|
| 23 |
込み用のIO |
| 24 |
def pacmd |
| 25 |
if defined?(@pacmd) && !@pacmd.closed? |
| 26 |
@pacmd |
| 27 |
else |
| 28 |
@default_sink = nil |
| 29 |
@pacmd = IO.popen('pacmd', File::Constants::WRONLY).tap do |pacmd| |
| 30 |
pacmd.close_on_exec = true |
| 31 |
pacmd.autoclose = true |
| 32 |
pacmd.sync = true |
| 33 |
end |
| 34 |
end |
| 35 |
end |
| 36 |
|
| 37 |
fault Sinkの名前を返す |
| 38 |
|
| 39 |
ルトシンクの名前 |
| 40 |
def default_sink |
| 41 |
@default_sink ||= Open3.popen2({'LC_ALL' => 'C'} ,'pactl info') do |input, output, _| |
| 42 |
input.close |
| 43 |
output |
| 44 |
.find{ |line| line.start_with?('Default Sink') } |
| 45 |
&.match(/^Default Sink\s*:\s*(.+)$/)&.captures&.first |
| 46 |
end |
| 47 |
end |
| 48 |
end |