Controlling Looping using Volume
In this application, I aimed to demonstrate a ‘game’ scenario where the user can only progress to the next section of the track by fulfilling the required conditions. For this example, I decided to keep it simple by using volume as a control. My overall idea is to create may variables such as filtering certain frequencies or activating effects, this is just to get the looping functionality down.
Also the ‘magic numbers’ in the code specify the loop start and end time in milliseconds. These were just estimates from Reaper, in the future I intend to have a text file or similar to store these numbers since they are different for each track.
#include "stdafx.h" #include "fmod.hpp" #include "fmod_errors.h" #include <windows.h> #include <stdio.h> #include <conio.h> void errchk(FMOD_RESULT); int _tmain(int argc, _TCHAR* argv[]) { FMOD::System *system; FMOD::Channel *channel = 0; FMOD::Sound *stream1; int key = 0, section = 1; unsigned int version; bool next = false; float vol = 0.5; FMOD_RESULT result; result = FMOD::System_Create(&system); errchk(result); result = system->getVersion(&version); errchk(result); if (version < FMOD_VERSION) { printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION); return 0; } result = system->init(1, FMOD_INIT_NORMAL, 0); errchk(result); result = system->createStream("C:/trackStreams/Balcony.wav", FMOD_HARDWARE | FMOD_LOOP_NORMAL | FMOD_2D, 0, &stream1); errchk(result); result = system->playSound(FMOD_CHANNEL_FREE, stream1, false, &channel); errchk(result); result = channel->setVolume(vol); errchk(result); result = channel->setLoopPoints(14, FMOD_TIMEUNIT_MS, 8395, FMOD_TIMEUNIT_MS); errchk(result); do { if(_kbhit()) { key = _getch(); switch(key) { case 'a': if(vol < 1.0) { vol += 0.1f; result = channel->setVolume(vol); errchk(result); break; } case 's': if(vol > 0.0) { vol -= 0.1f; result = channel->setVolume(vol); errchk(result); break; } } } switch(section) { case 1: { if(vol < 0.4) { channel->setMode(FMOD_LOOP_OFF); unsigned int pos; channel->getPosition(&pos, FMOD_TIMEUNIT_MS); if(pos > 8300) { printf("\n\nSection 2 started:%d\n", pos); result = channel->setLoopPoints(8395, FMOD_TIMEUNIT_MS, 13406, FMOD_TIMEUNIT_MS); errchk(result); section++; } } else { channel->setMode(FMOD_LOOP_NORMAL); } break; } case 2: { if(!next) { if(vol >= 0.7) { channel->setMode(FMOD_LOOP_OFF); unsigned int pos; channel->getPosition(&pos, FMOD_TIMEUNIT_MS); if(pos > 13350) { printf("\n\nSection 3 started:%d\n", pos); section++; next = true; } } else { channel->setMode(FMOD_LOOP_NORMAL); } } break; } } system->update(); { float volume; result = channel->getVolume(&volume); errchk(result); printf("Channel volume: %.f\r", volume*100); } Sleep(10); } while (key != 27); result = stream1->release(); errchk(result); result = system->close(); errchk(result); result = system->release(); errchk(result); return 0; } void errchk(FMOD_RESULT r) { if (r != FMOD_OK) { printf("FMOD error! (%d) %s\n", r, FMOD_ErrorString(r)); //exit(-1); } }
Looking at getting some screen captures done of these examples to explain them better. And somewhere better to store code….