SDL_mixer and OpenAL

Hi,
I would like to use openal with sdl, but I don’t know how to get Mix_Chunk data into openal? I tried google search, but it only gave me a lot of wrong or unanswered results. Is there any function for it or something? thanks in advance

ALuint buffer;
ALuint source;

//initiate OpenAL
ALCdevice* device;
device = alcOpenDevice(NULL);

ALCcontext* context;
context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);
alGenBuffers(1, buffer);
alGenSources(1 source);

//initiate SDL_Mixer
int audio_rate = 22050;
Uint16 audio_format = AUDIO_S16SYS;
int audio_channels = 2;
int audio_buffers = 4096;

if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
{
fprintf(stderr, “Unable to initialize audio: %s\n”, Mix_GetError());
exit(1);
}

//load wave file
Mix_Chunk *sound = Mix_LoadWAV(“ding.wav”);

if(sound == NULL)
{
fprintf(stderr, “Unable to load WAV file: %s\n”, Mix_GetError());
}

//transfer Mix_Chunk data and length to OpenAL buffer
alBufferData(buffer, AL_FORMAT_STEREO16, sound->abuf, sound->alen, 44100);

//generate new sound
alGenSources(1, &sourceID);
alSourcei(source, AL_BUFFER, buffer[num]);
alSourcef(source, AL_PITCH, 1.0f);
alSourcef(source, AL_GAIN, 1.0f);

if(loop)
alSourcei(source[num], AL_LOOPING, AL_TRUE);
else
alSourcei(source[num], AL_LOOPING, AL_FALSE);

note:
Really all that needed to be done was retrieve the data from Mix_Chunk, a good way to know what the object contains is to look through the API then you can compare what type of data it contains and what type of data your function takes

so after looking at the Mix_Chunk API it give you this

typedef struct {
int allocated;
Uint8 *abuf;
Uint32 alen;
Uint8 volume;
} Mix_Chunk;

so now you know you have access to all this data

not sure how efficient this would be since we are initializing 2 sound device but it works, when using OpenAL on windows I used alut library(OpenAL Utility Toolkit) and on my current iOS project I used audiotoolbox frameworks

Gekoncze wrote:> Hi,

I would like to use openal with sdl, but I don’t know how to get Mix_Chunk data into openal? I tried google search, but it only gave me a lot of wrong or unanswered results. Is there any function for it or something? thanks in advance

Thank you very much :slight_smile:

Alternatively, use ALmixer.
http://playcontrol.net/opensource/ALmixer/--
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/

Just a little note:
If you want to make 3D sound working in openal, you have to use MONO sound, so the following lines will be differend:

// before
int audio_channels = 2;

alBufferData(buffer, AL_FORMAT_STEREO16, sound->abuf, sound->alen, 44100);

// after
int audio_channels = 1;

alBufferData(buffer, AL_FORMAT_MONO16, sound->abuf, sound->alen, 44100);