view src/xs_slsup.c @ 957:0e60e5d56fdd

Change how the backend emulator library is initialized for libSIDPlay2 and FP, as it seems the engine configuration has some persistence despite reconfiguration between loaded files if same engine object is retained. This caused, for example, 2SID stereo tunes being played "mono" if played after a normal 1-SID tune. Duh.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 20 Nov 2012 22:13:48 +0200
parents 5048b4799310
children be2a8436461a
line wrap: on
line source

/*
   XMMS-SID - SIDPlay input plugin for X MultiMedia System (XMMS)

   File information window

   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 "xs_slsup.h"
#include "xs_config.h"


static XSSLDB *xs_sldb_db = NULL;
XS_MUTEX(xs_sldb_db);

static XSSTILDB *xs_stildb_db = NULL;
XS_MUTEX(xs_stildb_db);


/* STIL-database handling
 */
gint xs_stil_init(void)
{
    gint res = 0;
    XS_MUTEX_LOCK(xs_cfg);

    if (xs_cfg.stilDBPath == NULL)
    {
        XS_MUTEX_UNLOCK(xs_cfg);
        return -1;
    }

    XS_MUTEX_LOCK(xs_stildb_db);

    /* Check if already initialized */
    if (xs_stildb_db)
        xs_stildb_free(xs_stildb_db);

    /* Allocate database */
    xs_stildb_db = (XSSTILDB *) g_malloc0(sizeof(XSSTILDB));
    if (xs_stildb_db == NULL)
    {
        res = -2;
        goto error;
    }

    /* Read the database */
    if (xs_stildb_read(xs_stildb_db, xs_cfg.stilDBPath) != 0)
    {
        res = -3;
        goto error;
    }

    /* Create index */
    if (xs_stildb_index(xs_stildb_db) != 0)
    {
        res = -4;
        goto error;
    }

    XS_MUTEX_UNLOCK(xs_cfg);
    XS_MUTEX_UNLOCK(xs_stildb_db);
    return 0;

error:
    xs_stildb_free(xs_stildb_db);
    xs_stildb_db = NULL;
    XS_MUTEX_UNLOCK(xs_cfg);
    XS_MUTEX_UNLOCK(xs_stildb_db);
    return res;
}


void xs_stil_close(void)
{
    XS_MUTEX_LOCK(xs_stildb_db);
    xs_stildb_free(xs_stildb_db);
    xs_stildb_db = NULL;
    XS_MUTEX_UNLOCK(xs_stildb_db);
}


XSSTILNode *xs_stil_get(gchar *filename)
{
    XSSTILNode *result;

    XS_MUTEX_LOCK(xs_stildb_db);
    XS_MUTEX_LOCK(xs_cfg);

    if (xs_cfg.stilDBEnable && xs_stildb_db != NULL)
    {
        gchar *tmpFilename;

        if (xs_cfg.hvscPath != NULL)
        {
            /* Remove postfixed directory separator from HVSC-path */
            tmpFilename = strrchr(xs_cfg.hvscPath, '/');
            if (tmpFilename && tmpFilename[1] == 0)
                tmpFilename[0] = 0;

            /* Remove HVSC location-prefix from filename */
            tmpFilename = strstr(filename, xs_cfg.hvscPath);
            if (tmpFilename != NULL)
                tmpFilename += strlen(xs_cfg.hvscPath);
            else
                tmpFilename = filename;
        }
        else
            tmpFilename = filename;

        result = xs_stildb_get_node(xs_stildb_db, tmpFilename);
    }
    else
        result = NULL;

    XS_MUTEX_UNLOCK(xs_stildb_db);
    XS_MUTEX_UNLOCK(xs_cfg);

    return result;
}


/* Song length database handling glue
 */
