rave.pl 9.7 KB

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