/* * NotMerge * * Some portions of this program are licensed under the GNU General Public * License. The rest is all public domain. * * m1.cpp: Controls the M1 music player library * */#include <stdio.h>#include "m1.h"CM1Library m1lib;// message parserchar runningVersion[25];char root[256];int isBooting;int newSong=0;int isError=0;// This needs to be stdcall, otherwise newer versions crashint m1ui_message(void *ourThis, int message, char *txt, int iparm) { int curgame; char buf4[200];switch (message) { // called when switching to a new game case M1_MSG_SWITCHING: break; // called to show the current game's name case M1_MSG_GAMENAME: break; // called to show the driver's name case M1_MSG_DRIVERNAME: //if(dlog) fprintf(fil,"Driver: %s\n", txt); break; case M1_MSG_ERROR: MessageBox(NULL,txt,"Error",MB_OK); isError=1; break; // called to show the hardware description case M1_MSG_HARDWAREDESC: break; case M1_MSG_BOOTING: //MessageBox(NULL,"Now booting.","meh",MB_OK); isBooting=1; break; case M1_MSG_BOOTFINISHED: //MessageBox(NULL,"Boot successful.","meh",MB_OK); isBooting=0; break; // called when ROM loading fails for a game case M1_MSG_ROMLOADERR: MessageBox(NULL,"Error loading ROM.","oops",MB_OK); isError=1; break; case M1_MSG_MATCHPATH: //sprintf(buf4, "Size=%d\nPath=%s", strlen(txt), fn); strcpy(buf4,txt); // for newer versions (0.78+), we send the ROM directory name back. otherwise, we send the rom name into the buffer if (strstr(runningVersion,"0.7.8")) { m1lib.m1snd_set_info_str(M1_SINF_SET_ROMPATH,root,0,0,0); } else sprintf(txt,"%s\\%s",root,buf4); //MessageBox(NULL,txt,"dfsfs",0); return 1; break; case M1_MSG_STARTINGSONG: newSong=1; break; default: break; } return 0;}// class CM1Libraryint CM1Library::Create(char * dll, int samplerate) { lib=LoadLibrary(dll); if (!lib) return 1; m1snd_init = (void (*)(void *, int (*m1ui_message)(void *,int, char *, int))) GetProcAddress(lib,"m1snd_init"); m1snd_run = (int (*)(int command, int iparm)) GetProcAddress(lib,"m1snd_run"); m1snd_shutdown = (void (*)(void)) GetProcAddress(lib,"m1snd_shutdown"); m1snd_setoption = (void (*)(int option, int value)) GetProcAddress(lib,"m1snd_setoption"); m1snd_get_info_int = (int (*)(int iinfo, int parm)) GetProcAddress(lib,"m1snd_get_info_int"); m1snd_get_info_str = (char* (*)(int sinfo, int parm)) GetProcAddress(lib,"m1snd_get_info_str"); m1snd_get_info_str_ex = (char* (*)(int sinfo, int parm1, int parm2, int parm3)) GetProcAddress(lib,"m1snd_get_info_str_ex"); m1snd_set_info_str = (void (*)(int sinfo, char *info, int parm1, int parm2, int parm3)) GetProcAddress(lib,"m1snd_set_info_str"); m1snd_do_frame = (void (*)(unsigned long dwSamples, unsigned char *out)) GetProcAddress(lib,"m1snd_do_frame"); m1snd_setoption(M1_OPT_INTERNALSND, 1); m1snd_setoption(M1_OPT_RETRIGGER, 0); m1snd_setoption(M1_OPT_WAVELOG, 0); m1snd_setoption(M1_OPT_NORMALIZE, 1); m1snd_setoption(M1_OPT_LANGUAGE, M1_LANG_EN); m1snd_setoption(M1_OPT_RESETNORMALIZE, 1); m1snd_setoption(M1_OPT_SAMPLERATE, samplerate); m1snd_setoption(M1_OPT_STEREOMIX, 50); m1lib.m1snd_setoption(M1_OPT_SAMPLES_PER_FRAME, 576); m1snd_init(NULL, m1ui_message); // before initializing, what is the version of M1 we're running? strcpy(runningVersion,m1lib.m1snd_get_info_str(M1_SINF_COREVERSION,0)); return 0;}int CM1Library::Run(unsigned long length, unsigned char * buf) { m1snd_setoption(M1_OPT_SAMPLES_PER_FRAME, length/4); m1snd_do_frame(length/4,buf); return length;}int CM1Library::Free() { m1snd_shutdown(); FreeLibrary(lib); return 0;}// class CM1Playerint CM1Player::LoadLibraryA(char *m1DLL,int samplerate) { if (m1lib.Create(m1DLL,samplerate) != 0) return 1; return 0;}void CM1Player::SetRootDirectory(char * dir) { memset(root,0,256); strcpy(root,dir);}int CM1Player::LoadRom(char *romset) { int cgame, gameselected=0; for(int i=0;i < m1lib.m1snd_get_info_int(M1_IINF_TOTALGAMES,0);i++) { if(!stricmp(m1lib.m1snd_get_info_str(M1_SINF_ROMNAME, i),romset)) { cgame=i; gameselected=1; break; } } if (!gameselected) { MessageBox(NULL,"couldn't load rom","crap",MB_OK); return 1; } isError=0; m1lib.m1snd_run(M1_CMD_GAMEJMP, cgame); // We should let it boot here before we do anything else while (isBooting) { m1lib.m1snd_do_frame(0,NULL); } if (isError != 0) return 1; return 0;}int CM1Player::SoftReset(int track) { int i; newSong=0; do { m1lib.m1snd_run(M1_CMD_SONGJMP,track); m1lib.m1snd_do_frame(0,0); } while (!newSong); return 0;}int CM1Player::Run(unsigned char *buf, unsigned long buflength) { // We have to wait before the thing boots before filling samples m1lib.m1snd_do_frame(576,buf); //m1lib.m1snd_run(M1_CMD_IDLE,0); return buflength;}