📜 ⬆️ ⬇️

We build high-quality sound in Qt application (Qt + FMOD)

I remember a time when I was delighted with the Qt multimedia capabilities ... phonon is good, yes. But I also remember the time of disappointment: this phonon can do too little. At a minimum, he cannot reproduce two sounds simultaneously. Yes, and its interfaces are lame ...

Be that as it may, I began to look for something better, and cross-platform. After a couple of unsuccessful finds, I stumbled upon the FMOD Library . Despite the lack of source code, it is a free (for non-commercial use) cross-platform library.

The list of possibilities is huge: this is for you and the mixer, and low-latency DSP (including even the pitch shifter), and reading a whole heap of formats (mp3, wav, ogg, aiff, MIDI, etc.), and sound generation, and sound recording, and multi-channel audio support (for example, 5.1), and this, and this, and the fifth, and tenth ... in general, the functionality. And the interface is both for C and for C ++.
')
And besides, a big plus is the small weight of the library itself (under Windows only ~ 370 kilos).

So let's get started. I did it under Windows, but in 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 to use C ++ interfaces, hooked up #include <fmod.hpp>, and then do this:

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

3. create 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, many sounds can be connected to one system (each to its own channel), sounds can be looped (the FMOD_LOOP_NORMAL flag).

Now build the project. Unfortunately, C ++ interfaces in Windows are only available for the Microsoft Visual Studio compiler, and only C-interfaces are available for MinGW. Therefore, I compiled the project with 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 it is quite easy to switch from one to another.

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

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

FMOD :: Channel :: setVolume () and setPan () - set the volume and balance between channels (left - right) from the base set of features.

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

In order not to be unfounded, I quote my program, written with Qt + FMOD: DamnSampler . This is such a kind of musical instrument - a sound (or even several) is hung on each key of the keyboard, sounds can be simple, or they can be looped. Sources are attached.

Source: https://habr.com/ru/post/103346/


All Articles