LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2005, 06:05 AM   #1
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Rep: Reputation: 30
OpenAL?


Hello
I've got the following code to play FancyPants.wav:
Code:
	ALuint num = 1;
	soundinfo = new SoundInfo[num];
	alGenBuffers(num, &BufferIndices);
	alGenSources(num, &SourceIndices);
	int error;
	int index = 0;
	soundinfo[index].buffer = BufferIndices;
	soundinfo[index].source = SourceIndices;
	alutLoadWAVFile((ALbyte*)"FancyPants.wav", &soundinfo[index].format, &soundinfo[index].data, &soundinfo[index].size, &soundinfo[index].freq, &soundinfo[index].loop);
	alBufferData(soundinfo[index].buffer, soundinfo[index].format, soundinfo[index].data, soundinfo[index].size, soundinfo[index].freq);
	alutUnloadWAV(soundinfo[index].format, soundinfo[index].data, soundinfo[index].size, soundinfo[index].freq);
	alSourcei(soundinfo[index].source, AL_BUFFER, soundinfo[index].buffer);
	alSourcef(soundinfo[index].source, AL_PITCH, 1.0);
	alSourcef(soundinfo[index].source, AL_GAIN, 1.0);
	float sourcepos[3] = { 0.0, 0.0, 0.0 };
	float listenerpos[3] = { 0.0, 0.0, 0.0 };
	float velocitysource[3] = { 0.0, 0.0, 0.0 };
	float velocitylistener[3] = { 0.0, 0.0, 0.0 };
	float orientation[6] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0};
	alSourcefv(soundinfo[index].source, AL_POSITION, sourcepos);
	alSourcefv(soundinfo[index].source, AL_VELOCITY, velocitysource);
	alSourcei(soundinfo[index].source, AL_LOOPING, false);
	alListenerfv(AL_POSITION, listenerpos);
	alListenerfv(AL_VELOCITY, velocitylistener);
	alListenerfv(AL_ORIENTATION, orientation);
	while(true)
		alSourcePlay(soundinfo[index].source);
But my problem is that it doesn't work.
Can anyone see what's wrong with the code?
Sorry for being sucha noob.
Greetz Hylke
 
Old 05-21-2005, 10:49 AM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I'm still a newb when it comes to OpenAL, but here are a couple of things I noticed off the top of my head:
  • I don't see a call to alutInit anywhere. Maybe that is in another portion of code you didn't include, though.
  • I don't think you need to put the alSourcePlay into an infinite loop like that. You are just going to start playing it again and again and again...

Other than that, it looks pretty similar to some of the test code I did when first trying to get OpenAL to play, though it's arranged a bit differently.
 
Old 05-21-2005, 12:06 PM   #3
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Ok, thanx.
That did the trick.
Hylke
EDIT:
btw, which tutorial are you following?

Last edited by hylke; 05-21-2005 at 12:08 PM.
 
Old 05-21-2005, 02:37 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I don't remember to be honest. I wrote my OpenAL code to play wav files awhile ago and haven't had to do much with it since. I think I just used the OpenAL docs, though.
 
Old 05-22-2005, 03:14 AM   #5
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Oh ok.
 
Old 07-24-2005, 11:42 PM   #6
krock923
Member
 
Registered: Jul 2004
Posts: 171

Rep: Reputation: 30
can anyone post some openAL code that would allow me to just play a sound. I couldn't even edit out the little switch in the tutorial that I found online. This is quite confusing. . .
 
Old 07-25-2005, 10:32 AM   #7
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 31
OpenAngryLlama?? no I know, just kidding.
 
Old 07-25-2005, 10:37 AM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Here's some sample code I threw together as an example. I'm not at home right now so I can't test to make sure it actually works, but it compiles with:

g++ soundtest.cpp -o soundtest -lopenal

Code:
#include <string>
#include <vector>
#include <AL/al.h>
#include <AL/alut.h>

unsigned int LoadWav(const std::string &sFilename);
void PlayWav(unsigned int nWavID,
    const std::vector<float> &vecListenerPosition,
    const std::vector<float> &vecListenerVelocity,
    const std::vector<float> &vecListenerOrientation,
    const std::vector<float> &vecSoundPosition,
    const std::vector<float> &vecSoundVelocity);

int main(int argc, char** argv)
{
    unsigned int nWavID;
    std::vector<float> vecListenerPosition(3);
    std::vector<float> vecListenerVelocity(3);
    std::vector<float> vecListenerOrientation(3);
    std::vector<float> vecSoundPosition(3);
    std::vector<float> vecSoundVelocity(3);

    alutInit(&argc, argv);

    nWavID = LoadWav("somewavefile.wav");

    // Initialize vectors to 0, maybe not necessary, but safe
    for (int c=0;c<3;c++)
    {
        vecListenerPosition[c] = 0;
        vecListenerVelocity[c] = 0;
        vecListenerOrientation[c] = 0;
        vecSoundPosition[c] = 0;
        vecSoundVelocity[c] = 0;
    }

    // set orientation to face down z axis
    vecListenerOrientation[2] = 1;

    PlayWav(nWavID, vecListenerPosition, vecListenerVelocity,
            vecListenerOrientation, vecSoundPosition, vecSoundVelocity);

    return 0;
}

