From 041fc115a5038be79888f4754bdc435a8e000d99 Mon Sep 17 00:00:00 2001
From: Hans de Goede <hdegoede@redhat.com>
Date: Mon, 15 Apr 2013 09:48:26 +0200
Subject: [PATCH] sid: Add support for libsidplayfp

Loosely based on an initial attempt by Cristian Morales Vega from:
http://redmine.audacious-media-player.org/issues/173

I had to do quite some work to get it to compile with libsidplayfp-1.0.x,
and then some more work to make it actually work and feature complete.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 configure.ac            |   1 +
 extra.mk.in             |   2 +
 m4/sid.m4               |   9 +-
 m4/sidplay.m4           |  12 ++
 src/sid/Makefile        |   5 +-
 src/sid/xs_config.c     |  20 +--
 src/sid/xs_config.h     |   4 +-
 src/sid/xs_player.c     |  13 ++
 src/sid/xs_sidplay.h    |  27 ++-
 src/sid/xs_sidplayfp.cc | 454 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/sid/xs_sidplayfp.h  |  26 +++
 11 files changed, 555 insertions(+), 18 deletions(-)
 create mode 100644 src/sid/xs_sidplayfp.cc
 create mode 100644 src/sid/xs_sidplayfp.h

diff --git a/configure.ac b/configure.ac
index 87b6975..750ab55 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1166,6 +1166,7 @@ echo "  Free Lossless Audio Codec (flacng):     $have_flacng"
 echo "  Commodore 64 audio (SID):               $have_sidplay $xs_have_sidplay2_api"
 echo "    -> libSIDPlay1 support:               $xs_have_sidplay1"
 echo "    -> libSIDPlay2 support:               $xs_have_sidplay2"
+echo "    -> libSIDPlayFP support:              $xs_have_sidplayfp"
 echo "    -> distortion patched libSIDPlay2:    $xs_have_distortion"
 echo "  Game music (spc, nsf & gbs):            $have_console"
 echo "  PlayStation (psf/psf2) audio:           $enable_psf"
diff --git a/extra.mk.in b/extra.mk.in
index b01215e..8afbfcd 100644
--- a/extra.mk.in
+++ b/extra.mk.in
@@ -91,6 +91,8 @@ SIDPLAY1_CFLAGS ?= @SIDPLAY1_CFLAGS@
 SIDPLAY1_LIBS ?= @SIDPLAY1_LIBS@
 SIDPLAY2_CFLAGS ?= @SIDPLAY2_CFLAGS@
 SIDPLAY2_LIBS ?= @SIDPLAY2_LIBS@
+SIDPLAYFP_CFLAGS ?= @SIDPLAYFP_CFLAGS@
+SIDPLAYFP_LIBS ?= @SIDPLAYFP_LIBS@
 SNDFILE_CFLAGS ?= @SNDFILE_CFLAGS@
 SNDFILE_LIBS ?= @SNDFILE_LIBS@
 SNDIO_LIBS ?= @SNDIO_LIBS@
diff --git a/m4/sid.m4 b/m4/sid.m4
index 10d6631..b3f0a2b 100644
--- a/m4/sid.m4
+++ b/m4/sid.m4
@@ -21,6 +21,13 @@ if test "x$xs_have_sidplay2" = "xyes"; then
 	fi
 fi
 
+XS_ARG_WITH([sidplayfp], [libsidplayfp], [XS_PATH_LIBSIDPLAYFP])
+if test "x$xs_have_sidplayfp" = "xyes"; then
+	AC_DEFINE([HAVE_SIDPLAYFP])
+	AC_LANG_PUSH([C++])
+	AC_CHECK_HEADERS([sidplayfp/builders/residfp.h sidplayfp/builders/resid.h sidplayfp/builders/hardsid.h])
+fi
+
 if test "x$xs_have_sidplay2" = "xyes"; then
 	dnl *** Check for new COMI API
 	AC_MSG_CHECKING([libSIDPlay2 API style])
