LinuxQuestions.org
Review your favorite Linux distribution.
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 11-27-2006, 10:34 AM   #1
Hyakutake
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Slackware
Posts: 154

Rep: Reputation: 19
[openal] :sound file dosen't play well. Am i doing anything wrong?


Hello all.

After installing openal I tried some sound loading.

Copy/pasted this test code
Code:
Example: 'Hello, world' in ALUT

Here is the traditional first program for any language or library, but this time it is actually saying 'Hello, world!' instead of printing it:

#include <stdlib.h>
#include <AL/al.h>
#include <AL/alut.h>

int
main (int argc, char **argv)
{
  ALuint helloBuffer, helloSource;
  alutInit (&argc, argv);
  helloBuffer = alutCreateBufferHelloWorld ();
  alGenSources (1, &helloSource);
  alSourcei (helloSource, AL_BUFFER, helloBuffer);
  alSourcePlay (helloSource);
  alutSleep (1);
  alutExit (); 
}
Result is a mechanical 'hello world'.

To load from file Alut specification (from openal site) says to use
Code:
ALuint alutCreateBufferFromFile (const char *filename);
So altered the source to this

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

int main()
{ALuint Buffer, Source;

 alutInit (0, NULL);
 Buffer = alutCreateBufferFromFile("music.wav");
 alGenSources (1, &Source);
 alSourcei(Source, AL_BUFFER, Buffer);
 alSourcePlay (Source);

 char c;
 while(c!='q')
 {c=getchar(); }
 alutExit (); 
}
The problem is that sound is not good, lots of noise and totatly distorted.

Am I doing anything worng?

thanks in advance

BTW: the sound file is KDE_Startup_new.wav
 
Old 11-27-2006, 07:28 PM   #2
burninGpi
Member
 
Registered: Mar 2006
Location: Fort McMurray, Canada
Distribution: Gentoo ~amd64
Posts: 163

Rep: Reputation: 30
You see, you would compile and run the executable like this (assuming you compiled it to "hello"):
Code:
./hello music.wav
try compiling the original source code and running it like that and see what happens.
 
Old 11-27-2006, 08:28 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Hyakutake (or Hundred-Peaks, or Hundred-Bamboo), you are trying to open "music.wav" in the code, but the file is "KDE_Startup_new.wav". Check if Buffer is valid after the open. I don't seem to have ALUT, so I can't compile and verify myself.

If you check the first note here:
http://www.libsdl.org/cgi/docwiki.cgi/Mix_5fOpenAudio
You may also be having a format issue, but I don't really know.

Last edited by tuxdev; 11-27-2006 at 08:35 PM.
 
Old 11-28-2006, 06:35 PM   #4
Hyakutake
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Slackware
Posts: 154

Original Poster
Rep: Reputation: 19
Thanks for your replies tuxdev (again ) and burninGpi.

burninGpi

I've compiled the source (hello world) and tried to ./a.out music.wav.

The result is the same as if I only use ./a.out

tuxdev

The "music.wav" is a copy from "KDE_Startup_new.wav". Just said that if it was a problem with sample rate/size/channels because I guess whoever usess KDE has this file .

BTW i've changed it, now it's the 20th century music

Code:
Sample rate: 22 500
Sample Size: 16 bits
Channels: 2
Quote:
[tuxdev]: Check if Buffer is valid after the open. I don't seem to have ALUT, so I can't compile and verify myself.
How can I do that?

Any other ideas burninGpi, or anyone else who's reading this post?

PS: Sorry for a late response, kinda busy this last days.

EDIT: Hundred-Peaks or Hundred-Bamboo - cool! Just like it because it's a comet

Last edited by Hyakutake; 11-28-2006 at 06:37 PM.
 
Old 11-28-2006, 09:25 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
I don't really know the API, but it looks like ALUT is using a file-descriptor style. A typical error return would be -1. It really should be documented, I just don't know where.

BTW, I looked up that comet, and the second character isn't bamboo or mountain/peak like I originally thought, it is "military force". So I suppose the best translation would be "Great Military Force".
 
Old 11-29-2006, 06:56 AM   #6
Hyakutake
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Slackware
Posts: 154

Original Poster
Rep: Reputation: 19
After reading some stuff at OpenAL site's document section (alut specification and openal programmers guide) I've changed the code to this

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

