Sign in to follow this  

Unable to play WAV with OpenAL

This topic is 4715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First off, this error confuses me; because every time I run the program(as you'll see in the code), the program returns zero as if everything went correctly. However, I can't find, within both the tutorial on OpenAL on Gamedev, or the docs on OpenAL what might fix the problem. In all actuality, I have no clue what the problem IS, all I know is that the song that both exists, and is playable in media player and winamp. It isn't playing in my program, I don't get it..very weird to me. Here's my source, it compiles perfectly in VC++ 7.
#include "includes.h"

struct sourceAttrib
{
	ALfloat position[3];
	ALfloat velocity[3];
	ALfloat direction[3];
};
struct listenerAttrib
{
	ALfloat position[3];
	ALfloat velocity[3];
	ALfloat orientation[3];
};
 sourceAttrib source_01;
 listenerAttrib listener;


int logError(int lineNumber);
void defineSources();
void defineListener();

#pragma comment(lib, "Alut.lib")
#pragma comment(lib, "OpenAL32.lib")

int main()
{
	ALsizei size;
	ALsizei freq;
	ALenum format;
	ALvoid *data;
	ALboolean loop;

	defineSources();
	defineListener();

alutInit(0,NULL);
  alGetError();

  alutLoadWAVFile("d:\\AIM\\Sounds\\moo.wav",&format,&data,&size,&freq,&loop);
  if((logError(63)) == 1)
    return 1;

	ALuint buffers[1];
	 alGenBuffers(1, buffers);
	 alBufferData(buffers[0],format,data,size,freq);
  if((logError(69)) == 1)
    return 1;

	 alutUnloadWAV(format,data,size,freq);

  ALuint sources[1];
	 alGenSources(1,sources);
	  alSourcei(sources[0],AL_BUFFER,buffers[0]);
	   alSourcefv(sources[0],AL_POSITION,source_01.position);
	   alSourcefv(sources[0],AL_VELOCITY,source_01.velocity);
		 alSourcefv(sources[0],AL_DIRECTION,source_01.direction);
  if((logError(80)) == 1)
    return 1;

  alListenerfv(AL_POSITION, listener.position);
	alListenerfv(AL_VELOCITY, listener.velocity);
	alListenerfv(AL_ORIENTATION, listener.orientation);
  if((logError(86)) == 1)
    return 1;

	alSourcePlay(sources[0]);
  if((logError(90)) == 1)
    return 1;
  
	 alDeleteSources(1,sources);
	 alDeleteBuffers(1,buffers);
   if((logError(96)) == 1)
     return 1;

alutExit();
return 0;
}


int logError(int lineNum)
{
	ALenum error;
	if((error = alGetError()) != AL_NO_ERROR)
  {
	   std::ofstream logFile;
	    logFile.open("errorlog.txt");
			logFile << "Error returned: " << alGetString(error) 
				      << " on line " << lineNum << "\n";
      logFile.close();
    return 1;
  }
	else
		return 0;
}

void defineSources()
{
	for(int i = 0; i < 3; i++)
	{
	 source_01.position[i] = 0.0f;
	 source_01.velocity[i] = 0.0f;
	 source_01.direction[i] = 0.0f;
	}
}

void defineListener()
{
	for(int i = 0; i < 3; i++)
	{
	 listener.position[i] = 0.0f;
	 listener.velocity[i] = 0.0f;
	 listener.orientation[i] = 0.0f;
  }
}


[Edited by - Mezmirous on January 18, 2005 7:27:06 PM]

Share this post


Link to post
Share on other sites
Here is a great tutorial on getting OpenAL to work. It even comes with source code for you to modify. Take a look at that and compare your code with it.

However - You do not give the program a chance to play the sound - you exit right after it starts to play. Replace:


alSourcePlay(sources[0]);


with


alSourcePlay(sources[0]);
while(1)
{

}


I hope this helps!

- Drew

[edit] Wrong order lol

Share this post


Link to post
Share on other sites
Thanks for your help so far :)

Only problem to deal with now is how to stop the program from looping forever, replaying the last .3-4 seconds of the sound infinitely after it's played the sound once. Do you have any theories or code examples on how to do this? I'de wager it involves controlling the while loop by relating cpu ticks and the duration of the song, but I've never worked with anything like this before o.o

Also, I was wondering if you could help me with the optimization of my code. It currently takes my computer 2-3 seconds to start playing the sound. I have a decent computer, one that lives up to todays standards, and I'm not running that many programs either, nothing that intensive.

The thing is, I can understand why it takes so long to play the song. It has to calculate all the source/listener perspectives, load the sound, and render the sound taking into account the modifications, there's alot of code there that needs to process. Although, I can't help but wonder..I missing something? Is OpenAL really this slow on my computer?

Thanks for the help,
~Mezmirous

Share this post


Link to post
Share on other sites
No problem! Sorry for the delay lol, doing a billion things at once. Well first off - what are your system specs, os, etc...

1. I beleive orientation should be an array of 6 values it should be setup like this:


ALfloat listenerOri[]= { 0.0, 0.0, 1.0, 0.0, 1.0, 0.0 }; // LookAt then Up

//or in your case:

listener.orientation[0] = 0.0f;
listener.orientation[1] = 0.0f;
listener.orientation[2] = 1.0f;
listener.orientation[3] = 0.0f;
listener.orientation[4] = 1.0f;
listener.orientation[5] = 0.0f;


The next thing is how big is this sound you are using? Larger sounds will take larger times to load then play. It also depends on your sound card.

As for what you want to do about the properties of the sound, take a look at these tutorials. They show examples and explain lots of OpenAL. Anything that I could tell you would probabally come from these tutorials as well as the few on GameDev.

That Lesson 1 shows you exactly what you need in terms of not making the program run forever, so definitly have a look! If you need any further help feel free to ask [smile]

- Drew

Share this post


Link to post
Share on other sites

This topic is 4715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this