no ogg sound playback

Member
Posts: 34
Joined: 2004.07
Post: #1
I have all of it setup, wav sounds work fine (because of the alut function) but I can't get the
ogg to play for some reason.
As far as i can tell, from the debugger, the sound loads fine.

soundBuffer is initialised to:

soundBuffer = malloc(sizeof(ALuint));

in the init function of the class if that has any relevance at all

please help, i've been trying to get this to work for a couple of days now...

thanks in advance

ss2cire

here's the code:
(lisp paste for those who dont like the scrolling...)
http://paste.lisp.org/display/15539

Code:
//my sound class properties
        ALfloat         listenerPos[3];
        ALfloat         listenerVel[3];
        ALfloat         listenerOri[6];
        ALfloat         soundVolume; // a floating point value from 1.0 to 100.0, default to 100.0
        ALuint         *soundBuffer;
        ALuint         soundSource;
        GCSoundType         soundType;


//----------------------------------------------------------------------
- (void)loadOGGSoundAtPath:(NSString *)path
{
        ALsizei                format;
        ALsizei                freq;
        ALint                error;

        int endian = 1;                         // 0 for Little-Endian, 1 for Big-Endian
        int bitStream;
        long bytes;
        #define BUFFER_SIZE 1024
        char array[BUFFER_SIZE];                // Local fixed size array
        FILE *fileToRead;
        ALuint *buffer = nil;
        long totalBytes = 0;

        endian = 1;

        // Open for binary reading
        fileToRead = fopen([path UTF8String], "rb");
        vorbis_info *pInfo;
        OggVorbis_File oggFile;

        // Try opening the given file
        if (ov_open(fileToRead, &oggFile, nil, 0) != 0) {
                printf("Error opening %s for decoding...\n", [path UTF8String]);
                return;
        }
        pInfo = ov_info(&oggFile, -1);
        
        // Check the number of channels... always use 16-bit samples
        if (pInfo->channels == 1) {
                format = AL_FORMAT_MONO16;
        } else {
                format = AL_FORMAT_STEREO16;
        }

        // The frequency of the sampling rate
        freq = pInfo->rate;

        // Keep reading until all is read
        do {
                
                // Read up to a buffer's worth of decoded sound data
                bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream);
                
                if (bytes < 0) {
                        ov_clear(&oggFile);
                        printf("Error decoding %s...\n", [path UTF8String]);
                        return;
                }
                
                // Append to end of buffer
                totalBytes += bytes;
                buffer = realloc(buffer, totalBytes);
                memcpy(buffer + totalBytes - bytes, array, bytes);
        } while (bytes > 0);

        // Clean up!
        ov_clear(&oggFile);

        // Move to OpenAL        
        alBufferData(soundSource, format, buffer, totalBytes, freq);
        if ((error = alGetError()) != AL_NO_ERROR) return;
        
        // Delete Buffer
        free(buffer);
}

