audio.pl 2.9 KB

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