audio.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 $STATE_SOCK = 'AUDIO';
  21. my $OUTPUT_FILE = $DATA_DIR . $STATE_SOCK;
  22. my $OUTPUT_STATE = $OUTPUT_FILE . '_STATE';
  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. # State
  35. my $state = 'OFF';
  36. my $stateLast = $state;
  37. my $device = 'OFF';
  38. my $deviceLast = $device;
  39. my %exists = ();
  40. my $pushLast = 0;
  41. my $pullLast = time();
  42. my $update = 0;
  43. # Sockets
  44. DMX::stateSocket($STATE_SOCK);
  45. DMX::stateSubscribe($STATE_SOCK);
  46. # Always force the output to default at launch
  47. # In most components this happens before state subscription
  48. # but here we want to avoid making any changes while STATE is not available
  49. system(@AUDIO_SET, $DEVS{'DEFAULT'});
  50. # Loop forever
  51. while (1) {
  52. # Grab the current audio output device
  53. $deviceLast = $device;
  54. $device = capture(@AUDIO_GET);
  55. $device =~ s/\n$//;
  56. # If the device has changed, save the state to disk
  57. if ($deviceLast ne $device) {
  58. if ($DEBUG) {
  59. print STDERR 'New output device: ' . $deviceLast . ' => ' . $device . "\n";
  60. }
  61. my ($fh, $tmp) = tempfile($OUTPUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  62. print $fh $device . "\n";
  63. close($fh);
  64. rename($tmp, $OUTPUT_FILE);
  65. }
  66. # State is calculated; use newState to gather data
  67. my $newState = $state;
  68. # Wait for state updates
  69. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  70. if (defined($cmdState)) {
  71. $newState = $cmdState;
  72. $pullLast = time();
  73. }
  74. # Die if we don't see regular updates
  75. if (time() - $pullLast > $PULL_TIMEOUT) {
  76. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  77. }
  78. # Calculate the new state
  79. $stateLast = $state;
  80. if ($exists{'RAVE'}) {
  81. $state = 'RAVE';
  82. } elsif ($exists{'AUDIO_AMP'}) {
  83. $state = 'AMP';
  84. } else {
  85. $state = 'DEFAULT';
  86. }
  87. # Force updates on a periodic basis
  88. if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
  89. # Not for the audio output device
  90. #if ($DEBUG) {
  91. # print STDERR "Forcing periodic update\n";
  92. #}
  93. #$update = 1;
  94. }
  95. # Force updates when there is a physical state mistmatch
  96. if (!$update && $DEVS{$state} ne $device) {
  97. if ($DEBUG) {
  98. print STDERR 'State mismatch: ' . $device . ' => ' . $DEVS{$state} . "\n";
  99. }
  100. $update = 1;
  101. }
  102. # Update the audio output device
  103. if ($update) {
  104. # Update
  105. if ($DEBUG) {
  106. print STDERR 'Setting output to: ' . $DEVS{$state} . "\n";
  107. }
  108. system(@AUDIO_SET, $DEVS{$state});
  109. # Update the push time
  110. $pushLast = time();
  111. # Clear the update flag
  112. $update = 0;
  113. }
  114. # If the state has changed and the device has responded, save to disk
  115. if (!$update && $stateLast ne $state) {
  116. if ($DEBUG) {
  117. print STDERR 'New output state: ' . $stateLast . ' => ' . $state . "\n";
  118. }
  119. my ($fh, $tmp) = tempfile($OUTPUT_STATE . '.XXXXXXXX', 'UNLINK' => 0);
  120. print $fh $state . "\n";
  121. close($fh);
  122. rename($tmp, $OUTPUT_STATE);
  123. }
  124. }