rave.pl 9.7 KB

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