Interactive Multi-State (Audio Engine)

C++.  I wrote this  audio engine with FMOD API.  Its play state algorithm in the interactive music class is an open nested loop system in which its update function is continuously called. It uses two sub music channels  sharing the states by cross-fade. Cross-fades occur by means of embedded thread objects. State containers and segments can be set by order, loop, and randomness. It was set for millisecond workloads by fps assumption. It will not work under one millisecond workload. It is good for full random containers. I will improve the interactive music section by reducing bottlenecks later on. You can download MultiStateInteractive.zip file and run it. Diagram shows class dependency and interactive class state structure.

Let’s check its AudioEngine header and class files:

I implemented Music class as an interface for Interactive and Background music classes.  Music class is NOT inherited from AudioEngine class because they are totally different objects in the aspect of their structures. However, they carry similar behaviours such as play function. Let’see its header file, no need to see its cpp file because of full pure virtual functions:

Interactive members are containers, container members, and active usage playState members. Random generator works for each segment in the container. playState method’s algorithm is a complicated little bit. When update function sends calls continuously, the algorithm checks passing duration by comparing to segment duration. Then, it takes the call inside once it is very close to be completed. Inside embedded thread lambda expression takes the call to the parallel processing not to disturb the workload. Let’s see the header and cpp files:

Let’s see other derived class from Music class, called Background. It is for background tracks, and I might add some features later on:

You can see main cpp file: loading and setting files, creating state and containers, etc. Here, there are background, tension, and battle states. Tension and battle states are interactive. For example, battle state has two containers, first one plays normal, other plays randomly. First one has one segment with just once. The other one has 3 segments playing randomly, and each plays 2 times. I used SFML library for press key option to send a command into playState when while-loop is active. Moreover, I put an artificial workload (15-35ms) inside the loop to be similar to a game loop model. I used Query Performance Counter to measure the elapsed time because I developed this programme in Windows. Meanwhile, the random work value can be send as an argument to the update function parameter as done here for testing purposes.