/*********************************************************************

  jukebox.c

*********************************************************************/

#include "driver.h"
#include "ui_text.h"
#include <time.h>
#include <ctype.h>
#include "neogeo.h"
#include "cps1.h"
#include "video.h"
#include "jukebox.h"

/**************************************************************************/

#define MAX_PLAYLIST_ITEM 128
#define MAX_PLAYLIST_SECT 64
#define MAX_REMIXLIST_ITEM 512
#define MAX_REMIXLIST_SECT 64
#define ROWHEIGHT (Machine->uifontheight + Machine->uifontheight / 4)

/**************************************************************************/

struct bankcode_t
{
	const char *name;
	const int  sound_type;
	const UINT16 sound_bank[8];
};

static struct jukebox_t
{
	int num_bank;
	int curr_bank;
	int status;
	int command;
	int counter;
	int playlist;
	int playlist_bak;
	int playlist_format;
	int playlist_sect;
	int playlist_sbak;
	int remixlist;
	int remixlist_bak;
	int menu_init;
	UINT16 start;
	UINT16 stop1;
	UINT16 stop2;
	UINT16 bank[8];
	UINT16 code[2];
	UINT16 address[8];
	UINT16 que[8];
	UINT16 level[2];
} jukebox;

static struct playlist_t
{
	UINT16 code;
	char desc[80];
	char notes[80];
	int flag;
} playlist[MAX_PLAYLIST_SECT][MAX_PLAYLIST_ITEM];

static struct remixlist_t
{
	char title[80];
	int command;
	int playframes;
	UINT16 code;
	int items;
} remixlist[MAX_REMIXLIST_SECT][MAX_REMIXLIST_ITEM];

/**************************************************************************/

enum
{
	CODETYPE_BGM,
	CODETYPE_SE
};

enum
{
	JUKE_STOP = 0,
	JUKE_STOPWAIT,
	JUKE_START,
	JUKE_STARTWAIT,
	JUKE_PLAYING,
	JUKE_PLAYWAIT,
	JUKE_RECORDING,
	JUKE_RECORDWAIT,
	JUKE_IDLE
};

enum
{
	SOUNDTYPE_UNKNOWN = 0,
	SOUNDTYPE_SNK,
	SOUNDTYPE_ADK,
	SOUNDTYPE_DATAEAST,
	SOUNDTYPE_FACE,
	SOUNDTYPE_NAZKA,
	SOUNDTYPE_SUMMY,
	SOUNDTYPE_TECHNOS,
	SOUNDTYPE_TECMO,
	SOUNDTYPE_YUMEKOBO,
	SOUNDTYPE_CPS1,
	SOUNDTYPE_CPS1SP,
	SOUNDTYPE_QSOUND,
	SOUNDTYPE_MAX
};

enum
{
	COMMAND_PLAY = 0,
	COMMAND_STOP,
	COMMAND_RECORD
};

enum
{
	REMIX_STOP = 0,
	REMIX_PLAY,
	REMIX_FADEIN,
	REMIX_FADEOUT,
	REMIX_JUMP,
	REMIX_END,
	REMIX_MAX
};

/**************************************************************************/

struct bankcode_t bankcode[] =
{
	/* MVS NeoGeo Games */

