3

I'm new using OpenAl library. I'm following the OpenAl programming guide but i can't find.

I have this code extracted from page 10 of the OpenAl programming guide but still have no sound. I use OSX Snow Leopard, i know OSX doesn't have ALUT defined.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>

#include <OpenAL/al.h>
#include <OpenAL/alc.h>

using namespace std;

#define NUM_BUFFERS 3
#define BUFFER_SIZE 4096

int main(int argc, char **argv)
{
    ALCdevice *dev;
    ALCcontext *ctx;
    struct stat statbuf;

    Aluint buffer[NUM_BUFFERS];
    Aluint source[NUM_SOURCES];

    ALsizei size, freq;
    ALenum format;
    ALvoid *data;

    // Initialization 
    dev = alcOpenDevice(NULL); // select the "preferred dev" 

    if (dev) 
    { 
        ctx = alcCreateContext(dev,NULL); 
        alcMakeContextCurrent(ctx);  
    } 
    // Check for EAX 2.0 support 
    // g_bEAX = alIsExtensionPresent("EAX2.0");

    // Generate Buffers 
    alGetError(); // clear error code 
    alGenBuffers(NUM_BUFFERS, buffer); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("alGenBuffers :", error); 
        return 1; 
    } 
    // Load test.wav 
    loadWAVFile("sample.wav", &format, &data, &size, &freq, &loop); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("LoadWAVFile sample.wav : ", error); 
        alDeleteBuffers(NUM_BUFFERS, buffer); 
     return 1; 
    }

    // Copy test.wav data into AL Buffer 0 
    alBufferData(buffer[0], format, data, size, freq); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
     DisplayALError("alBufferData buffer 0 : ", error); 
     alDeleteBuffers(NUM_BUFFERS, buffer); 
     return 1; 
    } 

    // Unload test.wav 
    unloadWAV(format, data, size, freq); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("UnloadWAV : ", error); 
        alDeleteBuffers(NUM_BUFFERS, buffer); 
        return 1; 
    } 
    // Generate Sources 
    alGenSources(1, source); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("alGenSources 1 : ", error); 
        return 1; 
    } 
    // Attach buffer 0 to source
    alSourcei(source[0], AL_BUFFER, buffer[0]); 
    if ((error = alGetError()) != AL_NO_ERROR) 
    { 
        DisplayALError("alSourcei AL_BUFFER 0 : ", error); 
    } 

    // Exit 
    ctx = alcGetCurrentContext(); 
    dev = alcGetContextsDevice(ctx); 
    alcMakeContextCurrent(NULL); 
    alcDestroyContext(ctx); 
    alcCloseDevice(dev);

    return 0;
}

What things I missed to make this code work ??? What i'm doing wrong ???

Any advice could help, thanks.

Jorge Vega Sánchez
  • 7,430
  • 14
  • 55
  • 77

1 Answers1

3

You are not calling alSourcePlay(source[0]) to start the playback.

Mārtiņš Možeiko
  • 12,733
  • 2
  • 45
  • 45
  • 2
    People should also keep in mind that alSourcePlay() will execute asynchronously. So if you immediately clean up your audio resources after it returns, you probably won't hear anything at all before the program immediately exits. – Ionoclast Brigham Aug 21 '13 at 09:42