Are there any c interface examples?

I am having trouble initialising fmod using the c interface. The problem is that all the examples are for the c++ interface only.

1 Like

Hi Ed

Unfortunately we don’t ship any C examples, but converting any of our C++ examples to C is a pretty straight forward process.

Generally where in C++ you have a class or structure type FMOD::Classname the C type will be FMOD_CLASSNAME, and for FMOD::Studio::Classname it’s FMOD_STUDIO_CLASSNAME.

For member functions FMOD::Classname::memberFn becomes FMOD_Classname_MemberFn and FMOD::Studio::Classname::memberFn becomes FMOD_Studio_Classname_MemberFn. And for member functions you pass the instane pointer explicitly in C where C++ would pass it as the this pointer.

Just making those changes will mostly let you compile our examples using the C API. One or two functions like FMOD_Studio_System_Create need an extra parameter because the C++ API uses a default value, check the docs if you get a compilation error after changing a function to the C version.

If you run into any problems doing this or would like an example of one of our APIs in C which isn’t covered by any of the shipped examples just ask.

As an example, I ported our simple_event example from the C++ API to the C API (note that you still have to compile this as C++ because our example framework is written in C++, otherwise this should be legal C code):

Simple Event Example
Copyright (c), Firelight Technologies Pty, Ltd 2012-2019.

This example demonstrates the various ways of playing an event.

#### Explosion Event ####
This event is played as a one-shot and released immediately after it has been

#### Looping Ambience Event ####
A single instance is started or stopped based on user input.

#### Cancel Event ####
This instance is started and if already playing, restarted.

#include "fmod_studio.h"
#include "fmod.h"
#include "common.h"

int FMOD_Main()
    void *extraDriverData = NULL;
    FMOD_SYSTEM* lowLevelSystem = NULL;
    FMOD_STUDIO_BANK* masterBank = NULL;
    FMOD_STUDIO_BANK* stringsBank = NULL;
    FMOD_STUDIO_EVENTDESCRIPTION* loopingAmbienceDescription = NULL;
    FMOD_STUDIO_EVENTINSTANCE* loopingAmbienceInstance = NULL;
    FMOD_STUDIO_EVENTDESCRIPTION* explosionDescription = NULL;


    ERRCHECK( FMOD_Studio_System_Create(&system, FMOD_VERSION) );

    /* The example Studio project is authored for 5.1 sound, so set up the system output mode to match */
    ERRCHECK( FMOD_Studio_System_GetLowLevelSystem(system, &lowLevelSystem) );
    ERRCHECK( FMOD_System_SetSoftwareFormat(lowLevelSystem, 0, FMOD_SPEAKERMODE_5POINT1, 0) );

    ERRCHECK( FMOD_Studio_System_Initialize(system, 1024, FMOD_STUDIO_INIT_NORMAL, FMOD_INIT_NORMAL, extraDriverData) );

    ERRCHECK( FMOD_Studio_System_LoadBankFile(system, Common_MediaPath("Master"), FMOD_STUDIO_LOAD_BANK_NORMAL, &masterBank) );
    ERRCHECK( FMOD_Studio_System_LoadBankFile(system, Common_MediaPath("Master"), FMOD_STUDIO_LOAD_BANK_NORMAL, &stringsBank) );
    ERRCHECK( FMOD_Studio_System_LoadBankFile(system, Common_MediaPath(""), FMOD_STUDIO_LOAD_BANK_NORMAL, &sfxBank) );

    /* Get the Looping Ambience event */
    ERRCHECK( FMOD_Studio_System_GetEvent(system, "event:/Ambience/Country", &loopingAmbienceDescription) );
    ERRCHECK( FMOD_Studio_EventDescription_CreateInstance(loopingAmbienceDescription, &loopingAmbienceInstance) );

    /* Get the 4 Second Surge event */
    ERRCHECK( FMOD_Studio_System_GetEvent(system, "event:/UI/Cancel", &cancelDescription) );
    ERRCHECK( FMOD_Studio_EventDescription_CreateInstance(cancelDescription, &cancelInstance) );

    /* Get the Single Explosion event */
    ERRCHECK( FMOD_Studio_System_GetEvent(system, "event:/Weapons/Explosion", &explosionDescription) );

    /* Start loading explosion sample data and keep it in memory */
    ERRCHECK( FMOD_Studio_EventDescription_LoadSampleData(explosionDescription) );

        if (Common_BtnPress(BTN_ACTION1))
            /* One-shot event */
            FMOD_STUDIO_EVENTINSTANCE* eventInstance = NULL;
            ERRCHECK( FMOD_Studio_EventDescription_CreateInstance(explosionDescription, &eventInstance) );

            ERRCHECK( FMOD_Studio_EventInstance_Start(eventInstance) );

            /* Release will clean up the instance when it completes */
            ERRCHECK( FMOD_Studio_EventInstance_Release(eventInstance) );

        if (Common_BtnPress(BTN_ACTION2))
            ERRCHECK( FMOD_Studio_EventInstance_Start(loopingAmbienceInstance) );

        if (Common_BtnPress(BTN_ACTION3))
            ERRCHECK( FMOD_Studio_EventInstance_Stop(loopingAmbienceInstance, FMOD_STUDIO_STOP_IMMEDIATE) );

        if (Common_BtnPress(BTN_ACTION4))
            /* Calling start on an instance will cause it to restart if it's already playing */
            ERRCHECK( FMOD_Studio_EventInstance_Start(cancelInstance) );

        ERRCHECK( FMOD_Studio_System_Update(system) );

        Common_Draw("Simple Event Example.");
        Common_Draw("Copyright (c) Firelight Technologies 2012-2019.");
        Common_Draw("Press %s to fire and forget the explosion", Common_BtnStr(BTN_ACTION1));
        Common_Draw("Press %s to start the looping ambience", Common_BtnStr(BTN_ACTION2));
        Common_Draw("Press %s to stop the looping ambience", Common_BtnStr(BTN_ACTION3));
        Common_Draw("Press %s to start/restart the cancel sound", Common_BtnStr(BTN_ACTION4));
        Common_Draw("Press %s to quit", Common_BtnStr(BTN_QUIT));

    } while (!Common_BtnPress(BTN_QUIT));

    ERRCHECK( FMOD_Studio_Bank_Unload(sfxBank) );
    ERRCHECK( FMOD_Studio_Bank_Unload(stringsBank) );
    ERRCHECK( FMOD_Studio_Bank_Unload(masterBank) );

    ERRCHECK( FMOD_Studio_System_Release(system) );


    return 0;
1 Like

Thanks for the answer Derek, that example is quite helpful :smile:

1 Like