//
// hacky minifmod wrapper
// no pretty, but it works for now...
//

#include "minifmodHelper.h"
#include "Shlwapi.h"
#include "sys/msys_malloc.h"
#include <string.h>
#include "minifmod\minifmod.h"



// this is if you want to replace the samples with your own (in case you have compressed them)
void sampleloadcallback(void *buff, int lenbytes, int numbits, int instno, int sampno)
{
	//printf("pointer = %p length = %d bits = %d instrument %d sample %d\n", buff, lenbytes, numbits, instno, sampno);
}

void StopMod(FMUSIC_MODULE *mod)
{
	FMUSIC_StopSong(mod);
};

FMUSIC_MODULE* loadAndPlayMod(int audioCode)
{
	FMUSIC_MODULE *mod;
	//FSOUND_File_SetCallbacks(fileopen, fileclose, fileread, fileseek, filetell);
	FSOUND_File_SetCallbacks(memopen, memclose, memread, memseek, memtell);
	//TCHAR szFilePath[_MAX_PATH];
	//::GetModuleFileName(NULL, szFilePath, _MAX_PATH);
	//PathRemoveFileSpec(szFilePath);
	mod = FMUSIC_LoadSong((char*)memopen2(audioCode), NULL); //sampleloadcallback);
	//mod = FMUSIC_LoadSong((char*)&muz, NULL); //sampleloadcallback);
	FMUSIC_PlaySong(mod);
	return mod;
}

FMUSIC_MODULE* testFmod()
{
	FMUSIC_MODULE *mod;
	//FSOUND_File_SetCallbacks(fileopen, fileclose, fileread, fileseek, filetell);
	FSOUND_File_SetCallbacks(memopen, memclose, memread, memseek, memtell);
	//TCHAR szFilePath[_MAX_PATH];
	//::GetModuleFileName(NULL, szFilePath, _MAX_PATH);
	//PathRemoveFileSpec(szFilePath);
	mod = FMUSIC_LoadSong((char*)memopen2(0), NULL); //sampleloadcallback);
	//mod = FMUSIC_LoadSong((char*)&muz, NULL); //sampleloadcallback);
	FMUSIC_PlaySong(mod);
	return mod;
}

/*
void songcallback(FMUSIC_MODULE *mod, unsigned char param)
{
	printf("order = %d, row = %d      \r", FMUSIC_GetOrder(mod), FMUSIC_GetRow(mod));
}
*/

/*
[
	[DESCRIPTION]

	[PARAMETERS]
 
	[RETURN_VALUE]

	[REMARKS]

	[SEE_ALSO]
]
*/

/*
void main(int argc, char *argv[])
{
#ifdef USEFMOD
	FMUSIC_MODULE *mod;

#ifndef USEMEMLOAD
	FSOUND_File_SetCallbacks(fileopen, fileclose, fileread, fileseek, filetell);
#else
	FSOUND_File_SetCallbacks(memopen, memclose, memread, memseek, memtell);
#endif
#endif

	if (argc < 2)
	{
		printf("-------------------------------------------------------------\n");
		printf("MINIFMOD example XM player.\n");
		printf("Copyright (c) Firelight Technologies, 2000-2004.\n");
		printf("-------------------------------------------------------------\n");
		printf("Syntax: simplest infile.xm\n\n");
		return;
	}


#ifdef USEFMOD

	// ==========================================================================================
	// LOAD SONG
	// ==========================================================================================
	mod = FMUSIC_LoadSong(argv[1], NULL); //sampleloadcallback);
	if (!mod)
	{
		printf("Error loading song\n");
		return;
	}

	// ==========================================================================================
	// PLAY SONG
	// ==========================================================================================
	FMUSIC_PlaySong(mod);

#endif

	printf("Press any key to quit\n");
	printf("=========================================================================\n");
	printf("Playing song...\n");

	{
		char key = 0;
		do
		{
			int		ord = 0, row = 0;
			float	mytime = 0;
			if (_kbhit())
			{
				key = _getch();
			}

#ifdef USEFMOD
			ord = FMUSIC_GetOrder(mod);
			row = FMUSIC_GetRow(mod);
			mytime = (float)FMUSIC_GetTime(mod) / 1000.0f;
#endif

			printf("ord %2d row %2d seconds %5.02f %s      \r", ord, row, mytime, (row % 8 ? "    " : "TICK"));

		} while (key != 27);
	}


	printf("\n");

#ifdef USEFMOD
	FMUSIC_FreeSong(mod);
#endif
}
*/