rifftrax.pl 5.8 KB

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