int main()
{ALuint Buffer, Source;

 alutInit(0, NULL);

 alGetError(); //clear error state
 alGenBuffers(1, &Buffer);
 if(alGetError() != AL_NO_ERROR){printf("BUFFER ERROR\n"); } 

 alutGetError(); //clear error state
 Buffer = alutCreateBufferFromFile("teste.wav");
 switch(alutGetError()) //error type
 {case ALUT_ERROR_OUT_OF_MEMORY:
	printf("ERROR 1\n");
	break;
  case ALUT_ERROR_INVALID_OPERATION:
	printf("ERROR 2\n");
	break;
  case ALUT_ERROR_NO_CURRENT_CONTEXT:
	printf("ERROR 3\n");
	break;
  case ALUT_ERROR_AL_ERROR_ON_ENTRY:
	printf("ERROR 4\n");
	break;
  case ALUT_ERROR_ALC_ERROR_ON_ENTRY:
	printf("ERROR 5\n");
	break;
  case ALUT_ERROR_GEN_BUFFERS:
	printf("ERROR 6\n");
	break;
  case ALUT_ERROR_BUFFER_DATA:
	printf("ERROR 7\n");
	break;
  case ALUT_ERROR_IO_ERROR:
	printf("ERROR 8\n");
	break;
  case ALUT_ERROR_UNSUPPORTED_FILE_TYPE:
	printf("ERROR 9\n");
	break;
  case ALUT_ERROR_UNSUPPORTED_FILE_SUBTYPE:
	printf("ERROR 10\n");
	break;
  case ALUT_ERROR_CORRUPT_OR_TRUNCATED_DATA:
	printf("ERROR 11\n");
	break;
 }

 alGetError(); //clear error state
 alGenSources (1, &Source);
 if(alGetError() != AL_NO_ERROR){printf("SOURCE ERROR\n"); }

 alSourcei(Source, AL_BUFFER, Buffer);
 alSourcei(Source, AL_LOOPING, AL_TRUE);
 alSourcePlay(Source);

 char c;
 while(c!='q')
 {c=getchar(); }
 alutExit (); 
}
I'm not sure if this is correct but the result gives no error and sound output is still the same. I've noticed that besides all the noise the music comes out like in slow motion

I've took a look at http://www.libsdl.org/cgi/docwiki.cgi/Mix_5fOpenAudio and it looks interesting so I gave it a try.

Code:
#include <stdio.h>
#include <SDL/SDL_mixer.h>

int main()
{
 Mix_OpenAudio(22500, AUDIO_S16SYS, 2, 1024);
 Mix_Chunk *sound = Mix_LoadWAV("music.wav");
 Mix_PlayChannel(-1, sound, 1);

 char c;
 while(c!='q')
 {c = getchar();}
}
Very easy to load a sound, also "It supports WAVE-, MOD-, MIDI-, OGG- and MP3 files" (1)

It seem to be less hassle to load a file and play it but can I use this loader with openal (I like the idea of audio sources moving in a 3D space)? How can I do that?


(1) from here
 
Old 11-29-2006, 07:59 AM   #7
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
If your interested, I'm actually coding a small example app (just to test how they work) which loads an Ogg/Vorbis file (using libogg & libvorbis), and then plays it w/ OpenAL. I'll give you a copy of the code once it's done (or post it up here). Party tonight, but I should be done it tomorrow (if you haven't solved it by then). Nearly finished already, just getting some strange errors w/ one of the vorbis functions.

Last edited by 95se; 11-29-2006 at 08:00 AM.
 
Old 11-29-2006, 12:12 PM   #8
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
I don't think you can mix SDL and OpenAL. OpenAL still has advantages over SDL, in particular, OpenAL has finer support for positional sound. SuperTux uses OpenAL, so you could try to check out
http://svn.berlios.de/svnroot/repos/...tux/src/audio/
to see how they do it.
 
Old 11-30-2006, 05:50 AM   #9
Hyakutake
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Slackware
Posts: 154

Original Poster
Rep: Reputation: 19
Still no success here...

I'm reading some stuff at DevMaster's tutorial on openal and i'm messing with alc library (not very explicit on this lib but better than nothing) - it's supposed to "handle the devices on our own."

So here's what I'm doing, or trying to do.

Code:
#include <stdio.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alut.h>

int main(int argc, char **argv)
{ALuint Buffer, Source;

 ALCdevice* pDevice;
 ALCchar DeviceName[] = "DirectSound3D";
 pDevice = alcOpenDevice(DeviceName); 

 ALCcontext* pContext;
 pContext = alcCreateContext(pDevice, NULL);
 alcMakeContextCurrent(pContext);

 alcProcessContext(pContext);

 alGenBuffers(1, &Buffer);
 Buffer = alutCreateBufferFromFile("sound.wav");

 alGenSources(1, &Source);
 alSourcei(Source, AL_BUFFER, Buffer);
 alSourcePlay(Source);

 char c;
 while(c!='q')
 {c = getchar(); }
}
Result: No sound output. I'm lost

