audioOutput.pl 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Temp qw( tempfile );
  5. use Time::HiRes qw( usleep sleep time );
  6. use IPC::System::Simple qw( system capture );
  7. # Local modules
  8. use Cwd qw(abs_path);
  9. use File::Basename qw(dirname);
  10. use lib dirname(abs_path($0));
  11. use DMX;
  12. # App config
  13. my $DATA_DIR = DMX::dataDir();
  14. my $OUTPUT_FILE = $DATA_DIR . 'AUDIO_OUTPUT';
  15. my $DELAY = 1;
  16. my @AUDIO_GET = ('/Users/tv/bin/SwitchAudioSource', '-c');
  17. # Debug
  18. my $DEBUG = 0;
  19. if ($ENV{'DEBUG'}) {
  20. $DEBUG = 1;
  21. }
  22. # State
  23. my $device = '';
  24. my $deviceLast = $device;
  25. # Loop forever
  26. while (1) {
  27. # Save the last output device
  28. $deviceLast = $device;
  29. # Grab the current audio output device
  30. $device = capture(@AUDIO_GET);
  31. $device =~ s/\n$//;
  32. # If something has changed, save the state to disk
  33. if ($device ne $deviceLast) {
  34. if ($DEBUG) {
  35. print STDERR 'New output device: ' . $deviceLast . ' => ' . $device . "\n";
  36. }
  37. my ($fh, $tmp) = tempfile($OUTPUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  38. print $fh $device . "\n";
  39. close($fh);
  40. rename($tmp, $OUTPUT_FILE);
  41. }
  42. # Wait between updates
  43. sleep($DELAY);
  44. }