All pastes #1358978 Raw Edit

Untitled

public text v1 · immutable
#1358978 ·published 2009-03-12 12:57 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;
    buffer = 0;
    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(1, &buffer);
    alGenSources(1, &source);
    alSourcei(source, AL_BUFFER, 0);
    //alSourceQueueBuffers(source, 1, &buffer);
    fprintf(stderr, "current context error %d\n", alcGetError (device));
    fprintf(stderr, "Init error %d\n", alGetError());

    return;
}

void
S9xOpenALSoundDriver::terminate()
{

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

    //g_free(snd_buffer);
    alcMakeContextCurrent(NULL);
    alcDestroyContext(context);
    alcCloseDevice(device);

    return;
}

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

    return;
/*
    if (!thread)
    {
        thread_die = 0;
        thread = g_thread_create (openal_thread,
                                  (gpointer) this,
                                  TRUE,
                                  NULL);
    }

    return;
*/
}

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

    return;
/*
    if (thread != NULL)
    {
        thread_die = 1;
        g_thread_join (thread);
        thread = NULL;
    }

    return;
*/
}

bool
S9xOpenALSoundDriver::open_device(int mode, bool stereo, int buffer_size)
{
    ALint sourceState = 0;
    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);
    fprintf(stderr, "Opened with buffer_size %d, rate %d and format %d\n", snd_buffer_size, playback_rates[Settings.SoundPlaybackRate], format);
    if (alIsBuffer(buffer)) fprintf(stderr, "A valid buffer \n");
    alBufferData(buffer, format, (const ALvoid*) snd_buffer, snd_buffer_size, playback_rates[Settings.SoundPlaybackRate]);
    //alSourcei(source, AL_BUFFER, buffer);
    fprintf(stderr, "Setup error %d\n", alGetError());
    //buffer = alutCreateBufferWaveform(ALUT_WAVEFORM_SINE, 60.0, 0.0, 40);
    //alSourcei(source,AL_LOOPING,AL_TRUE);
    alGetBufferi(buffer, AL_SIZE, &sourceState);
    fprintf(stderr, "buffer size %d of buffer %d\n", sourceState, buffer);

    return true;
}

void
S9xOpenALSoundDriver::mix()
{
    //return;
    //fprintf(stderr, "Trying to play\n");
    ALint sourceState = 0;
    alSourceUnqueueBuffers(source, 1, &buffer);
    S9xMixSamples ((uint8 *)snd_buffer, snd_buffer_size);
    //fprintf(stderr, "mute_sound %d\n", so.mute_sound);
    //alBufferData(buffer, format, (const ALvoid*) snd_buffer, snd_buffer_size, playback_rates[Settings.SoundPlaybackRate]);
    //alBufferData(buffer, format, (const ALvoid*) snd_buffer, snd_buffer_size, playback_rates[Settings.SoundPlaybackRate]);
    //fprintf(stderr, "Play context error %d\n", alGetError());
    //usleep(5000);
    alSourceQueueBuffers(source, 1, &buffer);
    fprintf(stderr, "Playing with buffer_size %d, rate %d and format %d\n", snd_buffer_size, playback_rates[Settings.SoundPlaybackRate], format);
    alGetBufferi(buffer, AL_SIZE, &sourceState);
    fprintf(stderr, "buffer size %d of buffer %d\n", sourceState, buffer);
    //alSourcei(source, AL_BUFFER, buffer); 
    //usleep(5000);
/*
while (true)
{
  alGetSourcei(source, AL_SOURCE_STATE, &sourceState);
  if ((sourceState != AL_PLAYING) && (sourceState != AL_PAUSED))
    break;
}
*/
    //fprintf(stderr, "Stopped playing\n");
    //usleep(50000);
    //fprintf(stderr, "error context %d\n", alcGetError(device));
    //fprintf(stderr, "Playing\n");
    return;
}