freshfunkee

Spewing my thoughts online, so I don't forget them later

Month: November, 2012

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….

Basic Volume and Pan Controller using FMOD and SDL

Trying out some simple interactive controls for possible final year project ideas. This is a quick application which sets up a basic stream in FMOD and a window in SDL which allows the user to control the volume and pan of the track using the y and x positions of the mouse cursor in the window.


#include "stdafx.h"
#include "fmod.hpp"
#include "fmod_errors.h"
#include "SDL.h"

void errchk(FMOD_RESULT r);

int _tmain(int argc, _TCHAR* argv[])
{
FMOD::System      *system;
FMOD::Channel     *channel = 0;
FMOD::Sound       *sound;
float            x,y;
unsigned int    version;
FMOD_RESULT        result;

SDL_Surface        *screen = NULL;
SDL_Event        event;

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:/here.mp3", FMOD_HARDWARE | FMOD_LOOP_NORMAL | FMOD_2D, 0, &sound);
errchk(result);

result = system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);
errchk(result);

SDL_Init(SDL_INIT_EVERYTHING);

screen = SDL_SetVideoMode( 500, 500, 32, SDL_SWSURFACE );

SDL_Flip( screen );

while(true)
{
if (SDL_PollEvent(&event) )
{
if( event.type == SDL_QUIT )
{
exit(0);
}
else if( event.type == SDL_MOUSEMOTION )
{
x = event.motion.x;
y = event.motion.y;

result = channel->setVolume(y/500);
errchk(result);

result = channel->setPan((x/250) - 1.0);
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);
}
}

Must find a better way to share code :/

Hello world!

Welcome to WordPress.com! This is your very first post. Click the Edit link to modify or delete it, or start a new post. If you like, use this post to tell readers why you started this blog and what you plan to do with it.

Happy blogging!