void PlayWav(unsigned int nWavID,
    const std::vector<float> &vecListenerPosition,
    const std::vector<float> &vecListenerVelocity,
    const std::vector<float> &vecListenerOrientation,
    const std::vector<float> &vecSoundPosition,
    const std::vector<float> &vecSoundVelocity)
{
    unsigned int nSource;

    alListenerfv(AL_POSITION, &vecListenerPosition[0]);
    alListenerfv(AL_VELOCITY, &vecListenerVelocity[0]);
    alListenerfv(AL_ORIENTATION, &vecListenerOrientation[0]);

    alGenSources(1, &nSource);
    alSourcef(nSource, AL_PITCH, 1.0f);
    alSourcef(nSource, AL_GAIN, 1.0f);
    alSourcefv(nSource, AL_POSITION, &vecSoundPosition[0]);
    alSourcefv(nSource, AL_VELOCITY, &vecSoundVelocity[0]);
    alSourcei(nSource, AL_BUFFER, nWavID);
    alSourcei(nSource, AL_LOOPING, AL_FALSE);

    alSourcePlay(nSource);
}

unsigned int LoadWav(const std::string &sFilename)
{
    unsigned int nID;
    ALenum eFormat;
    ALvoid *pData;
    ALboolean b;
    ALsizei nSize;
    ALsizei nFreq;

    alGenBuffers(1, &nID);

    alutLoadWAVFile(const_cast<ALbyte*>(reinterpret_cast<const ALbyte*>(
                    sFilename.c_str())), &eFormat, &pData, &nSize, &nFreq, &b);
    alBufferData(nID, eFormat, pData, nSize, nFreq);
    alutUnloadWAV(eFormat, pData, nSize, nFreq);

    return nID;
}
 
Old 07-25-2005, 05:31 PM   #9
krock923
Member
 
Registered: Jul 2004
Posts: 171

Rep: Reputation: 30
thank you very much, however that wouldn't compile for some reason.
Code:
sound.C: In function `void PlayWav(unsigned int, const std::vector<float,
   std::allocator<float> >&, const std::vector<float, std::allocator<float> >&,
   const std::vector<float, std::allocator<float> >&, const std::vector<float,
   std::allocator<float> >&, const std::vector<float, std::allocator<float>
   >&)':
sound.C:55: invalid conversion from `const float*' to `ALfloat*'
sound.C:56: invalid conversion from `const float*' to `ALfloat*'
sound.C:57: invalid conversion from `const float*' to `ALfloat*'
sound.C:62: invalid conversion from `const float*' to `ALfloat*'
sound.C:63: invalid conversion from `const float*' to `ALfloat*'
Maybe I just don't understand the API well enough. I could only find minimal documentation.

Last edited by krock923; 07-25-2005 at 05:33 PM.
 
Old 07-25-2005, 05:49 PM   #10
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Interesting. I wonder if you have a different version of OpenAL than I do with the parameter of the fv version of functions not delcared const... You can try a few things.

1. Do a const_cast to get rid of the "constness" of the vector. Not usually the best thing to do, but in this case those functions SHOULDN'T be modifying the contents.

e.g.
alListenerfv(AL_POSITION, &vecListenerPosition[0]);
becomes:
alListenerfv(AL_POSITION, const_cast<ALfloat*>(&vecListenerPosition[0]));


2. Just do a C-style cast to ALfloat*
e.g.
alListenerfv(AL_POSITION, &vecListenerPosition[0]);
becomes:
alListenerfv(AL_POSITION, (ALfloat*)&vecListenerPosition[0]);

3. Switch to the 3f version
e.g.
alListenerfv(AL_POSITION, &vecListenerPosition[0]);
becomes:
alListener3f(AL_POSITION, vecListenerPosition[0], vecListenerPosition[1], vecListenerPosition[2]);

4. Just change my functions to take in ALfloat pointers and pass them directly to the function.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenAL and dmix Belegdol Linux - Software 0 11-01-2005 04:40 AM
OpenAl not working Suse9.3 usaf_sp SUSE / openSUSE 0 10-03-2005 12:27 PM
OpenAL Problems. DarkestHour Linux - Games 1 12-01-2004 09:08 AM
Threads and OpenAL clb Programming 4 11-14-2004 02:04 PM
OpenAL Installation Help Little_Devil Linux - Software 0 08-08-2002 02:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration