rave.pl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use POSIX;
  5. use File::Touch;
  6. use Time::HiRes qw( usleep );
  7. # Local modules
  8. use Cwd qw(abs_path);
  9. use File::Basename qw(dirname);
  10. use lib dirname(abs_path($0));
  11. use DMX;
  12. # Prototypes
  13. sub red_alert($$);
  14. sub rave_init($$);
  15. sub lsr_loop();
  16. # User config
  17. my @CHANNELS = (1, 2, 4, 5, 6, 7, 8, 9, 13, 14, 15);
  18. my %EFFECTS = (
  19. 'RED_ALERT' => { 'cmd' => \&red_alert },
  20. 'LSR' => { 'cmd' => \&rave_init, 'file' => '/mnt/media/DMX/Rave.mp3', 'next' => \&lsr_loop },
  21. );
  22. # App config
  23. my $DATA_DIR = DMX::dataDir();
  24. my $OUTPUT_FILE = $DATA_DIR . 'RAVE_CMD';
  25. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  26. my $RAVE_FILE = $DATA_DIR . 'RAVE';
  27. my $PUSH_TIMEOUT = 20;
  28. my $PULL_TIMEOUT = $PUSH_TIMEOUT * 3;
  29. my $DELAY = $PULL_TIMEOUT / 2;
  30. # Debug
  31. my $DEBUG = 0;
  32. if ($ENV{'DEBUG'}) {
  33. $DEBUG = 1;
  34. }
  35. # Construct a list of valid states
  36. my %VALID = ('STOP' => 1);
  37. foreach my $key (keys(%EFFECTS)) {
  38. $VALID{$key} = 1;
  39. }
  40. # Sockets
  41. DMX::stateSocket($STATE_SOCK);
  42. DMX::stateSubscribe($STATE_SOCK);
  43. # State
  44. my %exists = ();
  45. my $pullLast = time();
  46. my $update = 0;
  47. my $PID = undef();
  48. my $EFFECT = undef();
  49. my $PID_DATA = undef();
  50. # Always clear the RAVE file
  51. if (-e $RAVE_FILE) {
  52. unlink($RAVE_FILE);
  53. }
  54. # Loop forever
  55. while (1) {
  56. # State is transient during a RAVE
  57. my $newState = 'OFF';
  58. # Reduce the select delay when we're processing background effects
  59. # We don't want to hang waiting for event updates while we're running the show
  60. my $delay = $DELAY;
  61. if (defined($PID)) {
  62. $delay = 0.001;
  63. }
  64. # Wait for state updates
  65. {
  66. my %existsTmp = ();
  67. my $cmdState = DMX::readState($delay, \%existsTmp, undef(), \%VALID);
  68. if (defined($cmdState)) {
  69. $newState = $cmdState;
  70. $pullLast = time();
  71. }
  72. # Only record valid exists hashes
  73. if (scalar(keys(%existsTmp)) < 1) {
  74. %exists = %existsTmp;
  75. }
  76. }
  77. # Die if we don't see regular updates
  78. if (time() - $pullLast > $PULL_TIMEOUT) {
  79. die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
  80. }
  81. # Handle "STOP" commands
  82. if ($newState eq 'STOP') {
  83. if (defined($PID)) {
  84. if ($DEBUG) {
  85. print STDERR "Ending background processing\n";
  86. }
  87. kill(SIGTERM, $PID);
  88. }
  89. $update = 1;
  90. }
  91. # Reap zombie children
  92. if (defined($PID)) {
  93. my $kid = waitpid($PID, WNOHANG);
  94. if ($kid > 0) {
  95. if ($DEBUG) {
  96. print STDERR 'Reaped child: ' . $PID . "\n";
  97. }
  98. # Forget our local bypass state
  99. $PID = undef();
  100. $EFFECT = undef();
  101. $PID_DATA = undef();
  102. # Clear the RAVE flag
  103. if (-e $RAVE_FILE) {
  104. unlink($RAVE_FILE);
  105. }
  106. }
  107. }
  108. # Continue special processing
  109. if (defined($EFFECT)) {
  110. $EFFECT->();
  111. next;
  112. }
  113. # Handle effects by name
  114. if (defined($EFFECTS{$newState})) {
  115. # Initiate the RAVE state
  116. touch($RAVE_FILE);
  117. # Dispatch the effect
  118. $EFFECTS{$newState}{'cmd'}(\%exists, $EFFECTS{$newState});
  119. # Clear the RAVE state if there is no background process
  120. if (!defined($PID)) {
  121. unlink($RAVE_FILE);
  122. }
  123. next;
  124. }
  125. # Clear the RAVE flag, just in case
  126. if (-e $RAVE_FILE) {
  127. unlink($RAVE_FILE);
  128. die("Unexpected RAVE file\n");
  129. }
  130. }
  131. # ======================================
  132. # Effects routines
  133. # ======================================
  134. sub red_alert($$) {
  135. my ($exists, $effect) = @_;
  136. if ($DEBUG) {
  137. print STDERR "red_alert()\n";
  138. }
  139. my $file = '/mnt/media/DMX/Red Alert.mp3';
  140. my @sound = ('afplay', $file);
  141. my $ramp = 450;
  142. my $sleep = $ramp;
  143. # Stat the file to bring the network up-to-date
  144. stat($file);
  145. # Bring the B & G channels down to 0
  146. my @other = ();
  147. push(@other, { 'channel' => 13, 'value' => 0, 'time' => 0 });
  148. push(@other, { 'channel' => 15, 'value' => 0, 'time' => 0 });
  149. foreach my $data (@other) {
  150. DMX::dim($data);
  151. }
  152. my %high = ('channel' => 14, 'value' => 255, 'time' => $ramp);
  153. my %low = %high;
  154. $low{'value'} = 64;
  155. DMX::dim(\%high);
  156. system(@sound);
  157. DMX::dim(\%low);
  158. usleep($sleep * 1000);
  159. DMX::dim(\%high);
  160. system(@sound);
  161. DMX::dim(\%low);
  162. usleep($sleep * 1000);
  163. DMX::dim(\%high);
  164. system(@sound);
  165. DMX::dim(\%low);
  166. usleep($sleep * 1000);
  167. # Follow through on the loop
  168. return 0;
  169. }
  170. sub rave_init($$) {
  171. my ($exists, $effect) = @_;
  172. if ($DEBUG) {
  173. print STDERR "rave_init()\n";
  174. }
  175. if (!defined($effect->{'file'}) || !-r $effect->{'file'}) {
  176. die('Invalid RAVE audio file: ' . $effect->{'file'} . "\n");
  177. }
  178. # Config
  179. my $SND_APP = 'afplay';
  180. my $SILENCE = '/mnt/media/DMX/Silence.wav';
  181. my $SIL_DELAY = 1.3;
  182. my $AMP_SHORT = 5;
  183. my $AMP_LONG = $AMP_SHORT + 5;
  184. # Stat the file to bring the network up-to-date
  185. stat($SILENCE);
  186. stat($effect->{'file'});
  187. # Setup our loop handler
  188. if ($effect->{'next'}) {
  189. $EFFECT = $effect->{'next'};
  190. }
  191. # Save data for future runs
  192. my %data = ();
  193. $PID_DATA = \%data;
  194. # Initialize the channels hash
  195. my %chans = ();
  196. foreach my $chan (@CHANNELS) {
  197. $chans{$chan} = 0;
  198. }
  199. $data{'channels'} = \%chans;
  200. $data{'num_channels'} = scalar(keys(%chans));
  201. $data{'live_channels'} = 0;
  202. # Reduce the amp delay if the amp is already on
  203. my $amp_wait = $AMP_LONG;
  204. if ($exists->{'AMPLIFIER'}) {
  205. $amp_wait = $AMP_SHORT;
  206. }
  207. # Dim while we wait
  208. my @data_set = ();
  209. foreach my $chan (@CHANNELS) {
  210. push(@data_set, { 'channel' => $chan, 'value' => 0, 'time' => $amp_wait / 2 * 1000 });
  211. }
  212. DMX::applyDataset(\@data_set, 'RAVE', $OUTPUT_FILE);
  213. # Wait for the amp to power up
  214. if ($DEBUG) {
  215. print STDERR 'Waiting ' . $amp_wait . " seconds for the amp to boot\n";
  216. }
  217. sleep($amp_wait - $SIL_DELAY);
  218. # Play a short burst of silence to get all the audio in-sync
  219. my @sound = ($SND_APP, $SILENCE);
  220. system(@sound);
  221. # Play the sound in a child (i.e. in the background)
  222. $PID = fork();
  223. if (defined($PID) && $PID == 0) {
  224. my @sound = ($SND_APP, $effect->{'file'});
  225. exec(@sound)
  226. or die('Unable to play sound: ' . join(' ', @sound) . "\n");
  227. }
  228. # Record our start time
  229. $data{'start'} = Time::HiRes::time();
  230. # Do not pass GO, do not collect $200
  231. return 1;
  232. }
  233. sub lsr_loop() {
  234. if ($DEBUG) {
  235. print STDERR "lsr_loop()\n";
  236. }
  237. # Config
  238. my $max_dur = 375;
  239. my $max_val = 255;
  240. my $reserve = 0.75;
  241. my $ramp_dur = 10.20;
  242. my $hit_pos = 43.15;
  243. my $hit_dur = 100;
  244. my $fade_pos = 43.65;
  245. my $fade_dur = 2000;
  246. # How long have we been playing
  247. my $elapsed = Time::HiRes::time() - $PID_DATA->{'start'};
  248. if ($DEBUG) {
  249. print STDERR 'Elapsed: ' . $elapsed . "\n";
  250. }
  251. # Ramp up the number of channels in our effect
  252. if ($elapsed < $ramp_dur) {
  253. my $ratio = $PID_DATA->{'live_channels'} / ($PID_DATA->{'num_channels'} * (1 - $reserve));
  254. if ($PID_DATA->{'live_channels'} < 1 || $ratio < $elapsed / $ramp_dur) {
  255. my @keys = keys(%{ $PID_DATA->{'channels'} });
  256. my $index = int(rand($PID_DATA->{'num_channels'}));
  257. while ($PID_DATA->{'channels'}->{ $keys[$index] } > 0) {
  258. $index = int(rand($PID_DATA->{'num_channels'}));
  259. }
  260. $PID_DATA->{'channels'}->{ $keys[$index] } = 1;
  261. $PID_DATA->{'live_channels'}++;
  262. }
  263. } else {
  264. if ($PID_DATA->{'live_channels'} < $PID_DATA->{'num_channels'}) {
  265. foreach my $chan (keys(%{ $PID_DATA->{'channels'} })) {
  266. $PID_DATA->{'channels'}->{$chan} = 1;
  267. }
  268. $PID_DATA->{'live_channels'} = $PID_DATA->{'num_channels'};
  269. }
  270. }
  271. # Prepare data for each channel
  272. my @data_set = ();
  273. my $wait = $max_dur;
  274. foreach my $chan (keys(%{ $PID_DATA->{'channels'} })) {
  275. my $val = 0;
  276. my $dur = 0;
  277. # Random data on enabled channels until the fade
  278. if ($elapsed < $hit_pos) {
  279. if ($PID_DATA->{'channels'}->{$chan} > 0) {
  280. $dur = int(rand($max_dur));
  281. $val = int(rand($max_val));
  282. }
  283. } elsif ($elapsed < $fade_pos) {
  284. $val = $max_val;
  285. $dur = $hit_dur;
  286. $wait = ($fade_pos - $hit_pos) * 1000;
  287. } else {
  288. $val = 0;
  289. $dur = $fade_dur;
  290. $wait = $dur;
  291. }
  292. push(@data_set, { 'channel' => $chan, 'value' => $val, 'time' => $dur });
  293. }
  294. # Push the data set
  295. DMX::applyDataset(\@data_set, 'RAVE', $OUTPUT_FILE);
  296. # Wait for the fade interval
  297. usleep($wait * 1000);
  298. }