--- output.cc.org	2015-11-19 17:10:48.000000000 -0600
+++ output.cc.eqonly	2015-12-09 00:39:22.084886076 -0600
@@ -18,12 +18,12 @@
  */
 
 #include "output.h"
-
+#include <sys/stat.h>
 #include <math.h>
 #include <pthread.h>
 #include <stdlib.h>
 #include <string.h>
-
+#include "audstrings.h"
 #include "equalizer.h"
 #include "internal.h"
 #include "plugin.h"
@@ -76,6 +76,14 @@
 #define SIGNAL_MINOR pthread_cond_broadcast (& cond_minor)
 #define WAIT_MINOR pthread_cond_wait (& cond_minor, & mutex_minor)
 
+static bool s_songautoeq; /* JWT:ADDED TO ALLOW SONG/STREAM-SPECIFIC EQUALIZATION. */
+#ifdef _WINDOWS
+    static const char * path_sep = "\\";
+#endif
+#ifndef _WINDOWS
+    static const char * path_sep = "/";
+#endif
+
 static OutputPlugin * cop; /* current (primary) output plugin */
 static OutputPlugin * sop; /* secondary output plugin */
 
@@ -353,9 +361,56 @@
     write_output_raw (effect_finish (buffer1, end_of_playlist));
 }
 
+/* JWT:NEXT 2 FUNCTIONS ADDED TO ALLOW SONG/STREAM-SPECIFIC EQUALIZATION. */
+/* JWT:FIXME:I WANTED TO USE filename_build() BUT COULD NEVER GET IT TO WORK?! */
+static void do_save_eq_file (StringBuf filename)
+{
+    EqualizerPreset preset;
+    aud_eq_update_preset (preset);
+
+    VFSFile file (filename, "w");
+    if (file)
+        aud_save_preset_file (preset, file);
+}
+
+static void do_load_eq_file (StringBuf filename, bool load_songautoeq)
+{
+    EqualizerPreset preset;
+    bool havesongeqfile = true;
+
+    const char * filenamechar = (const char *)filename + 7;
+    if (filenamechar)
+    {
+    	   struct stat statbuf;
+    	   if (stat (filenamechar, &statbuf))
+    	       havesongeqfile = false;   /* SONG HAS NO SONG-SPECIFIC PRESET FILE, SO DO NOTHING */
+    }
+    if (havesongeqfile)
+    {
+        VFSFile file (filename, "r");
+        if (file && aud_load_preset_file (preset, file))
+        {
+            /* JWT:IF LOADING SONG-SPECIFIC PRESETS AND WE DON'T HAVE SONG-SPECIFIC ONES IN EFFECT NOW, THEN SAVE
+               THE CURRENT PRESETS OUT AS THE "DEFAULT" FOR LATER RESTORATION ON NEXT SONG W/O SONG-SPECIFIC PRESETS */
+            if (load_songautoeq && ! s_songautoeq)
+    	           do_save_eq_file (str_printf("%s%s%s%s", "file://",
+    	                   aud_get_path (AudPath::UserDir), path_sep, "_nonauto.preset"));
+
+            aud_eq_apply_preset (preset);   /* APPLY THE SONG-SPECIFIC PRESET FILE AND NOTE THAT WE DID THAT! */
+            s_songautoeq = load_songautoeq;
+        }
+        else
+            s_songautoeq = false;
+    }
+    else
+        s_songautoeq = false;
+}
+
 bool output_open_audio (const String & filename, const Tuple & tuple,
  int format, int rate, int channels, int start_time)
 {
+    bool prev_songautoeq = aud_get_bool("audacious", "equalizer_songauto");
+    s_songautoeq = prev_songautoeq;
     /* prevent division by zero */
     if (rate < 1 || channels < 1 || channels > AUD_MAX_CHANNELS)
         return false;
@@ -383,8 +438,42 @@
     in_frames = 0;
 
     setup_output ();
+    /* JWT:NEXT CONDITION BLOCK ADDED TO ALLOW SONG/STREAM-SPECIFIC EQUALIZATION. */
+    if (aud_get_bool (nullptr, "equalizer_autoload"))
+    {
+        const char * slash;
+        const char * base;
+        int ln = -1;
+        slash = filename ? strrchr (filename, '/') : nullptr;
+        base = slash ? slash + 1 : nullptr;
+        if (slash && (!base || base[0] == '\0')) // FILENAME ENDS IN A "/"!
+        {
+            do
+            {
+                --slash;
+                ++ln;
+            } while (slash && slash > filename && slash[0] != '/');
+            base = slash ? slash + 1 : nullptr;
+            if (ln > 0)
+                do_load_eq_file (str_printf("%s%s%s%.*s%s", "file://",
+                        aud_get_path (AudPath::UserDir), path_sep, ln, base, ".preset"), true);
+        }
+        else if (base && base[0] != '\0' && strncmp (base, "-.", 2))
+        	   do_load_eq_file (str_printf("%s%s%s%s%s", "file://",
+        	           aud_get_path (AudPath::UserDir), path_sep, base, ".preset"), true);
+    }
+    else
+        s_songautoeq = false;
+
+    /* JWT:IF PREV. SONG LOADED IT'S OWN PRESETS, BUT THE CURRENT SONG DOES NOT HAVE IT'S OWN,
+       THEN RESTORE THE "DEFAULT" (LAST KNOWN NON SONG-SPECIFIC) ONES */
+    if (prev_songautoeq && !s_songautoeq)
+        do_load_eq_file (str_printf("%s%s%s%s", "file://",
+    	           aud_get_path (AudPath::UserDir), path_sep, "_nonauto.preset"), false);
 
     UNLOCK_ALL;
+
+    aud_set_bool("audacious", "equalizer_songauto", s_songautoeq);
     return true;
 }
 
