⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 music.cpp

📁 一个96K的3D游戏源码
💻 CPP
字号:
//===============================================================================================
// STREAM.EXE
// Copyright (c), Firelight Technologies Pty, Ltd, 1999-2002.
//
// This example takes a command line parameter, a wav/mp2/mp3/ogg etc file, and uses the streamer 
// system to play it back.
//===============================================================================================

#include <string.h>
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <conio.h>
#include "fmod.h"
#include "fmod_errors.h"	// optional

int channel;

/*
[
	[DESCRIPTION]
	End of stream user callback, initialized with FSOUND_Stream_SetEndCallback or 
	FSOUND_Stream_SetSynchCallback

	[PARAMETERS]
	'stream'	A pointer to the stream that ended.
	'buff'		This is NULL for end of stream callbacks, or a string for synch callbacks.
	'len'		This is reserved and is always 0 for end and synch callbacks. ignore.
	'param'		This is the value passed to FSOUND_Stream_SetEndCallback or 
	            FSOUND_Stream_SetSynchCallback as a user data value.
 
	[RETURN_VALUE]
	TRUE or FALSE, the value is ignored.

	[REMARKS]

	[SEE_ALSO]
]
*/
signed char endcallback(FSOUND_STREAM *stream, void *buff, int len, int param)
{
	// end of stream callback doesnt have a 'buff' value, if it doesnt it could be a synch point.
	if (buff)
	{
		printf("\nSYNCHPOINT : \"%s\"\n", buff);
	}
	else
	{
		printf("\nSTREAM ENDED!!\n");
	}

	return TRUE;
}


/*
[
	[DESCRIPTION]
	main entry point into streamer example.

	[PARAMETERS]
	'argc'	Number of command line parameters.
	'argv'	Parameter list
 
	[RETURN_VALUE]
	void

	[REMARKS]

	[SEE_ALSO]
]
*/
	FSOUND_STREAM *stream;
	FSOUND_SAMPLE *sptr;
	char key;
    FILE *	fp;
	int		length;
	char *	data;

int InitMusic()
{
	if (FSOUND_GetVersion() < FMOD_VERSION)
	{
		printf("Error : You are using the wrong DLL version!  You should be using FMOD %.02f\n", FMOD_VERSION);
		return 1;
	}
	FSOUND_SetOutput(FSOUND_OUTPUT_DSOUND);
    FSOUND_SetDriver(0);					// Select sound card (0 = default)
    
	// ==========================================================================================
	// INITIALIZE
	// ==========================================================================================
	if (!FSOUND_Init(44100, 32, 0))
	{
		printf("Error!\n");
		printf("%s\n", FMOD_ErrorString(FSOUND_GetError()));
		FSOUND_Close();
		return 1;
	}
}

int LoadMP3(char *MusicN)
{
	// ==========================================================================================
	// OPEN STREAM (use #if 1 for streaming from memory)
	// ==========================================================================================

		fp = fopen(MusicN, "rb");
		if (!fp)
		{
			printf("Error!\n");
			printf("File Not Found\n");
			FSOUND_Close();
			return 1;
		}
		fseek(fp, 0, SEEK_END);
		length = ftell(fp);
		fseek(fp, 0, SEEK_SET);

		data = (char *)malloc(length);
		fread(data, length, 1, fp);
		fclose(fp);

}


int PlayMP3()
{
    stream = FSOUND_Stream_OpenFile(data, FSOUND_NORMAL | FSOUND_MPEGACCURATE | FSOUND_LOADMEMORY , length);

	// ==========================================================================================
	// SET AN END OF STREAM CALLBACK AND RIFF SYNCH POINTS CALLBACK
	// ==========================================================================================
	FSOUND_Stream_SetEndCallback(stream, endcallback, 0);
	FSOUND_Stream_SetSynchCallback(stream, endcallback, 0);
	// ==========================================================================================
	// PLAY STREAM
	// ==========================================================================================
	if ((channel = FSOUND_Stream_PlayEx(FSOUND_FREE, stream, NULL, TRUE)) == -1)
	{
		printf("Error!\n");
		printf("%s\n", FMOD_ErrorString(FSOUND_GetError()));
		FSOUND_Close();
		return 1;
	}
    FSOUND_SetPaused(channel, FALSE);

/*
	printf("=========================================================================\n");
	printf("Press SPACE to pause/unpause\n");
	printf("Press 'f'   to fast forward 2 seconds\n");
	printf("Press ESC   to quit\n");
	printf("=========================================================================\n");
	printf("Playing stream...\n\n");
*/
   
	sptr = FSOUND_Stream_GetSample(stream);
	if (sptr)
	{
		int freq;
		FSOUND_Sample_GetDefaults(sptr, &freq, NULL, NULL, NULL);
/*		printf("Name      : %s\n", FSOUND_Sample_GetName(sptr));
		printf("Frequency : %d\n\n", freq);
*/
	}

/*
    key = 0;
	do
	{
		if (kbhit())
		{
			static int timeoff = 0;

			key = getch();
			if (key == ' ')
            {
				FSOUND_SetPaused(channel, !FSOUND_GetPaused(channel));
            }
			if (key == 'f')
            {
				FSOUND_Stream_SetTime(stream, FSOUND_Stream_GetTime(stream) + 2000);
            }
		}

   		printf("pos %6d/%6d time %02d:%02d/%02d:%02d cpu %5.02f%%   \r", FSOUND_Stream_GetPosition(stream), 
                                                                         FSOUND_Stream_GetLength(stream), 
                                                                         FSOUND_Stream_GetTime(stream) / 1000 / 60, 
                                                                         FSOUND_Stream_GetTime(stream) / 1000 % 60, 
                                                                         FSOUND_Stream_GetLengthMs(stream) / 1000 / 60, 
                                                                         FSOUND_Stream_GetLengthMs(stream) / 1000 % 60, 
                                                                         FSOUND_GetCPUUsage());
		Sleep(1);
    
	} while (key != 27);

	printf("\n");
*/

}

void UnloadMusic()
{
	FSOUND_Stream_Close(stream);
	free(data);
}

void DoneMusic()
{
	FSOUND_Close();
}

long GetMPos()
{
  return(FSOUND_Stream_GetTime(stream));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -