view src/xmms-sid.cc @ 42:0f00ebab063d

Fixes
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 19 Jun 2003 23:50:01 +0000
parents 1788f4ce6a44
children 90cd38b28a72
line wrap: on
line source

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

   Main source file

   Written by Matti "ccr" Hamalainen <ccr@tnsp.org>

   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
extern "C" {
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <xmms/plugin.h>
#include <xmms/util.h>
#include <xmms/titlestring.h>
#include "xmms-sid.h"
#include "xs_interface.h"
#include "xs_glade.h"
#include "xs_support.h"
#include "xs_config.h"
#include "xs_length.h"
}

#ifdef HAVE_SIDPLAY1
#include <sidplay/player.h>
#include <sidplay/myendian.h>
#include <sidplay/fformat.h>
#endif

#ifdef HAVE_SIDPLAY2
#include <sidplay/sidplay2.h>
#endif


/*
 * Global variables
 */
#ifdef HAVE_SIDPLAY1
static struct emuConfig	xs_emuConf;
static emuEngine	xs_emuEngine;
typedef	sidTune		t_xs_tune;
#endif

#ifdef HAVE_SIDPLAY2
static sidplay2		xs_emuEngine;
#endif

static GtkWidget	*xs_ctrlwin	= NULL;
static pthread_t	xs_decode_thread;
struct t_xs_cfg		xs_cfg;

struct {
	gboolean	isError, isPlaying, allowNext;
	gint		currSong, nSongs;
	t_xs_tune	*currTune;
	gchar		*currFilename;
} xs_status;

pthread_mutex_t xs_mutex = PTHREAD_MUTEX_INITIALIZER;

/*
 * Create sub-song control window
 */
void xs_ctrlwin_open(void)
{
 /* Create sub-song control window */
 if (xs_ctrlwin != NULL)
	{
	gdk_window_raise(xs_ctrlwin->window);
	return;
	}

 xs_ctrlwin = create_xs_ctrlwin();
 gtk_widget_show(xs_ctrlwin);
}


/*
 * Initialize XMMS-SID
 */
void xs_init(void)
{
 XSDEBUG("xs_init()\n");

 /* Initialize and get configuration */
 memset(&xs_cfg, 0, sizeof(xs_cfg));

 xs_read_configuration();

 xs_status.isError	= FALSE;
 xs_status.isPlaying	= FALSE;
 xs_status.nSongs	= 0;
 xs_status.currSong	= 0;
 xs_status.allowNext	= TRUE;	// Initialize to TRUE to allow first song
 xs_status.currTune	= NULL;
 xs_status.currFilename	= NULL;


 /* Try to initialize libSIDPlay(s) */
#ifdef HAVE_SIDPLAY1
 if (!xs_emuEngine || !xs_emuEngine.verifyEndianess())
	{
	XSERR("Couldn't start SIDPlay1 emulator engine!\n");
	return;
	}
#endif

#ifdef HAVE_SIDPLAY2
#endif

 /* Read song-length database */
 if (xs_cfg.songlenDBEnable)
 if (xs_songlen_init() < 0)
	{
	XSERR("Error initializing song-length database!\n");
	}
	
 /* Initialize STIL structures */

 xs_ctrlwin_open();

// FIXME FIXME FIx ME

}


/*
 * Shut down XMMS-SID
 */
void xs_close(void)
{
 XSDEBUG("shutting down...\n");

 /* Stop playing */
 xs_plugin_ip.stop();

 /* Shutdown libSIDPlay(s) */
#ifdef HAVE_SIDPLAY1
#endif

#ifdef HAVE_SIDPLAY2
#endif

 /* Close sub-song control window */

 /* Free allocated memory */
 xs_songlen_close();

// FIXME FIXME: STIL-entries
}


/*
 * Check whether the given file is handled by this plugin
 */
gint xs_is_our_file(char *fileName)
{
 char *pcExt;

 /* Check the filename */
 if (fileName == NULL)
	return FALSE;

 /* Try to detect via libSIDPlay's detection routine, if required */
 if (xs_cfg.detectMagic)
	{
#ifdef HAVE_SIDPLAY1
	t_xs_tune *testTune = new sidTune(fileName);
	if (!testTune) return FALSE;
	if (!testTune->getStatus())
		{
		delete testTune;
		return FALSE;
		}

	delete testTune;
	return TRUE;
#endif
#ifdef HAVE_SIDPLAY2
#endif
	}

 /* Detect just by checking filename extension */
 pcExt = strrchr(fileName, '.');
 if (pcExt)
	{
	pcExt++;
	if (!strcasecmp(pcExt, "psid")) return TRUE;
	if (!strcasecmp(pcExt, "sid"))	return TRUE;
	if (!strcasecmp(pcExt, "dat"))	return TRUE;
	if (!strcasecmp(pcExt, "inf"))	return TRUE;
	if (!strcasecmp(pcExt, "info"))	return TRUE;
	}

 return FALSE;
}


/*
 * Main playing thread loop
 */
