Zachary Kotlarek 13 лет назад
Родитель
Сommit
cc7e91dc31
2 измененных файлов с 61 добавлено и 39 удалено
  1. 43 30
      dmx/amplifierPower.pl
  2. 18 9
      dmx/leds.pl

+ 43 - 30
dmx/amplifierPower.pl

@@ -57,47 +57,51 @@ while (1) {
 		$pullLast = time();
 		$pullLast = time();
 	}
 	}
 
 
+	# Die if we don't see regular updates
+	if (time() - $pullLast > $PULL_TIMEOUT) {
+		die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
+	}
+
 	# Calculate the new state
 	# Calculate the new state
 	$stateLast = $state;
 	$stateLast = $state;
-	if ($newState eq 'PLAY' || $newState eq 'PAUSE') {
+	if ($newState eq 'PLAY') {
 		$state = 'ON';
 		$state = 'ON';
 	} elsif ($exists{'RAVE'}) {
 	} elsif ($exists{'RAVE'}) {
 		$state = 'RAVE';
 		$state = 'RAVE';
+	} elsif ($newState eq 'PAUSE') {
+		$state = 'ON';
 	} else {
 	} else {
 		$state = 'OFF';
 		$state = 'OFF';
 	}
 	}
 
 
-	# Force updates when there is a physical state mistmatch
-	if ($state eq 'OFF') {
-		if ($exists{'AMPLIFIER'}) {
-			$update = 1;
-		}
-	} else {
-		if (!$exists{'AMPLIFIER'}) {
-			$update = 1;
-		}
-	}
-
 	# Force updates on a periodic basis
 	# Force updates on a periodic basis
-	if (time() - $pushLast > $PUSH_TIMEOUT) {
+	if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
 
 
 		# Not for the amp
 		# Not for the amp
+		#if ($DEBUG) {
+		#	print STDERR "Forcing periodic update\n";
+		#}
 		#$update = 1;
 		#$update = 1;
 	}
 	}
 
 
-	# Die if we don't see regular updates
-	if (time() - $pullLast > $PULL_TIMEOUT) {
-		die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
-	}
-
 	# Force updates on any state change
 	# Force updates on any state change
-	if ($stateLast ne $state) {
+	if (!$update && $stateLast ne $state) {
 		if ($DEBUG) {
 		if ($DEBUG) {
 			print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
 			print STDERR 'State change: ' . $stateLast . ' => ' . $state . "\n";
 		}
 		}
 		$update = 1;
 		$update = 1;
 	}
 	}
 
 
