view Plugins/Input/console/Audacious_Driver.cpp @ 109:c4876d6c9cc0 trunk

[svn] Add about box.
author nenolod
date Wed, 02 Nov 2005 22:07:17 -0800
parents cde5ca21ddc3
children 3fb426494de8
line wrap: on
line source

/*
 * Audacious: Cross platform multimedia player
 * Copyright (c) 2005  Audacious Team
 *
 * Driver for Game_Music_Emu library. See details at:
 * http://www.slack.net/~ant/libs/
 */

#include "Audacious_Driver.h"

extern "C" {

#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include "libaudacious/configfile.h"
#include "libaudacious/util.h"
#include "libaudacious/titlestring.h"
#include "audacious/output.h"

};

#include <cstring>

#ifdef WORDS_BIGENDIAN
# define MY_FMT FMT_S16_BE
#else
# define MY_FMT FMT_S16_LE
#endif

static Spc_Emu *spc = NULL;
static GThread *decode_thread;

static void *play_loop(gpointer arg);
static void console_init(void);
extern "C" void console_aboutbox(void);
static void console_stop(void);
static void console_pause(gshort p);
static int get_time(void);
static gboolean console_ip_is_going;

extern InputPlugin console_ip;

static int is_our_file(gchar *filename)
{
	gchar *ext;

	ext = strrchr(filename, '.');

	if (ext)
	{
		if (!strcasecmp(ext, ".spc"))
			return 1;
	}

	return 0;
}

static gchar *get_title(gchar *filename)
{
	gchar *title;
	Emu_Std_Reader reader;
	Spc_Emu::header_t header;

	reader.open(filename);
	reader.read(&header, sizeof(header));

	if (header.song)
	{
		TitleInput *tinput;

		tinput = bmp_title_input_new();

		tinput->performer = g_strdup(header.game);
		tinput->album_name = g_strdup(header.game);
		tinput->track_name = g_strdup(header.song);		

		tinput->file_name = g_path_get_basename(filename);
		tinput->file_path = g_path_get_dirname(filename);

		title = xmms_get_titlestring(xmms_get_gentitle_format(),
				tinput);

		g_free(tinput);
	}
	else
		title = g_strdup(filename);

	return title;
}

static void get_song_info(char *filename, char **title, int *length)
{
	(*title) = get_title(filename);
	(*length) = -1;
}

static void play_file(char *filename)
{
	gchar *name;
	Emu_Std_Reader reader;
	Spc_Emu::header_t header;

	reader.open(filename);
	reader.read(&header, sizeof(header));

	spc = new Spc_Emu;
	spc->init(32000);
	spc->load(header, reader);
	spc->start_track(0);

	console_ip_is_going = TRUE;

	name = get_title(filename);

	console_ip.set_info(name, -1, spc->voice_count(), 32000, 2);

	g_free(name);

	decode_thread = g_thread_create(play_loop, spc, TRUE, NULL);

        if (!console_ip.output->open_audio(MY_FMT, 32000, 2))
                 return;
}

static void seek(gint time)
{
	// XXX: Not yet implemented
}

InputPlugin console_ip = {
	NULL,
	NULL,
	NULL,
	console_init,
	console_aboutbox,
	NULL,
	is_our_file,
	NULL,
	play_file,
	console_stop,
	console_pause,
	seek,
	NULL,
	get_time,
	NULL,
	NULL,
	NULL,	
	NULL,
	NULL,
	NULL,
	NULL,
	get_song_info,
	NULL,
	NULL
};

static void console_stop(void)
{
	console_ip_is_going = FALSE;
        g_thread_join(decode_thread);
        console_ip.output->close_audio();
}

extern "C" InputPlugin *get_iplugin_info(void)
{
        console_ip.description = g_strdup_printf(_("SPC, GYM, NSF, VGM and GBS module decoder"));
        return &console_ip;
}

static void console_pause(gshort p)
{
        console_ip.output->pause(p);
}

static void *play_loop(gpointer arg)
{
	Spc_Emu *my_spc = (Spc_Emu *) arg;
        Music_Emu::sample_t buf[1024];

        while (my_spc->play(1024, buf) == NULL && console_ip_is_going == TRUE)
        {
		console_ip.add_vis_pcm(console_ip.output->written_time(),
			MY_FMT, 1, 2048, buf);
	        while(console_ip.output->buffer_free() < 2048)
			xmms_usleep(10000);
		console_ip.output->write_audio(buf, 2048);
	}

        delete spc;
        g_thread_exit(NULL);

        return NULL;
}

static int get_time(void)
{
        return console_ip.output->output_time();
}

static void console_init(void)
{

}

extern "C" void console_aboutbox(void)
{
	xmms_show_message(_("About the Console Music Decoder"),
			_("Console music decoder engine based on Game_Music_Emu 0.2.4.\n"
			  "Audacious implementation by: William Pitcock <nenolod@nenolod.net>"),
			_("Ok"),
			FALSE, NULL, NULL);
}