Spatial / 3D sounds

Hey everyone,

I am making a program in which you move a head around in a 3D space. Placed around this head are sound sources, the sound the user hears through their headphones should be a heard as if the virtual head would be their own head.

So if the sound source is far away the volume should be lower, when the sound source is placed next to the right side of the virtual head, the user should experience it as if there is an actual sound source next to their right ear. If the sound source moves around the head the user should experience that as well.

This video shows pretty much what I want to accomplish:
http://www.youtube.com/watch?v=P-87i9rLwZE
However, I think that the person that made this video uses sounds that are recorded with special hardware that enables them to record the sounds spatially. Is it possible to create a similar program that allows me to place any regular sound around the virtual head and make it sound spatial?

Any help would be very appreciated!
openAl allows you to dictate the location and velocity of sound sources.
I know DirectX 9 used to have support for this using DirectSound, but IIRC they planned on replacing DirectSound with XAudio2 at some point...so I'm not sure whether DirectX 10 has it, if not then XAudio2 can probably help you out anyway.
Last edited on
Thanks for the replies. I decided to try out openAl first since it seems easier to get started with. However, I've encountered a problem when following the tutorials provided here:
http://www.devmaster.net/articles/openal-tutorials/lesson1.php
I can't seem to figure out how to add directories in visual C++, i've tried some visual C++ tutorials, but they don't fully solve my problems.

I'm using Microsoft Visual C++ 2010 express edition, I use a Win32 console application. In application settings I chose 'console application' for application type and I chose empty project in additional options.

I then added a C++ file to the 'Source Files' folder of my project and put the code from the site in there. all the #include <....> from al/al.h down are marked in red. I've followed this:
http://mrl.nyu.edu/~dzorin/igf06/lecture10/OpenAL_install.txt
but even that won't help. After I did that I noticed I could remove the 'al' from al/al.h and al/alc.h and they would not be marked in red anymore, but the same doesn't work for the other ones since they don't exist in the specified folder.
Does anyone know what I'm doing wrong?

It is probably not in the right directory or you did not link Visual Studios to it. You have to link your libraries (.lib) and include (.h) files from OpenAL. To do this requires a setting in Visual Studios. In 2010 you have two options:

OPTION 1: All you need to do is go to Project -> <Project Name> Properties -> Configuration Properties -> VC++ Directories. This will tell Visual Studios what directories to look into for your libraries and header files. Change the properties of Include Directories and Library Directories to the location. This is usually under Program Files -> OpenAL <Version Number> SDK -> include for the include directory and Program Files -> OpenAL <Version Number> SDK -> libs for the library files.

OPTION 2: Copy and paste OpenAL's libraries and headers to Visual Studio Directories. This will save from the hassle of always having to do option 1. These are usually located in the Program Files under OpenAL <Version Number> SDK -> include and OpenAL <Version Number> SDK -> libs. Copy everything in these folders and paste to Program Files -> Microsoft Visual Studio 10.0 -> VC -> include or Program Files -> Microsoft Visual Studio 10.0 -> VC -> lib, respectively (i.e. include folder to include folder and libs to lib folder). Make sure you copy and paste exactly as they are. Notice that you have a al/al.h which assumes there is a folder called ah and that the header file al.h is contained within it.

let me know if this helps or you have any questions.
I did both options and Option2 seems to get me the furthest, it still gives me 10 errors though:
http://imageupload.org/?di=812984967810

I copied everything from
C:\Program Files\OpenAL 1.1 SDK\include to C:\Program Files\Microsoft Visual Studio 10.0\VC\include
and everything from
C:\Program Files\OpenAL 1.1 SDK\libs\Win32 to C:\Program Files\Microsoft Visual Studio 10.0\VC\lib

and I downloaded freealut-1.1.0-src which included the folder AL with the file alut.h in it, I copiedthat to C:\Program Files\Microsoft Visual Studio 10.0\VC\include

This is the source code I'm using:
The only things I changed are
change conio.h to iostream
change getche to getchar
(this was done in a tutorial I found)

I commented out #include <alu.h> because I can't find that file anywhere


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>  //changed from conio.h
#include <stdlib.h>
#include <al.h>
#include <alc.h>
//#include <alu.h> //does not exist..
#include <al/alut.h>


ALuint Buffer;
ALuint Source;
ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };
ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };

ALboolean LoadALData()
{

    ALenum format;
    ALsizei size;
    ALvoid* data;
    ALsizei freq;
    ALboolean loop;


 
    alGenBuffers(1, &Buffer);
    if (alGetError() != AL_NO_ERROR)
        return AL_FALSE;

    alutLoadWAVFile("wavdata/FancyPants.wav", &format, &data, &size, &freq, &loop);
    alBufferData(Buffer, format, data, size, freq);
    alutUnloadWAV(format, data, size, freq);

 
    alGenSources(1, &Source);

    if (alGetError() != AL_NO_ERROR)
        return AL_FALSE;

    alSourcei (Source, AL_BUFFER,   Buffer   );
    alSourcef (Source, AL_PITCH,    1.0f     );
    alSourcef (Source, AL_GAIN,     1.0f     );
    alSourcefv(Source, AL_POSITION, SourcePos);
    alSourcefv(Source, AL_VELOCITY, SourceVel);
    alSourcei (Source, AL_LOOPING,  loop     );

    
    if (alGetError() == AL_NO_ERROR)
        return AL_TRUE;

    return AL_FALSE;



void SetListenerValues()
{
    alListenerfv(AL_POSITION,    ListenerPos);
    alListenerfv(AL_VELOCITY,    ListenerVel);
    alListenerfv(AL_ORIENTATION, ListenerOri);
}



void KillALData()
{
    alDeleteBuffers(1, &Buffer);
    alDeleteSources(1, &Source);
    alutExit();
}



int main(int argc, char *argv[])
{
   

    alutInit(&argc, argv);
    alGetError();

	
    if (LoadALData() == AL_FALSE)
        return -1;

    SetListenerValues();

    atexit(KillALData);

	ALubyte c = ' ';

    while (c != 'q')
    {
        c = getchar(); //changed from getche

        switch (c)
        {
           
            case 'p': alSourcePlay(Source); break;

    
            case 's': alSourceStop(Source); break;

            
            case 'h': alSourcePause(Source); break;
        };
    }

    return 0;
}
You are missing a } after line 53. :P
fixed that, but now it gives me a bunch of different errors :/
http://imageupload.org/?di=12129849770113

Darn. That was my fault. I forgot to tell you to declare your libraries.

You can use pragma comment
1
2
#pragma comment (lib, "openal32.lib")
#pragma comment (lib, "alut.lib") 


Put that after you #include statements and it should fixed the unresolved errors.
hmmm, I found a way to resolve the first two errors. I added an alut.lib to C:\Program Files\Microsoft Visual Studio 10.0\VC\lib

this file was nowhere to be found in the C:\Program Files\OpenAL 1.1 SDK\libs folder though
Thank you! that did the trick!
I wonder why they were able to do without it in the tutorial.

I really appreciate everyone's help, thank you!
Ok.

Try running the OpenAL installer. It might be that the DLL is not be installed. Go to the SDK folder and go to 'redist' and run oalinst.exe and then rebuild your application to see if it fixes the problem.
it runs perfectly now :D
Good. No problem. Glad you got it fixed. I am very proficient with the OpenAL library, so if you have any questions let me know. I built my own gaming audio library using OpenAL, and it's pretty easy to use for me.
Topic archived. No new replies allowed.