rave.pl 9.2 KB

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