C Define my fmod callbacks

252 views Asked by At

i'm using fmod and i'm trying to use different callbacks to define my own file reading. But it seems that fmod doesn't use only my functions. Indeed, in this case, he returns its own result.

FMOD_RESULT F_CALLBACK myopen(const char *name, int unicode, unsigned int *filesize, void **handle, void **userdata)
{
    FILE *fp;

    fp = fopen("Pump.mp3", "rb");
    if (!fp)
    {
        return FMOD_ERR_FILE_NOTFOUND;
    }

    fseek(fp, 0, SEEK_END);
    *filesize = ftell(fp);

    FMOD_RESULT res = FMOD_System_CreateStream(gsystem, "Pump.mp3", FMOD_HARDWARE | FMOD_2D | FMOD_OPENONLY, 0, &son2);
    ERRCHECK(res);

    FMOD_Sound_SeekData(son2, 0);

    *handle = son2;

    return FMOD_OK;
}


int main()
{
   FMOD_CREATESOUNDEXINFO *settings = malloc(sizeof(FMOD_CREATESOUNDEXINFO));

   memset(info, 0, sizeof(FMOD_CREATESOUNDEXINFO));

   settings->cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
   settings->decodebuffersize  = 44100;
   settings->useropen = myopen;
   settings->userclose = myclose;
   settings->userread = myread;
   settings->userseek = myseek;

   result = FMOD_System_CreateSound(system, "Pump.mp3", FMOD_SOFTWARE | FMOD_2D, settings, &sound);
   // result != FMOD_OK if file doesn't exist for example

   return 0;
}

I tried different ways to pass to fmod my functions like FMOD_System_AttachFileSystem or FMOD_System_SetFileSystem.

What am i doing wrong ?

Thanks.

1

There are 1 answers

0
Mathew Block On

It looks like you have some application code mixed in with your callback. The open callback should not make calls to System::createStream. The usual process is as follows:

  • Pass 'name' to fopen
  • Seek to the end, use ftell to get the 'filesize'
  • Seek back to the start
  • Store the file handle in 'handle'

You would then use the other callbacks (not pictured in your code) myread, myseek, myclose to complete the file I/O.