Embed high-quality sound in a Qt application (Qt + FMOD)

    I remember the time when I was delighted with the multimedia capabilities of Qt ... phonon is good, yes. But I remember the time of disappointment: too few things can do this same phonon. At a minimum, he cannot play two sounds at the same time. Yes, and his interfaces are lame ...

    Anyway, but I started looking for something better, moreover, cross-platform. After a couple of bad finds, I came across an FMOD Library . Despite the lack of source code, this is a free (for non-commercial use) cross-platform library.

    The list of possibilities is huge: this is for you a mixer, and low-latency DSP (including even pitch shifter), and reading a whole bunch of formats (mp3, wav, ogg, aiff, MIDI, etc.), and sound generation, and sound recording, and support for multi-channel sound (for example, 5.1), and then, and this, and the fifth, and tenth ... in general, functionality. And there is an interface for both C and C ++.

    And besides, a big plus is the low weight of the library itself (under Windows only ~ 370 kilos).

    So let's get started. I did it under Windows, but on other systems it will be about the same. I downloaded their API for developers, dropped fmodex.dll and the inc and lib folders into the FMOD folder in my project. I chose that I will use C ++ interfaces, connected #include, and then I do this:

    1. in the class of my window I declare the variable FMOD :: System * system;
    2. in the class constructor, I initialize:
    1. FMOD::System_Create(&system);
    2. system->init(32 /*maximum number of channels*/, FMOD_INIT_NORMAL, 0);
    * This source code was highlighted with Source Code Highlighter.

    3. create a sound:
    1. // sound
    2. FMOD::Sound * sound;
    3. // sound channel
    4. FMOD::Channel * channel;
    5. // creating sound
    6. system->system->createSound("mysound.mp3", FMOD_SOFTWARE | FMOD_LOOP_OFF, 0, &sound);
    7. // playing sound (assigning it to a channel)
    8. system->playSound(1 /*channel #1*/, sound, true /*start paused*/, &channel);
    9. // actually play sound
    10. channel->setPaused(false);
    * This source code was highlighted with Source Code Highlighter.

    At the same time, you can connect a lot of sounds to one system (each on its own channel), sounds can be looped (flag FMOD_LOOP_NORMAL).

    Now build the project. Unfortunately, C ++ - interfaces in Windows are available only for the Microsoft Visual Studio compiler, and for MinGW only C-interfaces are available. Therefore, I built the project using Qt for VS2008. In principle, C-interfaces are not very different from C ++, for example, instead of FMOD :: System we write FMOD_System, and instead of system-> init () we write FMOD_System_init (), so moving from one to another is quite easy.

    To get the project together, write in the .pro file:

    1. INCLUDEPATH += FMOD/inc
    2. LIBS += -LFMOD/lib -lfmodex_vc
    * This source code was highlighted with Source Code Highlighter.

    From the basic set of features, it is worth noting FMOD :: Channel :: setVolume () and setPan () - setting the volume and balance between channels (left - right).

    Another of the advantages of FMOD: the documentation is quite adequate, there are examples for almost all occasions.

    To be unfounded, I bring my program written using Qt + FMOD: DamnSampler . This is such a peculiar musical instrument - a sound (or even several) is hung on each key of the keyboard, sounds can be simple, or can be looped. Sources are attached.

    Also popular now: