audio.pl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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{'PLEX'}) {
  89. $state = 'AMP';
  90. } else {
  91. $state = 'DEFAULT';
  92. }
  93. # Force updates on a periodic basis
  94. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  95. # Not for the audio output device
  96. #if ($DEBUG) {
  97. # print STDERR "Forcing periodic update\n";
  98. #}
  99. #$update = 1;
  100. }
  101. # Force updates when there is a physical state mistmatch
  102. if (!$update && $DEVS{$state} ne $device) {
  103. if ($DEBUG) {
  104. print STDERR 'State mismatch: ' . $device . ' => ' . $DEVS{$state} . "\n";
  105. }
  106. $update = 1;
  107. }
  108. # Update the audio output device
  109. if ($update) {
  110. # Update, dying on timeout
  111. if ($DEBUG) {
  112. print STDERR 'Setting output to: ' . $DEVS{$state} . "\n";
  113. }
  114. setAudio($state);
  115. # Update the push time
  116. $pushLast = time();
  117. # Clear the update flag
  118. $update = 0;
  119. }
  120. # If the state has changed and the device has responded, save to disk
  121. if (!$update && $stateLast ne $state) {
  122. if ($DEBUG) {
  123. print STDERR 'New output state: ' . $stateLast . ' => ' . $state . "\n";
  124. }
  125. my ($fh, $tmp) = tempfile($OUTPUT_STATE . '.XXXXXXXX', 'UNLINK' => 0);
  126. print $fh $state . "\n";
  127. close($fh);
  128. rename($tmp, $OUTPUT_STATE);
  129. }
  130. }
  131. sub getAudio() {
  132. my $device = '';
  133. timeout $TIMEOUT => sub {
  134. $device = capture(@AUDIO_GET);
  135. };
  136. if ($@) {
  137. die("Audio get command timed out\n");
  138. }
  139. $device =~ s/\n$//;
  140. return $device;
  141. }
  142. sub setAudio($) {
  143. my ($state) = @_;
  144. timeout $TIMEOUT => sub {
  145. system(@AUDIO_SET, $DEVS{$state});
  146. };
  147. if ($@) {
  148. die("Audio set command timed out\n");
  149. }
  150. }