All pastes #1359015 Raw Edit

Untitled

public text v1 · immutable
#1359015 ·published 2009-03-12 13:54 UTC
rendered paste body
#include "gtk_s9x.h"
#include "gtk_sound_driver_openal.h"
#include <cmath>
#include <assert.h>
#include <AL/alut.h>

S9xOpenALSoundDriver::S9xOpenALSoundDriver()
{
    device = NULL;
    context = NULL;
    source = 0;
    snd_buffer_size = 0;
    thread_die = 0;
    thread = NULL;

    return;
}

void
S9xOpenALSoundDriver::init()
{
    device = alcOpenDevice(NULL);
    assert( device != NULL );
    context = alcCreateContext(device, NULL);
    assert( context != NULL );
    alcMakeContextCurrent(context);

    alGenBuffers(5, buffers);
    alGenSources(1, &source);
    alSourcei(source, AL_BUFFER, 0);

    return;
}

void
S9xOpenALSoundDriver::terminate()
{

    alSourceStop(source);
    alSourcei(source, AL_BUFFER, 0);
    alDeleteSources(1, &source);
    alDeleteBuffers(1, buffers);

    alcMakeContextCurrent(NULL);
    alcDestroyContext(context);
    alcCloseDevice(device);

    return;
}

void
S9xOpenALSoundDriver::start()
{
    if (!(gui_config->mute_sound))
    {
    ALint sourceState = 0;
    alSourceQueueBuffers(source, 5, buffers);
    alGetSourcei(source, AL_SOURCE_STATE, &sourceState);
    if(sourceState != AL_PLAYING)
    {
	alSourcePlay(source);
    }
    }

    return;
}

void
S9xOpenALSoundDriver::stop()
{
    ALint sourceState = 0;
    alGetSourcei( source, AL_SOURCE_STATE, &sourceState );
    if( sourceState != AL_STOPPED ) {
	alSourceStop( source );
    }

    return;
}

bool
S9xOpenALSoundDriver::open_device(int mode, bool stereo, int buffer_size)
{
    ALenum snd_format;
    if (Settings.Stereo == true)
    {
	if (Settings.SixteenBitSound == true)
	{snd_format=AL_FORMAT_STEREO16;} else {snd_format=AL_FORMAT_STEREO8;}
    }
    else
    {
	if (Settings.SixteenBitSound == true)
	{snd_format=AL_FORMAT_MONO16;} else {snd_format=AL_FORMAT_MONO8;}
    }
    format = snd_format;
    snd_buffer_size = buffer_size * (stereo ? 2 : 1);
    snd_buffer = g_new0(ALuint, snd_buffer_size);
    for (int i = 0; i<5; i++)
    {
    alBufferData(buffers[i], format, (const ALvoid*) snd_buffer, snd_buffer_size, playback_rates[Settings.SoundPlaybackRate]);
    }

    return true;
}

void
S9xOpenALSoundDriver::mix()
{
    ALint processed;
    alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
    while(processed--)
    {
    ALuint buffer;
    alSourceUnqueueBuffers(source, 1, &buffer);
    S9xMixSamples ((uint8 *)snd_buffer, snd_buffer_size);
    alBufferData(buffer, format, (const ALvoid*) snd_buffer, snd_buffer_size, playback_rates[Settings.SoundPlaybackRate]);
    alSourceQueueBuffers(source, 1, &buffer);
    }
    return;
}