0
0

I was writing a short program using some code from the examples and realized that this program will only work if I sleep for a few seconds. It seems to be based on the length of the song or sample that I am playing. What if I was playing a 10 minute background song though? How do I know how long to sleep for? I’m pretty sure that I screwed up somewhere along the way.

Is there a way to make a song play without the sleep?

[code:34veeyvl]

include <stdio.h>

include <stdlib.h> /* For EXIT_FAILURE & EXIT_SUCCESS */

include <windows.h>

include <conio.h>

include "fmod.h"

include "fmod_errors.h"

void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
exit(EXIT_FAILURE);
}
}

int LoadFileIntoMemory(const char *name, void **buff, int *length)
{
FILE *fp;
int SR1, SR2; /* Seek result 1 & 2 */
int read;

if((fp = fopen(name, &quot;rb&quot;)) == NULL){
    puts( &quot;The file to be loaded through 'LoadFileIntoMemory' was not opened&quot; );getch();
    return EXIT_FAILURE;
}
SR1 = fseek(fp, 0, SEEK_END);
if(SR1){
    perror( &quot;Fseek 'SEEK_END' failed&quot; );getch();
    return EXIT_FAILURE;
}
*length = ftell(fp);
SR2 = fseek(fp, 0, SEEK_SET);
if(SR2){
    perror(&quot;Fseek 'SEEK_SET' failed&quot;);getch();
    return EXIT_FAILURE;
}
if ((*buff = malloc(*length)) == NULL){
    puts(&quot;Memory allocation error!&quot;);getch();
    return EXIT_FAILURE;
}
read = fread(*buff, *length, 1, fp);
fclose(fp);
return EXIT_SUCCESS;

}

int main(int argc, char *argv[])
{
FMOD_SYSTEM *system;
FMOD_SOUND *sound1;
FMOD_CHANNEL *channel = 0;
FMOD_RESULT result;
unsigned int version;
void *buff = 0;
int length = 0;
FMOD_CREATESOUNDEXINFO exinfo;

int loadSndErr = 0;

/*
    Create a System object and initialize.
*/
result = FMOD_System_Create(&amp;system);
ERRCHECK(result);

result = FMOD_System_GetVersion(system, &amp;version);
ERRCHECK(result);

if (version &lt; FMOD_VERSION)
{
    printf(&quot;Error!  You are using an old version of FMOD %08x.  This program requires %08x\n&quot;, version, FMOD_VERSION);
    return EXIT_FAILURE;
}

result = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, NULL);
ERRCHECK(result);

loadSndErr = LoadFileIntoMemory(&quot;media/drumloop.wav&quot;, &amp;buff, &amp;length);

if(loadSndErr)
    return EXIT_FAILURE;

memset(&amp;exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
exinfo.length = length;

result = FMOD_System_CreateSound(system, (const char *)buff, FMOD_HARDWARE | FMOD_OPENMEMORY, &amp;exinfo, &amp;sound1);
ERRCHECK(result);

result = FMOD_Sound_SetMode(sound1, FMOD_LOOP_OFF);
ERRCHECK(result);

free(buff); // don't need the original memory any more.  Note!  If loading as a stream, the memory must stay active so do not free it!

result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound1, 0, &amp;channel);
ERRCHECK(result);

FMOD_System_Update(system);

Sleep(3000); 

printf(&quot;\n&quot;);

/*
    Shut down
*/
result = FMOD_Sound_Release(sound1);
ERRCHECK(result);
result = FMOD_System_Close(system);
ERRCHECK(result);
result = FMOD_System_Release(system);
ERRCHECK(result);

getch();
return EXIT_SUCCESS;

}[/code:34veeyvl][/code]

  • You must to post comments
0
0

System_Update needs to be called… How often depends on what you are doing.

Sleeping and not calling System_Update for more and a few milliseconds is not recomended.

System_Update is in charge or updating the internals data, reading the next playing song chunks and what not. If you dont call it, the system will go waky.

so, you are missing a while loop

pseudo code…

define fps 30

//init system

//main loop
while(keyboard_get(vk_escape) != true)
{
FMOD_System_Update();
sleep(1000/fps);
}

  • You must to post comments
0
0

another way would be to execute the while loop on Channel::isPlaying if it is false, then exit the loop.

The point is that sounds play in the background. You’re supposed to keep yourself busy while the sound plays. Playing a sound is not supposed to lockup the whole application :)

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.