read listener position from ASCII file

E7d676561a0f1a6951b374ec9cd46e46
0
stranomavero 101 Sep 04, 2007 at 16:27

hello to everyone!

i’m studying the tutorial http://www.devmaster.net/articles/openal/ and i’m trying to modify it a bit,in order to fit it to my purpouses.
I’ve already got a 3D environment made with OGRE; it outputs the position of the listener as an ASCII file.
How can i make the code for the sound read that position?

any kind of tips and suggestions would be extremely precious…i’m groping in the dark! :)

16 Replies

Please log in or register to post a reply.

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Sep 04, 2007 at 16:31

You could use the Standard Input / Output Streams Library (a.k.a iostreams), or did I misunderstand your question? In any event, using a file to pass this information seems horribly inefficient.

E7d676561a0f1a6951b374ec9cd46e46
0
stranomavero 101 Sep 05, 2007 at 09:29

i’m sure it is really inefficient,but the problem is that the 3D environment was made by another department(i’m doing a PhD in a college) and i’m supposed to add the audio.
I have only the final product made with OGRE and not the code.
I’ll mention it to them,to see if we can avoid this bottle neck.

I’m a newbie in programming,so can you give few more tips?
what’s the command to read the coordinates from the file?

E7d676561a0f1a6951b374ec9cd46e46
0
stranomavero 101 Sep 05, 2007 at 09:29

ps:thank you very much for you attention! :)

B7568a7d781a2ebebe3fa176215ae667
0
Wernaeh 101 Sep 05, 2007 at 12:57

Hey there :)

That actually depends on how your file is formatted.

Say you have got a simple whitespace terminated ASCII file with three standard format IEEE floating point numbers in it, such as

0.73 0.1112 10.45

Then, you can read in your three coordinates like this

#include <fstream> // Header with std::[i,o]fstream classes

/* .... */

std::ifstream myfile; // Create a new input file stream
myfile.open("FILEDIR\\Filename.txt"); // Connect file stream to file

double x, y, z; // Coordinates
myfile >> x; // Read them out, one by one, whitespace automatically is ignored
myfile >> y;
myfile >> z;

Reading a different format (CSV or similar) might take a bit more time for implementation.

Cheers,
- Wernaeh

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Sep 05, 2007 at 15:03

@stranomavero

I’m a newbie in programming,so can you give few more tips?

Could you post a small example of the ASCII file in question?

E7d676561a0f1a6951b374ec9cd46e46
0
stranomavero 101 Sep 06, 2007 at 10:40

first of all,thank u guys for the quick replies!!

this is the format:

x315346.304077 z233442.343323 a25.650650

the user has can’t change the height (it’s fixed by default at 4m) but only the 2 dimensions of an horizontal plane x,z and the angle a

B7568a7d781a2ebebe3fa176215ae667
0
Wernaeh 101 Sep 06, 2007 at 12:40

Well, this makes things slightly more complicated.

If its just for a small project (i.e. no code reuse, no need for robustness), I’d suggest the following:

std::ifstream infile;
infile.open("filename");

float coordinates[3];
for (int i = 0;
      i < 3;
      ++i)
{
              infile.get();
              infile >> coordinates[i];
}

Note this assumes that there are no errors within the source file (i.e. leading char always present). For a more robust version, invent some sort of error checking on your own :)

Cheers,
- Wernaeh

E7d676561a0f1a6951b374ec9cd46e46
0
stranomavero 101 Sep 06, 2007 at 12:49

thank u very much!!!
it might be a stupid question,but i don’t get how the code you gave me “understands” the difference between the coordinate of a plan (x,z) and the orientation a.

thanks and sorry for bothering you again

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Sep 06, 2007 at 16:25

infile.get(); removes the leading character and infile >> coordinates;* parses the following floating point number. It doesn’t know the difference between xza, rather he is using the known order of the variables.
*

E7d676561a0f1a6951b374ec9cd46e46
0
stranomavero 101 Sep 06, 2007 at 16:41

cool!
i understand.
how should i tell the computer that a is the angle between the user’s view and the axis north-south? it was meant for the partition of the sound between the left and right earphone.
do you think it is useless or i don’t need it?

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Sep 06, 2007 at 21:26

Is the user always at the origin?

E7d676561a0f1a6951b374ec9cd46e46
0
stranomavero 101 Sep 07, 2007 at 10:12

yes,the user is always in the middle of the screen

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Sep 07, 2007 at 15:36

OK, I’ve never used OpenAL, but I imagine you will place the listener at the origin and then use the angle from the file to provide a direction vector for it. In plain English: the library needs to know which way the listener is facing in order to correctly place the sound relative to it in 3D.

EDIT: Alternatively, you could transform your x and z coordinates by rotating them about the origin by a if you only have a single source.

E7d676561a0f1a6951b374ec9cd46e46
0
stranomavero 101 Sep 07, 2007 at 15:45

i think i’m giving up to openal…i can’t fix a problem with alut.
anyway i have more than one source but i’ll try to understand the facing thing by studying a piece of code i found! :)

so do you think it will work reading the ASCII file that way and then defining the angle as you said?

ps:thank you very much for the explanation in plain english…at the present time it’s the only language i can understand…C is still too obscure!!! :)

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Sep 07, 2007 at 15:54

irrKlang just reached version 1.0. It’s from the creator of the Irrlicht engine. Perhaps it’s easier to use than OpenAL…

E7d676561a0f1a6951b374ec9cd46e46
0
stranomavero 101 Sep 10, 2007 at 10:57

thank you very much!
i’m gonna try that engine right now.