static void *xs_play_loop(void *argPointer)
{
 /* Don't allow next song to be set yet */
 pthread_mutex_lock(&xs_mutex);
 xs_status.allowNext = FALSE;
 pthread_mutex_unlock(&xs_mutex);



 XSDEBUG("exiting thread, bye.\n");
 return NULL;
}


/*
 * Start playing the given file
 */
void xs_play_file(char *fileName)
{
 t_xs_tune *newTune;
 struct sidTuneInfo sidInf;

 XSDEBUG("request to load '%s'\n", fileName);


 /* Initialize audio output */
 XSDEBUG("opening audio output plugin...\n");
 if (!xs_plugin_ip.output->open_audio(
	((xs_cfg.fmtBitsPerSample == 16) ? FMT_S16_NE : FMT_U8), xs_cfg.fmtFrequency,
	((xs_cfg.fmtChannels == XS_CHN_MONO) ? 1 : 2)))
	{
	XSERR("Couldn't open audio output plugin!\n");
	delete newTune;
	return;
	}


 /* Try to get the tune */
 newTune = new sidTune(fileName);
 if (newTune == NULL) return;

 XSDEBUG("tune ok, configuring SIDPlay engine\n");


 /* Get current configuration */
 xs_emuEngine.getConfig(xs_emuConf);


 /* Configure channels and stuff */
 switch (xs_cfg.fmtChannels) {

	case XS_CHN_AUTOPAN:
		xs_emuConf.channels = SIDEMU_STEREO;
		xs_emuConf.autoPanning = SIDEMU_CENTEREDAUTOPANNING;
		xs_emuConf.volumeControl = SIDEMU_FULLPANNING;
		break;

	case XS_CHN_STEREO:
		xs_emuConf.channels = SIDEMU_STEREO;
		xs_emuConf.autoPanning = SIDEMU_NONE;
		xs_emuConf.volumeControl = SIDEMU_NONE;
		break;

	case XS_CHN_MONO:
		xs_emuConf.channels = SIDEMU_MONO;
		xs_emuConf.autoPanning = SIDEMU_NONE;
		xs_emuConf.volumeControl = SIDEMU_NONE;
		break;

	default:
		XSERR("Internal: Invalid channels setting. Possibly corrupted configuration file.\n");
		delete newTune;
		return;
 }


 /* Memory mode settings */
 switch (xs_cfg.memoryMode) {
	case XS_MPU_BANK_SWITCHING:
		xs_emuConf.memoryMode = MPU_BANK_SWITCHING;
		break;

	case XS_MPU_TRANSPARENT_ROM:
		xs_emuConf.memoryMode = MPU_TRANSPARENT_ROM;
		break;

	case XS_MPU_PLAYSID_ENVIRONMENT:
		xs_emuConf.memoryMode = MPU_PLAYSID_ENVIRONMENT;
		break;

	default:
		XSERR("Internal: Invalid memoryMode setting. Possibly corrupted configuration file.\n");
		delete newTune;
		return;
 }


 /* Clockspeed settings */
 switch (xs_cfg.clockSpeed) {
	case XS_CLOCK_PAL:
		xs_emuConf.clockSpeed = SIDTUNE_CLOCK_PAL;
		break;

	case XS_CLOCK_NTSC:
		xs_emuConf.clockSpeed = SIDTUNE_CLOCK_NTSC;
		break;

	default:
		XSERR("Internal: Invalid clockSpeed setting. Possibly corrupted configuration file.\n");
		delete newTune;
		return;
 }


 /* Configure rest of the emulation */
 xs_emuConf.bitsPerSample	= xs_cfg.fmtBitsPerSample;
 xs_emuConf.frequency		= xs_cfg.fmtFrequency;
 xs_emuConf.sampleFormat	= SIDEMU_SIGNED_PCM;
 xs_emuConf.mos8580		= xs_cfg.mos8580;
 xs_emuConf.emulateFilter	= xs_cfg.emulateFilters;
 xs_emuConf.filterFs		= xs_cfg.filterFs;
 xs_emuConf.filterFm		= xs_cfg.filterFm;
 xs_emuConf.filterFt		= xs_cfg.filterFt;

 XSDEBUG("configuring engine..\n");


 /* Now set the emulator configuration */
 xs_emuEngine.setConfig(xs_emuConf);
 newTune->getInfo(sidInf);


 /* Initialize status information */
 XSDEBUG("initializing and starting playing thread!\n");

 xs_status.isError	= FALSE;
 xs_status.isPlaying	= TRUE;
 xs_status.allowNext	= TRUE;
 xs_status.currSong	= sidInf.startSong;
 xs_status.nSongs	= sidInf.songs;
 xs_status.currTune	= newTune;


 /* Start the playing thread! */
 if (pthread_create(&xs_decode_thread, NULL, xs_play_loop, NULL) < 0)
	{
	XSERR("Couldn't start playing thread! Possible reason reported by system: %s\n", strerror(errno));
	delete newTune;
	}

 /* Exit */
}


/*
 * Stop playing
 */
