audio.pl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. use Time::Out qw( timeout );
  8. # Local modules
  9. use Cwd qw(abs_path);
  10. use File::Basename qw(dirname);
  11. use lib dirname(abs_path($0));
  12. use DMX;
  13. # Config
  14. my %DEVS = (
  15. 'AMP' => 'Built-in Output',
  16. 'RAVE' => 'Built-in Output',
  17. 'DEFAULT' => 'USB Audio CODEC ',
  18. );
  19. # Prototypes
  20. sub getAudio();
  21. sub setAudio($);
  22. # App config
  23. my $DATA_DIR = DMX::dataDir();
  24. my $STATE_SOCK = 'AUDIO';
  25. my $OUTPUT_FILE = $DATA_DIR . $STATE_SOCK;
  26. my $OUTPUT_STATE = $OUTPUT_FILE . '_STATE';
  27. my $PUSH_TIMEOUT = 20;
  28. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  29. my $DELAY = 1;
  30. my $TIMEOUT = 10;
  31. my @AUDIO_CMD = ('/Users/tv/bin/SwitchAudioSource');
  32. my @AUDIO_GET = (@AUDIO_CMD, '-c');
  33. my @AUDIO_SET = (@AUDIO_CMD, '-s');
  34. # Debug
  35. my $DEBUG = 0;
  36. if ($ENV{'DEBUG'}) {
  37. $DEBUG = 1;
  38. }
  39. # State
  40. my $state = 'OFF';
  41. my $stateLast = $state;
  42. my $device = 'OFF';
  43. my $deviceLast = $device;
  44. my %exists = ();
  45. my $pushLast = 0;
  46. my $pullLast = time();
  47. my $update = 0;
  48. # Sockets
  49. DMX::stateSocket($STATE_SOCK);
  50. DMX::stateSubscribe($STATE_SOCK);
  51. # Always force the output to default at launch
  52. # In most components this happens before state subscription
  53. # but here we want to avoid making any changes while STATE is not available
  54. setAudio('DEFAULT');
  55. # Loop forever
  56. while (1) {
  57. # Grab the current audio output device
  58. $deviceLast = $device;
  59. $device = getAudio();
  60. # If the device has changed, save the state to disk
  61. if ($deviceLast ne $device) {
  62. if ($DEBUG) {
  63. print STDERR 'New output device: ' . $deviceLast . ' => ' . $device . "\n";
  64. }
  65. my ($fh, $tmp) = tempfile($OUTPUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  66. print $fh $device . "\n";
  67. close($fh);
  68. rename($tmp, $OUTPUT_FILE);
  69. }
  70. # State is calculated; use newState to gather data
  71. my $newState = $state;
  72. # Wait for state updates
  73. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  74. if (defined($cmdState)) {
  75. $newState = $cmdState;
  76. $pullLast = time();
  77. }
  78. # Die if we don't see regular updates
  79. if (time() - $pullLast > $PULL_TIMEOUT) {
  80. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  81. }
  82. # Calculate the new state
  83. $stateLast = $state;
  84. if ($exists{'RAVE'}) {
  85. $state = 'RAVE';
  86. } elsif ($exists{'AUDIO_AMP'}) {
  87. $state = 'AMP';
  88. } elsif ($exists{'GAME'}) {
  89. $state = 'DEFAULT';
  90. } elsif (!$exists{'PLEX'}) {
  91. $state = 'AMP';
  92. } else {
  93. $state = 'DEFAULT';
  94. }
  95. # Force updates on a periodic basis
  96. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  97. # Not for the audio output device
  98. #if ($DEBUG) {
  99. # print STDERR "Forcing periodic update\n";
  100. #}
  101. #$update = 1;
  102. }
  103. # Force updates when there is a physical state mistmatch
  104. if (!$update && $DEVS{$state} ne $device) {
  105. if ($DEBUG) {
  106. print STDERR 'State mismatch: ' . $device . ' => ' . $DEVS{$state} . "\n";
  107. }
  108. $update = 1;
  109. }
  110. # Update the audio output device
  111. if ($update) {
  112. # Update, dying on timeout
  113. if ($DEBUG) {
  114. print STDERR 'Setting output to: ' . $DEVS{$state} . "\n";
  115. }
  116. setAudio($state);
  117. # Update the push time
  118. $pushLast = time();
  119. # Clear the update flag
  120. $update = 0;
  121. }
  122. # If the state has changed and the device has responded, save to disk
  123. if (!$update && $stateLast ne $state) {
  124. if ($DEBUG) {
  125. print STDERR 'New output state: ' . $stateLast . ' => ' . $state . "\n";
  126. }
  127. my ($fh, $tmp) = tempfile($OUTPUT_STATE . '.XXXXXXXX', 'UNLINK' => 0);
  128. print $fh $state . "\n";
  129. close($fh);
  130. rename($tmp, $OUTPUT_STATE);
  131. }
  132. }
  133. sub getAudio() {
  134. my $device = '';
  135. timeout $TIMEOUT => sub {
  136. $device = capture(@AUDIO_GET);
  137. };
  138. if ($@) {
  139. die("Audio get command timed out\n");
  140. }
  141. $device =~ s/\n$//;
  142. return $device;
  143. }
  144. sub setAudio($) {
  145. my ($state) = @_;
  146. timeout $TIMEOUT => sub {
  147. system(@AUDIO_SET, $DEVS{$state});
  148. };
  149. if ($@) {
  150. die("Audio set command timed out\n");
  151. }
  152. }