Created
May 10, 2013 03:57
-
-
Save naftulikay/5552290 to your computer and use it in GitHub Desktop.
A quick and dirty script to transfer all PulseAudio streams between two sinks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from argparse import ArgumentParser | |
import json, os, re, subprocess | |
def main(): | |
parser = ArgumentParser() | |
args = parser.parse_args() | |
sinks = get_sinks() | |
current_sink = None | |
speaker_sink = None | |
headphones_sink = None | |
for sink in sinks: | |
if sink.state == "RUNNING": | |
current_sink = sink | |
if sink.name == "alsa_output.pci-0000_00_1b.0.analog-stereo": | |
speaker_sink = sink | |
elif sink.name == "alsa_output.usb-Corsair_Vengeance_2000-00-V2000.iec958-stereo": | |
headphones_sink = sink | |
if current_sink == headphones_sink: | |
# switch to headphones | |
print "Redirecting audio to speakers." | |
subprocess.call(["notify-send", "-i", "audio-card", | |
"Transferring audio playback to speakers.", | |
"Audio playback has been transferred to the analog output speaker " + | |
"system."]) | |
for stream in get_streams(): | |
move_stream(stream, speaker_sink) | |
else: | |
# switch to the main speaker output | |
print "Redirecting audio to headphones." | |
subprocess.call(["notify-send", "-i", "audio-card", | |
"Transferring audio playback to headphones.", | |
"Audio playback has been transferred to the Corsair Vengeance 2000 " + | |
"headset."]) | |
for stream in get_streams(): | |
move_stream(stream, headphones_sink) | |
def get_sinks(): | |
output = subprocess.check_output(["pactl", "list", "short", "sinks"]) | |
result = [] | |
for line in output.split("\n"): | |
if re.match(r'\w', line): | |
fields = re.split(r'\s+', line) | |
result.append(PulseAudioSink(*fields)) | |
return result | |
def get_streams(): | |
output = subprocess.check_output(["pactl", "list", "short", "sink-inputs"]) | |
result = [] | |
for line in output.split("\n"): | |
if re.match(r'\w', line): | |
fields = re.split(r'\s+', line) | |
result.append(PulseAudioStream(*fields)) | |
return result | |
def move_stream(stream, sink): | |
return_code = subprocess.call(["pactl", "move-sink-input", str(stream.id), str(sink.id)]) | |
if return_code != 0: | |
raise Exception("Unable to transfer the stream: {}".format(return_code)) | |
class PulseAudioSink: | |
id = None | |
name = None | |
driver = None | |
audio_format = None | |
audio_channels = None | |
audio_sample_rate = None | |
state = None | |
def __init__(self, id = None, name = None, | |
driver = None, audio_format = None, | |
audio_channels = None, audio_sample_rate = None, | |
state = None): | |
self.id = int(id) | |
self.name = name | |
self.driver = driver | |
self.audio_format = audio_format | |
self.audio_channels = re.match(r'(^\d+)ch', audio_channels).group(1) | |
self.audio_sample_rate = re.match(r'(\d+)Hz', audio_sample_rate).group(1) | |
self.state = state | |
class PulseAudioStream: | |
id = None | |
sink_id = None | |
client_id = None | |
driver = None | |
audio_format = None | |
audio_channels = None | |
audio_sample_rate = None | |
def __init__(self, id = None, sink_id = None, | |
client_id = None, driver = None, audio_format = None, | |
audio_channels = None, audio_sample_rate = None): | |
self.id = id | |
self.sink_id = sink_id | |
self.client_id = client_id | |
self.driver = driver | |
self.audio_format = audio_format | |
self.audio_channels = audio_channels | |
self.audio_sample_rate = audio_sample_rate | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment