360 sound positioning code and working example

So for the past day or so I've been plagued by a sound positioining code problem I was having. Basicly, I didn't understand what I was doing, and I was trying to re-invent the wheel. Either way, what came of it was this, and I figured I might save someone else the trouble of doing what I did and what not. So here's the code to pan and pitch bend a sound according to it's location in relation to the player.
void thetaPos(double soundA, double soundB, sound@ SoundHandle, double myA, double myB, double myTheta)
{
double absolutetheta;
double relativetheta;
double tempH;
double tempV;
double tVolume;
double temp1 = soundA-myA;
double temp2 = myB - soundB;
double d1=power(temp1, 2);
double d2=power(temp2,2);
double d=square_root(d1+d2);
if(d<75)
{
SoundHandle.play_looped();
if(temp1>0)
{
absolutetheta=arc_tangent(temp2/temp1);
}
else if(temp1<0)
{
absolutetheta=arc_tangent(temp2/temp1)-90;
}
else if(x == 0)
{
  if (y > 0)
{
absolutetheta=90;
}
else if(temp2<0)
{
absolutetheta=-90;
}
  else if (y == 0)
{
absolutetheta=-1;
}
}
absolutetheta=absolutetheta*(180 /pi);
relativetheta = absolutetheta -myTheta;
tempH=cosine((relativetheta*pi/180));
SoundHandle.pan=tempH*100;
tempV=sine((relativetheta*3.14/180));
tVolume=(d*-100)/75;
{

}
if(tempV>0)
{
SoundHandle.volume=tVolume;
SoundHandle.pitch=80+absolute(tempH*20);
}
else
{
SoundHandle.volume=tVolume;
SoundHandle.pitch=120-absolute(tempH*20);
}
}
else
{
SoundHandle.pause();
}
}
The example below will work if you copy and paste it into a new file and adjust the sound path to the sound of your own choosing. Don't forget to make it a .bgt file.
//Decla ring pi.
const double pi=3.141592653589793;
//declaring our listener's x and y values, as well as their degrees.
double x=0;
double degrees=270;
double y=0;
//declare our sound's location.
double soundx=10;
double soundy=10;
sound test;
void main()
{
//show our window and load up some values before running into a loop where the user can press escape to close the program.
show_game_window("test");
test.load("sounds/EnemyTeamMate.ogg");
test.play_looped();
timer time;
do
{
//Flips the degrees by 180 when the user presses the S key.
if(key_pressed(KEY_S))
{
degrees-=180;
}
//Announces the degrees the player is facing when the user presses space.
if(key_pressed(KEY_SPACE))
{
alert("HI", degrees);
}
//turns the user to the right when they press the right arrow.
if(key_down(KEY_RIGHT))
{
degr ees+=.2;
}
//turns the user to the left when the left arrow key is pushed.
if(key_down(KEY_LEFT))
{
degrees+=-.2;
}
//realigns our user's degrees in case they fall out of their appropriate values.
if(degrees>359.95)
{
degrees=0;
}
if(degrees<0)
{
degrees=359.95;
}
//Our function that accepts the sound's x and y value, the listener's x and y values, and the listener's degrees and pans the sound accordingly.
thetaPos(soundx, soundy, test, x, y, degrees);
//wait and allow other programs access to the cpu.
wait(5);

}
while(!key_pressed(KEY_ESCAPE));
}
void thetaPos(double soundA, double soundB, sound@ SoundHandle, double myA, double myB, double myTheta)
{
double absolutetheta;
double relativetheta;
double tempH;
double tempV;
double tVolume;
double temp1 = soundA-myA;
double temp2 = myB - soundB;
double d1=power(temp1, 2);
double d2=power(temp2,2);
double d=square_root(d1+d2);
if(d<75)
{
SoundHandle.play_looped();
if(temp1>0)
{
absolutetheta=arc_tangent(temp2/temp1);
}
else if(temp1<0)
{
absolutetheta=arc_tangent(temp2/temp1)-90;
}
else if(x == 0)
{
  if (y > 0)
{
absolutetheta=90;
}
else if(temp2<0)
{
absolutetheta=-90;
}
  else if (y == 0)
{
absolutetheta=-1;
}
}
absolutetheta=absolutetheta*(180 /pi);
relativetheta = absolutetheta -myTheta;
tempH=cosine((relativetheta*pi/180));
SoundHandle.pan=tempH*100;
tempV=sine((relativetheta*3.14/180));
tVolume=(d*-100)/75;
{

}
if(tempV>0)
{
SoundHandle.volume=tVolume;
SoundHandle.pitch=80+absolute(tempH*20);
}
else
{
SoundHandle.volume=tVolume;
SoundHandle.pitch=120-absolute(tempH*20);}
}
else
{
SoundHandle.pause();
}
}

/*
I'm not going to explain the code unless someone specificly asks me, but you can modify how it sounds by changing the lines in the last if statements where soundhandle volume and pitchs are assigned. The 80+ the absolute value of tempH tells the code if the sound is behind us, set it's pitch to 80, the further it is to the extreme left or right, the higher pitch it is. The line in the if statement where pitch is equal to tempH/20 does the exact opposite, making it the highest pitch when it's directly in front of us, and lower pitched when it's to the far right or far left.

All that's really important about this code is that it moves the sound left to right and changes the volume and pitch based on the location in the stereo field. When it's directly behind us, it's the lowest pitch, when it's in front of us it's the highest pitch... I hope I'm e xplaining this well enough LOL.
Just try the example out and you'll see what I mean.
*/

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector

Reply via email to