Click here to Skip to main content
15,891,841 members
Articles / Desktop Programming / MFC
Article

Little Audio Player

Rate me:
Please Sign up or sign in to vote.
1.44/5 (19 votes)
3 Jan 2005 97K   3.7K   34   19
This article demonstrates a very simple way to play MP3 files and with a very efficient programming engine from FMOD.

Sample Image

Introduction

The article I will present is nothing much really. I just tried to make a very good copy of Winamp. The SDK can easily be found on here with full support even for Linux OS-s and with all the documentation you need.

Using the code

The code is really very easy to use. The functions from the FMOD SDK look like this:

FSOUND_Sample_Load(
int index,
const char *name_or_data,
unsigned int inputmode,
int offset,
int length
);

or

FSOUND_PlaySound(
int channel,
FSOUND_SAMPLE *sptr
);

The SDK from FMOD is very useful. The thing I liked about it is that it loads and decodes a static sound file into memory. This includes such files as .WAV, .MP2, .MP3, .OGG, .RAW and others. There is no big deal to explain about the code because it is all in the documentation. I posted this article for the ones that need a very powerful and easy to use SDK for game developing.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Romania Romania
I love VC++

Comments and Discussions

 
QuestionArray Pin
khalil2me26-Jun-07 9:57
khalil2me26-Jun-07 9:57 
Generalhelp me !! Pin
MoNa MoOn17-May-06 22:56
MoNa MoOn17-May-06 22:56 
AnswerRe: help me !! Pin
TowerTurtle18-Aug-08 13:34
TowerTurtle18-Aug-08 13:34 
Generalfmod library Pin
kamax831-Mar-06 10:17
kamax831-Mar-06 10:17 
GeneralRe: fmod library Pin
gamitech1-Mar-06 13:34
gamitech1-Mar-06 13:34 
QuestionRe: fmod library Pin
indee4u21-Jun-07 0:25
indee4u21-Jun-07 0:25 
AnswerRe: fmod library Pin
indee4u21-Jun-07 4:16
indee4u21-Jun-07 4:16 
AnswerRe: fmod library Pin
indee4u21-Jun-07 4:18
indee4u21-Jun-07 4:18 
GeneralRe: fmod library Pin
RomeoRus7772-Dec-06 21:37
RomeoRus7772-Dec-06 21:37 
Where can I download an example for Fmod- MFC project?

When I compile my project - it's all OK, but then I take a message "Unhandled exception in 1.exe (FMOD.DLL): 0xC0000005: Access Violation.