Anyone knows were to find a good tutorial on openal - other than DevMaster?

95se: sure I'll take a look at your source and try to learn some more - I'm not a hardcore programmer or anything like that, more of an enthusiast

tuxdev: SDL no mix with OpenAL - best of both worlds wasted...
Thanks for those links, allot of stuff to read there but I guess it's too much for me.

Thanks for your time.
 
Old 11-30-2006, 07:03 AM   #10
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Im may be wrong here as I have never used OpenAL just DirectSound, but I would assume you would need a listener that has a location just as you have a source.

If a tree falls in the woods and there is not a listener, does it make a sound.


[edit]
from OpenAL programmers guide
Quote:
Introduction to OpenAL
Use of OpenAL revolves around the use of three fundamental objects – Buffers, Sources, and a
Listener. A buffer can be filled with audio data, and can then be attached to a source. The
source can then be positioned and played. How the source is heard is determined by its position
and orientation relative to the Listener object (there is only one Listener). Creating a number of
sources and buffers and a single listener and then updating the positions and orientations of the
sources and listener dynamically can present a convincing 3D audio world.

Last edited by dmail; 11-30-2006 at 07:19 AM.
 
Old 12-01-2006, 12:39 PM   #11
Hyakutake
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Slackware
Posts: 154

Original Poster
Rep: Reputation: 19
You are correct I need those 3 things. Although I have tried with all of them the result is the same.

Here's what I have

Code:
#include <stdio.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alut.h>

int main()
{ALuint Buffer, Source;

 alutInit(0, NULL);

 alGenBuffers(1, &Buffer);

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

 alutLoadWAVFile((ALbyte*)"sound.wav", &format, &data, &size, &freq, &loop);
 alBufferData(Buffer, format, data, size, freq);
 alutUnloadWAV(format, data, size, freq);

 alGenSources(1, &Source);

 ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
 ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
 alSourcei(Source, AL_BUFFER, Buffer);
 alSourcefv(Source, AL_VELOCITY, SourceVel);
 alSourcefv(Source, AL_POSITION, SourcePos);

 ALfloat list_pos[]={0.0f, 0.0f, 0.0f};
 ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
 ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
 alListenerfv(AL_POSITION, list_pos);
 alListenerfv(AL_VELOCITY,    ListenerVel);
 alListenerfv(AL_ORIENTATION, ListenerOri);

 const ALCchar *type;
 type = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
 printf("%s", type);

 alSourcePlay(Source);
 char c;
 while(c!='q')
 {c = getchar(); }
}
The bold text is something I added to get the default output device.

Code:
bash-3.1$ ./a.out
'((sampling-rate 44100) (device '(native))
native device? is this the correct device to use? I'm totally lost...

Any more thoughts are welcome.
 
Old 12-03-2006, 04:55 PM   #12
Hyakutake
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Slackware
Posts: 154

Original Poster
Rep: Reputation: 19
I guess the problem is not the code but maybe the library itself (I guess).

Compiled from source freealut (from openal.org) and ran the examples witch came with it.

The output is the same, allot of noise and slowed playback - both on helloworld and playfile.
 
Old 12-03-2006, 07:11 PM   #13
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Ah, I've seen somebody else encounter that issue. Didn't make the connection. He fixed it by installing the CVS version.
 
Old 12-04-2006, 09:21 AM   #14
Hyakutake
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Slackware
Posts: 154

Original Poster
Rep: Reputation: 19
OK, I'll try that...

The problem is I've never used cvs before ( here)

Can you help out doing it please?

Thanks.
 
Old 12-04-2006, 09:24 AM   #15
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
It seems they use SVN. From the site:
Code:
svn checkout http://www.openal.org/repos/openal/trunk openal
That's it when it comes to getting the sources.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
gave wrong syntax for tar as tar -cvzf file file.tgz how to recover the file gautham Linux - General 4 04-13-2005 03:15 AM
how to play sound file with c code? khucinx Programming 1 05-11-2004 06:28 AM
when i try to play a sound file with mpg123 or mpg321 i get an error VioLaToR Linux - Software 5 02-24-2004 12:25 PM
How to play a media file/ video file/mp3 file recorded in harddisk/cd-rom arindam Linux - Newbie 2 09-05-2003 10:31 AM
unable to play more than one sound file at a time. wonka_vision Linux - Hardware 2 06-10-2003 06:22 PM

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

All times are GMT -5. The time now is 04:45 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