@@ -64,7 +71,7 @@ if test "x$xs_have_sidplay2" = "xyes"; then
 	CPPFLAGS="$tmp_CPPFLAGS"
 fi
 
-if test "x$xs_have_sidplay1" = "xyes" || test "x$xs_have_sidplay2" = "xyes"; then
+if test "x$xs_have_sidplay1" = "xyes" || test "x$xs_have_sidplay2" = "xyes" || test "x$xs_have_sidplayfp" = "xyes"; then
 	m4_ifvaln([$1], [$1], [:])
 	m4_ifvaln([$2], [else $2])
 fi
diff --git a/m4/sidplay.m4 b/m4/sidplay.m4
index e6cd52e..8fd0eac 100644
--- a/m4/sidplay.m4
+++ b/m4/sidplay.m4
@@ -383,3 +383,15 @@ AC_DEFUN([XS_BUILDERS_FIND], [dnl
     AC_MSG_RESULT([$xs_builders_works])
     AC_SUBST([BUILDERS_LIBS])
 ])
+
+dnl -------------------------------------------------------------------------
+dnl Try to find SIDPLAYFP library and header files.
+dnl $xs_have_sidplayfp will be "yes" or "no"
+dnl @SIDPLAYFP_LIBS@ will be substituted with linker parameters
+dnl @SIDPLAYFP_INCLUDES@ will be substituted with compiler parameters
+dnl -------------------------------------------------------------------------
+AC_DEFUN([XS_PATH_LIBSIDPLAYFP],
+[
+    PKG_CHECK_MODULES(SIDPLAYFP, [libsidplayfp],
+                      [xs_have_sidplayfp=yes], [xs_have_sidplayfp=no])
+])
diff --git a/src/sid/Makefile b/src/sid/Makefile
index 0f7192e..65d2854 100644
--- a/src/sid/Makefile
+++ b/src/sid/Makefile
@@ -9,6 +9,7 @@ SRCS = xs_init.c	\
        xs_filter.c	\
        xs_sidplay1.cc	\
        xs_sidplay2.cc	\
+       xs_sidplayfp.cc	\
        xs_slsup.c	\
        xs_player.c	\
        xmms-sid.c
@@ -21,5 +22,5 @@ plugindir := ${plugindir}/${INPUT_PLUGIN_DIR}
 LD = ${CXX}
 CFLAGS += ${PLUGIN_CFLAGS}
 CXXFLAGS += ${PLUGIN_CFLAGS}
-CPPFLAGS += ${PLUGIN_CPPFLAGS} -D_REENTRANT -I../.. ${SIDPLAY1_CFLAGS} ${SIDPLAY2_CFLAGS} ${BUILDERS_CFLAGS} ${GLIB_CFLAGS}
-LIBS += -lm ${BUILDERS_LDFLAGS} ${SIDPLAY1_LIBS} ${SIDPLAY2_LIBS} ${BUILDERS_LIBS} ${GLIB_LIBS}
+CPPFLAGS += ${PLUGIN_CPPFLAGS} -D_REENTRANT -DDATADIR="\"$(datadir)/\"" -I../.. ${SIDPLAY1_CFLAGS} ${SIDPLAY2_CFLAGS} ${SIDPLAYFP_CFLAGS} ${BUILDERS_CFLAGS} ${GLIB_CFLAGS}
+LIBS += -lm ${BUILDERS_LDFLAGS} ${SIDPLAY1_LIBS} ${SIDPLAY2_LIBS} ${SIDPLAYFP_LIBS} ${BUILDERS_LIBS} ${GLIB_LIBS}
diff --git a/src/sid/xs_config.c b/src/sid/xs_config.c
index 1d430e8..0e3fe5e 100644
--- a/src/sid/xs_config.c
+++ b/src/sid/xs_config.c
@@ -53,17 +53,17 @@ void xs_init_configuration(void)
     xs_cfg.sid1Filter.fm = XS_SIDPLAY1_FM;
     xs_cfg.sid1Filter.ft = XS_SIDPLAY1_FT;
 
