OpenAL Initialization Problem

Spike7d5 101 Jul 22, 2007 at 10:55

OpenAL captured my interest a couple of days ago. So, I began looking for tutorials and I’ve managed to play sound by using alut, but, if I want to use AL only, the sound just doesn’t hear. The code is very simple and i’ve looked through it many times, but still I haven’t found the problem. Could you please help me? I must mention that if i define USE_ALUT it works fine.

#pragma comment(lib, "openal32.lib")
#pragma comment(lib, "alut.lib")

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

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

//#define USE_ALUT

int main(int argc, char *argv[])

#ifndef USE_ALUT
    const ALCchar *defaultDevice = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
    if (defaultDevice == NULL) return 1;

    ALCdevice *device = alcOpenDevice(defaultDevice);
    if (device == NULL) return 1;

    ALCcontext *context = alcCreateContext(device, NULL);
    if (context == NULL) return 1;

    if (alcMakeContextCurrent(context) == AL_FALSE) return 1;
    alutInit(&argc, argv);

    ALuint buffer, source;

    ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };
    ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
    ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };
    ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
    ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };

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

    alGenBuffers(1, &buffer);

    data = alutLoadMemoryFromFile("Rupem Baraka.wav", &format, &size, &freq);
    alBufferData(buffer, format, data, size, ALsizei(freq));
    alutUnloadWAV(format, data, size, ALsizei(freq));

    alGenSources(1, &source);

    alSourcei (source, AL_BUFFER,   buffer   );
    alSourcef (source, AL_PITCH,    1.0f     );
    alSourcef (source, AL_GAIN,  1.0f    );
    alSourcefv(source, AL_POSITION, SourcePos);
    alSourcefv(source, AL_VELOCITY, SourceVel);
    alSourcei (source, AL_LOOPING,  1        );

    alListenerfv(AL_POSITION,   ListenerPos);
    alListenerfv(AL_VELOCITY,   ListenerVel);
    alListenerfv(AL_ORIENTATION, ListenerOri);

    printf("1 = play\n");
    printf("2 = pause\n");
    printf("3 = stop\n");
    printf("0 = exit\n");

    char key;
        key = _getch();
        if (key == '1') alSourcePlay(source);
        if (key == '2') alSourcePause(source);
        if (key == '3') alSourceStop(source);
    while (key != '0');

    alDeleteBuffers(1, &buffer);
    alDeleteSources(1, &source);

    return 0;

3 Replies

Please log in or register to post a reply.

TheNut 179 Jul 22, 2007 at 12:19

If you supply a null for alcOpenDevice(), it will automatically use the default device so you don’t need to make that additional getString() call. Other than that, the rest of your code looks fine.

Spike7d5 101 Jul 22, 2007 at 14:21

I’ve finally found the problem: using alut functions without prior calling of alutInit isn’t going to work. So the device was correctly initialized, but nothing was buffered. I’ve written my own loadWAV function and everything works fine. No need for alut anymore :D

Thanks for your time

stranomavero 101 Sep 05, 2007 at 12:05

can u explain me better how u solved your problem?
i have problems with alut too