Browse Source

Track the audio output device

Zachary Kotlarek 13 years ago
parent
commit
04086890ee
5 changed files with 99 additions and 4 deletions
  1. 1 1
      dmx/DMX.pm
  2. 53 0
      dmx/audioOutput.pl
  3. 42 0
      dmx/launchd/com.uberzach.audioOutput.plist
  4. 1 1
      dmx/reset.sh
  5. 2 2
      dmx/state.pl

+ 1 - 1
dmx/DMX.pm

@@ -134,7 +134,7 @@ sub parseState($$$) {
 	}
 	if (defined($exists_text)) {
 		foreach my $exists_val (split(/\s*,\s*/, $exists_text)) {
-			my ($name, $value, $time) = $exists_val =~ /(\w+)\:([\w\.\-]+)\:(\d+)/;
+			my ($name, $value, $time) = $exists_val =~ /(\w+)\:([\w\.\- ]+)\:(\d+)/;
 			if (!defined($name) || !defined($value) || !defined($time)) {
 				print STDERR 'State parse error (exists): ' . $text . "\n";
 				next;

+ 53 - 0
dmx/audioOutput.pl

@@ -0,0 +1,53 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use File::Temp qw( tempfile );
+use Time::HiRes qw( usleep sleep time );
+use IPC::System::Simple qw( system capture );
+
+# Local modules
+use Cwd qw(abs_path);
+use File::Basename qw(dirname);
+use lib dirname(abs_path($0));
+use DMX;
+
+# App config
+my $DATA_DIR     = DMX::dataDir();
+my $OUTPUT_FILE  = $DATA_DIR . 'AUDIO_OUTPUT';
+my $DELAY        = 1;
+my @AUDIO_GET    = ('/Users/tv/bin/SwitchAudioSource', '-c');
+
+# Debug
+my $DEBUG = 0;
+if ($ENV{'DEBUG'}) {
+	$DEBUG = 1;
+}
+
+# State
+my $device     = '';
+my $deviceLast = $device;
+
+# Loop forever
+while (1) {
+
+	# Save the last output device
+	$deviceLast = $device;
+
+	# Grab the current audio output device
+	$device = capture(@AUDIO_GET);
+	$device =~ s/\n$//;
+
+	# If something has changed, save the state to disk
+	if ($device ne $deviceLast) {
+		if ($DEBUG) {
+			print STDERR 'New output device: ' . $deviceLast . ' => ' . $device . "\n";
+		}
+		my ($fh, $tmp) = tempfile($OUTPUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
+		print $fh $device . "\n";
+		close($fh);
+		rename($tmp, $OUTPUT_FILE);
+	}
+
+	# Wait between updates
+	sleep($DELAY);
+}

+ 42 - 0
dmx/launchd/com.uberzach.audioOutput.plist

@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>LimitLoadToSessionType</key>
+	<string>Aqua</string>
+	<key>LimitLoadToHosts</key>
+	<array>
+		<string>heady.uberzach.com</string>
+		<string>Heady.local</string>
+	</array>
+	<key>RunAtLoad</key>
+	<true/>
+	<key>Label</key>
+	<string>com.uberzach.audioOutput</string>
+	<key>KeepAlive</key>
+	<dict>
+		<key>OtherJobEnabled</key>
+		<dict>
+			<key>com.uberzach.amplifier</key>
+			<true/>
+			<key>com.uberzach.state</key>
+			<true/>
+		</dict>
+	</dict>
+	<key>ProgramArguments</key>
+	<array>
+		<string>/Users/tv/bin/video/dmx/audioOutput.pl</string>
+	</array>
+	<!--
+		<key>EnvironmentVariables</key>
+		<dict>
+			<key>DEBUG</key>
+			<string>1</string>
+		</dict>
+		<key>StandardOutPath</key>
+		<string>/Users/tv/com.uberzach.audioOutput_log.txt</string>
+		<key>StandardErrorPath</key>
+		<string>/Users/tv/com.uberzach.audioOutput_errlog.txt</string>
+	-->
+</dict>
+</plist>

+ 1 - 1
dmx/reset.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 
-PROCS="rope.pl leds.pl bias.pl fan.pl overhead.pl projectorPower.pl amplifierPower.pl garage.pl rave.pl"
+PROCS="rope.pl leds.pl bias.pl fan.pl overhead.pl projectorPower.pl amplifierPower.pl garage.pl rave.pl debug.pl"
 
 # Find the PID for each named process
 PIDS=""

+ 2 - 2
dmx/state.pl

@@ -32,6 +32,7 @@ my %MON_FILES     = (
 	'AMPLIFIER_VOL'   => 'STATUS_VALUE',
 	'AMPLIFIER_MODE'  => 'STATUS_VALUE',
 	'AMPLIFIER_INPUT' => 'STATUS_VALUE',
+	'AUDIO_OUTPUT'    => 'STATUS_VALUE',
 
 	'/mnt/media/DMX/cmd/GARAGE_CMD' => 'EXISTS_CLEAR',
 );
@@ -192,8 +193,7 @@ while (1) {
 					$file->{'status'} = 1;
 				}
 			} elsif ($file->{'type'} eq 'STATUS_VALUE') {
-				$text =~ s/^\s*//;
-				$text =~ s/\s*$//;
+				$text =~ s/\n$//;
 				$file->{'status'} = $text;
 			} else {
 				if ($text =~ /1/) {