five35
New member
This patch adds a satisfying, woody "crunch" sound to smashing barrels during smash parties or while in the Barrel full of Barrels. Mechanically, this works by attaching the sound to the same events which trigger the explosion of barrel particles. The sound used was dedicated to the public domain and, for simplicity, I dedicate my changes to it (primarily format conversion) to the public domain as well.
The sound has been placed in the images/ directory, for lack of a more obviously correct place to put it. It cannot be located in the relay/ directory due to (I believe) files in that folder being assumed to be UTF-8 rather than binary, causing the file to be corrupted when downloaded by the browser.
The sound has been placed in the images/ directory, for lack of a more obviously correct place to put it. It cannot be located in the relay/ directory due to (I believe) files in that folder being assumed to be UTF-8 rather than binary, causing the file to be corrupted when downloaded by the browser.
Code:
Index: src/data/defaults.txt
===================================================================
--- src/data/defaults.txt (revision 16319)
+++ src/data/defaults.txt (working copy)
@@ -108,6 +108,7 @@
global previousUpdateRevision 0
global protectAgainstOverdrink true
global proxySet false
+global relayAddSounds true
global relayAddsCustomCombat true
global relayAddsDiscoHelper false
global relayAddsGraphicalCLI false
Index: src/images/barrel_smash.mp3
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/images/barrel_smash.mp3
===================================================================
--- src/images/barrel_smash.mp3 (revision 0)
+++ src/images/barrel_smash.mp3 (working copy)
Property changes on: src/images/barrel_smash.mp3
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: src/net/sourceforge/kolmafia/KoLConstants.java
===================================================================
--- src/net/sourceforge/kolmafia/KoLConstants.java (revision 16319)
+++ src/net/sourceforge/kolmafia/KoLConstants.java (working copy)
@@ -393,6 +393,7 @@
public static final int MALLPRICES_VERSION = 0xF00D5;
// The current versioned name of each KoLmafia-supplied relay file
+ public static final String BARREL_SOUNDS_JS = "barrel_sounds.js";
public static final String BASEMENT_JS = "basement.js";
public static final String BASICS_CSS = "basics.1.css";
public static final String BASICS_JS = "basics.js";
@@ -409,6 +410,7 @@
public static final String[] RELAY_FILES =
{
+ BARREL_SOUNDS_JS,
BASEMENT_JS,
BASICS_CSS,
BASICS_JS,
Index: src/net/sourceforge/kolmafia/RequestEditorKit.java
===================================================================
--- src/net/sourceforge/kolmafia/RequestEditorKit.java (revision 16319)
+++ src/net/sourceforge/kolmafia/RequestEditorKit.java (working copy)
@@ -438,6 +438,7 @@
RequestEditorKit.fixTavernCellar( buffer );
StationaryButtonDecorator.decorate( location, buffer );
RequestEditorKit.addChoiceSpoilers( location, buffer );
+ RequestEditorKit.addBarrelSounds( buffer );
}
else if ( location.startsWith( "clan_hobopolis.php" ) )
{
@@ -1991,6 +1992,19 @@
buffer.append( text.substring( index1 ) );
}
+ private static final void addBarrelSounds( final StringBuffer buffer )
+ {
+ if ( !Preferences.getBoolean( "relayAddSounds" ) )
+ {
+ return;
+ }
+
+ if ( buffer.indexOf( "barrelpart" ) != -1 )
+ {
+ StringUtilities.insertBefore( buffer, "</html>", "<script src=\"/" + KoLConstants.BARREL_SOUNDS_JS + "\"></script>" );
+ }
+ }
+
private static final void decorateChoiceResponse( final String location, final StringBuffer buffer )
{
Matcher matcher = RequestEditorKit.CHOICE2_PATTERN.matcher( location );
Index: src/net/sourceforge/kolmafia/swingui/OptionsFrame.java
===================================================================
--- src/net/sourceforge/kolmafia/swingui/OptionsFrame.java (revision 16319)
+++ src/net/sourceforge/kolmafia/swingui/OptionsFrame.java (working copy)
@@ -232,6 +232,7 @@
{},
{ "relayAllowRemoteAccess", "Allow network devices to access relay browser (requires restart)" },
{ "relayOverridesImages", "Override certain KoL images" },
+ { "relayAddSounds", "Add sounds to certain events" },
{},
{ "relayAddsWikiLinks", "Check wiki for item descriptions (fails for unknowns)" },
{ "relayAddsQuickScripts", "Add quick script links to menu bar (see Links tab)" },
Index: src/relay/barrel_sounds.js
===================================================================
--- src/relay/barrel_sounds.js (revision 0)
+++ src/relay/barrel_sounds.js (working copy)
@@ -0,0 +1,56 @@
+(function(window) {
+ var BARREL_SMASH_SOUND = "/images/barrel_smash.mp3";
+
+ var AudioContext = window.AudioContext || window.webkitAudioContext;
+
+ if (AudioContext) {
+ // web audio API
+
+ var context = new AudioContext();
+ var audioBuffer;
+
+ function play_source() {
+ if (audioBuffer) {
+ var source = context.createBufferSource();
+
+ source.buffer = audioBuffer;
+ source.playbackRate.value = 1 + Math.random() / 2;
+ source.connect(context.destination)
+ source.start(context.currentTime);
+ }
+ }
+
+ var request = new XMLHttpRequest();
+
+ request.open("GET", BARREL_SMASH_SOUND);
+ request.responseType = "arraybuffer";
+
+ request.onload = function() {
+ context.decodeAudioData(request.response, function(decodedBuffer) {
+ audioBuffer = decodedBuffer;
+
+ if ($(document).live) {
+ $(".spot").live("click", play_source);
+ } else {
+ $(document).on("click", ".spot", play_source);
+ }
+ });
+ }
+
+ request.send();
+ } else if (window.Audio) {
+ // HTML5 audio element
+
+ var audio = new Audio(BARREL_SMASH_SOUND);
+
+ function play_element() {
+ audio.play();
+ }
+
+ if ($(document).live) {
+ $(".spot").live("click", play_element);
+ } else {
+ $(document).on("click", ".spot", play_element);
+ }
+ }
+})(this);