+	# Force updates when there is a physical state mistmatch
+	if (!$update) {
+		if (($state eq 'OFF' && $exists{'AMPLIFIER'}) || ($state eq 'ON' && !$exists{'AMPLIFIER'})) {
+			if ($DEBUG) {
+				print STDERR 'Physical state mismatch: ' . $state . ':' . $exists{'AMPLIFIER'} . "\n";
+			}
+			$update = 1;
+		}
+	}
+
 	# Update the amp
 	# Update the amp
 	if ($update) {
 	if ($update) {
 
 
@@ -118,16 +122,28 @@ while (1) {
 			  or die('Unable to write command to amp socket: ' . $cmd . ": ${!}\n");
 			  or die('Unable to write command to amp socket: ' . $cmd . ": ${!}\n");
 		}
 		}
 
 
-		# Reset to TV @ 5.1 at power on
-		if (defined($cmd) && $cmd eq 'ON') {
-
-			# Set the audio source back to the default
+		# Select an output device
+		if ($state eq 'RAVE') {
 			my @CMD = @AUDIO_SET;
 			my @CMD = @AUDIO_SET;
-			push(@CMD, $DEFAULT_DEV);
+			push(@CMD, $RAVE_DEV);
 			system(@CMD);
 			system(@CMD);
 
 
+		} else {
+			if (defined($cmd) && $cmd eq 'ON') {
+				# Set the audio source back to the default
+				my @CMD = @AUDIO_SET;
+				push(@CMD, $DEFAULT_DEV);
+				system(@CMD);
+			}
+		}
+
+		# Reset to TV @ 5.1 at power on
+		if (defined($cmd) && $cmd eq 'ON') {
+
 			# Wait for the amp to boot
 			# Wait for the amp to boot
-			sleep($START_DELAY);
+			if (!$exists{'AMPLIFIER'}) {
+				sleep($START_DELAY);
+			}
 
 
 			# Set the mode
 			# Set the mode
 			$amp->send('TV')
 			$amp->send('TV')
@@ -139,12 +155,9 @@ while (1) {
 		# Rave through the main amp
 		# Rave through the main amp
 		if ($state eq 'RAVE') {
 		if ($state eq 'RAVE') {
 
 
-			# Set the audio source to the RAVE device
-			my @CMD = @AUDIO_SET;
-			push(@CMD, $RAVE_DEV);
-			system(@CMD);
-
 			# Set the mode
 			# Set the mode
+			$amp->send('TV')
+			  or die('Unable to write command to amp socket: TV' . ": ${!}\n");
 			$amp->send('STEREO')
 			$amp->send('STEREO')
 			  or die('Unable to write command to amp socket: STEREO' . ": ${!}\n");
 			  or die('Unable to write command to amp socket: STEREO' . ": ${!}\n");
 		}
 		}

+ 18 - 9
dmx/leds.pl

@@ -357,12 +357,16 @@ sub rave($) {
 	}
 	}
 
 
 	# Config
 	# Config
-	my $file      = '/mnt/media/DMX/Rave.mp3';
-	my @sound     = ('afplay', $file);
-	my $AMP_DELAY = 9;
+	my $SND_APP   = 'afplay';
+	my $RAVE_MP3  = '/mnt/media/DMX/Rave.mp3';
+	my $SILENCE   = '/mnt/media/DMX/Silence.wav';
+	my $SIL_DELAY = 1.3;
+	my $AMP_SHORT = 5;
+	my $AMP_LONG  = $AMP_SHORT + 5;
 
 
 	# Stat the file to bring the network up-to-date
 	# Stat the file to bring the network up-to-date
-	stat($file);
+	stat($SILENCE);
+	stat($RAVE_MP3);
 
 
 	# Initiate the RAVE state
 	# Initiate the RAVE state
 	touch($RAVE_FILE);
 	touch($RAVE_FILE);
@@ -393,28 +397,33 @@ sub rave($) {
 	$data{'live_channels'} = 0;
 	$data{'live_channels'} = 0;
 
 
 	# Reduce the amp delay if the amp is already on
 	# Reduce the amp delay if the amp is already on
-	my $amp_wait = $AMP_DELAY;
+	my $amp_wait = $AMP_LONG;
 	if ($exists->{'AMPLIFIER'}) {
 	if ($exists->{'AMPLIFIER'}) {
-		$amp_wait = 0.5;
+		$amp_wait = $AMP_SHORT;
 	}
 	}
 
 
 	# Dim while we wait
 	# Dim while we wait
 	my @data_set = ();
 	my @data_set = ();
 	foreach my $chan (keys(%channels)) {
 	foreach my $chan (keys(%channels)) {
-		push(@data_set, { 'channel' => $chan, 'value' => 0, 'time' => $amp_wait * 1000 });
+		push(@data_set, { 'channel' => $chan, 'value' => 0, 'time' => $amp_wait / 2 * 1000 });
 	}
 	}
 	DMX::applyDataset(\@data_set, 'RAVE', $OUTPUT_FILE);
 	DMX::applyDataset(\@data_set, 'RAVE', $OUTPUT_FILE);
 	$pushLast = time();
 	$pushLast = time();
 
 
-	# Wait for the amp to boot
+	# Wait for the amp to power up
 	if ($DEBUG) {
 	if ($DEBUG) {
 		print STDERR 'Waiting ' . $amp_wait . " seconds for the amp to boot\n";
 		print STDERR 'Waiting ' . $amp_wait . " seconds for the amp to boot\n";
 	}
 	}
-	sleep($amp_wait);
+	sleep($amp_wait - $SIL_DELAY);
+
+	# Play a short burst of silence to get all the audio in-sync
+	my @sound = ($SND_APP, $SILENCE);
+	system(@sound);
 
 
 	# Play the sound in a child (i.e. in the background)
 	# Play the sound in a child (i.e. in the background)
 	$PID = fork();
 	$PID = fork();
 	if (defined($PID) && $PID == 0) {
 	if (defined($PID) && $PID == 0) {
+		my @sound = ($SND_APP, $RAVE_MP3);
 		exec(@sound)
 		exec(@sound)
 		  or die('Unable to play sound: ' . join(' ', @sound) . "\n");
 		  or die('Unable to play sound: ' . join(' ', @sound) . "\n");
 	}
 	}