Here is my small module
#include <stdio.h><br />
#include <stdlib.h><br />
#include "fmod.h"<br />
#include "fmod_errors.h"	/* optional */<br />
#define	ENABLEREVERB	TRUE<br />
#define RECORDRATE      /*22050*/11025<br />
#define RECORDLEN       (RECORDRATE *1 )    /*  seconds at RECORDRATE khz */<br />
#define OUTPUTRATE      /*22050*/11025<br />
#define REVERB_NUMTAPS	7<br />
typedef struct<br />
{<br />
FSOUND_DSPUNIT	*Unit;<br />
	char			*historybuff;		/* storage space for tap history */<br />
	char			*workarea;			/* a place to hold 1 buffer worth of data (for reverb) */<br />
	int				delayms;			/* delay of reverb tab in milliseconds */<br />
	int				volume;				/* volume of reverb tab */<br />
	int				pan;				/* pan of reverb tab */<br />
	int				historyoffset;		/* running offset into history buffer */<br />
	int				historylen;			/* size of history buffer in SAMPLES */<br />
} REVERBTAP;<br />
<br />
/*<br />
    Reverb stuff<br />
*/<br />
<br />
REVERBTAP		DSP_ReverbTap[REVERB_NUMTAPS];<br />
<br />
<br />
int umain()<br />
{signed char key;<br />
<br />
	FILE *fp;<br />
//key=key - '1';<br />
//if(key=='1') gss(key);<br />
<br />
<br />
<br />
char *tptr;<br />
char *the_my_transfer;<br />
FSOUND_SAMPLE *samp1;<br />
//signed char key;<br />
signed char key0;<br />
int driver, i, channel, originalfreq;<br />
int bits     = /*(mode & FSOUND_16BITS) /*? 16 : */8;<br />
					int channels = /*(mode & FSOUND_STEREO)/*? 2  : */ 1;<br />
	 				int lenbytes = FSOUND_Sample_GetLength(samp1) * channels * bits / 8;<br />
					int lenytes = FSOUND_Sample_GetLength(samp1) * 1 * bits / 8;<br />
					void *ptr1, *ptr2;<br />
					unsigned int len1, len2;<br />
<br />
<br />
<br />
<br />
	if (FSOUND_GetVersion() < FMOD_VERSION){printf("Error : You are using the wrong DLL version!  You should be using FMOD %.02f\n", FMOD_VERSION);return 0;}<br />
	FSOUND_SetOutput(FSOUND_OUTPUT_DSOUND);<br />
	printf("Direct Sound");<br />
    printf(" Driver list\n");	<br />
	printf("---------------------------------------------------------\n");	<br />
	<br />
	for (i=0; i < FSOUND_GetNumDrivers(); i++) <br />
	{printf("%d - %s\n", i+1, FSOUND_GetDriverName(i));}<br />
	<br />
	printf("---------------------------------------------------------\n");	/* print driver names */<br />
	printf("Press a corresponding number or ESC to quit\n");<br />
	<br />
	do<br />
	{//key = getch();<br />
	if (key == 27) {FSOUND_Close();return 0;}<br />
	driver = key - '1';}<br />
	while (driver < 0 || driver >= FSOUND_GetNumDrivers());<br />
<br />
	FSOUND_SetDriver(driver);					/* Select sound card (0 = default) */<br />
	FSOUND_SetMixer(FSOUND_MIXER_QUALITY_AUTODETECT);<br />
	if (!FSOUND_Init(OUTPUTRATE, 64, FSOUND_INIT_ACCURATEVULEVELS))<br />
	{printf("Error!\n");printf("%s\n", FMOD_ErrorString(FSOUND_GetError()));return 0;}<br />
<br />
	printf(" Recording device driver list\n");	<br />
	printf("---------------------------------------------------------\n");	<br />
	for (i=0; i < FSOUND_Record_GetNumDrivers(); i++) <br />
	{printf("%d - %s\n", i+1, FSOUND_Record_GetDriverName(i));	/* print driver names */}<br />
	printf("---------------------------------------------------------\n");	/* print driver names */<br />
	printf("Press a corresponding number or ESC to quit\n");<br />
	<br />
	do<br />
	{//key = getch();<br />
	if (key == 27) return 0;<br />
	driver = key - '1';} <br />
	while (driver < 0 || driver >= FSOUND_Record_GetNumDrivers());<br />
<br />
	<br />
<br />
	<br />
printf("SERVER OR CILENT?\n");//key0=getch();<br />
//key0=my_choice;<br />
<br />
	if (FSOUND_GetOutput() == FSOUND_OUTPUT_OSS)<br />
					{samp1 = FSOUND_Sample_Alloc(FSOUND_UNMANAGED, RECORDLEN, FSOUND_IMAADPCM|FSOUND_MONO | FSOUND_8BITS | FSOUND_UNSIGNED, RECORDRATE, 255, 128, 255);}<br />
					else<br />
					{samp1 = FSOUND_Sample_Alloc(FSOUND_UNMANAGED, RECORDLEN, FSOUND_IMAADPCM|FSOUND_STEREO | FSOUND_16BITS , RECORDRATE, 255, 128, 255);}<br />
					printf("\n");<br />
<br />
////// SERVER //////////////////////<br />
if(key0=='1')<br />
{			<br />
					printf("=========================================================================\n");<br />
					printf("Press a key to start recording 10 seconds worth of data\n");<br />
					printf("=========================================================================\n");<br />
					//getch();<br />
					if (!FSOUND_Record_StartSample(samp1, FALSE))	/* it will record into this sample for 5 seconds then stop */<br />
					{<br />
						printf("Error!\n");<br />
						printf("%s\n", FMOD_ErrorString(FSOUND_GetError()));<br />
<br />
						FSOUND_Close();<br />
						return 0;<br />
					}<br />
<br />
					do<br />
					{<br />
						printf("Recording position = %d\r", FSOUND_Record_GetPosition());<br />
						Sleep(50);<br />
					} while (FSOUND_Record_GetPosition() < RECORDLEN /*&& *kbhit()*/); //HERE<br />
<br />
					FSOUND_Record_Stop();	/* it already stopped anyway */<br />
<br />
					printf("\n=========================================================================\n");<br />
					printf("Press a key to play back recorded data\n");<br />
					printf("=========================================================================\n");<br />
					<br />
	FSOUND_Sample_Lock(samp1, 0, lenbytes, &ptr1, &ptr2, &len1, &len2);<br />
<br />
	fp = fopen("temp.wws", "wb");<br />
        fwrite(ptr1, len1, 1, fp);<br />
		the_my_transfer=new char[len1];<br />
		for(int zz=0;zz<len1;zz++)<br />
			{<br />
		//	the_my_transfer[zz]=*(ptr1+len1);<br />
			}<br />
		delete[]the_my_transfer;<br />
		fclose(fp);<br />
      FSOUND_Sample_Unlock(samp1, ptr1, ptr2, len1, len2);<br />
<br />
}<br />
////// CILENT //////////////////////<br />
else<br />
{<br />
printf("CILENT!! Press a key to play back recorded data\n");<br />
<br />
FSOUND_Sample_Lock(samp1, 0, lenbytes, &ptr1, &ptr2, &len1, &len2);<br />
<br />
fp = fopen("temp.wws", "rb");fread(ptr1, len1, 1, fp);fclose(fp);<br />
				<br />
FSOUND_Sample_Upload(samp1,ptr1,FSOUND_NORMAL);<br />
channel = FSOUND_PlaySound(FSOUND_FREE, samp1);printf("Playing back sound...\n");<br />
FSOUND_Sample_Unlock(samp1, ptr1, ptr2, len1, len2);<br />
channel = FSOUND_PlaySound(FSOUND_FREE, samp1);printf("Playing back sound...\n");<br />
			/*	channel = FSOUND_PlaySound(FSOUND_FREE, samp1);<br />
<br />
						printf("Playing back sound...\n");<br />
						do<br />
						{FSOUND_SetVolume(channel, 255);<br />
							//printf("Playback position = %d\r", FSOUND_GetCurrentPosition(channel));<br />
						printf("Playback position = %d\r", FSOUND_GetVolume(channel));<br />
							Sleep(50);<br />
						} while (FSOUND_IsPlaying(channel) && !kbhit());*/<br />
// ???<br />
<br />
}<br />
printf("END!!\n");<br />
    if (FSOUND_GetOutput() == FSOUND_OUTPUT_OSS)<br />
    { FSOUND_Sample_Free(samp1); FSOUND_Close();  return 0; }<br />
    /*<br />
        SAVED TO <br />
    */<br />
  /*  SaveToWav(samp1);<br />
<br />
    printf("\nSaved to record.wav!\n");<br />
*/<br />
//    getch();<br />
	FSOUND_StopSound(channel);<br />
	FSOUND_Record_Stop();<br />
    FSOUND_Sample_Free(samp1);<br />
    FSOUND_Close();<br />
    return 0;<br />
}<br />

