rifftrax.pl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. use Audio;
  13. # Config
  14. my $RIFF_PATH = `~/bin/video/mediaPath` . '/iTunes/iTunes Music/RiffTrax';
  15. my %RIFFS = (
  16. '82481' => {
  17. 'file' => 'Harry Potter/Harry Potter 1_ The Sorcerer\'s Stone.mp3',
  18. 'offset' => 100,
  19. 'rate' => 1.0,
  20. },
  21. '82469' => {
  22. 'file' => 'Harry Potter/Harry Potter 2_ The Chamber of Secrets.mp3',
  23. 'offset' => 133,
  24. 'rate' => 1.0,
  25. },
  26. '82474' => {
  27. 'file' => 'Harry Potter/Harry Potter 3_ The Prisoner of Azkaban.mp3',
  28. 'offset' => 92.25,
  29. 'rate' => 1.0,
  30. },
  31. '82471' => {
  32. 'file' => 'Harry Potter/Harry Potter 4_ The Goblet of Fire.mp3',
  33. 'offset' => 153,
  34. 'rate' => 1.0,
  35. },
  36. );
  37. # App config
  38. my $DATA_DIR = DMX::dataDir();
  39. my $OUTPUT_FILE = $DATA_DIR . 'RIFF';
  40. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  41. my $PULL_TIMEOUT = 60;
  42. my $DELAY = $PULL_TIMEOUT / 2;
  43. # Debug
  44. my $DEBUG = 0;
  45. if ($ENV{'DEBUG'}) {
  46. $DEBUG = 1;
  47. }
  48. # Sockets
  49. DMX::stateSocket($STATE_SOCK);
  50. DMX::stateSubscribe($STATE_SOCK);
  51. # State
  52. my $state = 'OFF';
  53. my $stateLast = $state;
  54. my $riff = 0;
  55. my $riffLast = $riff;
  56. my $url = '';
  57. my $urlLast = $url;
  58. my $nudge = 0;
  59. my %exists = ();
  60. my $pullLast = time();
  61. # Validate the path for all our riff files at launch
  62. foreach my $id (keys(%RIFFS)) {
  63. # Store the ID internally
  64. $RIFFS{$id}{'id'} = $id;
  65. # Construct an absolute path
  66. if ($RIFFS{$id}{'file'} =~ /^\//) {
  67. $RIFFS{$id}{'path'} = $RIFFS{$riff}{'file'};
  68. } else {
  69. $RIFFS{$id}{'path'} = $RIFF_PATH . '/' . $RIFFS{$id}{'file'};
  70. }
  71. # Ensure the path is valid
  72. if (!-r $RIFFS{$id}{'path'}) {
  73. die('Invalid riff file: ' . $id . ' => ' . $RIFFS{$id}{'path'} . "\n");
  74. }
  75. }
  76. # Loop forever
  77. while (1) {
  78. # Save the last RIFF and state
  79. $stateLast = $state;
  80. $riffLast = $riff;
  81. # Wait for state updates
  82. my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
  83. if (defined($cmdState)) {
  84. $state = $cmdState;
  85. $pullLast = time();
  86. }
  87. # Die if we don't see regular updates
  88. if (time() - $pullLast > $PULL_TIMEOUT) {
  89. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  90. }
  91. # Record nudges
  92. if ($state eq 'NUDGE_FORWARD') {
  93. $nudge += 0.1;
  94. next;
  95. } elsif ($state eq 'NUDGE_BACK') {
  96. $nudge -= 0.1;
  97. next;
  98. }
  99. # Save the last URL, so we can find changes
  100. # Do not delete the last URL if no new one is provided
  101. if ($exists{'PLAYING_URL'}) {
  102. $urlLast = $url;
  103. $url = $exists{'PLAYING_URL'};
  104. }
  105. # Update our state when the PLAYING_URL changes
  106. if ($url ne $urlLast) {
  107. # If a RIFF was active, clear it
  108. if ($riff) {
  109. if ($DEBUG) {
  110. print STDERR "RIFF complete\n";
  111. }
  112. Audio::drop('RIFF');
  113. $riff = 0;
  114. $nudge = 0;
  115. }
  116. # Activate a new RIFF, if applicable
  117. if ($url =~ /\/library\/parts\/(\d+)\//) {
  118. if (exists($RIFFS{$1})) {
  119. if ($DEBUG) {
  120. print STDERR 'Matched RIFF: ' . $1 . ' => ' . $RIFFS{$1}{'path'};
  121. }
  122. $riff = $1;
  123. Audio::addLoad('RIFF', $RIFFS{$riff}{'path'});
  124. Audio::rate('RIFF', $RIFFS{$riff}{'rate'});
  125. Audio::background('RIFF');
  126. }
  127. }
  128. }
  129. # If the RIFF has changed, save the state to disk
  130. if ($riff ne $riffLast) {
  131. my $new = '<none>';
  132. if ($riff) {
  133. $new = $RIFFS{$riff}{'id'};
  134. }
  135. my $old = $new;
  136. if ($riffLast) {
  137. $old = $RIFFS{$riffLast}{'id'};
  138. }
  139. if ($DEBUG) {
  140. print STDERR 'New RIFF: ' . $old . ' => ' . $new . "\n";
  141. }
  142. my ($fh, $tmp) = tempfile($OUTPUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  143. print $fh $new . "\n";
  144. close($fh);
  145. rename($tmp, $OUTPUT_FILE);
  146. }
  147. # Sync
  148. if ($riff) {
  149. if ($DEBUG) {
  150. print STDERR "Syncing\n";
  151. }
  152. # Play/pause to match the video
  153. if ($stateLast ne $state) {
  154. if ($state eq 'PAUSE') {
  155. Audio::pause('RIFF');
  156. } elsif ($state eq 'PLAY') {
  157. Audio::background('RIFF');
  158. }
  159. }
  160. # Get the video and riff playback positions
  161. my $riffTime = Audio::position('RIFF', undef());
  162. my $videoTime = $exists{'PLAYING_POSITION'};
  163. # Convert to seconds
  164. my @parts = split(/\-/, $videoTime);
  165. $videoTime = 0;
  166. if (scalar(@parts) == 3) {
  167. $videoTime = ($parts[0] * 3600) + ($parts[1] * 60) + $parts[2];
  168. } elsif (scalar(@parts) == 2) {
  169. $videoTime = ($parts[0] * 60) + $parts[1];
  170. } else {
  171. $videoTime = $parts[0];
  172. }
  173. # Calculate the adjusted riff time and the error between the riff and video times
  174. my $riffAdjTime = ($riffTime * $RIFFS{$riff}{'rate'}) - $RIFFS{$riff}{'offset'} + $nudge;
  175. my $error = $videoTime - $riffAdjTime;
  176. # Debug
  177. if ($DEBUG) {
  178. print STDERR "\tRiff time: " . $riffTime . "\n";
  179. print STDERR "\tVideo time: " . $videoTime . "\n";
  180. print STDERR "\tOffset: " . $RIFFS{$riff}{'offset'} . "\n";
  181. print STDERR "\tRate: " . $RIFFS{$riff}{'rate'} . "\n";
  182. print STDERR "\tNudge: " . $nudge . "\n";
  183. print STDERR "\tAdjusted riff time: " . $riffAdjTime . "\n";
  184. print STDERR "\tError: " . $error . "\n";
  185. }
  186. # Adjust the riff position if we're off by 1 second or more
  187. if (abs($error) >= 1) {
  188. my $newPos = $riffTime + $error;
  189. if ($newPos < 0) {
  190. Audio::pause('RIFF');
  191. } else {
  192. # Play/pause as needed, before we adjust
  193. # The state-change detection should handle most of this
  194. # But double-check while adjusting in case things get out-of-sync
  195. my $playing = Audio::playing('RIFF');
  196. if ($state eq 'PLAY' && !$playing) {
  197. Audio::background('RIFF');
  198. } elsif ($state ne 'PLAY' && $playing) {
  199. Audio::pause('RIFF');
  200. }
  201. # Always adjust
  202. Audio::position('RIFF', $newPos);
  203. }
  204. }
  205. }
  206. }