void xs_stop(void)
{
 /* If playing, stop. */
 XSDEBUG("request to stop.\n");

 if (xs_status.isPlaying)
	{
	XSDEBUG("stopping...\n");
	pthread_mutex_lock(&xs_mutex);
	xs_status.isPlaying = 0;
	pthread_mutex_unlock(&xs_mutex);

	pthread_join(xs_decode_thread, NULL);

	xs_plugin_ip.output->close_audio();
	}
}


/*
 * Pause the playing
 */
void xs_pause(short pauseState)
{
 xs_plugin_ip.output->pause(pauseState);
}


/*
 * Set the time-seek position
 * (the playing thread will do the "seeking" aka song-change)
 */
void xs_seek(int iTime)
{
#ifdef HAVE_SONG_POSITION
 pthread_mutex_lock(&xs_mutex);

 if ((iTime > 0) && (iTime <= xs_status.nSongs) && xs_status.isPlaying)
	{
	xs_status.currSong = iTime;
	}

 pthread_mutex_unlock(&xs_mutex);
#endif
}


/*
 * Return the playing "position/time"
 */
int xs_get_time(void)
{
 pthread_mutex_lock(&xs_mutex);
 if (xs_status.isError)
	return -2;

 if (!xs_status.isPlaying)
	return -1;

#ifdef HAVE_SONG_POSITION
 set_song_position(xs_status.currSong, 1, xs_status.nSongs);
#endif

 pthread_mutex_unlock(&xs_mutex);

 return xs_plugin_ip.output->output_time();
}


/*
 * Return song information
 */
void xs_get_song_info(char *songFilename, char **songTitle, int *songLength)
{
 struct sidTuneInfo sidInf;
 t_xs_tune *testTune = new sidTune(songFilename);

 /* Check if the tune exists and is readable */
 if (!testTune) return;
 if (!testTune->getStatus())
	{
	delete testTune;
	return;
	}

 /* Get general tune information */
 testTune->getInfo(sidInf);
 delete testTune;

 /* Get titlestring */
 *songTitle = NULL;

 /* Get song length (in milliseconds), negative if no known length */
 *songLength = xs_songlen_get(songFilename, sidInf.startSong) * 1000;
}


/*
 * Create the SID-tune description string from the tune's information
 * formatted by the user-specified format-string.
 */
gchar *xs_filetitle_get(gchar *pcFilename, void *pfInfo, gint iSubTune)
{
 gint i, j, iLength;
 gchar *pcResult;
 struct sidTuneInfo *finfo = (struct sidTuneInfo *) pfInfo;
#ifdef HAVE_XMMSEXTRA
 TitleInput *ptInput;
#endif

 // FIXME FIXME: get STIL-info
	

#ifdef HAVE_XMMSEXTRA
 /* Check if the titles are overridden or not */
 if (!xs_cfg.titleOverride)
	{
	/* Use generic XMMS titles */
	/* XMMS_NEW_TITLEINPUT(ptInput);
	 * We duplicate and add typecast to the code here due to XMMS's braindead headers
	 */
	ptInput = (TitleInput *) g_malloc0(sizeof(TitleInput));
	ptInput->__size = XMMS_TITLEINPUT_SIZE;
	ptInput->__version = XMMS_TITLEINPUT_VERSION;

	/* Create the input fields */
	ptInput->file_name	= pcFilename;
	ptInput->file_ext	= pcFilename;
	ptInput->file_path	= pcFilename;

	ptInput->track_name	= finfo->nameString;
	ptInput->track_number	= iSubTune;
	ptInput->album_name	= NULL;
	ptInput->performer	= finfo->authorString;
	xs_strcalloc(&ptInput->date, "");
	ptInput->year		= 0;
	xs_strcalloc(&ptInput->genre, "SID-tune");
	ptInput->comment	= finfo->copyrightString;

	/* Create the string */
	pcResult = xmms_get_titlestring(xmms_get_gentitle_format(), ptInput);

	/* Dispose all allocated memory */
	g_free(ptInput->date);
	g_free(ptInput->genre);
	g_free(ptInput);
	} else {
#endif
	/* Check the info strings */
	if (finfo->numberOfInfoStrings != 3)
		{
		if (finfo->numberOfInfoStrings < 1)
			return 0;

		return g_strdup(finfo->infoString[0]);
		}

	/* Check the format-string for NULL */
	if (xs_cfg.titleFormat == NULL)
		return g_strdup_printf("%s - %s", finfo->nameString, finfo->authorString);

	/* Construct the final result info */
	for (j = i = 0; i < strlen(xs_cfg.titleFormat); i++)
		{
		if (xs_cfg.titleFormat[i] == '%')
			{
			switch (xs_cfg.titleFormat[++i]) {
			case '1':
				xs_strpcat(&pcResult, &j, finfo->authorString);
				break;

			case '2':
				xs_strpcat(&pcResult, &j, finfo->nameString);
				break;

			case '3':
				xs_strpcat(&pcResult, &j, finfo->copyrightString);
				break;

			case '4':
				xs_strpcat(&pcResult, &j, finfo->formatString);
				break;
			} /* case */
			} else
			pcResult[j++] = xs_cfg.titleFormat[i];
		}

	pcResult[j] = 0;
#ifdef HAVE_XMMSEXTRA
	}
#endif

 return pcResult;
}