-#ifdef HAVE_SIDPLAY2
+#if defined HAVE_SIDPLAYFP
+    xs_cfg.playerEngine = XS_ENG_SIDPLAYFP;
+#elif defined HAVE_SIDPLAY2
     xs_cfg.playerEngine = XS_ENG_SIDPLAY2;
     xs_cfg.memoryMode = XS_MPU_REAL;
-#else
-#ifdef HAVE_SIDPLAY1
+#elif defined HAVE_SIDPLAY1
     xs_cfg.playerEngine = XS_ENG_SIDPLAY1;
     xs_cfg.memoryMode = XS_MPU_BANK_SWITCHING;
 #else
 #error This should not happen! No emulator engines configured in!
 #endif
-#endif
 
     xs_cfg.clockSpeed = XS_CLOCK_PAL;
     xs_cfg.forceSpeed = FALSE;
@@ -71,17 +71,15 @@ void xs_init_configuration(void)
     xs_cfg.sid2OptLevel = 0;
     xs_cfg.sid2NFilterPresets = 0;
 
-#ifdef HAVE_RESID_BUILDER
+#if defined HAVE_SIDPLAYFP_BUILDERS_RESIDFP_H
+    xs_cfg.sid2Builder = XS_BLD_RESIDFP;
+#elif defined HAVE_SIDPLAYFP_BUILDERS_RESID_H || defined HAVE_RESID_BUILDER
     xs_cfg.sid2Builder = XS_BLD_RESID;
-#else
-#ifdef HAVE_HARDSID_BUILDER
+#elif defined HAVE_SIDPLAYFP_BUILDERS_HARDSID_H || defined HAVE_HARDSID_BUILDER
     xs_cfg.sid2Builder = XS_BLD_HARDSID;
-#else
-#ifdef HAVE_SIDPLAY2
+#elif defined HAVE_SIDPLAYFP || defined HAVE_SIDPLAY2
 #error This should not happen! No supported SIDPlay2 builders configured in!
 #endif
-#endif
-#endif
 
     xs_cfg.oversampleEnable = FALSE;
     xs_cfg.oversampleFactor = XS_MIN_OVERSAMPLE;
