audio.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. # Config
  13. my %DEVS = (
  14. 'AMP' => 'Built-in Output',
  15. 'RAVE' => 'Built-in Output',
  16. 'DEFAULT' => 'USB Audio CODEC ',
  17. );
  18. # App config
  19. my $DATA_DIR = DMX::dataDir();
  20. my $OUTPUT_FILE = $DATA_DIR . 'AUDIO';
  21. my $OUTPUT_STATE = $OUTPUT_FILE . '_STATE';
  22. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  23. my $PUSH_TIMEOUT = 20;
  24. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  25. my $DELAY = 1;
  26. my @AUDIO_CMD = ('/Users/tv/bin/SwitchAudioSource');
  27. my @AUDIO_GET = (@AUDIO_CMD, '-c');
  28. my @AUDIO_SET = (@AUDIO_CMD, '-s');
  29. # Debug
  30. my $DEBUG = 0;
  31. if ($ENV{'DEBUG'}) {
  32. $DEBUG = 1;
  33. }
  34. # Sockets
  35. DMX::stateSocket($STATE_SOCK);
  36. DMX::stateSubscribe($STATE_SOCK);
  37. # State
  38. my $state = 'OFF';
  39. my $stateLast = $state;
  40. my $device = 'OFF';
  41. my $deviceLast = $device;
  42. my %exists = ();
  43. my $pushLast = 0;
  44. my $pullLast = time();
  45. my $update = 0;
  46. # Always force the output to default at launch
  47. system(@AUDIO_SET, $DEVS{'DEFAULT'});
  48. # Loop forever
  49. while (1) {
  50. # Grab the current audio output device
  51. $deviceLast = $device;
  52. $device = capture(@AUDIO_GET);
  53. $device =~ s/\n$//;
  54. # If the device has changed, save the state to disk
  55. if ($deviceLast ne $device) {
  56. if ($DEBUG) {
  57. print STDERR 'New output device: ' . $deviceLast . ' => ' . $device . "\n";
  58. }
  59. my ($fh, $tmp) = tempfile($OUTPUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  60. print $fh $device . "\n";
  61. close($fh);
  62. rename($tmp, $OUTPUT_FILE);
  63. }
  64. # State is calculated; use newState to gather data
  65. my $newState = $state;
  66. # Wait for state updates
  67. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  68. if (defined($cmdState)) {
  69. $newState = $cmdState;
  70. $pullLast = time();
  71. }
  72. # Die if we don't see regular updates
  73. if (time() - $pullLast > $PULL_TIMEOUT) {
  74. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  75. }
  76. # Calculate the new state
  77. $stateLast = $state;
  78. if ($exists{'RAVE'}) {
  79. $state = 'RAVE';
  80. } elsif ($exists{'AUDIO_AMP'}) {
  81. $state = 'AMP';
  82. } else {
  83. $state = 'DEFAULT';
  84. }
  85. # Force updates on a periodic basis
  86. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  87. # Not for the audio output device
  88. #if ($DEBUG) {
  89. # print STDERR "Forcing periodic update\n";
  90. #}
  91. #$update = 1;
  92. }
  93. # Force updates when there is a physical state mistmatch
  94. if (!$update && $DEVS{$state} ne $device) {
  95. if ($DEBUG) {
  96. print STDERR 'State mismatch: ' . $device . ' => ' . $DEVS{$state} . "\n";
  97. }
  98. $update = 1;
  99. }
  100. # Update the audio output device
  101. if ($update) {
  102. # Update
  103. if ($DEBUG) {
  104. print STDERR 'Setting output to: ' . $DEVS{$state} . "\n";
  105. }
  106. system(@AUDIO_SET, $DEVS{$state});
  107. # Update the push time
  108. $pushLast = time();
  109. # Clear the update flag
  110. $update = 0;
  111. }
  112. # If the state has changed and the device has responded, save to disk
  113. if (!$update && $stateLast ne $state) {
  114. if ($DEBUG) {
  115. print STDERR 'New output state: ' . $stateLast . ' => ' . $state . "\n";
  116. }
  117. my ($fh, $tmp) = tempfile($OUTPUT_STATE . '.XXXXXXXX', 'UNLINK' => 0);
  118. print $fh $state . "\n";
  119. close($fh);
  120. rename($tmp, $OUTPUT_STATE);
  121. }
  122. }