Problem with 3d sound

Blitz3D Forums/Blitz3D Programming/Problem with 3d sound

eos(Posted 2010) [#1]
Hi everyone!

I'm trying to do a very easy example of 3d sound.
I have a car standing in the desert emiting an engine sound, what i want is that when the camera moves away, the sound fades out.
It should be simple with CreateListener and EmitSound, but it is not working.

I have read several posts and cannnot spot the problem in my code...

The camera has the listener attached as a child and the car emits the sound with EmitSound, although i can hear the sound it doesnt change its volume when i move away the camera with the arrows....

What is weird is that i noticed that when i rotate the camera, the sound is being panned...

Hope you can help, heres the code:
using System;
using bb = Blitz3DSDK;

namespace Clase01
{
    static class ClaseMain
    {
        //--GAME CONSTS------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------------------
        const int WIDTH         = 1024;
        const int HEIGHT        = 768;
        const int DEPTH         = 32;
        const int GRAPHIC_MODE  = bb.GFX_WINDOWED;

        const int FONT_HEIGHT       = 24;
        const int FONT_BOLD         = 1;
        const int FONT_ITALIC       = 0;
        const int FONT_UNDERLINED   = 1;
        const int FONT_COLOR_RED    = 255;
        const int FONT_COLOR_GREEN  = 255;
        const int FONT_COLOR_BLUE   = 255;

        static int CAMERA           = 0;
        static int LEFT_ARROW       = 0;
        static int RIGHT_ARROW      = 0;

        static bool FULL_SCREEN     = false;

        //--METHODS----------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------------------
        
        [STAThread]
        static void Main() 
        {
            bb.BeginBlitz3D();
                        
            setGraphicMode();

            setTextForm();

            //Camera
            CAMERA = bb.CreateCamera();
            bb.CameraClsColor(CAMERA, 189, 224, 251);
            bb.PositionEntity(CAMERA, 0, 2, 0);
            bb.AmbientLight(255, 255, 255);
            bb.CameraViewport(CAMERA, 64, 490, 896, 202);
            bb.ClsColor(255, 255, 255);
            //Camera Listener
            int t = bb.CreateListener(CAMERA);

            //Ground
            int plane = bb.CreatePlane();
            int texture = bb.LoadTexture("Images/ground2.jpg");
            bb.ScaleTexture(texture, 8, 8);
            bb.EntityTexture(plane, texture);

            //Imagen de fondo
            int img = bb.LoadImage("Images/FondoComic.png");
            bb.MaskImage(img, 1, 0, 0);
            
            //Car
            int car = bb.LoadMesh("nova/nova.3ds");
            bb.ScaleEntity(car, 0.01f, 0.01f, 0.01f);
            bb.PositionEntity(car, 0, 0f, 14);
            bb.RotateEntity(car, 0, -90, 0);
            
            //Sounds
            int motorSound = bb.bbLoadSound("Sounds/hotidle.wav");
            bb.LoopSound(motorSound);
         

            //Arrow
            RIGHT_ARROW = bb.LoadImage("Images/Flecha avanzar.jpg");
            bb.ScaleImage(RIGHT_ARROW, 0.5f, 0.5f);
            bb.MidHandle(RIGHT_ARROW);
            int channel = bb.EmitSound(motorSound, car); 
            while(bb.KeyDown (bb.KEY_ESCAPE) == 0)
            {
                bb.Cls();
                keyMovements();
                bb.UpdateWorld();
                if (bb.ChannelPlaying(channel) == 0)
                {
                    channel = bb.EmitSound(motorSound, car);


                }
                bb.RenderWorld();
                if (!FULL_SCREEN)
                {
                    bb.DrawImage(img, 64, 64);
                    bb.CameraViewport(CAMERA, 64, 490, 896, 202);
                }
                else 
                {
                    bb.CameraViewport(CAMERA, 64, 64, 896, 640);
                
                }
                bb.DrawImage(RIGHT_ARROW, 930, 736);
            
                bb.Flip();
                //bb.Cls();
            }

            bb.EndBlitz3D();
        }
        
        /// <summary>
        /// Set the graphic mode
        /// </summary>
        static private void setGraphicMode() 
        {
            bb.Graphics3D(WIDTH, HEIGHT, DEPTH, GRAPHIC_MODE);

            bb.SetBlitz3DTitle("Práctico 02", "¿Seguro desea salir del juego?");
        }

        /// <summary>
        /// Set the text and the font of the game
        /// </summary>
        static private void setTextForm() 
        {
            int font = bb.LoadFont("tahoma",FONT_HEIGHT, FONT_BOLD, FONT_ITALIC);
            bb.SetFont(font);

            bb.Color(FONT_COLOR_RED, FONT_COLOR_GREEN, FONT_COLOR_BLUE);
        }

        public static void keyMovements()
        {
            if (bb.KeyDown(bb.KEY_UP) == 1)
            {
                bb.MoveEntity(CAMERA, 0, 0, 1);
            }
            if (bb.KeyDown(bb.KEY_DOWN) == 1)
            {
                bb.MoveEntity(CAMERA, 0, 0, -1);
            }
            if (bb.KeyDown(bb.KEY_LEFT) == 1)
            {
                bb.TurnEntity(CAMERA, 0, 1, 0);
            }
            if (bb.KeyDown(bb.KEY_RIGHT) == 1)
            {
                bb.TurnEntity(CAMERA, 0, -1, 0);                
            }
            if (bb.KeyDown(bb.KEY_A) == 1)
            {
                bb.MoveEntity(CAMERA, 0, 1, 0);
            }
            if (bb.KeyDown(bb.KEY_Z) == 1)
            {
                bb.MoveEntity(CAMERA, 0, -1, 0);
            }
            if(bb.KeyDown(bb.KEY_M) == 1)
            {
                FULL_SCREEN = !FULL_SCREEN;
            }
        }

    }
}

Thanks, Carolina.


_PJ_(Posted 2010) [#2]
I think this probably ought to be in the Blitz3DSDK forums or something.. but anyway, Nothing seems to stand out in your code which could cause this problem.

The only thing I can really consider is that the sound here:

            int motorSound = bb.bbLoadSound("Sounds/hotidle.wav");

maybe it should be
int motorSound=bb.Load3DSound("Sounds\hotidle.wav")

Though if you have it working at least to somehow pan the sound when you rotate the camera then it may not be important.

Perhaps the actual parameters for 3D sounds, i.e. Rolloff# values may be what's causing the issue?


eos(Posted 2010) [#3]
Yes.. i didnt see the Blitz3DSDK forum before posting,sorry...
I just found that forum now, i was looking for the bb.Load3DSound function (because i couldnt see it in my SDK) and i saw a post there saying that in Blitz3DSDK the bb.Load3DSound doesnt exists and there is only bb.LoadSound which works for everything.

So that can't be.

The other posibility doesn't seem to be the problem, i have played with the parameters of CreateListener from 0.001 to 10000 and it doesn't make any difference when i move away or closer to the emiter; the volume keeps the same...

thanks for replying Malice -puki, this is the first time i post in a forum!


Matty(Posted 2010) [#4]
Are you using a 'mono' sound? I've heard that 3d sounds have to be mono, not stereo sounds.


stanrol(Posted 2010) [#5]
No indentation; try it!


eos(Posted 2010) [#6]
Yes, i'm using a mono sound....

=(


Who was John Galt?(Posted 2010) [#7]
A mono sound is what you need for 3D sounds. It is basically working as a 3D sound because you have the panning effect. Try hacking your code so you can move a very large distance from the sound source quickly. Does this affect the volume? Is it possible to print the coordinates of a listener? If so, try this to ensure it's not somehow stuck next to the source.


Who was John Galt?(Posted 2010) [#8]
Sorry, I don't use B3D, so I may be barking up the wrong tree, but does bb.loopsound set a sound into looping mode, or does it just play a looped sound? Maybe your bb.loopsound is just playing a looped (non localised) sound?


_PJ_(Posted 2010) [#9]
Incidentally, the use of LoopSound does seem rredundant since this section is included:

if (bb.ChannelPlaying(channel) == 0)
{
channel = bb.EmitSound(motorSound, car);


}
But that doesn't seem to have any bearing on the issue of 3D distance affecting the sound.


Who was John Galt?(Posted 2010) [#10]
Dang, wish I had B3D here...

Okay, start by cutting out the redundant code Malice identified. Do you still get the panning effect?


eos(Posted 2010) [#11]
You are right, the loopsound is redundant. I forgot to delete it after i implemented a manual loop to see if the problem was there.

I will try what you say John, it could be that the camera isnt really moving...

I will delete the redundant part and print the coordinates onscreen while i move away very fast, and i'll be back with the news...

Carolina


eos(Posted 2010) [#12]
Ok, i removed the loopsound function, and i printed the coordinates.

I moved away 4.5E+07 units and the sound keeps on the same level!!!

I tried moving in all the coordinates and nothing.

I also noticed that even when im very far away from the emiter, if I turn around the camera with the listener the sound gets panned...

I'm lost, any suggestions will be apprecieted.

I will try with another sound, or another emitter...

Carolina


_PJ_(Posted 2010) [#13]
The only other thing I can really think of is the parameters for CreateListener.


CreateListener ( parent[,rolloff_factor#][,doppler_scale#][,distance_scale#]


In your code so far,
int t = bb.CreateListener(CAMERA);

you don't set any of these parameters, so they will be at their defaults which in theory SHOULD work fine, but it may well be worth playing with some values here just in case (Especially distance_scale# and rollof_factor#)


Who was John Galt?(Posted 2010) [#14]
Good point, Malice.

If this doesn't work, Eos, post the latest version of your code so we can see what we have. You could also try initialising your camera at a greater distance from the car to see what happens.


_PJ_(Posted 2010) [#15]
oops, I just read this bit :(


The other posibility doesn't seem to be the problem, i have played with the parameters of CreateListener from 0.001 to 10000 and it doesn't make any difference when i move away or closer to the emiter; the volume keeps the same...




eos(Posted 2010) [#16]
Ok, i have created a cube with other mono sound and the behaviour is the same... I have increased the ratios of CreateListener to the maximum while i move at a great speed away from the emiter, and NOTHING....

Don't worry Malice, i really appreciate the help you are giving me. =)

What i have done to solve the problem momentaneously is a manual function of update channel volume, very simple and very sad...

Here is the code, maybe you can try it with the box part that is commented and see if the same happens to you.
using System;
using bb = Blitz3DSDK;

namespace Clase01
{
    static class ClaseMain
    {
        //--GAME CONSTS------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------------------
        const int WIDTH         = 1024;
        const int HEIGHT        = 768;
        const int DEPTH         = 32;
        const int GRAPHIC_MODE  = bb.GFX_WINDOWED;

        const int FONT_HEIGHT       = 24;
        const int FONT_BOLD         = 1;
        const int FONT_ITALIC       = 0;
        const int FONT_UNDERLINED   = 1;
        const int FONT_COLOR_RED    = 255;
        const int FONT_COLOR_GREEN  = 255;
        const int FONT_COLOR_BLUE   = 255;

        static int CAMERA           = 0;
        static int LEFT_ARROW       = 0;
        static int RIGHT_ARROW      = 0;

        static bool FULL_SCREEN     = false;
        static int CHANNEL          =0;

       
        static double distAnterior = 0;
        static float volume = 1;

        //--METHODS----------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------------------
        
        [STAThread]
        static void Main() 
        {
            bb.BeginBlitz3D();
                        
            setGraphicMode();

                  
          
            //Camera
            CAMERA = bb.CreateCamera();
            bb.CameraClsColor(CAMERA, 189, 224, 251);
            bb.PositionEntity(CAMERA, -3, 1,-10);
            bb.AmbientLight(255, 255, 255);
            bb.CameraViewport(CAMERA, 64, 490, 896, 202);
            bb.ClsColor(0, 0, 0);
            //Camera Listener
            int t = bb.CreateListener(CAMERA);

            //Ground
            int plane = bb.CreatePlane();
            int texture = bb.LoadTexture("Images/ground2.jpg");
            bb.ScaleTexture(texture, 8, 8);
            bb.EntityTexture(plane, texture);

            //Imagen de fondo
            int img = bb.LoadImage("Images/FondoComic.png");
            bb.MaskImage(img, 1, 0, 0);
            
            //Car
            int car = bb.LoadMesh("nova/nova.3ds");
            bb.ScaleEntity(car, 0.01f, 0.01f, 0.01f);
            bb.PositionEntity(car, 0, 0f, 0);
            bb.RotateEntity(car, 0, -90, 0);
            
            //Sounds
            int motorSound = bb.bbLoadSound("Sounds/hotidle.wav");
            bb.LoopSound(motorSound);
           
         
            ////box
            //int box = bb.CreateCube();
            //bb.PositionEntity(box, 1, 1, 44);
            //int music = bb.LoadSound("Sounds/ontarget.ogg");

            //Trees
            int tree = bb.LoadSprite("Images/tree.tga",2);
            bb.ScaleSprite(tree, 3, 3);
            bb.PositionEntity(tree, 10, 3,10);
            bb.PositionEntity(bb.CopyEntity(tree), 7, 3, 20);
            bb.PositionEntity(bb.CopyEntity(tree), -3, 3, 23);
            bb.PositionEntity(bb.CopyEntity(tree), -8, 3, 23);
            bb.PositionEntity(bb.CopyEntity(tree), -15, 3, 23);
            bb.PositionEntity(bb.CopyEntity(tree), -10, 3, 43);
            bb.PositionEntity(bb.CopyEntity(tree), 14, 3, 17);

            //Arrow
            RIGHT_ARROW = bb.LoadImage("Images/Flecha avanzar.jpg");
            bb.ScaleImage(RIGHT_ARROW, 0.5f, 0.5f);
            bb.MidHandle(RIGHT_ARROW);

            LEFT_ARROW =bb.CopyImage(RIGHT_ARROW);
            bb.RotateImage(LEFT_ARROW, 180);

           int cover =  bb.LoadImage("Images/cover.jpg");
            string pantalla = "cover";
            
            while(bb.KeyDown (bb.KEY_ESCAPE) == 0)
            {
                bb.Cls();
                if (pantalla == "comic")
                {
                    keyMovements();

                    if (!FULL_SCREEN)
                    {
                        bb.DrawImage(img, 64, 64);
                        bb.CameraViewport(CAMERA, 64, 490, 896, 202);
                    }
                    else
                    {
                        bb.CameraViewport(CAMERA, 64, 64, 896, 640);

                    }


                    bb.UpdateWorld();
                    bb.RenderWorld();

                }
                else {

                    bb.DrawImage(cover, 0, 0);
                    if (bb.KeyHit(bb.KEY_RIGHT) > 0) {
                        pantalla = "comic";
                        CHANNEL = bb.EmitSound(motorSound, car); 
                    
                    }
                }
                bb.Text(0, 0, "car: x:" + (bb.EntityX(car).ToString()) + " y:" + bb.EntityY(car) + " z:" + bb.EntityZ(car));
                bb.Text(0, 20, "camera: x:" + (bb.EntityX(CAMERA).ToString()) + " y:" + bb.EntityY(CAMERA) + " z:" + bb.EntityZ(CAMERA));
                bb.Text(0, 45, volume.ToString());

                bb.DrawImage(RIGHT_ARROW, 930, 736);
                bb.DrawImage(LEFT_ARROW, 91, 736);
                
                bb.Flip();
                //bb.Cls();
            }

            bb.EndBlitz3D();
        }


        static private void changeVolume(int chann) {

            double dist = Math.Sqrt( bb.EntityX(CAMERA) * bb.EntityX(CAMERA) + bb.EntityY(CAMERA) * bb.EntityY(CAMERA) + bb.EntityZ(CAMERA) * bb.EntityY(CAMERA));
            
            if (dist > distAnterior) {
               volume= volume - 0.05f;
                bb.ChannelVolume(chann,volume );
            }
            if (dist < distAnterior) {
                volume = volume + 0.05f;
                bb.ChannelVolume(chann,volume);            
               
            }
            if (volume < 0.05) volume = 0;
           

            distAnterior = dist;
            

         }
        /// <summary>
        /// Set the graphic mode
        /// </summary>
        static private void setGraphicMode() 
        {
            bb.Graphics3D(WIDTH, HEIGHT, DEPTH, GRAPHIC_MODE);

            bb.SetBlitz3DTitle("Práctico 02", "¿Seguro desea salir del juego?");
        }

        /// <summary>
        /// Set the text and the font of the game
        /// </summary>
        static private void setTextForm() 
        {
            int font = bb.LoadFont("tahoma",FONT_HEIGHT, FONT_BOLD, FONT_ITALIC);
            bb.SetFont(font);

            bb.Color(FONT_COLOR_RED, FONT_COLOR_GREEN, FONT_COLOR_BLUE);
        }

        public static void keyMovements()
        {
            if (bb.KeyDown(bb.KEY_UP) == 1)
            {
                bb.MoveEntity(CAMERA, 0, 0, 1);
                changeVolume(CHANNEL);
            }
            if (bb.KeyDown(bb.KEY_DOWN) == 1)
            {
                bb.MoveEntity(CAMERA, 0, 0,-1);
                changeVolume(CHANNEL);
            }
            if (bb.KeyDown(bb.KEY_LEFT) == 1)
            {
                bb.TurnEntity(CAMERA, 0, 1, 0);
            }
            if (bb.KeyDown(bb.KEY_RIGHT) == 1)
            {
                bb.TurnEntity(CAMERA, 0, -1, 0);                
            }
            if (bb.KeyDown(bb.KEY_A) == 1)
            {
                bb.MoveEntity(CAMERA, 0, 1, 0);
                changeVolume(CHANNEL);
            }
            if (bb.KeyDown(bb.KEY_Z) == 1)
            {
                bb.MoveEntity(CAMERA, 0, -1, 0);
                changeVolume(CHANNEL);
            }
            if(bb.KeyDown(bb.KEY_M) == 1)
            {
                FULL_SCREEN = !FULL_SCREEN;
            }
           
        }

    }
}


I will try what you suggest John, and create the camera far far away.

I'll be back with updates, this has become a matter of honor!


_PJ_(Posted 2010) [#17]
I'm beginning to wonder if this might be something not related to your program but more internal, such as DirectX or maybe even sound/graphics hardware?

Are you using Windows 7 at all maybe?
Not that I have any answers or know anything specifically, but it all seems very strange that this code isn't working right. :S


Who was John Galt?(Posted 2010) [#18]
I don't have B3D anymore, but don't you have to create an emitter or something? You're just looping a sound... how does blitz know that the sound is coming from the car?


Who was John Galt?(Posted 2010) [#19]
Ah... I see in your first version you were using bb.emitsound. This is what you need to use.

So, make the sound from bb.emitsound looping (or replay it multiple times if the is no loop option) and remove the bb.loopsound line.


eos(Posted 2010) [#20]
Yes, i tried that....

I made that handcrafted loop function and commented the loopsound function, to see if the problem was there.
That was what happened when Malice spoted that the loopsound funcion was redundant.
So I already know that's not the problem...

I will try the code in other computer. You're right Malice that it could be somithing wrong with the Direct X.

Currently I'm working in a msi netbook with windows XP...

Thanks again, i will post again with news
Carolina


eos(Posted 2010) [#21]
So, I tried the code in another computer much more powerful than mine and the problem persists....

Any more ideas anyone?

If no, do you know if there's a way to contact someone of the blitz company to let him know of this issue?

What do you think i should do?

Carolina


_PJ_(Posted 2010) [#22]
I am all out of ideas for the main problem itself. I would suggest posting something in the BlitzSDK forums, since the users there will have more experience and can test stuff with blitzsdk (I only have blitz3D myself). If you get no luck there, then a post in the Bug Forums will alert the company to the issue :)


Tab(Posted 2010) [#23]
What is the sample format of your sound?

Try with 16 bits if your format is higher.


dlgebert(Posted 2010) [#24]
.


dlgebert(Posted 2010) [#25]
.


jfk EO-11110(Posted 2010) [#26]
Someone may remove the "bb." and other non-plain-blitz3D related stuff and then run it in Standalone Blitz3D. If it's ok there then there must be a diffrence between the SDK version and the standalone version of Blitz3D.


_PJ_(Posted 2010) [#27]
Well I did a quick test with the following (rough translation to B3D) :
        Const WIDTH         = 1024;
        Const HEIGHT        = 768;
        Const DEPTH         = 32;

	Const KEY_UP=200
	Const KEY_DOWN=208
	Const KEY_LEFT=203
	Const KEY_RIGHT=205
	Global channel
        Global CAMERA           = 0;

Graphics3D WIDTH,HEIGHT,DEPTH,7
		
            CAMERA = CreateCamera();
            CameraClsColor(CAMERA, 189, 224, 251);
            PositionEntity(CAMERA, 0, 2, 0);
            AmbientLight(255, 255, 255);
            CameraViewport(CAMERA, 64, 490, 896, 202);
            ClsColor(255, 255, 255);
            ;//Camera Listener
           t = CreateListener(CAMERA);

            ;//Ground
            plane = CreatePlane();
            ;Int texture = LoadTexture("Images/ground2.jpg");
           ; ScaleTexture(texture, 8, 8);
            ;EntityTexture(plane, texture);

           ; //Imagen de fondo
           ;img = LoadImage("Images/FondoComic.png");
            ;MaskImage(img, 1, 0, 0);
            
            ;//Car
            car = CreateCube();
            ScaleEntity(car, 0.1, 0.1, 0.1);
            PositionEntity(car, 0, 0, 14);
            RotateEntity(car, 0, -90, 0);
            
            ;//Sounds
            motorSound = Load3DSound("Engine.wav");
            LoopSound(motorSound);
         
            ;//Arrow
           ; RIGHT_ARROW = LoadImage("Images/Flecha avanzar.jpg");
           ; ScaleImage(RIGHT_ARROW, 0.5f, 0.5f);
           ; MidHandle(RIGHT_ARROW);
           channel = EmitSound(motorSound, car); 
            While (KeyDown (1) = 0)
                keyMovements();
                UpdateWorld();
                If (ChannelPlaying(channel) = 0) Then channel = EmitSound(motorSound, car);
                RenderWorld();
                Flip();
 	Wend
End

Function keyMovements()
        
            If (KeyDown(KEY_UP) = 1) Then                 MoveEntity(CAMERA, 0, 0, 1);

            If (KeyDown(KEY_DOWN) = 1) Then                 MoveEntity(CAMERA, 0, 0, -1);

            If (KeyDown(KEY_LEFT) =1) Then             TurnEntity(CAMERA, 0, 1, 0);

            If (KeyDown(KEY_RIGHT) = 1) Then                TurnEntity(CAMERA, 0, -1, 0);                
End Function



For me, though.. I didn't get any sound at all.
One thing I did notice, though, was the use of "channel". Going purely by the B3D docs, there's no mention of using a Channel handle or a return value for EmitSound whatsoever, subsequently, when I run the above, "channel" always returns 0. Though this highlights some difference between B3D and presumably the SDK, it doesn';t seem to shed any real light on eos' problem :S


eos(Posted 2010) [#28]
Tab: The bit rate of the sound i'm using is 8bit...

Malice: thank you very much for taking the time to make the test!!!!

I checked the docs for the sdk and it says that the emitSound function returns a channel for future use...

If you dont hear anything and the channel returned is zero i think it means that the sound didnt load properly or something like that.... also it should be a mono sound and maybe there's a problem with the CreateListener parameters....

Anyway, i think i will give up on this one... =(

Thank you all for your time

Carolina


_PJ_(Posted 2010) [#29]
Sorry to hear that you didn't get a solution, eos.
It was no bother making the test, it was quite straightforward really, aside from some little syntax changes things were mostly the same.

Did you get any help from the actual BlitzSDK forums? Because it seems your code is fine, your sound is okay so the issue must be with the SDK or with your computer (windows possibly?) and maybe someone else has had the same problem too?
If it's a common problem, then there's likely more chance of a workaround or a fix made than if it's some ery unusual one-off kinda bug.