rave.pl 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use POSIX;
  5. use File::Touch;
  6. use File::Basename;
  7. use Time::HiRes qw( usleep sleep time );
  8. use IPC::System::Simple qw( system capture );
  9. use Sys::Hostname;
  10. # Local modules
  11. use Cwd qw(abs_path);
  12. use File::Basename qw(dirname);
  13. use lib dirname(abs_path($0));
  14. use DMX;
  15. use Audio;
  16. # Effect Prototypes
  17. sub red_alert($$$);
  18. sub lsr_init($$$);
  19. sub lsr_run($$$);
  20. sub lsr_loop($$$);
  21. # User config
  22. my $MEDIA_PATH = `~/bin/video/mediaPath` . '/DMX';
  23. my @CHANNELS = ();
  24. my %EFFECTS = ();
  25. my %FILES = ();
  26. # Host-specific config
  27. my $HOST = Sys::Hostname::hostname();
  28. if ($HOST =~ /loki/i) {
  29. # Rave-controlled channels
  30. push(@CHANNELS, 1, 2, 4, 5, 6, 7, 8, 9, 13, 14, 15);
  31. # Red Alert
  32. $FILES{'RED_ALERT'} = 'DMX/Red Alert.mp3';
  33. $EFFECTS{'RED_ALERT'} = { 'cmd' => \&red_alert };
  34. # Light-switch rave
  35. $EFFECTS{'LSR'} = { 'cmd' => \&lsr_init, 'next' => \&lsr_run, 'loop' => \&lsr_loop, 'done' => \&lsr_done };
  36. $FILES{'LSR'} = 'DMX/Rave.mp3';
  37. } elsif ($HOST =~ /beddy/i) {
  38. } elsif ($HOST =~ /heady/i) {
  39. }
  40. # Utility prototypes
  41. sub ampWait($$$);
  42. # App config
  43. my $DATA_DIR = DMX::dataDir();
  44. my $OUTPUT_FILE = $DATA_DIR . 'RAVE_CMD';
  45. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  46. my $RAVE_FILE = $DATA_DIR . 'RAVE';
  47. my $EFFECT_FILE = $DATA_DIR . 'EFFECT';
  48. my $PUSH_TIMEOUT = 20;
  49. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  50. my $DELAY = $PULL_TIMEOUT / 2;
  51. my $AMP_DELAY = 6;
  52. my $AMP_BOOTING = 0;
  53. # Debug
  54. my $DEBUG = 0;
  55. if ($ENV{'DEBUG'}) {
  56. $DEBUG = 1;
  57. }
  58. # Load all our audio files
  59. Audio::init();
  60. foreach my $file (keys(%FILES)) {
  61. Audio::addLoad($file, $FILES{$file});
  62. }
  63. # Sockets
  64. DMX::stateSocket($STATE_SOCK);
  65. DMX::stateSubscribe($STATE_SOCK);
  66. # State
  67. my %exists = ();
  68. my %last = ();
  69. my $pullLast = time();
  70. my $update = 0;
  71. my $PID = undef();
  72. my $NAME = undef();
  73. my $NEXT = undef();
  74. my $PID_DATA = undef();
  75. # Always clear the RAVE and EFFECT files
  76. if (-e $RAVE_FILE) {
  77. unlink($RAVE_FILE);
  78. }
  79. if (-e $EFFECT_FILE) {
  80. unlink($EFFECT_FILE);
  81. }
  82. # Loop forever
  83. while (1) {
  84. # State is transient during a RAVE
  85. my $newState = 'OFF';
  86. %last = %exists;
  87. # Reduce the select delay when we're processing background effects
  88. # We don't want to hang waiting for event updates while we're running the show
  89. my $delay = $DELAY;
  90. if (defined($PID)) {
  91. $delay = 0.001;
  92. } elsif (defined($NEXT)) {
  93. $delay = 0.25;
  94. }
  95. # Wait for state updates
  96. {
  97. my $cmdState = DMX::readState($delay, \%exists, undef(), undef());
  98. if (defined($cmdState)) {
  99. $newState = $cmdState;
  100. $pullLast = time();
  101. }
  102. }
  103. # Die if we don't see regular updates
  104. if (time() - $pullLast > $PULL_TIMEOUT) {
  105. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  106. }
  107. # Handle "STOP" commands
  108. if ($newState eq 'STOP') {
  109. if (defined($PID)) {
  110. if ($DEBUG) {
  111. print STDERR "Ending background processing\n";
  112. }
  113. Audio::stop(undef());
  114. kill(SIGTERM, $PID);
  115. }
  116. $update = 1;
  117. }
  118. # Reap zombie children
  119. if (defined($PID)) {
  120. my $kid = waitpid($PID, WNOHANG);
  121. if ($kid > 0) {
  122. if ($DEBUG) {
  123. print STDERR 'Reaped child: ' . $PID . "\n";
  124. }
  125. # Run the cleanup routine (if any)
  126. if ($EFFECTS{$NAME}{'done'}) {
  127. $EFFECTS{$NAME}{'done'}($NAME, \%exists, $EFFECTS{$NAME});
  128. }
  129. # Forget our local bypass state
  130. $PID = undef();
  131. $NAME = undef();
  132. $NEXT = undef();
  133. $PID_DATA = undef();
  134. # Clear the RAVE and EFFECT flags
  135. if (-e $RAVE_FILE) {
  136. unlink($RAVE_FILE);
  137. }
  138. if (-e $EFFECT_FILE) {
  139. unlink($EFFECT_FILE);
  140. }
  141. }
  142. }
  143. # Continue special processing
  144. if (defined($NEXT)) {
  145. $NEXT->($NAME, \%exists, $EFFECTS{$NAME});
  146. next;
  147. }
  148. # Handle effects by name
  149. if (defined($EFFECTS{$newState})) {
  150. # Initiate the EFFECT state
  151. touch($EFFECT_FILE);
  152. # Store the effect name
  153. $NAME = $newState;
  154. # Dispatch the effect
  155. $EFFECTS{$NAME}{'cmd'}($NAME, \%exists, $EFFECTS{$NAME});
  156. # Clear the EFFECT state if there is no background process
  157. if (!defined($PID)) {
  158. if (-e $EFFECT_FILE) {
  159. unlink($EFFECT_FILE);
  160. }
  161. }
  162. next;
  163. }
  164. # Clear the RAVE and EFFECT flags, just in case
  165. if (-e $RAVE_FILE) {
  166. unlink($RAVE_FILE);
  167. die("Unexpected RAVE file\n");
  168. }
  169. if (-e $EFFECT_FILE) {
  170. unlink($EFFECT_FILE);
  171. die("Unexpected EFFECT file\n");
  172. }
  173. }
  174. # ======================================
  175. # Utility routines
  176. # ======================================
  177. sub runApplescript($) {
  178. my ($script) = @_;
  179. if ($DEBUG) {
  180. print STDERR 'Running AppleScript: ' . $script . "\n";
  181. }
  182. my $retval = capture('osascript', '-e', $script);
  183. if ($DEBUG) {
  184. print STDERR "\tAppleScript result: " . $retval . "\n";
  185. }
  186. return $retval;
  187. }
  188. sub ampWait($$$) {
  189. my ($name, $exists, $params) = @_;
  190. if ($DEBUG) {
  191. print STDERR "ampWait()\n";
  192. }
  193. # Just loop until the amp is up, then set a new "next" handler
  194. if ($exists{'AMPLIFIER'}) {
  195. if ($DEBUG) {
  196. print STDERR "\tAmplifier ready\n";
  197. }
  198. # Once the amp is up, be sure we've started "RAVE" mode for audio output
  199. if ($exists{'AUDIO_STATE'} eq 'RAVE') {
  200. if ($DEBUG) {
  201. print STDERR "\tAudio device ready\n";
  202. }
  203. # Select the next function
  204. if ($params->{'next'}) {
  205. $NEXT = $params->{'next'};
  206. } else {
  207. $NEXT = undef();
  208. }
  209. # Always wait just a bit for good luck
  210. sleep($AMP_DELAY / 4);
  211. }
  212. # Wait for the amp to boot if it wasn't running when we first checked
  213. if ($AMP_BOOTING) {
  214. if ($DEBUG) {
  215. print STDERR "\tWaiting " . $AMP_DELAY . " seconds for amp to boot\n";
  216. }
  217. sleep($AMP_DELAY);
  218. }
  219. # Reset the amp boot delay, now that it's running
  220. $AMP_BOOTING = 0;
  221. } else {
  222. # The amp wasn't up when we checked, so it will need a boot delay
  223. $AMP_BOOTING = 1;
  224. }
  225. # Do not pass GO, do not collect $200
  226. return 1;
  227. }
  228. # ======================================
  229. # Effects routines
  230. # ======================================
  231. sub red_alert($$$) {
  232. my ($name, $exists, $params) = @_;
  233. if ($DEBUG) {
  234. print STDERR "red_alert()\n";
  235. }
  236. my $ramp = 450;
  237. my $sleep = $ramp;
  238. # Bring the B & G channels down to 0
  239. my @other = ();
  240. push(@other, { 'channel' => 13, 'value' => 0, 'time' => 0 });
  241. push(@other, { 'channel' => 15, 'value' => 0, 'time' => 0 });
  242. foreach my $data (@other) {
  243. DMX::dim($data);
  244. }
  245. # Set the high/low values for the ramp
  246. my %high = ('channel' => 14, 'value' => 255, 'time' => $ramp);
  247. my %low = %high;
  248. $low{'value'} = 64;
  249. # Three blasts
  250. for (my $i = 0 ; $i < 3 ; $i++) {
  251. DMX::dim(\%high);
  252. Audio::play($name);
  253. DMX::dim(\%low);
  254. usleep($sleep * 1000);
  255. }
  256. # Follow through on the loop
  257. return 0;
  258. }
  259. sub lsr_init($$$) {
  260. my ($name, $exists, $params) = @_;
  261. if ($DEBUG) {
  262. print STDERR "lsr_init()\n";
  263. }
  264. # Initiate the RAVE state
  265. DMX::say('Light switch rave: initiated');
  266. touch($RAVE_FILE);
  267. # Save data for future runs
  268. my %data = ();
  269. $PID_DATA = \%data;
  270. # Initialize the channels hash
  271. my %chans = ();
  272. foreach my $chan (@CHANNELS) {
  273. $chans{$chan} = 0;
  274. }
  275. $data{'channels'} = \%chans;
  276. $data{'num_channels'} = scalar(keys(%chans));
  277. $data{'live_channels'} = 0;
  278. # Dim while we wait
  279. my @data_set = ();
  280. foreach my $chan (@CHANNELS) {
  281. push(@data_set, { 'channel' => $chan, 'value' => 0, 'time' => $AMP_DELAY * 1000 });
  282. }
  283. DMX::applyDataset(\@data_set, 'RAVE', $OUTPUT_FILE);
  284. # Wait for the amp to power up
  285. $NEXT = \&ampWait;
  286. # Do not pass GO, do not collect $200
  287. return 1;
  288. }
  289. sub lsr_run($$$) {
  290. my ($name, $exists, $params) = @_;
  291. if ($DEBUG) {
  292. print STDERR "lsr_run()\n";
  293. }
  294. # Play a short burst of silence to get all the audio devices in-sync
  295. Audio::play('SILENCE');
  296. # Play the sound in a child (i.e. in the background)
  297. $PID = fork();
  298. if (defined($PID) && $PID == 0) {
  299. Audio::play($name);
  300. exit(0);
  301. }
  302. # Record our start time
  303. $PID_DATA->{'start'} = Time::HiRes::time();
  304. # Start the main loop
  305. $NEXT = $params->{'loop'};
  306. # Do not pass GO, do not collect $200
  307. return 1;
  308. }
  309. sub lsr_loop($$$) {
  310. my ($name, $exists, $params) = @_;
  311. if ($DEBUG) {
  312. print STDERR "lsr_loop()\n";
  313. }
  314. # Config
  315. my $max_dur = 375;
  316. my $max_val = 255;
  317. my $reserve = 0.75;
  318. my $ramp_dur = 10.20;
  319. my $hit_pos = 43.15;
  320. my $hit_dur = 100;
  321. my $fade_pos = 43.65;
  322. my $fade_dur = 2000;
  323. # How long have we been playing
  324. my $elapsed = Time::HiRes::time() - $PID_DATA->{'start'};
  325. if ($DEBUG) {
  326. print STDERR 'Elapsed: ' . $elapsed . "\n";
  327. }
  328. # Ramp up the number of channels in our effect
  329. if ($elapsed < $ramp_dur) {
  330. my $ratio = $PID_DATA->{'live_channels'} / ($PID_DATA->{'num_channels'} * (1 - $reserve));
  331. if ($PID_DATA->{'live_channels'} < 1 || $ratio < $elapsed / $ramp_dur) {
  332. my @keys = keys(%{ $PID_DATA->{'channels'} });
  333. my $index = int(rand($PID_DATA->{'num_channels'}));
  334. while ($PID_DATA->{'channels'}->{ $keys[$index] } > 0) {
  335. $index = int(rand($PID_DATA->{'num_channels'}));
  336. }
  337. $PID_DATA->{'channels'}->{ $keys[$index] } = 1;
  338. $PID_DATA->{'live_channels'}++;
  339. }
  340. } else {
  341. if ($PID_DATA->{'live_channels'} < $PID_DATA->{'num_channels'}) {
  342. foreach my $chan (keys(%{ $PID_DATA->{'channels'} })) {
  343. $PID_DATA->{'channels'}->{$chan} = 1;
  344. }
  345. $PID_DATA->{'live_channels'} = $PID_DATA->{'num_channels'};
  346. }
  347. }
  348. # Prepare data for each channel
  349. my @data_set = ();
  350. my $wait = $max_dur;
  351. foreach my $chan (keys(%{ $PID_DATA->{'channels'} })) {
  352. my $val = 0;
  353. my $dur = 0;
  354. # Random data on enabled channels until the fade
  355. if ($elapsed < $hit_pos) {
  356. if ($PID_DATA->{'channels'}->{$chan} > 0) {
  357. $dur = int(rand($max_dur));
  358. $val = int(rand($max_val));
  359. }
  360. } elsif ($elapsed < $fade_pos) {
  361. $val = $max_val;
  362. $dur = $hit_dur;
  363. $wait = ($fade_pos - $hit_pos) * 1000;
  364. } else {
  365. $val = 0;
  366. $dur = $fade_dur;
  367. $wait = $dur;
  368. }
  369. push(@data_set, { 'channel' => $chan, 'value' => $val, 'time' => $dur });
  370. }
  371. # Push the data set
  372. DMX::applyDataset(\@data_set, 'RAVE', $OUTPUT_FILE);
  373. # Wait for the fade interval
  374. usleep($wait * 1000);
  375. }
  376. sub lsr_done($$$) {
  377. my ($name, $exists, $params) = @_;
  378. if ($DEBUG) {
  379. print STDERR "lsr_done()\n";
  380. }
  381. DMX::say('Light switch rave: terminated');
  382. }