- (void)play
{
        alSourcei (soundSource, AL_BUFFER, *soundBuffer);

        alSource3f(soundSource, AL_POSITION, 0.0f, 0.0f, 0.0f);

        alSourcei(soundSource, AL_SOURCE_RELATIVE, AL_TRUE);
        alSourcef (soundSource, AL_GAIN, soundVolume);//use the sound volume the programmer set.

        alSourcePlay(soundSource);
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Some relevant bits of Outnumbered:

this for short sounds:

Code:
static int _load_sound(const char *path, ALuint *sound)
{
    FILE *file = fopen(path, "rb");
    if (file == NULL)
    {
        return 0;
    }
    
    OggVorbis_File ovfile;
    vorbis_info *info;
    
    ov_open(file, &ovfile, NULL, 0);
    
    info = ov_info(&ovfile, -1);
    unsigned channel_count = info->channels == 1 ? 1 : 2;
    ALenum format = channel_count == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
    ALsizei frequency = info->rate;
    
    ogg_int64_t pcm_total = ov_pcm_total(&ovfile, -1);
    ALsizei buffer_size = pcm_total * channel_count * sizeof(ALshort);
    void *buffer = malloc(buffer_size);
    char *cursor = buffer;
    
    int bitstream;
    long count;
    while ((count = ov_read(
        &ovfile,
        cursor,
        4096,
#if defined(__BIG_ENDIAN__)
        1,
#else
        0,
#endif
        2,
        1,
        &bitstream)) > 0)
    {
        cursor += count;
    }
    
    if (count < 0)
    {
        free(buffer);
        return 0;
    }
    
    ov_clear(&ovfile);
    
    alGenBuffers(1, sound);
    assert(alIsBuffer(*sound));
    
    alBufferData(
        *sound,
        format,
        buffer,
        buffer_size,
        frequency);
    
    free(buffer);
    
    return 1;
}

this for music:

(note: crashes with the OpenAL shipped with Tiger; I'm using a pre-release version of 1.1 from CVS)

Code:
#include <assert.h>
#include <stdlib.h>

#include <vorbis/vorbisfile.h>

#include "music.h"
#include "openal.h"
#include "options.h"
#include "sound.h"

static OggVorbis_File _ovfile;
static ALenum         _format;
static ALsizei        _frequency;

#define BUFFER_SIZE 65536
#define BUFFER_COUNT 4

static char _data[BUFFER_SIZE];
static ALuint _buffers[BUFFER_COUNT];

static ALuint _source = 0;

static int _open_music(void)
{
    FILE *file = fopen("Music/Noise.ogg", "rb");
    if (file == NULL)
    {
        return 0;
    }
    
    vorbis_info *info;
    
    ov_open(file, &_ovfile, NULL, 0);
    
    info = ov_info(&_ovfile, -1);
    unsigned channel_count = info->channels == 1 ? 1 : 2;
    _format = channel_count == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
    _frequency = info->rate;
    
    return 1;
}

static void _fill_buffer(ALuint buffer)
{
    char *cursor = _data;
    long read_bytes = 0;

    int bitstream;
    long count;
    while (read_bytes < BUFFER_SIZE && (count = ov_read(
        &_ovfile,
        cursor,
        BUFFER_SIZE - read_bytes,
#if defined(__BIG_ENDIAN__)
        1,
#else
        0,
#endif
        2,
        1,
        &bitstream)) >= 0)
    {
        if (count == 0)
        {
            ov_raw_seek(&_ovfile, 0);
        }
        else
        {
            cursor += count;
            read_bytes += count;
        }
    }
    
    alBufferData(buffer, _format, _data, BUFFER_SIZE, _frequency);
}

static void _initial_fill_buffers(void)
{
    alGenBuffers(BUFFER_COUNT, _buffers);
    
    unsigned i;
    for (i = 0; i < BUFFER_COUNT; ++i)
    {
        _fill_buffer(_buffers[i]);
    }
}

static void _initialize_music(void)
{
    if (!_open_music())
    {
        return;
    }
    _initial_fill_buffers();
    
    alGenSources(1, &_source);
    
    alSourceQueueBuffers(_source, BUFFER_COUNT, _buffers);
    alSourcef(_source, AL_GAIN, 0.05f);
}

static void _start_music(void)
{
    if (_source == 0)
    {
        _initialize_music();
    }
    
    alSourcePlay(_source);
}

static void _stop_music(void)
{
    if (_source == 0)
    {
        _initialize_music();
    }
    
    alSourceStop(_source);
}

void update_music(void)
{
    if (!music)
    {
        return;
    }

    ALint processed_count;
    alGetSourcei(_source, AL_BUFFERS_PROCESSED, &processed_count);
    assert(processed_count <= BUFFER_COUNT);
    
    if (processed_count == BUFFER_COUNT)
    {
        _stop_music();
        _start_music();
    }
    else
    {
        ALint i;
        for (i = 0; i < processed_count; ++i)
        {
            ALuint buffer;
            alSourceUnqueueBuffers(_source, 1, &buffer);
            assert(alIsBuffer(buffer));
            
            _fill_buffer(buffer);
            alSourceQueueBuffers(_source, 1, &buffer);
        }
    }
}
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenAL WAV Playback DylanE 7 4,617 Nov 27, 2006 02:21 PM
Last Post: DylanE
  24 Bit Audio Playback TREMS 2 3,294 Sep 29, 2006 06:00 PM
Last Post: Jones
  Dynamic sound playback? napthali 6 3,244 Dec 7, 2003 10:38 PM
Last Post: kelvin