	/* SNK */
	{ "nam1975",	SOUNDTYPE_SNK,		{ 0x0722, 0x0780, 0x1811, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "bstars",		SOUNDTYPE_SNK,		{ 0x0726, 0x0784, 0x1811, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "tpgolf",		SOUNDTYPE_SNK,		{ 0x0721, 0x0780, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "mahretsu",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1811, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ridhero",	SOUNDTYPE_SNK,		{ 0x0721, 0x0781, 0x1811, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "alpham2",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1811, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "cyberlip",	SOUNDTYPE_SNK,		{ 0x0721, 0x07cc, 0x1811, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "superspy",	SOUNDTYPE_SNK,		{ 0x0723, 0x07c0, 0x1830, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "mutnat",		SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "kotm",		SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "sengoku",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "burningf",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "lbowling",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "gpilots",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "joyjoy",		SOUNDTYPE_SNK,		{ 0x0723, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "quizdais",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "lresort",	SOUNDTYPE_SNK,		{ 0x0725, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "eightman",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "legendos",	SOUNDTYPE_SNK,		{ 0x0721, 0x07d0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "2020bb",		SOUNDTYPE_SNK,		{ 0x0722, 0x07c0, 0x1811, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "socbrawl",	SOUNDTYPE_SNK,		{ 0x0725, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "fatfury1",	SOUNDTYPE_SNK,		{ 0x0725, 0x07c0, 0x184c, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "roboarmy",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1830, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "fbfrenzy",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1831, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "kotm2",		SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1820, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "sengoku2",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x183b, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "bstars2",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1843, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "quizdai2",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c1, 0x1824, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "3countb",	SOUNDTYPE_SNK,		{ 0x0726, 0x07c0, 0x1a12, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "aof",		SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a11, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "samsho",		SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a11, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "tophuntr",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a12, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "fatfury2",	SOUNDTYPE_SNK,		{ 0x0722, 0x07c0, 0x1a12, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ssideki",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "kof94",		SOUNDTYPE_SNK,		{ 0x0721, 0x1a20, 0x1c20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "aof2",		SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a11, 0x1c11, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "fatfursp",	SOUNDTYPE_SNK,		{ 0x0723, 0x07b0, 0x1a13, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "savagere",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a11, 0x1c11, 0x1e11, 0x0000, 0x0000, 0x0000 } },
	{ "ssideki2",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a12, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "samsho2",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a11, 0x1c11, 0x1e11, 0x0000, 0x0000, 0x0000 } },
	{ "fatfury3",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a11, 0x1c11, 0x1e11, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ssideki3",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a11, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "kof95",		SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a11, 0x1e11, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "samsho3",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a11, 0x1c11, 0x1d11, 0x1e11, 0x0000, 0x0000 } },
	{ "rbff1",		SOUNDTYPE_SNK,		{ 0x0722, 0x1a11, 0x1c11, 0x1d11, 0x1e11, 0x0000, 0x0000, 0x0000 } },
	{ "aof3",		SOUNDTYPE_SNK,		{ 0x0721, 0x1a11, 0x1c11, 0x1d11, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "kof96",		SOUNDTYPE_SNK,		{ 0x0721, 0x1a11, 0x1b11, 0x1c11, 0x1d11, 0x1e11, 0x0000, 0x0000 } },
	{ "ssideki4",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a11, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "kizuna",		SOUNDTYPE_SNK,		{ 0x0721, 0x1a11, 0x1c11, 0x1d11, 0x1e11, 0x0000, 0x0000, 0x0000 } },
	{ "samsho4",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a04, 0x1b04, 0x1c04, 0x1d11, 0x1e11, 0x0000, 0x0000 } },
	{ "rbffspec",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a11, 0x1b04, 0x1c04, 0x1d04, 0x1e04, 0x0000, 0x0000 } },
	{ "kof97",		SOUNDTYPE_SNK,		{ 0x0721, 0x1a11, 0x1b11, 0x1c11, 0x1d11, 0x1e11, 0x0000, 0x0000 } },
	{ "lastblad",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a04, 0x1b11, 0x1c11, 0x1d11, 0x1e04, 0x0000, 0x0000 } },
	{ "irrmaze",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "rbff2",		SOUNDTYPE_SNK,		{ 0x0721, 0x1604, 0x1711, 0x1a04, 0x1b11, 0x1c11, 0x1d04, 0x1e04 } },
	{ "mslug2",		SOUNDTYPE_NAZKA,	{ 0x0320, 0x1020, 0x1122, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "kof98",		SOUNDTYPE_SNK,		{ 0x0721, 0x1604, 0x1704, 0x1a11, 0x1b11, 0x1c04, 0x1d04, 0x1e11 } },
	{ "lastbld2",	SOUNDTYPE_SNK,		{ 0x0721, 0x1604, 0x1704, 0x1b04, 0x1c04, 0x1d04, 0x1e04, 0x0000 } },
	{ "neocup98",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a11, 0x1c11, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "mslugx",		SOUNDTYPE_NAZKA,	{ 0x0320, 0x1020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "kof99",		SOUNDTYPE_SNK,		{ 0x0721, 0x1604, 0x1704, 0x1a11, 0x1b04, 0x1c04, 0x1d04, 0x1e04 } },
	{ "garou",		SOUNDTYPE_SNK,		{ 0x0721, 0x1604, 0x1704, 0x1b04, 0x1c04, 0x1d04, 0x1e04, 0x0000 } },
	{ "mslug3",		SOUNDTYPE_NAZKA,	{ 0x0320, 0x1020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "kof2000",	SOUNDTYPE_SNK,		{ 0x0721, 0x1604, 0x1704, 0x1a04, 0x1b04, 0x1c04, 0x1d04, 0x1e04 } },
	{ "zupapa",		SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },	// JukeBox only

	/* Alpha Denshi Co. / ADK (changed name in 1993) */
	{ "maglord",	SOUNDTYPE_ADK,		{ 0x0004, 0x002d, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ncombat",	SOUNDTYPE_ADK,		{ 0x0004, 0x001e, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "bjourney",	SOUNDTYPE_ADK,		{ 0x0004, 0x0041, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "crsword",	SOUNDTYPE_ADK,		{ 0x0004, 0x0024, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "trally",		SOUNDTYPE_ADK,		{ 0x0004, 0x001f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ncommand",	SOUNDTYPE_ADK,		{ 0x0004, 0x0024, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "wh1",		SOUNDTYPE_ADK,		{ 0x0004, 0x0024, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "wh2",		SOUNDTYPE_ADK,		{ 0xfc04, 0xfe04, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "wh2j",		SOUNDTYPE_ADK,		{ 0xfc04, 0xfe04, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "aodk",		SOUNDTYPE_ADK,		{ 0xfc04, 0xfe05, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "whp",		SOUNDTYPE_ADK,		{ 0xfc04, 0xfe05, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "mosyougi",	SOUNDTYPE_ADK,		{ 0xfc04, 0xfe05, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "overtop",	SOUNDTYPE_ADK,		{ 0xfc04, 0xfe05, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ninjamas",	SOUNDTYPE_ADK,		{ 0xfcc0, 0xfd04, 0xfe04, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "twinspri",	SOUNDTYPE_ADK,		{ 0xfc04, 0xfe05, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Aicom */
	{ "janshin",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "pulstar",	SOUNDTYPE_SNK,		{ 0x0780, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Data East Corporation */
	{ "spinmast",	SOUNDTYPE_DATAEAST,	{ 0x1622, 0x1911, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "wjammers",	SOUNDTYPE_DATAEAST,	{ 0x1602, 0x1801, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "karnovr",	SOUNDTYPE_DATAEAST,	{ 0x1602, 0x1801, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "strhoop",	SOUNDTYPE_DATAEAST,	{ 0x1601, 0x1801, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "magdrop2",	SOUNDTYPE_DATAEAST,	{ 0x1601, 0x1801, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "magdrop3",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a11, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Eleven */
	{ "nitd",		SOUNDTYPE_SNK,		{ 0x0721, 0x0760, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Eolith */
	{ "kof2001",	SOUNDTYPE_SNK,		{ 0x0721, 0x1704, 0x1a04, 0x1b04, 0x1c04, 0x1d04, 0x1e04, 0x1f04 } },	// JukeBox only
	{ "kof2002",	SOUNDTYPE_SNK,		{ 0x0721, 0x1704, 0x1a04, 0x1b04, 0x1c04, 0x1d04, 0x1e04, 0x1f04 } },	// JukeBox only

	/* Face */
	{ "gururin",	SOUNDTYPE_FACE,		{ 0x0021, 0x0041, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "miexchng",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a21, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Hudson Soft */
	{ "panicbom",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "kabukikl",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a21, 0x1c21, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "neobombe",	SOUNDTYPE_SNK,		{ 0x0722, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Mega Enterprise */
	{ "mslug4",		SOUNDTYPE_SNK,		{ 0x0721, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },	// JukeBox Only

	/* Monolith Corp. */
	{ "minasan",	SOUNDTYPE_SNK,		{ 0x0721, 0x0780, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "bakatono",	SOUNDTYPE_SNK,		{ 0x0730, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Nazca */
	{ "turfmast",	SOUNDTYPE_NAZKA,	{ 0x0322, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "mslug",		SOUNDTYPE_NAZKA,	{ 0x0320, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* NMK */
	{ "zedblade",	SOUNDTYPE_SNK,		{ 0x0071, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Noise Factory */
	{ "sengoku3",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1904, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },	// JukeBox only
	{ "rotd",		SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },	// JukeBox only

	/* Psikyo */
	{ "s1945p",		SOUNDTYPE_SNK,		{ 0x0720, 0x0740, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Sammy */
	{ "viewpoin",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c1, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Saurus */
	{ "quizkof",	SOUNDTYPE_SNK,		{ 0x0722, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "stakwin",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ragnagrd",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "pgoal",		SOUNDTYPE_SNK,		{ 0x0722, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "stakwin2",	SOUNDTYPE_SNK,		{ 0x0721, 0x1820, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "shocktro",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "shocktr2",	SOUNDTYPE_SNK,		{ 0x0021, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Sunsoft */
	{ "galaxyfg",	SOUNDTYPE_SNK,		{ 0x0722, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "wakuwak7",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Taito */
	{ "pbobblen",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "pbobbl2n",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a21, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Takara */
	{ "marukodq",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Technos */
	{ "doubledr",	SOUNDTYPE_TECHNOS,	{ 0x00dc, 0x0004, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "gowcaizr",	SOUNDTYPE_SNK,		{ 0x0721, 0x1a60, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "sdodgeb",	SOUNDTYPE_TECHNOS,	{ 0x0082, 0x0004, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Tecmo */
	{ "tws96",		SOUNDTYPE_TECMO,	{ 0x0004, 0x000f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Yumekobo */
	{ "blazstar",	SOUNDTYPE_SNK,		{ 0x0720, 0x07c0, 0x1820, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "preisle2",	SOUNDTYPE_SNK,		{ 0x0721, 0x0760, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Viccom */
	{ "fightfev",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a20, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Video System Co. */
	{ "pspikes2",	SOUNDTYPE_SNK,		{ 0x0720, 0x0740, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "sonicwi2",	SOUNDTYPE_SNK,		{ 0x0720, 0x0740, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "sonicwi3",	SOUNDTYPE_SNK,		{ 0x0720, 0x0750, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "popbounc",	SOUNDTYPE_SNK,		{ 0x0020, 0x0740, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Visco */
	{ "androdun",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "puzzledp",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "neomrdo",	SOUNDTYPE_SNK,		{ 0x0723, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "goalx3",		SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "neodrift",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "breakers",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a20, 0x1c20, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "flipshot",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a20, 0x1c20, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ctomaday",	SOUNDTYPE_SNK,		{ 0x0021, 0x07c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ganryu",		SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1a20, 0x1c20, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "bangbead",	SOUNDTYPE_SNK,		{ 0x0721, 0x07c0, 0x1820, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Unknown */
	{ "diggerma",	SOUNDTYPE_FACE,		{ 0x0021, 0x0041, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Capcom CPS1 games */
	
	/* Capcom */
	{ "forgottn",	SOUNDTYPE_CPS1,		{ 0x0001, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ghouls",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "strider",	SOUNDTYPE_CPS1,		{ 0x0001, 0x0030, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "dw",			SOUNDTYPE_CPS1,		{ 0x0001, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "willow",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0045, 0x0020, 0x0065, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "unsquad",	SOUNDTYPE_CPS1,		{ 0x0001, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ffight",		SOUNDTYPE_CPS1,		{ 0x0040, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "1941",		SOUNDTYPE_CPS1,		{ 0x0040, 0x0001, 0x0060, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "mercs",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0021, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "mtwins",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "msword",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0057, 0x0021, 0x0080, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "cawing",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0021, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "nemo",		SOUNDTYPE_CPS1,		{ 0x0001, 0x001e, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "sf2",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0079, 0x0021, 0x0085, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "3wonders",	SOUNDTYPE_CPS1,		{ 0x0020, 0x0001, 0x0046, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "kod",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0018, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "captcomm",	SOUNDTYPE_CPS1,		{ 0x0080, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "knights",	SOUNDTYPE_CPS1,		{ 0x0001, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "sf2ce",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0079, 0x0021, 0x0085, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "varth",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "cworld2j",	SOUNDTYPE_CPS1,		{ 0x0001, 0x0046, 0x001c, 0x0057, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "qad",		SOUNDTYPE_CPS1,		{ 0x0020, 0x0001, 0x0050, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "qtono2",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0030, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "megaman",	SOUNDTYPE_CPS1SP,	{ 0x0001, 0x0029, 0x0056, 0x0070, 0x0101, 0x0000, 0x0000, 0x0000 } },

	{ "wof",		SOUNDTYPE_QSOUND,	{ 0x0060, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "dino",		SOUNDTYPE_QSOUND,	{ 0x0000, 0x002c, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "punisher",	SOUNDTYPE_QSOUND,	{ 0x0000, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "slammast",	SOUNDTYPE_QSOUND,	{ 0x0020, 0x0001, 0x0040, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Compile */
	{ "pnickj",		SOUNDTYPE_CPS1,		{ 0x0020, 0x0001, 0x0046, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Mitchell */
	{ "pang3",		SOUNDTYPE_CPS1,		{ 0x0001, 0x0030, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* CPS Changer */
	{ "sfzch",		SOUNDTYPE_CPS1SP,	{ 0x0001, 0x0030, 0x0101, 0x0130, 0x0000, 0x0000, 0x0000, 0x0000 } },

	/* Capcom CPS2 games */

	/* Capcom */
	{ "ssf2",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0040, 0x00c0, 0x0060, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ecofghtr",	SOUNDTYPE_QSOUND,	{ 0x0000, 0x0020, 0x00a0, 0x0100, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ddtod",		SOUNDTYPE_QSOUND,	{ 0x0000, 0x00ed, 0x0118, 0x002b, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "avsp",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0030, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "dstlk",		SOUNDTYPE_QSOUND,	{ 0x0000, 0x0030, 0x0110, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "ringdest",	SOUNDTYPE_QSOUND,	{ 0x0001, 0x0031, 0x009f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "armwar",		SOUNDTYPE_QSOUND,	{ 0x0000, 0x0030, 0x0100, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "xmcota",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0070, 0x0100, 0x0080, 0x0190, 0x0000, 0x0000, 0x0000 } },
	{ "nwarr",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0030, 0x0110, 0x03ea, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "cybots",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x00e8, 0x0300, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "sfa",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x00f7, 0x00e0, 0x0160, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "rckmanj",	SOUNDTYPE_QSOUND,	{ 0x0001, 0x0101, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },	// JukeBox only
	{ "msh",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0060, 0x00f0, 0x0070, 0x01a0, 0x0000, 0x0000, 0x0000 } },
	{ "19xx",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0040, 0x0070, 0x00c0, 0x0182, 0x0200, 0x0000, 0x0000 } },
	{ "ddsom",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x003c, 0x008c, 0x00f8, 0x0042, 0x00a0, 0x0000, 0x0000 } },
	{ "sfa2",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0080, 0x0100, 0x0050, 0x00a1, 0x0180, 0x0000, 0x0000 } },
	{ "spf2t",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x00e0, 0x0091, 0x0120, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "qndream",	SOUNDTYPE_QSOUND,	{ 0x0001, 0x0041, 0x0070, 0x0030, 0x0060, 0x0080, 0x0000, 0x0000 } },
	{ "megaman2",	SOUNDTYPE_QSOUND,	{ 0x0001, 0x00d8, 0x00fd, 0x018f, 0x00f0, 0x0130, 0x0000, 0x0000 } },
	{ "xmvsf",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0080, 0x0100, 0x008b, 0x0180, 0x02a0, 0x0000, 0x0000 } },
	{ "batcir",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0030, 0x0100, 0x0060, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "vsav",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x00dd, 0x01a0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "mshvsf",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0100, 0x0040, 0x0180, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "cscluba",	SOUNDTYPE_QSOUND,	{ 0x0001, 0x00e0, 0x0040, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "sgemf",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0040, 0x00e0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "vhunt2",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x00e4, 0x01a0, 0x0200, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "vsav2",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x00e2, 0x01d0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "mvsc",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0100, 0x0030, 0x0180, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "sfa3",		SOUNDTYPE_QSOUND,	{ 0x0001, 0x0080, 0x00c0, 0x0200, 0x0100, 0x0300, 0x0000, 0x0000 } },
	{ "gigawing",	SOUNDTYPE_QSOUND,	{ 0x0001, 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
	{ "mmatrix",	SOUNDTYPE_QSOUND,	{ 0x0001, 0x0010, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },	// JukeBox only

	/* Raizing */
	{ "dimahoo",	SOUNDTYPE_QSOUND,	{ 0x00c0, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 } },
};

/**************************************************************************/

static int num_bankcode = sizeof(bankcode) / sizeof(bankcode[0]);

static int in_jukebox = 0;
static int playing_no = 0;

static int playlist_selected = 0;
static int soundtest_selected = 0;
static int remixlist_selected = 0;
static int line_volume[2] = { 0, 0 };
static int line_counter[2] = { 0, 0 };
static int volume_delay = 0;
static int random_play = 0;
static int playing_time = 0;
static int playing_wait = 0;
static int nosound_time = 0;
static int randomplay_time = 0;
static int fadeout_timing = 0;
static int remixlist_command = 0;
static int fadein_counter = 0;
static int timer_view = 0;
static int remix_play = -1;

static int max_code = 0;

/**************************************************************************/

int random_time     = 120;
int fadeout_frame   = 600;
int fadeout_counter = 0;

char *sound_filename = "sound.dat";
char *remix_filename = "remix.dat";

/**************************************************************************/

extern int wave_recording;
extern int wave_counter;
extern int wave_seconds;
extern int wave_minutes;

/**************************************************************************/

static int get_playingno(const char* name)
{
	int i;

	for (i = 0; i < num_bankcode; i++)
	{
		if (!strcmp(name, bankcode[i].name))
			return i + 1;
	}

	return 0;
}

static int atohex(char *s)
{
	int res = 0;

	while (*s)
	{
		res *= 0x10;
		if ((*s >= 'A' && *s <= 'F')
		||  (*s >= 'a' && *s <= 'f'))
		{
			res += (toupper(*s) - 'A') + 10;
		}
		else
		{
			res += *s - '0';
		}
		s++;
	}

	return res;
}

static int get_level(UINT16 val)
{
	int i;

	UINT16 level_table[64] =
	{
		  128,   138,   149,   161,   174,   188,   203,   219,
		  237,   256,   276,   299,   322,   348,   376,   406,
		  439,   474,   512,   553,   597,   645,   697,   753,
		  813,   878,   948,  1024,  1106,  1195,  1290,  1393,
		 1505,  1625,  1756,  1896,  2048,  2212,  2389,  2580,
		 2787,  3010,  3251,  3511,  3792,  4096,  4424,  4778,
		 5161,  5574,  6020,  6502,  7023,  7585,  8192,  8848,
		 9556, 10321, 11148, 12040, 13004, 14045, 15170, 16384
	};

	val >>= 1;

	for (i= 0; i < 64; i++)
	{
		if (val < level_table[i])
			break;
	}

	return i;
}

static void volume_window(struct mame_bitmap *bitmap)
{
	int i, j, x, y;
	static int delay[2] = { 0, 0 };
	static int counter[2];

	x = (Machine->uiwidth - 212) / 2 + 15;
	y = (Machine->uiheight - 7*Machine->uifontheight/2) + Machine->uifontheight / 2 + 2;

	ui_drawbox(bitmap, (Machine->uiwidth - 212) / 2,
			  (Machine->uiheight - 7*Machine->uifontheight/2) + 2,
			  212,
			  3*Machine->uifontheight - 4, 1);

	ui_text(bitmap, "L", x - 10, y - 2);
	ui_text(bitmap, "R", x - 10, y + 11);

	for (i = 0; i < 2; i++)
	{
		for (j = 0; j < 42; j++)
			ui_plotbox(bitmap, x + j * 3, y + (i * 12), 2, 8, VOLUME_DARKGREEN);
		for (j = 42; j < 58; j++)
			ui_plotbox(bitmap, x + j * 3, y + (i * 12), 2, 8, VOLUME_DARKYELLOW);
		for (j = 58; j < 64; j++)
			ui_plotbox(bitmap, x + j * 3, y + (i * 12), 2, 8, VOLUME_DARKRED);

		if (volume_delay)
		{
			counter[0] = 0;
			counter[1] = 0;
			volume_delay--;
			ui_plotbox(bitmap, x - 1, y + (i * 12), 1, 8, VOLUME_MOVEBAR);
		}
		else
		{
			extern UINT16 current_level[2];
			int level;

			level = get_level(current_level[i]);

			if (delay[i]-- <= 0)
			{
				if (jukebox.level[i] > 1)
					jukebox.level[i] -= 2;
				else if (jukebox.level[i] == 1)
					jukebox.level[i] = 0;
				delay[i] = 1;
			}

			if (jukebox.level[i] < level)
			{
				jukebox.level[i] = level;
				delay[i] = 1;
			}

			if (line_volume[i] - counter[i] < jukebox.level[i])
			{
				line_volume[i] = jukebox.level[i];
				line_counter[i] = 0;
				counter[i] = 0;
			}
			else if (line_volume[i] - counter[i] > jukebox.level[i])
			{
				line_counter[i]++;
				counter[i] = (int)(line_counter[i] * line_counter[i] * 0.005);
				if (line_volume[i] - counter[i] < 0)
				{
					line_volume[i] = 0;
					line_counter[i] = 0;
					counter[i] = 0;
				}
			}

			ui_plotbox(bitmap, x + (line_volume[i] - counter[i]) * 3 - 1, y + (i * 12), 1, 8, VOLUME_MOVEBAR);

			if (jukebox.level[i] <= 42)
			{
				for (j = 0; j < jukebox.level[i]; j++)
					ui_plotbox(bitmap, x + j * 3, y + (i * 12), 2, 8, VOLUME_LIGHTGREEN);
			}
			else if (jukebox.level[i] <= 58)
			{
				for (j = 0; j < 42; j++)
					ui_plotbox(bitmap, x + j * 3, y + (i * 12), 2, 8, VOLUME_LIGHTGREEN);
				for (j = 42; j < jukebox.level[i]; j++)
					ui_plotbox(bitmap, x + j * 3, y + (i * 12), 2, 8, VOLUME_LIGHTYELLOW);
			}
			else 
			{
				for (j = 0; j < 42; j++)
					ui_plotbox(bitmap, x + j * 3, y + (i * 12), 2, 8, VOLUME_LIGHTGREEN);
				for (j = 42; j < 58; j++)
					ui_plotbox(bitmap, x + j * 3, y + (i * 12), 2, 8, VOLUME_LIGHTYELLOW);
				for (j = 58; j < jukebox.level[i]; j++)
					ui_plotbox(bitmap, x + j * 3, y + (i * 12), 2, 8, VOLUME_LIGHTRED);
			}
		}
	}
}

static int playlist_open(void)
{
	FILE *fp;
	char name[256];
	char setname[256];
	char tmpbuf[256], tmpnum[5] = { 0, 0, 0, 0, 0 };
	UINT16 tmpcode;
	int i, j, name_length;
	int sect, item;

	memset(&playlist, 0, sizeof(struct playlist_t) * MAX_PLAYLIST_SECT * MAX_PLAYLIST_ITEM);

	strcpy(setname, Machine->gamedrv->name);

loading:
	name_length = strlen(setname);

	sprintf(name, sound_filename);
	fp = fopen(name, "r");
	if (fp == NULL)
		return 0;

	jukebox.playlist_sect = 0;
	jukebox.playlist_format = 2;
	playlist[0][0].code = 0;
	item = 0;
	sect = 0;

	/* Ex.: kof98:0721:Opening */
	/* Ex.: kof98:sect:BGM */
	while (1)
	{
		if (fgets(tmpbuf, 255, fp) == NULL)
		{
			if( jukebox.playlist_format )
				playlist[0][0].code = item;
			break;
		}

		if (tmpbuf[name_length] != ':') continue;

		if (tmpbuf[0] == ';') continue;

		if (strncmp (tmpbuf, setname, name_length) != 0)
		{
			if (jukebox.playlist_format == 2)
			{
				continue;
			}
			else
			{
				if( jukebox.playlist_format )
					playlist[0][0].code = item;
				break;
			}
		}

		for ( i = 0 , j = name_length + 1 ; i <= 3 ; i++ , j++ )
		{
			tmpnum[i] = tmpbuf[j];
		}

		if (jukebox.playlist_format == 2)
		{
			if (!(strncmp(tmpnum, "secs", 3)) )
			{
				jukebox.playlist_format = 0;
			}
			else
			{
				jukebox.playlist_sect = 1;
				jukebox.playlist_format = 1;
				sect = 1;
			}
		}

		if (!(strncmp(tmpnum, "sect", 3)) )
		{
			if( jukebox.playlist_format )
				continue;

			if (sect > 0)
				playlist[0][sect].code = item;

			item = 0;

			if (tmpnum[3] == 'e')
			{
				playlist[0][0].code = sect;
				break;
			}

			i = 0;
			j = name_length + 6;

			while (tmpbuf[j] != '\n')
			{
				playlist[0][sect].desc[i] = tmpbuf[j];
				i++;
				j++;
			}

//			playlist[0][item].desc[++i] = '\n';
			playlist[0][0].code = sect + 1;
			sect++;

			if (sect >= MAX_PLAYLIST_SECT)
				break;

			continue;
		}

		if (!(strncmp(tmpnum, "name", 4)) )
		{
			i = 0;
			j = name_length + 6;
			while (tmpbuf[j] != '\n')
			{
				setname[i] = tmpbuf[j];
				i++;
				j++;
			}
			setname[i] = '\0';
			fclose(fp);
			goto loading;
		}

		tmpcode = atohex(tmpnum);

		if (tmpcode == 0 && item)
		{
			if( jukebox.playlist_format )
			{
				playlist[0][0].code = item;
				break;
			}
			else
			{
				continue;
			}
		}

		if (item >= MAX_PLAYLIST_ITEM)
		{
			if( jukebox.playlist_format )
			{
				playlist[0][0].code = item;
				break;
			}
			else
			{
				continue;
			}
		}

		if (tmpcode == 0xffff)	// notes code
		{
			i = 0;
			j = name_length + 6;
			while (tmpbuf[j] != '\n')
			{
				playlist[sect][item - 1].notes[i] = tmpbuf[j];
				i++;
				j++;
			}
		}
		else
		{
			playlist[sect][item].code = tmpcode;

			i = 0;
			j = name_length + 6;
			while (tmpbuf[j] != '\n')
			{
				playlist[sect][item].desc[i] = tmpbuf[j];
				i++;
				j++;
			}
//			playlist[sect][item].desc[++i] = '\n';
			item++;
		}
	}

	fclose(fp);

	return playlist[0][0].code;
}

static int remixlist_open(void)
{
	FILE *fp;
	char name[256];
	char setname[256];
	char tmpbuf[256], tmpnum[5] = { 0, 0, 0, 0, 0 };
	UINT16 tmpcode;
	int i, j, name_length;
	int sect = -1, item;

	const char command_name[REMIX_MAX][4] =
	{
		"stop",
		"play",
		"fdin",
		"fdot",
		"jump",
		"quit"
	};

	memset(&remixlist, 0, sizeof(struct remixlist_t) * MAX_REMIXLIST_SECT * MAX_REMIXLIST_ITEM);

	strcpy(setname, Machine->gamedrv->name);

//loading:
	name_length = strlen(setname);

	sprintf(name, remix_filename);
	fp = fopen(name, "r");
	if (fp == NULL)
		return 0;

	item = 0;

	/* Ex.: kof98:title:Remix List */
	/* Ex.: kof98:01200:play:0721 */
	while (1)
	{
		if (fgets(tmpbuf, 255, fp) == NULL) break;

		if (tmpbuf[name_length] != ':') continue;
		if (strncmp (tmpbuf, setname, name_length) != 0) continue;
		if (tmpbuf[0] == ';') continue;

		for ( i = 0 , j = name_length + 1 ; i <= 4 ; i++ , j++ )
		{
			tmpnum[i] = tmpbuf[j];
		}
		tmpnum[i] = '\0';

		if (strncmp(tmpnum, "title", 5) == 0 )
		{
			i = 0;
			j = name_length + 7;

			if (sect != -1)
			{
				remixlist[sect][0].items = item;
//				logerror("remixlist[%d][0].items = %d\n", sect, item);
			}
			sect++;
			item = 0;

			while (tmpbuf[j] != '\n')
			{
				remixlist[sect][0].title[i] = tmpbuf[j];
				i++;
				j++;
			}

			continue;
		}

		if (sect == -1)	continue;

		tmpcode = atoi(tmpnum);
		remixlist[sect][item].playframes = tmpcode;

//		logerror("remixlist[%d][%d].playframes = %d\n", sect, item, tmpcode);

		for ( i = 0 , j = name_length + 7 ; i <= 3 ; i++ , j++ )
		{
			tmpnum[i] = tmpbuf[j];
		}
		tmpnum[i] = '\0';

		tmpcode = REMIX_MAX;
		for ( i = 0 ; i < REMIX_MAX ; i++ )
		{
			if (strncmp(tmpnum, command_name[i], 4) == 0)
				tmpcode = i;
		}

		if (tmpcode == REMIX_MAX)
		{
			logerror("Not Exist Command %s\n", tmpnum);
			continue;
		}
		else
		{
			remixlist[sect][item].command = tmpcode;
//			logerror("remixlist[%d][%d].command = %d\n", sect, item, tmpcode);
		}

		i = 0;
		j = name_length + 12;
		while (tmpbuf[j] != '\n')
		{
			tmpnum[i] = tmpbuf[j];
			i++;
			j++;
		}
		tmpnum[i] = '\0';

		if (remixlist[sect][item].command == REMIX_PLAY)
			tmpcode = atohex(tmpnum);
		else
			tmpcode = atoi(tmpnum);

		remixlist[sect][item].code = tmpcode;
//		logerror("remixlist[%d][%d].code = %x\n", sect, item, tmpcode);

		item++;
	}

	fclose(fp);

	if (sect != -1)
	{
		remixlist[sect][0].items = item;
//		logerror("remixlist[%d][0].items = %d\n", sect, item);
	}

	return sect + 1;
}

/**************************************************************************/

static void jukebox_reset(int type)
{
	switch (type)
	{
		case JUKEBOX_NEOGEO:

			if (jukebox.playlist)
			{
				jukebox.code[0] = playlist[jukebox.playlist_sect][playlist_selected].code >> 8;
				jukebox.code[1] = playlist[jukebox.playlist_sect][playlist_selected].code & 0xff;
			}
			else
			{
				jukebox.code[0]   = jukebox.bank[0] >> 8;
				jukebox.code[1]   = jukebox.bank[0] & 0xff;
			}

			jukebox.que[0]    = jukebox.start;
			jukebox.que[1]    = jukebox.stop2;
			jukebox.que[2]    = jukebox.stop1;
			jukebox.counter   = 3;
			
			break;

		case JUKEBOX_CPS:

			if (jukebox.playlist)
			{
				jukebox.code[0] = playlist[jukebox.playlist_sect][playlist_selected].code;
			}
			else
			{
				jukebox.code[0] = jukebox.bank[0];
			}

			jukebox.que[0]    = jukebox.start;
			jukebox.que[1]    = jukebox.stop1;
			jukebox.counter   = 2;

			break;

		case JUKEBOX_QSOUND:

			if (jukebox.playlist)
			{
				jukebox.code[0] = playlist[jukebox.playlist_sect][playlist_selected].code;
			}
			else
			{
				jukebox.code[0] = jukebox.bank[0];
			}

			jukebox.que[0]     = 0xff;
			jukebox.address[0] = 0xfff;
			jukebox.que[1]     = 0x88;
			jukebox.address[1] = 0xffd;
			jukebox.que[2]     = 0xff;
			jukebox.address[2] = 0xffe;
			jukebox.que[3]     = 0x10;
			jukebox.address[3] = 0x007;
			jukebox.que[4]     = 0x00;
			jukebox.address[4] = 0x00f;
			jukebox.que[5]     = jukebox.stop1;
			jukebox.address[5] = 0x000;
			jukebox.que[6]     = jukebox.stop2;
			jukebox.address[6] = 0x001;
			jukebox.que[7]     = 0xff;
			jukebox.address[7] = 0x00f;
			jukebox.counter    = 8;

			break;
	}

	jukebox.menu_init = 0;

	jukebox.curr_bank = 0;
	jukebox.level[0]  = 0;
	jukebox.level[1]  = 0;
	jukebox.command   = 0;
	jukebox.status    = JUKE_STARTWAIT;
	line_volume[0]    = 0;
	line_volume[1]    = 0;
	line_counter[0]   = 0;
	line_counter[1]   = 0;
}

static void jukebox_start(int type)
{
	const struct GameDriver *drv;
	int i;

	srand((unsigned)time(NULL));

	in_jukebox = 1;
	volume_delay = 20;
	playing_no = 0;

	memset(&jukebox, 0, sizeof(struct jukebox_t));

	drv = Machine->gamedrv;
	do
	{
		playing_no = get_playingno(drv->name);
		drv = drv->clone_of;
	} while (!playing_no && drv);

	switch (bankcode[playing_no - 1].sound_type)
	{
		case SOUNDTYPE_SNK:
			jukebox.start = 0x07;
			jukebox.stop1 = 0x00;
			jukebox.stop2 = 0x03;
			break;

		case SOUNDTYPE_ADK:
		case SOUNDTYPE_DATAEAST:
		case SOUNDTYPE_TECHNOS:
		case SOUNDTYPE_TECMO:
			jukebox.start = 0x03;
			jukebox.stop1 = 0x00;
			jukebox.stop2 = 0x03;
			break;

		case SOUNDTYPE_NAZKA:
			jukebox.start = 0x00;
			jukebox.stop1 = 0x00;
			jukebox.stop2 = 0x03;
			break;

		case SOUNDTYPE_FACE:
			jukebox.start = 0x20;
			jukebox.stop1 = 0x00;
			jukebox.stop2 = 0x03;
			break;

		case SOUNDTYPE_CPS1:
			jukebox.start = 0xff00;
			jukebox.stop1 = 0x00f0;
			max_code = 0x0ff;
			break;

		case SOUNDTYPE_CPS1SP:
			jukebox.start = 0xff00;
			jukebox.stop1 = 0xf0ff;
			max_code = 0x1ff;
			break;

		case SOUNDTYPE_QSOUND:
			jukebox.stop1 = 0xff;
			jukebox.stop2 = 0x00;
			max_code = 0xfff;
			break;
	}

	memcpy(jukebox.bank, bankcode[playing_no - 1].sound_bank, 8 * sizeof(UINT16));
	for (i = 0; i < 8; i++)
	{
		if (!jukebox.bank[i])
			break;
	}
	jukebox.num_bank  = i;

	jukebox.playlist_bak = playlist_open();
	jukebox.playlist_sbak = jukebox.playlist_sect;
	jukebox.playlist = 0;

	memset(&remixlist, 0, sizeof(struct remixlist_t) * MAX_REMIXLIST_SECT * MAX_REMIXLIST_ITEM);
	jukebox.remixlist_bak = remixlist_open();
	jukebox.remixlist = 0;

	line_volume[0] = 0;
	line_volume[1] = 0;
	line_counter[0] = 0;
	line_counter[1] = 0;

	random_play = 0;
	playing_time = 0;
	playing_wait = 0;
	nosound_time = 0;
	randomplay_time = 0;
	fadeout_timing = 0;
	fadeout_frame = 600;
	fadeout_counter = 0;
	remixlist_command = 0;
	fadein_counter = 0;
	timer_view = 0;

	playlist_selected = 0;
	soundtest_selected = 0;
	remixlist_selected = 0;

	jukebox_reset(type);
}

static void jukebox_command(int type, int command)
{
	switch (type * 3 + command)
	{
		case 0:	// (NeoGeo-Play)
			jukebox.que[0]  = jukebox.code[1];
			jukebox.que[1]  = jukebox.code[0];
			jukebox.counter = 2;
			jukebox.status  = JUKE_PLAYING;
			break;

		case 1:	// (NeoGeo-Stop)
			jukebox.que[0]  = jukebox.start;
			jukebox.que[1]  = jukebox.stop2;
			jukebox.que[2]  = jukebox.stop1;
			jukebox.counter = 3;
			jukebox.status  = JUKE_IDLE;
			break;

		case 2:	// (NeoGeo-Record)
			jukebox.que[0]  = jukebox.code[1];
			jukebox.que[1]  = jukebox.code[0];
			jukebox.que[2]  = jukebox.start;
			jukebox.que[3]  = jukebox.stop2;
			jukebox.que[4]  = jukebox.stop1;
			jukebox.counter = 5;
			jukebox.status  = JUKE_RECORDWAIT;
			break;

		case 3:	// (CPS-Play)
			jukebox.que[0]  = jukebox.start;
			jukebox.que[1]  = jukebox.code[0];
			jukebox.counter = 2;
			jukebox.status  = JUKE_PLAYING;
			break;

		case 4:	// (CPS-Stop)
			jukebox.que[0]  = jukebox.start;
			jukebox.que[1]  = jukebox.stop1;
			jukebox.counter = 2;
			jukebox.status  = JUKE_IDLE;
			break;

		case 5:	// (CPS-Record)
			jukebox.que[0]  = jukebox.start;
			jukebox.que[1]  = jukebox.code[0];
			jukebox.que[2]  = jukebox.start;
			jukebox.que[3]  = jukebox.stop1;
			jukebox.counter = 4;
			jukebox.status  = JUKE_RECORDWAIT;
			break;

		case 6:	// (QSound-Play)
			jukebox.que[0]      = 0x00;
			jukebox.address[0]  = 0x00f;
			jukebox.que[1]      = jukebox.code[0] >> 8;
			jukebox.address[1]  = 0x000;
			jukebox.que[2]      = jukebox.code[0] & 0xff;
			jukebox.address[2]  = 0x001;
			jukebox.que[3]      = 0xff;
			jukebox.address[3]  = 0x00f;
			jukebox.counter = 4;
			jukebox.status  = JUKE_PLAYING;
			break;

		case 7:	// (QSound-Stop)
			jukebox.que[0]      = 0x00;
			jukebox.address[0]  = 0x00f;
			jukebox.que[1]      = jukebox.stop1;
			jukebox.address[1]  = 0x000;
			jukebox.que[2]      = jukebox.stop2;
			jukebox.address[2]  = 0x001;
			jukebox.que[3]      = 0xff;
			jukebox.address[3]  = 0x00f;
			jukebox.counter = 4;
			jukebox.status  = JUKE_IDLE;
			break;

		case 8:	// (QSound-Record)
			jukebox.que[0]      = 0x00;
			jukebox.address[0]  = 0x00f;
			jukebox.que[1]      = jukebox.code[0] >> 8;
			jukebox.address[1]  = 0x000;
			jukebox.que[2]      = jukebox.code[0] & 0xff;
			jukebox.address[2]  = 0x001;
			jukebox.que[3]      = 0xff;
			jukebox.address[3]  = 0x00f;
			jukebox.que[4]      = 0x00;
			jukebox.address[4]  = 0x00f;
			jukebox.que[5]      = jukebox.stop1;
			jukebox.address[5]  = 0x000;
			jukebox.que[6]      = jukebox.stop2;
			jukebox.address[6]  = 0x001;
			jukebox.que[7]      = 0xff;
			jukebox.address[7]  = 0x00f;
			jukebox.counter = 8;
			jukebox.status  = JUKE_RECORDWAIT;
			break;
	}
}

static void jukebox_playcommand(struct mame_bitmap *bitmap, int type)
{
	int i, sel, menu_type, total = 0, arrowise = 0;
	static const char *bank_name[8];
	static const char *command_name[8];
	static const char *menu_item[2][9];
	static const char *menu_subitem[10];
	char buf[2][4], buf2[80];

	if (jukebox.status == JUKE_STARTWAIT)
	{
		for (i = 0 ; i <= 7 ; i++)
		{
			bank_name[i] = ui_getstring(UI_bgm + i);
		}

		command_name[0] = ui_getstring(UI_jukeplay);
		command_name[1] = ui_getstring(UI_jukestop);
		command_name[2] = ui_getstring(UI_jukerecord);

		if (type == JUKEBOX_NEOGEO)
		{
			menu_item[0][0] = ui_getstring(UI_bank);
			menu_item[0][1] = ui_getstring(UI_code1);
			menu_item[0][2] = ui_getstring(UI_code2);
			menu_item[0][3] = ui_getstring(UI_jukecommand);
			menu_item[1][0] = ui_getstring(UI_jukestop);
			menu_item[0][4] = menu_item[0][5] = menu_item[1][1] = 0;	/* terminate array */
		}
		else
		{
			menu_item[0][0] = ui_getstring(UI_bank);
			menu_item[0][1] = ui_getstring(UI_code);
			menu_item[0][2] = ui_getstring(UI_jukecommand);
			menu_item[1][0] = ui_getstring(UI_jukestop);
			menu_item[0][3] = menu_item[0][4] = menu_item[1][1] = 0;	/* terminate array */
		}
	}

	for (i = 0; i < 10; i++)
		menu_subitem[i] = 0;

	if (jukebox.status == JUKE_RECORDING || jukebox.status == JUKE_RECORDWAIT)
		menu_type = 1;
	else
		menu_type = 0;

	if (!menu_type)
	{
		if (type == JUKEBOX_NEOGEO)
		{
			sprintf(buf[0], "%02X", jukebox.code[0]);
			sprintf(buf[1], "%02X", jukebox.code[1]);

			menu_subitem[0] = bank_name[jukebox.curr_bank];
			menu_subitem[1] = (const char *)buf[0];
			menu_subitem[2] = (const char *)buf[1];
			menu_subitem[3] = command_name[jukebox.command];

			sel = soundtest_selected;

			if (jukebox.playlist_bak)
			{
				menu_item[0][4] = ui_getstring(UI_gotoplaylist);
				menu_item[0][5] = ui_getstring(UI_gotorandomplay);

				if (jukebox.remixlist_bak)
				{
					menu_item[0][6] = ui_getstring(UI_remixplay);
					menu_item[0][7] = 0;
					total = 7;
				}
				else
				{
					menu_item[0][6] = 0;
					total = 6;
				}
			}
			else
			{
				if (jukebox.remixlist_bak)
				{
					menu_item[0][total++] = ui_getstring(UI_remixplay);
					menu_item[0][total] = 0;
					total = 5;
				}
				else
				{
					menu_item[0][4] = 0;
					total = 4;
				}
			}

			switch (sel)
			{
				case 0:
					if (jukebox.curr_bank > 0)
						arrowise |= 1;
					if (jukebox.curr_bank < jukebox.num_bank - 1)
						arrowise |= 2;
					break;

				case 1:
				case 2:
					arrowise = 3;
					break;

				case 3:
					if (jukebox.command > 0)
						arrowise |= 1;
					if (jukebox.command < 2)
						arrowise |= 2;
					break;
			}
		}
		else
		{
			if (max_code == 0x0ff)
				sprintf(buf[0], "%02X", jukebox.code[0]);
			else
				sprintf(buf[0], "%03X", jukebox.code[0]);

			menu_subitem[0] = bank_name[jukebox.curr_bank];
			menu_subitem[1] = (const char *)buf[0];
			menu_subitem[2] = command_name[jukebox.command];

			sel = soundtest_selected;

			if (jukebox.playlist_bak)
			{
				menu_item[0][3] = ui_getstring(UI_gotoplaylist);
				menu_item[0][4] = ui_getstring(UI_gotorandomplay);

				if (jukebox.remixlist_bak)
				{
					menu_item[0][5] = ui_getstring(UI_remixplay);
					menu_item[0][6] = 0;
					total = 6;
				}
				else
				{
					menu_item[0][5] = 0;
					total = 5;
				}
			}
			else
			{
				if (jukebox.remixlist_bak)
				{
					menu_item[0][3] = ui_getstring(UI_remixplay);
					menu_item[0][4] = 0;
					total = 4;
				}
				else
				{
					menu_item[0][3] = 0;
					total = 3;
				}
			}

			switch (sel)
			{
				case 0:
					if (jukebox.curr_bank > 0)
						arrowise |= 1;
					if (jukebox.curr_bank < jukebox.num_bank - 1)
						arrowise |= 2;
					break;

				case 1:
					arrowise = 3;
					break;

				case 2:
					if (jukebox.command > 0)
						arrowise |= 1;
					if (jukebox.command < 2)
						arrowise |= 2;
					break;
			}
		}
	}
	else
	{
		sel = 0;
		total = 2;
	}

	ui_displaymenu(bitmap, menu_item[menu_type], menu_subitem, 0, ui_getstring(UI_jukebox), sel, arrowise);

	volume_window(bitmap);

	if (!menu_type)
	{
		if (input_ui_pressed(IPT_UI_CANCEL) && jukebox.playlist_bak)
		{
			jukebox.playlist = jukebox.playlist_bak;
			jukebox.playlist_sect = jukebox.playlist_sbak;
			jukebox.playlist_bak = 0;
			jukebox.menu_init = 0;
			jukebox_reset(type);
			return;
		}

		if (input_ui_pressed(IPT_UI_SELECT))
		{
			if (type == JUKEBOX_NEOGEO)
			{
				switch (sel)
				{
					case 0: // bank
					case 1: // code1
					case 2: // code2
						jukebox_command(type, COMMAND_PLAY);
						break;

					case 3: // command
						jukebox.level[0] = 0;
						jukebox.level[1] = 0;
						line_volume[0] = 0;
						line_volume[1] = 0;
						line_counter[0] = 0;
						line_counter[1] = 0;
						jukebox_command(type, jukebox.command);
						break;

					case 4: // playlist or remixlist
						if (jukebox.playlist_bak)
						{
							jukebox.playlist = jukebox.playlist_bak;
							jukebox.playlist_sect = jukebox.playlist_sbak;
							jukebox.playlist_bak = 0;
							jukebox.menu_init = 0;
							jukebox_reset(type);
						}
						else
						{
							jukebox.remixlist = jukebox.remixlist_bak;
							jukebox.remixlist_bak = 0;
							jukebox.menu_init = 0;
							jukebox_reset(type);
						}
						break;

					case 5: // random play
						if (jukebox.playlist_format)
							jukebox.playlist = jukebox.playlist_bak;
						else
							jukebox.playlist = playlist[0][1].code;
						jukebox.playlist_sect = 1;
						jukebox.playlist_bak = 0;
						jukebox.menu_init = 0;
						random_play = 1;
						jukebox_reset(type);
						break;

					case 6: // remixlist
						jukebox.remixlist = jukebox.remixlist_bak;
						jukebox.remixlist_bak = 0;
						jukebox.menu_init = 0;
						jukebox_reset(type);
						break;
				}
			}
			else
			{
				switch (sel)
				{
					case 0: // bank
					case 1: // code
						jukebox_command(type, COMMAND_PLAY);
						break;

					case 2: // command
						jukebox.level[0] = 0;
						jukebox.level[1] = 0;
						line_volume[0] = 0;
						line_volume[1] = 0;
						line_counter[0] = 0;
						line_counter[1] = 0;
						jukebox_command(type, jukebox.command);
						break;

					case 3: // playlist or remixlist
						if (jukebox.playlist_bak)
						{
							jukebox.playlist = jukebox.playlist_bak;
							jukebox.playlist_sect = jukebox.playlist_sbak;
							jukebox.playlist_bak = 0;
							jukebox.menu_init = 0;
							jukebox_reset(type);
						}
						else
						{
							jukebox.remixlist = jukebox.remixlist_bak;
							jukebox.remixlist_bak = 0;
							jukebox.menu_init = 0;
							jukebox_reset(type);
						}
						break;

					case 4: // random play
						if (jukebox.playlist_format)
							jukebox.playlist = jukebox.playlist_bak;
						else
							jukebox.playlist = playlist[0][1].code;
						jukebox.playlist_sect = 1;
						jukebox.playlist_bak = 0;
						jukebox.menu_init = 0;
						random_play = 1;
						jukebox_reset(type);
						break;

					case 5: // remixlist
						jukebox.remixlist = jukebox.remixlist_bak;
						jukebox.remixlist_bak = 0;
						jukebox.menu_init = 0;
						jukebox_reset(type);
						break;
				}
			}
		}

		if (input_ui_pressed_repeat(IPT_UI_LEFT, 4))
		{
			if (type == JUKEBOX_NEOGEO)
			{
				switch (sel)
				{
					case 0:
						if (jukebox.curr_bank > 0)
						{
							jukebox.curr_bank--;
							jukebox.code[0] = jukebox.bank[jukebox.curr_bank] >> 8;
							jukebox.code[1] = jukebox.bank[jukebox.curr_bank] & 0xff;
						}
						break;

					case 1:
					case 2:
						jukebox.code[sel - 1]--;
						jukebox.code[sel - 1] &= 0xff;
						break;

					case 3:
						if (jukebox.command > 0)
							jukebox.command--;
						break;
				}
			}
			else
			{
				switch (sel)
				{
					case 0:
						if (jukebox.curr_bank > 0)
						{
							jukebox.curr_bank--;
							jukebox.code[0] = jukebox.bank[jukebox.curr_bank];
						}
						break;

					case 1:
						jukebox.code[0]--;
						jukebox.code[0] &= max_code;
						break;

					case 2:
						if (jukebox.command > 0)
							jukebox.command--;
						break;
				}
			}
		}

		if (input_ui_pressed_repeat(IPT_UI_RIGHT, 4))
		{
			if (type == JUKEBOX_NEOGEO)
			{
				switch (sel)
				{
					case 0:
						if (jukebox.curr_bank < jukebox.num_bank - 1)
						{
							jukebox.curr_bank++;
							jukebox.code[0] = jukebox.bank[jukebox.curr_bank] >> 8;
							jukebox.code[1] = jukebox.bank[jukebox.curr_bank] & 0xff;
						}
						break;

					case 1:
					case 2:
						jukebox.code[sel - 1]++;
						jukebox.code[sel - 1] &= 0xff;
						break;

					case 3:
						if (jukebox.command < 2)
							jukebox.command++;
						break;
				}
			}
			else
			{
				switch (sel)
				{
					case 0:
						if (jukebox.curr_bank < jukebox.num_bank - 1)
						{
							jukebox.curr_bank++;
							jukebox.code[0] = jukebox.bank[jukebox.curr_bank];
						}
						break;

					case 1:
						jukebox.code[0]++;
						jukebox.code[0] &= max_code;
						break;

					case 2:
						if (jukebox.command < 2)
							jukebox.command++;
						break;
				}
			}
		}

		if (code_pressed_memory_repeat(KEYCODE_PGDN, 4))
		{
			if (type == JUKEBOX_NEOGEO)
			{
				if (sel == 1 || sel == 2)
				{
					jukebox.code[sel - 1] -= 0x10;
					jukebox.code[sel - 1] &= 0xff;
				}
			}
			else
			{
				if (sel == 1)
				{
					if (max_code > 0x0ff && (code_pressed(KEYCODE_LSHIFT) || code_pressed(KEYCODE_RSHIFT)))
						jukebox.code[0] -= 0x100;
					else
						jukebox.code[0] -= 0x10;

					jukebox.code[0] &= max_code;
				}
			}
		}

		if (code_pressed_memory_repeat(KEYCODE_PGUP, 4))
		{
			if (type == JUKEBOX_NEOGEO)
			{
				if (sel == 1 || sel == 2)
				{
					jukebox.code[sel - 1] += 0x10;
					jukebox.code[sel - 1] &= 0xff;
				}
			}
			else
			{
				if (sel == 1)
				{
					if (max_code > 0x0ff && (code_pressed(KEYCODE_LSHIFT) || code_pressed(KEYCODE_RSHIFT)))
						jukebox.code[0] += 0x100;
					else
						jukebox.code[0] += 0x10;

					jukebox.code[0] &= max_code;
				}
			}
		}

		if (input_ui_pressed_repeat(IPT_UI_UP, 8))
		{
			if (sel > -1) sel--;
			if (sel == -1) sel = total - 1;
		}

		if (input_ui_pressed_repeat(IPT_UI_DOWN, 8))
		{
			if (sel < total) sel++;
			if (sel == total) sel = 0;
		}

		if (code_pressed_memory(KEYCODE_S))
		{
			if (jukebox.status == JUKE_RECORDING)
			{
				osd_stop_log_wave();
				wave_recording = 0;
			}

			jukebox_command(type, COMMAND_STOP);
		}

		if (code_pressed_memory(KEYCODE_R))
		{
			jukebox_command(type, COMMAND_RECORD);
		}

		if (code_pressed(KEYCODE_SPACE))
		{
			sprintf(buf2, "%s  %s\n%s",
				ui_getstring(UI_jukestop_exp),
				ui_getstring(UI_jukerecord_exp),
				ui_getstring(UI_jukefastforward_exp));
			ui_displaymessagewindow(bitmap, buf2, 0);
		}
	}
	else
	{
		if (input_ui_pressed(IPT_UI_SELECT) || input_ui_pressed(IPT_UI_CANCEL) || code_pressed_memory(KEYCODE_S))
		{
			if (jukebox.status == JUKE_RECORDING)
			{
				osd_stop_log_wave();
				wave_recording = 0;
			}

			jukebox_command(type, COMMAND_STOP);
		}

		if (code_pressed(KEYCODE_SPACE))
		{
			sprintf(buf2, "%s  %s", ui_getstring(UI_jukestop_exp), ui_getstring(UI_jukefastforward_exp));
			displaymessage(bitmap, buf2, 0);
		}
	}

	soundtest_selected = sel;
}

static void jukebox_playlist(struct mame_bitmap *bitmap, int type)
{
	static const char *menu_item[MAX_PLAYLIST_ITEM + 3];
	static char flag[MAX_PLAYLIST_ITEM + 3];
	static int total;
	static int notes = 0;
	int i, sel, visible;
	char buf[256];
	const char *title;

	sel = playlist_selected;
	visible = (Machine->uiheight / ROWHEIGHT) - 6;

	if (jukebox.menu_init == 0)
	{
		for (i = 0 ; i < MAX_PLAYLIST_ITEM + 3 ; i++)
		{
			menu_item[i] = 0;
			flag[i] = 0;
		}

		for (total = 0; total < jukebox.playlist; total++)
		{
			menu_item[total] = playlist[jukebox.playlist_sect][total].desc;
			if (strlen(playlist[jukebox.playlist_sect][total].notes))
				flag[total] = 1;
		}

		if (random_play == 0)
		{
			if (jukebox.playlist_sect && !jukebox.playlist_format)
				menu_item[total] = ui_getstring(UI_returntoprior);
			else
				menu_item[total] = ui_getstring(UI_gotomanualcode);
		}
		else
		{
			playing_time = 0;
			playing_wait = 0;
			nosound_time = 0;
			randomplay_time = random_time * 60;
			for (i = 0 ; i < jukebox.playlist ; i++)
				playlist[1][i].flag = 0;
			if (randomplay_time < 6000)
				fadeout_timing = randomplay_time * 4 / 5;
			else
				fadeout_timing = randomplay_time - 1200;
		}

		menu_item[total + 1] = 0;	/* terminate array */
		total++;
		jukebox.menu_init = 1;
		fadeout_counter = 0;
	}

	if (random_play)
		title = ui_getstring(UI_randomplay);
	else if (jukebox.playlist_format || jukebox.playlist_sect == 0)
		title = ui_getstring(UI_soundlist);
	else
		title = playlist[0][jukebox.playlist_sect - 1].desc;

	ui_displaymenu_jukebox(bitmap, menu_item, flag, title, sel, notes);

	volume_window(bitmap);

	if (random_play)
	{
		if (playing_time == 0)
		{
			int play_flag = 0;

			for (i = 0; i < jukebox.playlist; i++)
			{
				if (playlist[1][i].flag == 0)
					play_flag = 1;
			}

			if (play_flag == 0)
			{
				for (i = 0; i < jukebox.playlist; i++)
				{
					playlist[1][i].flag = 0;
				}
			}

			while (1)
			{
				sel = rand();
				if (sel < jukebox.playlist && playlist[1][sel].flag == 0)
					break;
			}

			playlist_selected = sel;
			playlist[1][sel].flag = 1;

			jukebox.level[0] = 0;
			jukebox.level[1] = 0;
			line_volume[0] = 0;
			line_volume[1] = 0;
			line_counter[0] = 0;
			line_counter[1] = 0;

			if (type == JUKEBOX_NEOGEO)
			{
				jukebox.code[0] = playlist[1][sel].code >> 8;
				jukebox.code[1] = playlist[1][sel].code & 0xff;
			}
			else
			{
				jukebox.code[0] = playlist[1][sel].code;
			}

			jukebox_command(type, COMMAND_PLAY);
		}

		playing_time++;

		if (fadeout_counter)
			fadeout_counter++;

		if (playing_time == fadeout_timing)
		{
			fadeout_counter = 1;
			fadeout_frame = randomplay_time - fadeout_timing;
		}

		if (playing_time >= randomplay_time)
		{
			if (playing_wait == 0)
			{
				jukebox_command(type, COMMAND_STOP);
			}

			if (playing_wait == 300)
			{
				playing_time = 0;
				playing_wait = 0;
			}
			else
			{
				playing_wait++;
			}

			nosound_time = 0;
		}

		if (jukebox.level[0] == 0 && jukebox.level[1] == 0)
		{
			nosound_time++;

			if (nosound_time == 300)
			{
				jukebox_command(type, COMMAND_STOP);
				playing_time = 0;
				playing_wait = 0;
				nosound_time = 0;
			}
		}
		else
		{
			nosound_time = 0;
		}
		
		if (code_pressed_memory(KEYCODE_S) || input_ui_pressed(IPT_UI_CANCEL))
		{
			jukebox.level[0] = 0;
			jukebox.level[1] = 0;
			line_volume[0] = 0;
			line_volume[1] = 0;
			line_counter[0] = 0;
			line_counter[1] = 0;

			sel = 0;
			jukebox.playlist_bak = jukebox.playlist;
			jukebox.playlist_sbak = jukebox.playlist_sect;
			jukebox.playlist = 0;
			playing_time = 0;
			playing_wait = 0;
			nosound_time = 0;
			random_play = 0;
			jukebox_reset(type);
			return;
		}

		if (code_pressed_memory(KEYCODE_X))
		{
			jukebox_command(type, COMMAND_STOP);
			playing_time = 0;
			playing_wait = 0;
			nosound_time = 0;
		}

		if (code_pressed_memory(KEYCODE_N))
			notes ^= 1;

		if (notes)
		{
			if (sel < total - 1 && jukebox.playlist_sect)
			{
				if (strlen(playlist[jukebox.playlist_sect][sel].notes))
				{
					sprintf(buf, "%s", playlist[jukebox.playlist_sect][sel].notes);
					displaymessage(bitmap, buf, 0);
				}
			}
		}

		if (code_pressed(KEYCODE_SPACE))
		{
			sprintf(buf, "%s  %s\n%s  %s\n%s",
				ui_getstring(UI_jukerandomstop_exp),
				ui_getstring(UI_jukenext_exp),
				ui_getstring(UI_jukefastforward_exp),
				ui_getstring(UI_jukecode_exp),
				ui_getstring(UI_jukenotes_exp));
			ui_displaymessagewindow(bitmap, buf, 0);
		}

		if (code_pressed(KEYCODE_C))
		{
			if (type == JUKEBOX_NEOGEO)
			{
				sprintf(buf, "%s : %02X  %s : %02X",
					ui_getstring(UI_code1),
					playlist[1][playlist_selected].code >> 8,
					ui_getstring(UI_code2),
					playlist[1][playlist_selected].code & 0xff);
			}
			else if (max_code == 0x0ff)
			{
				sprintf(buf, "%s : %02X",
					ui_getstring(UI_code),
					playlist[1][playlist_selected].code);
			}
			else
			{
				sprintf(buf, "%s : %03X",
					ui_getstring(UI_code),
					playlist[1][playlist_selected].code);
			}

			displaymessage(bitmap, buf, 0);
		}
	}
	else
	{
		if (input_ui_pressed(IPT_UI_CANCEL))
		{
			if (!jukebox.playlist_sect || jukebox.playlist_format)
			{
				sel = 0;
				jukebox.playlist_bak = jukebox.playlist;
				jukebox.playlist_sbak = jukebox.playlist_sect;
				jukebox.playlist = 0;
			}
			else
			{
				jukebox.playlist = playlist[0][0].code;
				playlist_selected = jukebox.playlist_sect - 1;
				jukebox.playlist_sect = 0;
			}

			jukebox_reset(type);
			return;
		}

		if (code_pressed_memory(KEYCODE_N))
			notes ^= 1;

		if (notes)
		{
			if (sel < total - 1 && jukebox.playlist_sect)
			{
				if (strlen(playlist[jukebox.playlist_sect][sel].notes))
				{
					sprintf(buf, "%s", playlist[jukebox.playlist_sect][sel].notes);
					displaymessage(bitmap, buf, 0);
				}
			}
		}

		if (code_pressed_memory(KEYCODE_S))
		{
			if (jukebox.status == JUKE_RECORDING)
			{
				osd_stop_log_wave();
				wave_recording = 0;
			}

			jukebox_command(type, COMMAND_STOP);
		}

		if (jukebox.status != JUKE_RECORDING && jukebox.status != JUKE_RECORDWAIT)
		{
			if (code_pressed(KEYCODE_SPACE))
			{
				if (jukebox.playlist_sect)
				{
					sprintf(buf, "%s  %s\n%s  %s\n%s",
						ui_getstring(UI_jukestop_exp),
						ui_getstring(UI_jukerecord_exp),
						ui_getstring(UI_jukefastforward_exp),
						ui_getstring(UI_jukecode_exp),
						ui_getstring(UI_jukenotes_exp));
					ui_displaymessagewindow(bitmap, buf, 0);
				}
			}

			if (code_pressed_memory(KEYCODE_R))
			{
				if (sel < total - 1 && jukebox.playlist_sect)
				{
					jukebox_command(type, COMMAND_RECORD);
				}
			}

			if (code_pressed(KEYCODE_C))
			{
				if (sel < total - 1 && jukebox.playlist_sect)
				{
					if (type == JUKEBOX_NEOGEO)
					{
						sprintf(buf, "%s : %02X  %s : %02X",
							ui_getstring(UI_code1),
							playlist[jukebox.playlist_sect][sel].code >> 8,
							ui_getstring(UI_code2),
							playlist[jukebox.playlist_sect][sel].code & 0xff);
					}
					else if (max_code == 0x0ff)
					{
						sprintf(buf, "%s : %02X",
							ui_getstring(UI_code),
							playlist[jukebox.playlist_sect][sel].code);
					}
					else
					{
						sprintf(buf, "%s : %03X",
							ui_getstring(UI_code),
							playlist[jukebox.playlist_sect][sel].code);
					}

					displaymessage(bitmap, buf, 0);
				}
			}

			if (input_ui_pressed(IPT_UI_SELECT))
			{
				jukebox.level[0] = 0;
				jukebox.level[1] = 0;
				line_volume[0] = 0;
				line_volume[1] = 0;
				line_counter[0] = 0;
				line_counter[1] = 0;

				if (sel == (total - 1))
				{
					if (!jukebox.playlist_sect || jukebox.playlist_format)
					{
						sel = 0;
						jukebox.playlist_bak = jukebox.playlist;
						jukebox.playlist_sbak = jukebox.playlist_sect;
						jukebox.playlist = 0;
					}
					else
					{
						jukebox.playlist = playlist[0][0].code;
						playlist_selected = jukebox.playlist_sect - 1;
						jukebox.playlist_sect = 0;
					}

					jukebox_reset(type);
					return;
				}
				else
				{
					if (jukebox.playlist_sect == 0)
					{
						jukebox.playlist_sect = sel + 1;
						jukebox.playlist = playlist[0][sel + 1].code;
						playlist_selected = 0;
						jukebox_reset(type);
						return;
					}
					else
					{
						jukebox_command(type, COMMAND_PLAY);
					}
				}
			}
		}
		else
		{
			sprintf(buf, "%s  %s",
				ui_getstring(UI_jukestop_exp),
				ui_getstring(UI_jukefastforward_exp));
			displaymessage(bitmap, buf, 0);
		}

		if (input_ui_pressed_repeat(IPT_UI_DOWN, 8))
		{
			sel = (sel + 1) % total;

			if (type == JUKEBOX_NEOGEO)
			{
				jukebox.code[0] = playlist[jukebox.playlist_sect][sel].code >> 8;
				jukebox.code[1] = playlist[jukebox.playlist_sect][sel].code & 0xff;
			}
			else
			{
				jukebox.code[0] = playlist[jukebox.playlist_sect][sel].code;
			}
		}

		if (input_ui_pressed_repeat(IPT_UI_UP, 8))
		{
			sel = (sel + total - 1) % total;

			if (type == JUKEBOX_NEOGEO)
			{
				jukebox.code[0] = playlist[jukebox.playlist_sect][sel].code >> 8;
				jukebox.code[1] = playlist[jukebox.playlist_sect][sel].code & 0xff;
			}
			else
			{
				jukebox.code[0] = playlist[jukebox.playlist_sect][sel].code;
			}
		}

		if (input_ui_pressed_repeat(IPT_UI_LEFT, 8))
		{
			sel -= (visible - 1);

			if (sel < 0)
				sel = 0;

			if (type == JUKEBOX_NEOGEO)
			{
				jukebox.code[0] = playlist[jukebox.playlist_sect][sel].code >> 8;
				jukebox.code[1] = playlist[jukebox.playlist_sect][sel].code & 0xff;
			}
			else
			{
				jukebox.code[0] = playlist[jukebox.playlist_sect][sel].code;
			}
		}

		if (input_ui_pressed_repeat(IPT_UI_RIGHT, 8))
		{
			sel += (visible - 1);

			if (sel >= total)
				sel = total - 1;

			if (type == JUKEBOX_NEOGEO)
			{
				jukebox.code[0] = playlist[jukebox.playlist_sect][sel].code >> 8;
				jukebox.code[1] = playlist[jukebox.playlist_sect][sel].code & 0xff;
			}
			else
			{
				jukebox.code[0] = playlist[jukebox.playlist_sect][sel].code;
			}
		}
	}

	playlist_selected = sel;
}

static void jukebox_remixlist(struct mame_bitmap *bitmap, int type)
{
	static const char *menu_item[MAX_REMIXLIST_ITEM + 3];
	static int total;
	int i, sel, visible;
	char buf[256];
//	const char *title;

	sel = remixlist_selected;
	visible = (Machine->uiheight / ROWHEIGHT) - 6;

	if (jukebox.menu_init == 0)
	{
		for (i = 0 ; i < MAX_REMIXLIST_ITEM + 3 ; i++)
		{
			menu_item[i] = 0;
		}

		for (total = 0; total < jukebox.remixlist; total++)
		{
			menu_item[total] = remixlist[total][0].title;
		}

		menu_item[total] = ui_getstring(UI_gotomanualcode);

		menu_item[total + 1] = 0;	/* terminate array */
		total++;

		remix_play = -1;
		playing_time = 0;
		remixlist_command = 0;
		jukebox.menu_init = 1;
	}

	volume_window(bitmap);

	if (remix_play == -1)	// Menu
	{
		ui_displaymenu_jukebox(bitmap, menu_item, 0, ui_getstring(UI_remixplay), sel, 0);

		if (input_ui_pressed(IPT_UI_SELECT))
		{
			jukebox.level[0] = 0;
			jukebox.level[1] = 0;
			line_volume[0] = 0;
			line_volume[1] = 0;
			line_counter[0] = 0;
			line_counter[1] = 0;

			if (sel == (total - 1))
			{
				sel = 0;
				remix_play = -1;
				jukebox.remixlist_bak = jukebox.remixlist;
				jukebox.remixlist = 0;
				playing_time = 0;
				remixlist_command = 0;
				fadein_counter = 0;
				jukebox_reset(type);
				return;
			}
			else
			{
				remix_play = sel;
			}
		}

		if (input_ui_pressed_repeat(IPT_UI_DOWN, 8))
		{
			sel = (sel + 1) % total;
		}

		if (input_ui_pressed_repeat(IPT_UI_UP, 8))
		{
			sel = (sel + total - 1) % total;
		}
	}
	else					// Play
	{
		if (fadein_counter && fadein_counter < fadeout_frame)
		{
			fadein_counter++;
			fadeout_counter = fadeout_frame - fadein_counter;
		}
		else if (fadeout_counter && fadeout_counter < fadeout_frame)
			fadeout_counter++;

		if (playing_time == remixlist[remix_play][remixlist_command].playframes)
		{
			switch (remixlist[remix_play][remixlist_command].command)
			{
				case REMIX_STOP:
					jukebox_command(type, COMMAND_STOP);
					break;

				case REMIX_PLAY:
					jukebox.level[0] = 0;
					jukebox.level[1] = 0;
					line_volume[0] = 0;
					line_volume[1] = 0;
					line_counter[0] = 0;
					line_counter[1] = 0;

					if (type == JUKEBOX_NEOGEO)
					{
						jukebox.code[0] = remixlist[remix_play][remixlist_command].code >> 8;
						jukebox.code[1] = remixlist[remix_play][remixlist_command].code & 0xff;
					}
					else
					{
						jukebox.code[0] = remixlist[remix_play][remixlist_command].code;
					}

					jukebox_command(type, COMMAND_PLAY);
					break;

				case REMIX_FADEIN:
					fadeout_counter = 0;
					fadein_counter = 1;
					fadeout_frame = remixlist[remix_play][remixlist_command].code;
					break;

				case REMIX_FADEOUT:
					fadeout_counter = 1;
					fadein_counter = 0;
					fadeout_frame = remixlist[remix_play][remixlist_command].code;
					break;

				case REMIX_JUMP:
					remixlist_command = remixlist[remix_play][remixlist_command].code;
					playing_time = remixlist[remix_play][remixlist_command].playframes;
					remixlist_command--;
					playing_time--;
					break;

				case REMIX_END:
					jukebox.level[0] = 0;
					jukebox.level[1] = 0;
					line_volume[0] = 0;
					line_volume[1] = 0;
					line_counter[0] = 0;
					line_counter[1] = 0;

					remix_play = -1;
					playing_time = 0;
					remixlist_command = 0;
					fadein_counter = 0;
					jukebox_reset(type);
					return;
			}

			remixlist_command++;
		}

		playing_time++;

		if (code_pressed(KEYCODE_SPACE))
		{
			sprintf(buf, "%s  %s\n%s  %s",
				ui_getstring(UI_remixback_exp),
				ui_getstring(UI_remixnext_exp),
				ui_getstring(UI_remixstop_exp),
				ui_getstring(UI_remixtimer_exp));
			ui_displaymessagewindow(bitmap, buf, 0);
		}

		if (input_ui_pressed(IPT_UI_CANCEL))
		{
			jukebox.level[0] = 0;
			jukebox.level[1] = 0;
			line_volume[0] = 0;
			line_volume[1] = 0;
			line_counter[0] = 0;
			line_counter[1] = 0;

			sel = 0;
			remix_play = -1;
			jukebox.remixlist_bak = jukebox.remixlist;
			jukebox.remixlist = 0;
			playing_time = 0;
			remixlist_command = 0;
			fadein_counter = 0;
			jukebox_reset(type);
			return;
		}

		if (code_pressed_memory(KEYCODE_S))
		{
			jukebox.level[0] = 0;
			jukebox.level[1] = 0;
			line_volume[0] = 0;
			line_volume[1] = 0;
			line_counter[0] = 0;
			line_counter[1] = 0;

			remix_play = -1;
			playing_time = 0;
			remixlist_command = 0;
			fadein_counter = 0;
			jukebox_reset(type);
			return;
		}

		if (code_pressed_memory(KEYCODE_T))
		{
			timer_view++;
			if (timer_view == 3)	timer_view = 0;
		}

		if (timer_view)
		{
			int width;
			int minutes, seconds;

			if (timer_view == 1)	// "ss:dd"
			{
				minutes = playing_time / 3600;
				seconds = (playing_time - minutes * 3600) / 60;

				sprintf(buf, "%02d:%02d", minutes, seconds);
			}
			else					// "fffff"
			{
				sprintf(buf, "%05d", playing_time);
			}

			width = strlen(buf) * Machine->uifontwidth;
			ui_text_bk(bitmap, buf, Machine->uiwidth - width, Machine->uiheight - Machine->uifontheight);
		}

		if (code_pressed(KEYCODE_LSHIFT) || code_pressed(KEYCODE_RSHIFT))
		{
			if (input_ui_pressed(IPT_UI_UP) && remixlist_command > 2)
			{
				remixlist_command -= 3;
				playing_time = remixlist[remix_play][remixlist_command].playframes;
			}

			if (input_ui_pressed(IPT_UI_DOWN) && remixlist_command < remixlist[remix_play][0].items - 2)
			{
				remixlist_command++;
				playing_time = remixlist[remix_play][remixlist_command].playframes;
			}
		}
		else
		{
			if (input_ui_pressed(IPT_UI_UP) && remixlist_command > 1)
			{
				remixlist_command -= 2;
				playing_time = remixlist[remix_play][remixlist_command].playframes;
			}

			if (input_ui_pressed(IPT_UI_DOWN) && remixlist_command < remixlist[remix_play][0].items - 1)
			{
				playing_time = remixlist[remix_play][remixlist_command].playframes;
			}
		}
	}

	remixlist_selected = sel;
}


/**************************************************************************/

void jukebox_play(struct mame_bitmap *bitmap, int type)
{
	if (in_jukebox)
	{
		if (jukebox.counter)
		{
			jukebox.counter--;
			
			switch (type)
			{
				case JUKEBOX_NEOGEO:
					neojuke_send_message(jukebox.que[jukebox.counter]);
					break;

				case JUKEBOX_CPS:
					cpsjuke_send_message(jukebox.que[jukebox.counter]);
					break;

				case JUKEBOX_QSOUND:
					qsoundjuke_send_message(jukebox.address[jukebox.counter], jukebox.que[jukebox.counter]);
					break;
			}

			if (jukebox.counter == 0)
			{
				int plist_tmp, plistbak_tmp;
				int psect_tmp, psectbak_tmp;

				if (!jukebox.remixlist)
					fadeout_counter = 0;

				switch (jukebox.status)
				{
					case JUKE_RECORDWAIT:
						jukebox.status = JUKE_RECORDING;
						osd_start_log_wave();
						wave_recording = 1;
						wave_counter   = 0;
						wave_seconds   = 0;
						wave_minutes   = 0;
						break;

					case JUKE_STOPWAIT:
						plist_tmp = jukebox.playlist;
						plistbak_tmp = jukebox.playlist_bak;
						psect_tmp = jukebox.playlist_sect;
						psectbak_tmp = jukebox.playlist_sbak;
						memset(&jukebox, 0, sizeof(struct jukebox_t));
						jukebox.playlist = plist_tmp;
						jukebox.playlist_bak = plistbak_tmp;
						jukebox.playlist_sect = psect_tmp;
						jukebox.playlist_sbak = psectbak_tmp;
						jukebox.status = JUKE_STOP;
						break;

					case JUKE_STARTWAIT:
						jukebox.status = JUKE_IDLE;
						break;
				}
			}
		}

		if (jukebox.playlist)
		{
			jukebox_playlist(bitmap, type);
		}
		else if (jukebox.remixlist)
		{
			jukebox_remixlist(bitmap, type);
		}
		else
		{
			jukebox_playcommand(bitmap, type);
		}
	}
	else
	{
		jukebox_start(type);
	}
}
