From e5d90eebd76f46d34b6fe376ebba401cfb4553d5 Mon Sep 17 00:00:00 2001
From: twinaphex <libretro@gmail.com>
Date: Sat, 21 Apr 2018 06:29:56 +0200
Subject: [PATCH] Updates to libretro-common

---
 libretro-common/audio/dsp_filters/Tremolo.dsp |   7 +
 libretro-common/audio/dsp_filters/Vibrato.dsp |   7 +
 libretro-common/audio/dsp_filters/tremolo.c   | 133 ++++++++++++++
 libretro-common/audio/dsp_filters/vibrato.c   | 169 ++++++++++++++++++
 libretro-common/compat/compat_vscprintf.c     |  44 +++++
 libretro-common/formats/libchdr/chd.c         |   4 +-
 6 files changed, 362 insertions(+), 2 deletions(-)
 create mode 100644 libretro-common/audio/dsp_filters/Tremolo.dsp
 create mode 100644 libretro-common/audio/dsp_filters/Vibrato.dsp
 create mode 100644 libretro-common/audio/dsp_filters/tremolo.c
 create mode 100644 libretro-common/audio/dsp_filters/vibrato.c
 create mode 100644 libretro-common/compat/compat_vscprintf.c

diff --git a/libretro-common/audio/dsp_filters/Tremolo.dsp b/libretro-common/audio/dsp_filters/Tremolo.dsp
new file mode 100644
index 0000000000..50523333ac
--- /dev/null
+++ b/libretro-common/audio/dsp_filters/Tremolo.dsp
@@ -0,0 +1,7 @@
+filters = 1
+filter0 = tremolo
+
+# Defaults.
+#tremolo_frequency = 4.0
+#tremolo_depth = 0.9
+
diff --git a/libretro-common/audio/dsp_filters/Vibrato.dsp b/libretro-common/audio/dsp_filters/Vibrato.dsp
new file mode 100644
index 0000000000..b1ea8e1107
--- /dev/null
+++ b/libretro-common/audio/dsp_filters/Vibrato.dsp
@@ -0,0 +1,7 @@
+filters = 1
+filter0 = vibrato
+
+# Defaults.
+#vibrato_frequency = 5.0
+#vibrato_depth = 0.5
+
diff --git a/libretro-common/audio/dsp_filters/tremolo.c b/libretro-common/audio/dsp_filters/tremolo.c
new file mode 100644
index 0000000000..38b535742c
--- /dev/null
+++ b/libretro-common/audio/dsp_filters/tremolo.c
@@ -0,0 +1,133 @@
+/* Copyright  (C) 2010-2017 The RetroArch team
+ *
+ * ---------------------------------------------------------------------------------------
+ * The following license statement only applies to this file (tremolo.c).
+ * ---------------------------------------------------------------------------------------
+ *
+ * Permission is hereby granted, free of charge,
+ * to any person obtaining a copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <retro_miscellaneous.h>
+#include <libretro_dspfilter.h>
+#include <string/stdstring.h>
+
+#define sqr(a) ((a) * (a))
+
+struct tremolo_core
+{
+      float freq;
+      float depth;
+	float* wavetable;
+      int index;
+	int maxindex;
+};
+
+struct tremolo
+{
+   struct tremolo_core left, right;
+};
+
+static void tremolo_free(void *data)
+{
+   struct tremolo *tre = (struct tremolo*)data;
+   free(tre->left.wavetable);
+   free(tre->right.wavetable);
+   free(data);
+}
+
+static void tremolocore_init(struct tremolo_core *core,float depth,int samplerate,float freq)
+{
+      const double offset = 1. - depth / 2.;
+      unsigned i;
+      double env;
+      core->index = 0;
+	core->maxindex = samplerate/freq;	
+	core->wavetable = malloc(core->maxindex*sizeof(float));
+	memset(core->wavetable, 0, core->maxindex * sizeof(float));
+	for (i = 0; i < core->maxindex; i++) {
+	env = freq * i / samplerate;
+	env = sin((M_PI*2) * fmod(env + 0.25, 1.0));
+	core->wavetable[i] = env * (1 - fabs(offset)) + offset;
+      }	
+}
+
+float tremolocore_core(struct tremolo_core *core,float in)
+{
+      core->index = core->index % core->maxindex;
+      return in * core->wavetable[core->index++];
+}
+
+static void tremolo_process(void *data, struct dspfilter_output *output,
+      const struct dspfilter_input *input)
+{
+   unsigned i;
+   float *out;
+   struct tremolo *tre = (struct tremolo*)data;
+
+   output->samples         = input->samples;
+   output->frames          = input->frames;
+   out                     = output->samples;
+
+   for (i = 0; i < input->frames; i++, out += 2)
+   {
+      float in[2] = { out[0], out[1] };
+
+      out[0] = tremolocore_core(&tre->left, in[0]);
+      out[1] = tremolocore_core(&tre->right, in[1]);
+   }
+}
+
+static void *tremolo_init(const struct dspfilter_info *info,
+      const struct dspfilter_config *config, void *userdata)
+{
+   float freq, depth;
+   struct tremolo *tre   = (struct tremolo*)calloc(1, sizeof(*tre));
+   if (!tre)
+      return NULL;
+
+   config->get_float(userdata, "freq", &freq,4.0f);
+   config->get_float(userdata, "depth", &depth, 0.9f);
+   tremolocore_init(&tre->left,depth,info->input_rate,freq);
+   tremolocore_init(&tre->right,depth,info->input_rate,freq);
+   return tre;
+}
+
+static const struct dspfilter_implementation tremolo_plug = {
+   tremolo_init,
+   tremolo_process,
+   tremolo_free,
+
+   DSPFILTER_API_VERSION,
+   "Tremolo",
+   "tremolo",
+};
+
+#ifdef HAVE_FILTERS_BUILTIN
+#define dspfilter_get_implementation tremolo_dspfilter_get_implementation
+#endif
+
+const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
+{
+   (void)mask;
+   return &tremolo_plug;
+}
+
+#undef dspfilter_get_implementation
+
diff --git a/libretro-common/audio/dsp_filters/vibrato.c b/libretro-common/audio/dsp_filters/vibrato.c
new file mode 100644
index 0000000000..6e79d2f0fb
--- /dev/null
+++ b/libretro-common/audio/dsp_filters/vibrato.c
@@ -0,0 +1,169 @@
+/* Copyright  (C) 2010-2017 The RetroArch team
+ *
+ * ---------------------------------------------------------------------------------------
+ * The following license statement only applies to this file (vibrato.c).
+ * ---------------------------------------------------------------------------------------
+ *
+ * Permission is hereby granted, free of charge,
+ * to any person obtaining a copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <retro_miscellaneous.h>
+#include <libretro_dspfilter.h>
+#include <string/stdstring.h>
+
+#define sqr(a) ((a) * (a))
+
+
+const float BASE_DELAY_SEC = 0.002; // 2 ms
+const float VIBRATO_FREQUENCY_DEFAULT_HZ = 2;
+const float VIBRATO_FREQUENCY_MAX_HZ = 14;
+const float VIBRATO_DEPTH_DEFAULT_PERCENT = 50;
+const int add_delay = 3;
+
+float hermite_interp(float x, float *y)
+{
+	float c0, c1, c2, c3;
+	c0 = y[1];
+	c1 = (1.0 / 2.0)*(y[2] - y[0]);
+	c2 = (y[0] - (5.0 / 2.0)*y[1]) + (2.0*y[2] - (1.0 / 2.0)*y[3]);
+	c3 = (1.0 / 2.0)*(y[3] - y[0]) + (3.0 / 2.0)*(y[1] - y[2]);
+	return ((c3*x + c2)*x + c1)*x + c0;
+}
+
+struct vibrato_core
+{
+      float freq;
+      float samplerate;
+      int phase;
+      float depth;
+	float* buffer;
+	int writeindex;
+      int size;
+};
+
+struct vibrato
+{
+   struct vibrato_core left, right;
+};
+
+static void vibrato_free(void *data)
+{
+   struct vibrato *vib = (struct vibrato*)data;
+   free(vib->left.buffer);
+   free(vib->right.buffer);
+   free(data);
+}
+
+static void vibratocore_init(struct vibrato_core *core,float depth,int samplerate,float freq)
+{
+	core->size = BASE_DELAY_SEC * samplerate * 2;
+	core->buffer = malloc((core->size + add_delay)*sizeof(float));
+	memset(core->buffer, 0, (core->size + add_delay) * sizeof(float));
+	core->samplerate = samplerate;
+	core->freq = freq;
+	core->depth = depth;
+	core->phase = 0;
+	core->writeindex = 0;		
+}
+
+float vibratocore_core(struct vibrato_core *core,float in)
+{
+            float M = core->freq / core->samplerate;
+		int maxphase = core->samplerate / core->freq;
+		float lfo = sin(M * 2. * M_PI * core->phase++);
+		core->phase = core->phase % maxphase;
+	      lfo = (lfo + 1) * 1.; // transform from [-1; 1] to [0; 1]
+		int maxdelay = BASE_DELAY_SEC * core->samplerate;
+		float delay =  lfo * core->depth * maxdelay;
+		delay += add_delay;
+		float readindex = core->writeindex - 1 - delay;
+		while (readindex < 0)readindex += core->size;
+		while (readindex >= core->size)readindex -= core->size;
+		int ipart = (int)readindex; // integer part of the delay
+		float fpart = readindex - ipart; // fractional part of the delay
+		float value = hermite_interp(fpart, &(core->buffer[ipart]));
+		core->buffer[core->writeindex] = in;
+		if (core->writeindex < add_delay){
+			core->buffer[core->size + core->writeindex] = in;
+		}
+		core->writeindex++;
+		if (core->writeindex == core->size) {
+			core->writeindex = 0;
+		}
+            return value;
+}
+
+static void vibrato_process(void *data, struct dspfilter_output *output,
+      const struct dspfilter_input *input)
+{
+   unsigned i;
+   float *out;
+   struct vibrato *vib = (struct vibrato*)data;
+
+   output->samples         = input->samples;
+   output->frames          = input->frames;
+   out                     = output->samples;
+
+   for (i = 0; i < input->frames; i++, out += 2)
+   {
+      float in[2] = { out[0], out[1] };
+
+      out[0] = vibratocore_core(&vib->left, in[0]);
+      out[1] = vibratocore_core(&vib->right, in[1]);
+   }
+}
+
+static void *vibrato_init(const struct dspfilter_info *info,
+      const struct dspfilter_config *config, void *userdata)
+{
+   float freq, depth;
+   struct vibrato *vib   = (struct vibrato*)calloc(1, sizeof(*vib));
+   if (!vib)
+      return NULL;
+
+   config->get_float(userdata, "freq", &freq,5.0f);
+   config->get_float(userdata, "depth", &depth, 0.5f);
+   vibratocore_init(&vib->left,depth,info->input_rate,freq);
+   vibratocore_init(&vib->right,depth,info->input_rate,freq);
+   return vib;
+}
+
+static const struct dspfilter_implementation vibrato_plug = {
+   vibrato_init,
+   vibrato_process,
+   vibrato_free,
+
+   DSPFILTER_API_VERSION,
+   "Vibrato",
+   "vibrato",
+};
+
+#ifdef HAVE_FILTERS_BUILTIN
+#define dspfilter_get_implementation vibrato_dspfilter_get_implementation
+#endif
+
+const struct dspfilter_implementation *dspfilter_get_implementation(dspfilter_simd_mask_t mask)
+{
+   (void)mask;
+   return &vibrato_plug;
+}
+
+#undef dspfilter_get_implementation
+
diff --git a/libretro-common/compat/compat_vscprintf.c b/libretro-common/compat/compat_vscprintf.c
new file mode 100644
index 0000000000..ddffeb7e77
--- /dev/null
+++ b/libretro-common/compat/compat_vscprintf.c
@@ -0,0 +1,44 @@
+/* Copyright  (C) 2010-2017 The RetroArch team
+ *
+ * ---------------------------------------------------------------------------------------
+ * The following license statement only applies to this file (compat_snprintf.c).
+ * ---------------------------------------------------------------------------------------
+ *
+ * Permission is hereby granted, free of charge,
+ * to any person obtaining a copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/* THIS FILE HAS NOT BEEN VALIDATED ON PLATFORMS BESIDES MSVC */
+#ifdef _MSC_VER
+
+#include <retro_common.h>
+
+#include <stdio.h>
+#include <stdarg.h>
+
+#if defined(_MSC_VER) && _MSC_VER < 1800
+#define va_copy(dst, src) ((dst) = (src))
+#endif
+
+int c89_vscprintf_retro__(const char *format, va_list pargs)
+{
+   int retval;
+   va_list argcopy;
+   va_copy(argcopy, pargs);
+   retval = vsnprintf(NULL, 0, format, argcopy);
+   va_end(argcopy);
+   return retval;
+}
+#endif
diff --git a/libretro-common/formats/libchdr/chd.c b/libretro-common/formats/libchdr/chd.c
index bb8e91de11..32e9dd1045 100644
--- a/libretro-common/formats/libchdr/chd.c
+++ b/libretro-common/formats/libchdr/chd.c
@@ -1706,7 +1706,7 @@ chd_error chd_get_metadata(chd_file *chd, UINT32 searchtag, UINT32 searchindex,
 {
 	metadata_entry metaentry;
 	chd_error err;
-	int64_t count;
+	UINT32 count;
 
 	/* if we didn't find it, just return */
 	err = metadata_find_entry(chd, searchtag, searchindex, &metaentry);
@@ -2240,7 +2240,7 @@ static chd_error map_read(chd_file *chd)
 	UINT8 raw_map_entries[MAP_STACK_ENTRIES * MAP_ENTRY_SIZE];
 	UINT64 fileoffset, maxoffset = 0;
 	UINT8 cookie[MAP_ENTRY_SIZE];
-	int64_t count;
+	UINT32 count;
 	chd_error err;
 	int i;