gint xs_songlen_init(void)
{
    gint res = 0;

    XS_MUTEX_LOCK(xs_cfg);
    if (xs_cfg.songlenDBPath == NULL)
    {
        XS_MUTEX_UNLOCK(xs_cfg);
        return -1;
    }

    XS_MUTEX_LOCK(xs_sldb_db);

    /* Check if already initialized */
    if (xs_sldb_db != NULL)
        xs_sldb_free(xs_sldb_db);

    /* Allocate database */
    xs_sldb_db = (XSSLDB *) g_malloc0(sizeof(XSSLDB));
    if (xs_sldb_db == NULL)
    {
        res = -1;
        goto error;
    }

    /* Read the database */
    if (xs_sldb_read(xs_sldb_db, xs_cfg.songlenDBPath) != 0)
    {
        res = -3;
        goto error;
    }

    /* Create index */
    if (xs_sldb_index(xs_sldb_db) != 0)
    {
        res = -4;
        goto error;
    }

    XS_MUTEX_UNLOCK(xs_cfg);
    XS_MUTEX_UNLOCK(xs_sldb_db);
    return 0;

error:
    xs_sldb_free(xs_sldb_db);
    xs_sldb_db = NULL;
    XS_MUTEX_UNLOCK(xs_cfg);
    XS_MUTEX_UNLOCK(xs_sldb_db);
    return res;
}


void xs_songlen_close(void)
{
    XS_MUTEX_LOCK(xs_sldb_db);
    xs_sldb_free(xs_sldb_db);
    xs_sldb_db = NULL;
    XS_MUTEX_UNLOCK(xs_sldb_db);
}


XSSLDBNode *xs_songlen_get(const gchar * filename)
{
    XSSLDBNode *result;

    XS_MUTEX_LOCK(xs_sldb_db);

    if (xs_cfg.songlenDBEnable && xs_sldb_db)
        result = xs_sldb_get(xs_sldb_db, filename);
    else
        result = NULL;

    XS_MUTEX_UNLOCK(xs_sldb_db);

    return result;
}


/* Allocate a new tune information structure
 */
XSTuneInfo *xs_tuneinfo_new(const gchar * filename,
        gint nsubTunes, gint startTune, const gchar * sidName,
        const gchar * sidComposer, const gchar * sidCopyright,
        gint loadAddr, gint initAddr, gint playAddr,
        gint dataFileLen, const gchar *sidFormat, gint sidModel)
{
    XSTuneInfo *result;
    XSSLDBNode *tmpLength;
    gint i;

    /* Allocate structure */
    result = (XSTuneInfo *) g_malloc0(sizeof(XSTuneInfo));
    if (result == NULL)
    {
        xs_error("Could not allocate memory for tuneinfo ('%s')\n",
            filename);
        return NULL;
    }

    if ((result->sidFilename = XS_CS_FILENAME(filename)) == NULL)
    {
        xs_error("Could not allocate sidFilename ('%s')\n",
            filename);
        g_free(result);
        return NULL;
    }

    /* Allocate space for subtune information */
    result->subTunes = g_malloc0(sizeof(XSSubTuneInfo) * (nsubTunes + 1));
    if (!result->subTunes)
    {
        xs_error("Could not allocate memory for subtuneinfo ('%s', %i)\n",
            filename, nsubTunes);

        g_free(result->sidFilename);
        g_free(result);
        return NULL;
    }

    /* The following allocations don't matter if they fail */
    result->sidName = XS_CS_SID(sidName);
    result->sidComposer = XS_CS_SID(sidComposer);
    result->sidCopyright = XS_CS_SID(sidCopyright);

    result->nsubTunes = nsubTunes;
    result->startTune = startTune;

    result->loadAddr = loadAddr;
    result->initAddr = initAddr;
    result->playAddr = playAddr;
    result->dataFileLen = dataFileLen;
    result->sidFormat = XS_CS_SID(sidFormat);
    
    result->sidModel = sidModel;

    /* Get length information (NOTE: Do not free this!) */
    tmpLength = xs_songlen_get(filename);
    
    /* Fill in sub-tune information */
    for (i = 0; i < result->nsubTunes; i++)
    {
        result->subTunes[i].tuneLength =
            (tmpLength && (i < tmpLength->nlengths)) ? tmpLength->lengths[i] : -1;
        result->subTunes[i].tuneSpeed = -1;
    }
    
    return result;
}


/* Free given tune information structure
 */
void xs_tuneinfo_free(XSTuneInfo * tune)
{
    if (tune == NULL)
        return;

    g_free(tune->subTunes);
    g_free(tune->sidFilename);
    g_free(tune->sidName);
    g_free(tune->sidComposer);
    g_free(tune->sidCopyright);
    g_free(tune->sidFormat);
    g_free(tune);
}