rave.pl 9.4 KB

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