diff --git a/src/sid/xs_config.h b/src/sid/xs_config.h
index 0bdac96..376acda 100644
--- a/src/sid/xs_config.h
+++ b/src/sid/xs_config.h
@@ -12,12 +12,14 @@ extern "C" {
  */
 enum XS_EMUENGINE {
     XS_ENG_SIDPLAY1 = 1,
-    XS_ENG_SIDPLAY2
+    XS_ENG_SIDPLAY2,
+    XS_ENG_SIDPLAYFP
 };
 
 
 enum XS_BUILDER {
     XS_BLD_RESID = 1,
+    XS_BLD_RESIDFP,
     XS_BLD_HARDSID
 };
 
diff --git a/src/sid/xs_player.c b/src/sid/xs_player.c
index 57c3fed..8b751f0 100644
--- a/src/sid/xs_player.c
+++ b/src/sid/xs_player.c
@@ -6,6 +6,9 @@
 #ifdef HAVE_SIDPLAY2
 #include "xs_sidplay2.h"
 #endif
+#ifdef HAVE_SIDPLAYFP
+#include "xs_sidplayfp.h"
+#endif
 
 
 /* List of emulator engines
@@ -31,6 +34,16 @@ static const xs_engine_t xs_enginelist[] = {
      xs_sidplay2_flush
     },
 #endif
+#ifdef HAVE_SIDPLAYFP
+    {XS_ENG_SIDPLAYFP,
+     xs_sidplayfp_probe,
+     xs_sidplayfp_init, xs_sidplayfp_close,
+     xs_sidplayfp_initsong, xs_sidplayfp_fillbuffer,
+     xs_sidplayfp_load, xs_sidplayfp_delete,
+     xs_sidplayfp_getinfo, xs_sidplayfp_updateinfo,
+     xs_sidplayfp_flush
+    },
+#endif
 };
 
 static const gint xs_nenginelist = (sizeof(xs_enginelist) / sizeof(xs_enginelist[0]));
diff --git a/src/sid/xs_sidplay.h b/src/sid/xs_sidplay.h
index 4b830b1..5d704f2 100644
--- a/src/sid/xs_sidplay.h
+++ b/src/sid/xs_sidplay.h
@@ -22,6 +22,7 @@
 */
 
 
+#ifndef XS_SIDPLAYFP_H
 /* This function gets most of the information, though we do miss some
  * (those variables that are only set by libSIDPlay when tune is initialized).
  * Rest of the information is acquired in TFUNCTION2()
@@ -78,6 +79,7 @@ xs_tuneinfo_t *TFUNCTION(const gchar *sidFilename)
 
     return pResult;
 }
+#endif
 
 
 /* Updates the information of currently playing tune
@@ -101,8 +103,7 @@ gboolean TFUNCTION2(xs_status_t *myStatus)
     /* Get currently playing tune information */
 #ifdef XS_SIDPLAY1_H
     myTune->getInfo(myInfo);
-#endif
-#ifdef XS_SIDPLAY2_H
+#else
     myInfo = myTune->getInfo();
 #endif
 
@@ -110,12 +111,20 @@ gboolean TFUNCTION2(xs_status_t *myStatus)
      * SIDTUNE_SIDMODEL_* similarly to our enums in xs_config.h ...
      */
     i = myStatus->tuneInfo;
-    i->sidModel = myInfo.sidModel;
+#ifdef XS_SIDPLAYFP_H
+    i->sidModel = myInfo->sidModel1();
+#else
+    i->sidModel = myInfo.sidModel1;
+#endif
 
     if ((myStatus->currSong > 0) && (myStatus->currSong <= i->nsubTunes)) {
         gint tmpSpeed = -1;
         
+#ifdef XS_SIDPLAYFP_H
+        switch (myInfo->clockSpeed()) {
+#else
         switch (myInfo.clockSpeed) {
+#endif
         case SIDTUNE_CLOCK_PAL:
             tmpSpeed = XS_CLOCK_PAL;
             break;
@@ -126,7 +135,11 @@ gboolean TFUNCTION2(xs_status_t *myStatus)
             tmpSpeed = XS_CLOCK_ANY;
             break;
         case SIDTUNE_CLOCK_UNKNOWN:
+#ifdef XS_SIDPLAYFP_H
+            switch (myInfo->songSpeed()) {
+#else
             switch (myInfo.songSpeed) {
+#endif
             case SIDTUNE_SPEED_VBI:
                 tmpSpeed = XS_CLOCK_VBI;
                 break;
@@ -134,11 +147,19 @@ gboolean TFUNCTION2(xs_status_t *myStatus)
                 tmpSpeed = XS_CLOCK_CIA;
                 break;
             default:
+#ifdef XS_SIDPLAYFP_H
+                tmpSpeed = myInfo->songSpeed();
+#else
                 tmpSpeed = myInfo.songSpeed;
+#endif
                 break;
             }
         default:
+#ifdef XS_SIDPLAYFP_H
+            tmpSpeed = myInfo->clockSpeed();
+#else
             tmpSpeed = myInfo.clockSpeed;
+#endif
             break;
         }
             
diff --git a/src/sid/xs_sidplayfp.cc b/src/sid/xs_sidplayfp.cc
new file mode 100644
index 0000000..d051908
--- /dev/null
+++ b/src/sid/xs_sidplayfp.cc
@@ -0,0 +1,454 @@
+/*
+   XMMS-SID - SIDPlay input plugin for X MultiMedia System (XMMS)
+
+   libSIDPlay FP support
+
+   Programmed and designed by Matti 'ccr' Hamalainen <ccr@tnsp.org>
+   (C) Copyright 1999-2009 Tecnic Software productions (TNSP)
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along
+   with this program; if not, write to the Free Software Foundation, Inc.,
+   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+#include "xmms-sid.h"
+
+#ifdef HAVE_SIDPLAYFP
+
+#include <stdio.h>
+#include "xs_sidplayfp.h"
+#include "xs_config.h"
+
+
+#include <sidplayfp/sidplayfp.h>
+#include <sidplayfp/SidDatabase.h>
+#include <sidplayfp/SidInfo.h>
+#include <sidplayfp/SidTune.h>
+#include <sidplayfp/SidTuneInfo.h>
+#include <sidplayfp/sidbuilder.h>
+
+class xs_sidplayfp_t {
+public:
+    sidplayfp *currEng;
+    sidbuilder *currBuilder;
+    SidConfig currConfig;
+    SidTune *currTune;
+    guint8 *buf;
+    size_t bufSize;
+
+    xs_sidplayfp_t(void);
+    virtual ~xs_sidplayfp_t(void) { ; }
+};
+
+
+#ifdef HAVE_SIDPLAYFP_BUILDERS_RESID_H
+#  include <sidplayfp/builders/resid.h>
+#endif
+#ifdef HAVE_SIDPLAYFP_BUILDERS_RESIDFP_H
+#  include <sidplayfp/builders/residfp.h>
+#endif
+#ifdef HAVE_SIDPLAYFP_BUILDERS_HARDSID_H
+#  include <sidplayfp/builders/hardsid.h>
+#endif
+
+
+xs_sidplayfp_t::xs_sidplayfp_t(void)
+:currEng(NULL)
+{
+    buf = NULL;
+    bufSize = 0;
+    currTune = NULL;
+    currBuilder = NULL;
+}
+
+
+/* We need to 'export' all this pseudo-C++ crap */
+extern "C" {
+
+
+/* Return song information
+ */
+#define TFUNCTION2  xs_sidplayfp_updateinfo
+#define TTUNEINFO   const SidTuneInfo *
+#define TTUNE       SidTune
+#define TENGINE     xs_sidplayfp_t
+#define SIDTUNE_CLOCK_UNKNOWN SidTuneInfo::CLOCK_UNKNOWN
+#define SIDTUNE_CLOCK_PAL SidTuneInfo::CLOCK_PAL
+#define SIDTUNE_CLOCK_NTSC SidTuneInfo::CLOCK_NTSC
+#define SIDTUNE_CLOCK_ANY SidTuneInfo::CLOCK_ANY
+#define SIDTUNE_SPEED_VBI SidTuneInfo::SPEED_VBI
+#define SIDTUNE_SPEED_CIA_1A SidTuneInfo::SPEED_CIA_1A
+#include "xs_sidplay.h"
+
+/* Check if we can play the given file
+ */
+gboolean xs_sidplayfp_probe(xs_file_t *f)
+{
+    gchar tmpBuf[5];
+
+    if (f == NULL) return FALSE;
+
+    if (xs_fread(tmpBuf, sizeof(gchar), 4, f) != 4)
+        return FALSE;
+
+    if (!strncmp(tmpBuf, "PSID", 4) || !strncmp(tmpBuf, "RSID", 4))
+        return TRUE;
+    else
+        return FALSE;
+}
+
+
+/* Initialize SIDPlayFP
+ */
+gboolean xs_sidplayfp_init(xs_status_t * status)
+{
+    xs_sidplayfp_t *engine;
+    assert(status != NULL);
+
+    /* Allocate internal structures */
+    engine = new xs_sidplayfp_t();
+    status->sidEngine = engine;
+    if (!engine) return FALSE;
+
+    /* Initialize the engine */
+    engine->currEng = new sidplayfp;
+    if (!engine->currEng) {
+        xs_error("[SIDPlayFP] Could not initialize emulation engine.\n");
+        return FALSE;
+    }
+
+    /* Get current configuration */
+    engine->currConfig = engine->currEng->config();
+
+    /* Configure channels and stuff */
+    switch (status->audioChannels) {
+
+    case XS_CHN_AUTOPAN:
+        engine->currConfig.playback = SidConfig::MONO;
+        break;
+
+    case XS_CHN_STEREO:
+        engine->currConfig.playback = SidConfig::STEREO;
+        break;
+
+    case XS_CHN_MONO:
+    default:
+        engine->currConfig.playback = SidConfig::MONO;
+        status->audioChannels = XS_CHN_MONO;
+        break;
+    }
+    status->audioFormat = FMT_S16_NE;
+
+    /* Audio parameters sanity checking and setup */
+    engine->currConfig.frequency = status->audioFrequency;
+    if (status->oversampleEnable)
+        engine->currConfig.frequency *= status->oversampleFactor;
+
+    /* Initialize builder object */
+    XSDEBUG("init builder #%i, maxsids=%i\n", xs_cfg.sid2Builder, engine->currEng->info().maxsids());
+#ifdef HAVE_SIDPLAYFP_BUILDERS_RESID_H
+    if (xs_cfg.sid2Builder == XS_BLD_RESID) {
+        ReSIDBuilder *rs = new ReSIDBuilder("ReSID builder");
+        engine->currBuilder = (sidbuilder *) rs;
+    }
+#endif
+#ifdef HAVE_SIDPLAYFP_BUILDERS_RESIDFP_H
+    if (xs_cfg.sid2Builder == XS_BLD_RESIDFP) {
+        ReSIDfpBuilder *rs = new ReSIDfpBuilder("ReSIDfp builder");
+        engine->currBuilder = (sidbuilder *) rs;
+    }
+#endif
+#ifdef HAVE_SIDPLAYFP_BUILDERS_HARDSID_H
+    if (xs_cfg.sid2Builder == XS_BLD_HARDSID) {
+        HardSIDBuilder *hs = new HardSIDBuilder("HardSID builder");
+        engine->currBuilder = (sidbuilder *) hs;
+    }
+#endif
+
+    if (!engine->currBuilder) {
+        xs_error("[SIDPlayFP] Could not initialize SIDBuilder object.\n");
+        return FALSE;
+    }
+
+    /* Builder object created, initialize it */
+    engine->currBuilder->create(engine->currEng->info().maxsids());
+    if (!engine->currBuilder->getStatus()) {
+        xs_error("reSID->create() failed.\n");
+        return FALSE;
+    }
+
+    XSDEBUG("filter setting NOT supported with libsidplayfp.\n");
+
+    engine->currBuilder->filter(xs_cfg.emulateFilters);
+    if (!engine->currBuilder->getStatus()) {
+        xs_error("reSID->filter(%d) failed.\n", xs_cfg.emulateFilters);
+        return FALSE;
+    }
+
+    engine->currConfig.sidEmulation = engine->currBuilder;
+    XSDEBUG("%s\n", engine->currBuilder->credits());
+
+    /* Clockspeed settings */
+    switch (xs_cfg.clockSpeed) {
+    case XS_CLOCK_NTSC:
+        engine->currConfig.defaultC64Model = SidConfig::NTSC;
+        break;
+
+    default:
+        xs_error("[SIDPlayFP] Invalid clockSpeed=%d, falling back to PAL.\n",
+            xs_cfg.clockSpeed);
+
+    case XS_CLOCK_PAL:
+        engine->currConfig.defaultC64Model = SidConfig::PAL;
+        xs_cfg.clockSpeed = XS_CLOCK_PAL;
+        break;
+    }
+
+    /* Configure rest of the emulation */
+    if (xs_cfg.mos8580)
+        engine->currConfig.defaultSidModel = SidConfig::MOS8580;
+    else
+        engine->currConfig.defaultSidModel = SidConfig::MOS6581;
+
+    engine->currConfig.forceSidModel = xs_cfg.forceModel;
+
+    /* Now set the emulator configuration */
+    if (engine->currEng->config(engine->currConfig) < 0) {
+        xs_error("[SIDPlayFP] Emulator engine configuration failed!\n");
+        return FALSE;
+    }
+
+    /* Create the sidtune */
+    engine->currTune = new SidTune(0);
+    if (!engine->currTune) {
+        xs_error("[SIDPlayFP] Could not initialize SIDTune object.\n");
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+
+/* Close SIDPlayFP engine
+ */
+void xs_sidplayfp_close(xs_status_t * status)
+{
+    xs_sidplayfp_t *engine;
+    assert(status != NULL);
+
+    engine = (xs_sidplayfp_t *) status->sidEngine;
+
+    /* Free internals */
+    if (engine->currBuilder) {
+        delete engine->currBuilder;
+        engine->currBuilder = NULL;
+    }
+
+    if (engine->currEng) {
+        delete engine->currEng;
+        engine->currEng = NULL;
+    }
+
+    if (engine->currTune) {
+        delete engine->currTune;
+        engine->currTune = NULL;
+    }
+
+    xs_sidplayfp_delete(status);
+
+    delete engine;
+    status->sidEngine = NULL;
+}
+
+
+/* Initialize current song and sub-tune
+ */
+gboolean xs_sidplayfp_initsong(xs_status_t * status)
+{
+    xs_sidplayfp_t *engine;
+    assert(status != NULL);
+
+    engine = (xs_sidplayfp_t *) status->sidEngine;
+    if (engine == NULL) return FALSE;
+
+    if (!engine->currTune->selectSong(status->currSong)) {
+        xs_error("[SIDPlayFP] currTune->selectSong() failed\n");
+        return FALSE;
+    }
+
+    if (engine->currEng->load(engine->currTune) < 0) {
+        xs_error("[SIDPlayFP] currEng->load() failed\n");
+        return FALSE;
+    }
+
+    status->isInitialized = TRUE;
+
+    return TRUE;
+}
+
+
+/* Emulate and render audio data to given buffer
+ */
+guint xs_sidplayfp_fillbuffer(xs_status_t * status, gchar * audioBuffer, guint audioBufSize)
+{
+    xs_sidplayfp_t *engine;
+    assert(status != NULL);
+
+    engine = (xs_sidplayfp_t *) status->sidEngine;
+    if (!engine) return 0;
+
+    return engine->currEng->play((short *)audioBuffer, audioBufSize / 2) * 2;
+}
+
+
+/* Load a given SID-tune file
+ */
+gboolean xs_sidplayfp_load(xs_status_t * status, const gchar * pcFilename)
+{
+    /* This is safe, since xmms-sid.c always calls us with xs_status locked */
+    static int loaded_roms = 0;
+
+    xs_sidplayfp_t *engine;
+    assert(status != NULL);
+    status->isInitialized = FALSE;
+
+    engine = (xs_sidplayfp_t *) status->sidEngine;
+    if (!engine) return FALSE;
+
+    /* In xs_sidplayfp_init aud-vfs is not initialized yet, so try to load
+       the optional rom files on the first xs_sidplayfp_load call. */
+    if (!loaded_roms) {
+        size_t size = 0;
+        guint8 *kernal = NULL, *basic = NULL, *chargen = NULL;
+
+        if (xs_fload_buffer("file://" DATADIR "sidplayfp/kernal",
+                            &kernal, &size) != 0 || size != 8192) {
+            g_free(kernal);
+            kernal = NULL;
+        }
+        if (xs_fload_buffer("file://" DATADIR "sidplayfp/basic",
+                            &basic, &size) != 0 || size != 8192) {
+            g_free(basic);
+            basic = NULL;
+        }
+        if (xs_fload_buffer("file://" DATADIR "sidplayfp/chargen",
+                            &chargen, &size) != 0 || size != 4096) {
+            g_free(chargen);
+            chargen = NULL;
+        }
+        engine->currEng->setRoms(kernal, basic, chargen);
+        g_free(kernal);
+        g_free(basic);
+        g_free(chargen);
+        loaded_roms = 1;
+    }
+
+    /* Try to get the tune */
+    if (!pcFilename) return FALSE;
+
+    if (xs_fload_buffer(pcFilename, &(engine->buf), &(engine->bufSize)) != 0)
+        return FALSE;
+
+    engine->currTune->read(engine->buf, engine->bufSize);
+
+    return engine->currTune->getStatus();
+}
+
+
+/* Delete INTERNAL information
+ */
+void xs_sidplayfp_delete(xs_status_t * status)
+{
+    xs_sidplayfp_t *engine;
+    assert(status != NULL);
+
+    engine = (xs_sidplayfp_t *) status->sidEngine;
+    if (engine == NULL) return;
+
+    g_free(engine->buf);
+    engine->buf = NULL;
+    engine->bufSize = 0;
+}
+
+
+/* Hardware backend flushing
+ */
+void xs_sidplayfp_flush(xs_status_t * status)
+{
+    assert(status != NULL);
+
+#ifdef HAVE_HARDSID_BUILDER
+    if (xs_cfg.sid2Builder == XS_BLD_HARDSID)
+        ((HardSIDBuilder *) status->currBuilder)->flush();
+#endif
+}
+
+xs_tuneinfo_t* xs_sidplayfp_getinfo(const gchar *sidFilename)
+{
+    /* This is safe, since xmms-sid.c always calls us with xs_status locked */
+    static int got_db = -1;
+    static SidDatabase database;
+
+    xs_tuneinfo_t *result;
+    const SidTuneInfo *myInfo;
+    SidTune *myTune;
+    guint8 *buf = NULL;
+    size_t bufSize = 0;
+
+    /* Load file */
+    if (!sidFilename) return NULL;
+
+    if (xs_fload_buffer(sidFilename, &buf, &bufSize) != 0)
+        return NULL;
+
+    /* Check if the tune exists and is readable */
+    if ((myTune = new SidTune(buf, bufSize)) == NULL) {
+        g_free(buf);
+        return NULL;
+    }
+    g_free(buf);
+
+    if (!myTune->getStatus()) {
+        delete myTune;
+        return NULL;
+    }
+
+    /* Get general tune information */
+    myInfo = myTune->getInfo();
+
+    /* Allocate tuneinfo structure and set information */
+    result = xs_tuneinfo_new(sidFilename,
+        myInfo->songs(), myInfo->startSong(),
+        myInfo->infoString(0), myInfo->infoString(1), myInfo->infoString(2),
+        myInfo->loadAddr(), myInfo->initAddr(), myInfo->playAddr(),
+        myInfo->dataFileLen(), myInfo->formatString(), myInfo->sidModel1());
+
+    for (int i = 0; i < result->nsubTunes; i++) {
+        if (result->subTunes[i].tuneLength >= 0)
+            continue;
+
+        if (got_db == -1)
+            got_db = database.open(DATADIR "sidplayfp/Songlengths.txt");
+
+        if (got_db) {
+            myTune->selectSong(i + 1);
+            result->subTunes[i].tuneLength = database.length(*myTune);
+        }
+    }
+
+    delete myTune;
+
+    return result;
+}
+
+}    /* extern "C" */
+#endif    /* HAVE_SIDPLAYFP */
diff --git a/src/sid/xs_sidplayfp.h b/src/sid/xs_sidplayfp.h
new file mode 100644
index 0000000..e922f80
--- /dev/null
+++ b/src/sid/xs_sidplayfp.h
@@ -0,0 +1,26 @@
+#ifndef XS_SIDPLAYFP_H
+#define XS_SIDPLAYFP_H
+
+#include "xs_player.h"
+#include "xs_support.h"
+#include "xs_slsup.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+gboolean    xs_sidplayfp_probe(xs_file_t *);
+void        xs_sidplayfp_close(xs_status_t *);
+gboolean    xs_sidplayfp_init(xs_status_t *);
+gboolean    xs_sidplayfp_initsong(xs_status_t *);
+guint        xs_sidplayfp_fillbuffer(xs_status_t *, gchar *, guint);
+gboolean    xs_sidplayfp_load(xs_status_t *, const gchar *);
+void        xs_sidplayfp_delete(xs_status_t *);
+xs_tuneinfo_t*    xs_sidplayfp_getinfo(const gchar *);
+gboolean    xs_sidplayfp_updateinfo(xs_status_t *);
+void        xs_sidplayfp_flush(xs_status_t *);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* XS_SIDPLAYFP_H */
-- 
1.8.2

