rave.pl 7.7 KB

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