GeneralRe: fmod library Pin
sfengtfw26-Mar-07 17:07
sfengtfw26-Mar-07 17:07 
Questionhi,can your liberary be used for commercial intention? Pin
suchuhui1-Dec-05 16:44
suchuhui1-Dec-05 16:44 
AnswerRe: hi,can your liberary be used for commercial intention? Pin
gamitech5-Dec-05 3:39
gamitech5-Dec-05 3:39 
GeneralFMOD's commerical license isn't free Pin
Markus000016-Nov-05 3:24
Markus000016-Nov-05 3:24 
GeneralMemory leaks Pin
Roger655-Jan-05 4:21
Roger655-Jan-05 4:21 
GeneralRe: Memory leaks Pin
gamitech6-Jan-05 8:51
gamitech6-Jan-05 8:51 
GeneralRe: Memory leaks Pin
Roger656-Jan-05 10:23
Roger656-Jan-05 10:23 
GeneralRe: Memory leaks Pin
gamitech6-Jan-05 10:26
gamitech6-Jan-05 10:26 
GeneralEuacela!!!! Pin
ThatsAlok3-Jan-05 21:40
ThatsAlok3-Jan-05 21:40 
GeneralRe: Euacela!!!! Pin
gamitech4-Jan-05 10:39
gamitech4-Jan-05 10:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.