rave.pl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. 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::stopAll();
  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 ampWait($$$) {
  177. my ($name, $exists, $params) = @_;
  178. if ($DEBUG) {
  179. print STDERR "ampWait()\n";
  180. }
  181. # Just loop until the amp is up, then set a new "next" handler
  182. if ($exists{'AMPLIFIER'}) {
  183. if ($DEBUG) {
  184. print STDERR "\tAmplifier ready\n";
  185. }
  186. # Once the amp is up, be sure we've started "RAVE" mode for audio output
  187. if ($exists{'AUDIO_STATE'} eq 'RAVE') {
  188. if ($DEBUG) {
  189. print STDERR "\tAudio device ready\n";
  190. }
  191. # Select the next function
  192. if ($params->{'next'}) {
  193. $NEXT = $params->{'next'};
  194. } else {
  195. $NEXT = undef();
  196. }
  197. # Always wait just a bit for good luck
  198. sleep($AMP_DELAY / 4);
  199. }
  200. # Wait for the amp to boot if it wasn't running when we first checked
  201. if ($AMP_BOOTING) {
  202. if ($DEBUG) {
  203. print STDERR "\tWaiting " . $AMP_DELAY . " seconds for amp to boot\n";
  204. }
  205. sleep($AMP_DELAY);
  206. }
  207. # Reset the amp boot delay, now that it's running
  208. $AMP_BOOTING = 0;
  209. } else {
  210. # The amp wasn't up when we checked, so it will need a boot delay
  211. $AMP_BOOTING = 1;
  212. }
  213. # Do not pass GO, do not collect $200
  214. return 1;
  215. }
  216. # ======================================
  217. # Effects routines
  218. # ======================================
  219. sub red_alert($$$) {
  220. my ($name, $exists, $params) = @_;
  221. if ($DEBUG) {
  222. print STDERR "red_alert()\n";
  223. }
  224. my $ramp = 450;
  225. my $sleep = $ramp;
  226. # Bring the B & G channels down to 0
  227. my @other = ();
  228. push(@other, { 'channel' => 13, 'value' => 0, 'time' => 0 });
  229. push(@other, { 'channel' => 15, 'value' => 0, 'time' => 0 });
  230. foreach my $data (@other) {
  231. DMX::dim($data);
  232. }
  233. # Set the high/low values for the ramp
  234. my %high = ('channel' => 14, 'value' => 255, 'time' => $ramp);
  235. my %low = %high;
  236. $low{'value'} = 64;
  237. # Three blasts
  238. for (my $i = 0 ; $i < 3 ; $i++) {
  239. DMX::dim(\%high);
  240. Audio::play($name);
  241. DMX::dim(\%low);
  242. usleep($sleep * 1000);
  243. }
  244. # Follow through on the loop
  245. return 0;
  246. }
  247. sub lsr_init($$$) {
  248. my ($name, $exists, $params) = @_;
  249. if ($DEBUG) {
  250. print STDERR "lsr_init()\n";
  251. }
  252. # Initiate the RAVE state
  253. DMX::say('Light switch rave: initiated');
  254. touch($RAVE_FILE);
  255. # Save data for future runs
  256. my %data = ();
  257. $PID_DATA = \%data;
  258. # Initialize the channels hash
  259. my %chans = ();
  260. foreach my $chan (@CHANNELS) {
  261. $chans{$chan} = 0;
  262. }
  263. $data{'channels'} = \%chans;
  264. $data{'num_channels'} = scalar(keys(%chans));
  265. $data{'live_channels'} = 0;
  266. # Dim while we wait
  267. my @data_set = ();
  268. foreach my $chan (@CHANNELS) {
  269. push(@data_set, { 'channel' => $chan, 'value' => 0, 'time' => $AMP_DELAY * 1000 });
  270. }
  271. DMX::applyDataset(\@data_set, 'RAVE', $OUTPUT_FILE);
  272. # Wait for the amp to power up
  273. $NEXT = \&ampWait;
  274. # Do not pass GO, do not collect $200
  275. return 1;
  276. }
  277. sub lsr_run($$$) {
  278. my ($name, $exists, $params) = @_;
  279. if ($DEBUG) {
  280. print STDERR "lsr_run()\n";
  281. }
  282. # Play a short burst of silence to get all the audio devices in-sync
  283. Audio::play('SILENCE');
  284. # Play the sound in a child (i.e. in the background)
  285. $PID = fork();
  286. if (defined($PID) && $PID == 0) {
  287. Audio::play($name);
  288. exit(0);
  289. }
  290. # Record our start time
  291. $PID_DATA->{'start'} = Time::HiRes::time();
  292. # Start the main loop
  293. $NEXT = $params->{'loop'};
  294. # Do not pass GO, do not collect $200
  295. return 1;
  296. }
  297. sub lsr_loop($$$) {
  298. my ($name, $exists, $params) = @_;
  299. if ($DEBUG) {
  300. print STDERR "lsr_loop()\n";
  301. }
  302. # Config
  303. my $max_dur = 375;
  304. my $max_val = 255;
  305. my $reserve = 0.75;
  306. my $ramp_dur = 10.20;
  307. my $hit_pos = 43.15;
  308. my $hit_dur = 100;
  309. my $fade_pos = 43.65;
  310. my $fade_dur = 2000;
  311. # How long have we been playing
  312. my $elapsed = Time::HiRes::time() - $PID_DATA->{'start'};
  313. if ($DEBUG) {
  314. print STDERR 'Elapsed: ' . $elapsed . "\n";
  315. }
  316. # Ramp up the number of channels in our effect
  317. if ($elapsed < $ramp_dur) {
  318. my $ratio = $PID_DATA->{'live_channels'} / ($PID_DATA->{'num_channels'} * (1 - $reserve));
  319. if ($PID_DATA->{'live_channels'} < 1 || $ratio < $elapsed / $ramp_dur) {
  320. my @keys = keys(%{ $PID_DATA->{'channels'} });
  321. my $index = int(rand($PID_DATA->{'num_channels'}));
  322. while ($PID_DATA->{'channels'}->{ $keys[$index] } > 0) {
  323. $index = int(rand($PID_DATA->{'num_channels'}));
  324. }
  325. $PID_DATA->{'channels'}->{ $keys[$index] } = 1;
  326. $PID_DATA->{'live_channels'}++;
  327. }
  328. } else {
  329. if ($PID_DATA->{'live_channels'} < $PID_DATA->{'num_channels'}) {
  330. foreach my $chan (keys(%{ $PID_DATA->{'channels'} })) {
  331. $PID_DATA->{'channels'}->{$chan} = 1;
  332. }
  333. $PID_DATA->{'live_channels'} = $PID_DATA->{'num_channels'};
  334. }
  335. }
  336. # Prepare data for each channel
  337. my @data_set = ();
  338. my $wait = $max_dur;
  339. foreach my $chan (keys(%{ $PID_DATA->{'channels'} })) {
  340. my $val = 0;
  341. my $dur = 0;
  342. # Random data on enabled channels until the fade
  343. if ($elapsed < $hit_pos) {
  344. if ($PID_DATA->{'channels'}->{$chan} > 0) {
  345. $dur = int(rand($max_dur));
  346. $val = int(rand($max_val));
  347. }
  348. } elsif ($elapsed < $fade_pos) {
  349. $val = $max_val;
  350. $dur = $hit_dur;
  351. $wait = ($fade_pos - $hit_pos) * 1000;
  352. } else {
  353. $val = 0;
  354. $dur = $fade_dur;
  355. $wait = $dur;
  356. }
  357. push(@data_set, { 'channel' => $chan, 'value' => $val, 'time' => $dur });
  358. }
  359. # Push the data set
  360. DMX::applyDataset(\@data_set, 'RAVE', $OUTPUT_FILE);
  361. # Wait for the fade interval
  362. usleep($wait * 1000);
  363. }
  364. sub lsr_done($$$) {
  365. my ($name, $exists, $params) = @_;
  366. if ($DEBUG) {
  367. print STDERR "lsr_done()\n";
  368. }
  369. DMX::say('Light switch rave: terminated');
  370. }