Sign in to follow this  
Amresh Kumar

A simple application based on OpenAl and Ogg, Vorbis library is crashing.

Recommended Posts

Ok I got a fix for my previous problem, actually BUFFER_SIZE was incorrect. But still it crashes at: if (ov_open(f, &oggFile, NULL, 0) != 0) return false; Plz someone help me on this. #include "AL/al.h" #include "AL/alut.h" #include "vorbis/vorbisfile.h" #include <cstdio> #include <iostream> #include <vector> #define BUFFER_SIZE 327680 using namespace std; // This function loads a .ogg file into a memory buffer and returns // the format and frequency. bool LoadOGG(string fileName, vector <char> &buffer, ALenum &format, ALsizei &freq) { int endian = 0; int bitStream; long bytes; char array[BUFFER_SIZE]; FILE *f (NULL); f = fopen(fileName.c_str (), "rb"); if (f == NULL) return false; vorbis_info *pInfo; OggVorbis_File oggFile; if (ov_open(f, &oggFile, NULL, 0) != 0) return false; pInfo = ov_info(&oggFile, -1); if (pInfo->channels == 1) format = AL_FORMAT_MONO16; else format = AL_FORMAT_STEREO16; freq = pInfo->rate; do { bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream); if (bytes < 0) { ov_clear(&oggFile); return false; } buffer.insert(buffer.end(), array, array + bytes); } while (bytes > 0); ov_clear(&oggFile); return true; } int main(int argc, char *argv[]) { ALint state; // The state of the sound source ALuint bufferID; // The OpenAL sound buffer ID ALuint sourceID; // The OpenAL sound source ALenum format; // The sound data format ALsizei freq; // The frequency of the sound data vector<char> bufferData; // The sound buffer data from file if (argc < 2) { cerr << "Syntax: " << argv[0] << " OGGFile" << endl; return -1; } alutInit(&argc, argv); alGenBuffers(1, &bufferID); alGenSources(1, &sourceID); alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f); alSource3f(sourceID, AL_POSITION, 0.0f, 0.0f, 0.0f); string strTmp = argv[1]; if (!LoadOGG(strTmp, bufferData, format, freq)) { cerr << "Couldn't load file." << endl; return -1; } alBufferData(bufferID, format, &bufferData[0], static_cast<ALsizei>(bufferData.size()), freq); alSourcei(sourceID, AL_BUFFER, bufferID); alSourcePlay(sourceID); do alGetSourcei(sourceID, AL_SOURCE_STATE, &state); while (state != AL_STOPPED); alDeleteBuffers(1, &bufferID); alDeleteSources(1, &sourceID); alutExit(); return 0; } // end of main

Share this post


Link to post
Share on other sites
My code seems to be based on the same article you used (except I'm allocating my buffer on the heap, and have changed the interface a little). It looks like I should be checking for errors on the ov_* calls, oops. Anyway, I looked for differences last night but couldn't find anything obvious. I'd probably try allocating a smaller buffer on the heap and if you are still having issues I'd recommend stepping through in a debugger to try to pinpoint the error. Keep us posted...

Edit: Oops, I seem to be having issues counting zeros in your buffer size...

Share this post


Link to post
Share on other sites
The other guy who works on my project with me pointed out that according to the ov_open() man page (see the bottom of the page) when you are in Windows you must statically link to libvorbisfile or use ov_open_callbacks(). Not sure how that would manifest itself (I'm mostly a linux guy) but it's worth looking into...

Share this post


Link to post
Share on other sites
Hi Grinch,
I tried the code given in the link. Now its not crashing now but it stucks at the point pointed in the code. Plz check if its possible.

Note: Platform - WindowsXP

#include "vorbis/vorbisfile.h"
#include "AL/al.h"
#include "AL/alut.h"
//#include "Log.h"
#include "soundfile.h"
#include <iostream>

using namespace std;

#define BUFFER_SIZE 32768 // 32 KB buffers

static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){
if(f==NULL)return(-1);
return fseek(f,off,whence);
}


bool LoadOgg(const char *fileName, ALuint *buffer) {
int endian = 0; // 0 for Little-Endian, 1 for Big-Endian
int bitStream;
long bytes;
char *array;
vector <char> data;
FILE *f;
ALenum format;
ALsizei freq = 0;
vorbis_info *pInfo;
OggVorbis_File oggFile;
// Open for binary reading
f = fopen(fileName, "rb");
if (!f) return false;

ov_callbacks callbacks = {
(size_t (*)(void *, size_t, size_t, void *)) fread,
(int (*)(void *, ogg_int64_t, int)) _fseek64_wrap,
(int (*)(void *)) fclose,
(long (*)(void *)) ftell
};
ov_open_callbacks(f, &oggFile, NULL, 0, callbacks);
// Get some information about the OGG file
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;
array = new char[BUFFER_SIZE];
do {
// Read up to a buffer's worth of decoded sound data
bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream);
// Append to end of buffer
data.insert(data.end(), array, array + bytes);
} while (bytes > 0);
delete []array;
ov_clear(&oggFile);
// Don't call fclose(f), ogg vorbis does it
alGenBuffers(1, buffer);
if (alGetError() != AL_NO_ERROR) return false;
alBufferData(*buffer, format, &data[0], data.size(), freq);
return true;
}

bool LoadWav(const char *filename, ALuint *buffer) {
ALenum format;
ALsizei size;
ALvoid* data;
ALsizei freq;
ALboolean loop;
// Load wav data into a buffer.
alGenBuffers(1, buffer);
if (alGetError() != AL_NO_ERROR) return false;
alutLoadWAVFile((ALbyte*)filename, &format, &data, &size, &freq, &loop);
if (alGetError() != AL_NO_ERROR) {
alDeleteBuffers(1, buffer);
return false;
}
alBufferData(*buffer, format, data, size, freq);
alutUnloadWAV(format, data, size, freq);
return true;
}

ALuint GenSource() {
ALuint source;
bool looping = false;
ALfloat sourcePos[] = {0.0, 0.0, 0.0};
ALfloat sourceVel[] = {0.0, 0.0, 0.0};
// Bind buffer with a source.
alGenSources(1, &source);
if (alGetError() != AL_NO_ERROR) return 0;
alSourcef(source, AL_PITCH, 1.0);
alSourcef(source, AL_GAIN, 1.0);
alSourcefv(source, AL_POSITION, sourcePos);
alSourcefv(source, AL_VELOCITY, sourceVel);
alSourcei(source, AL_LOOPING, looping);
return source;
}

ALuint GenSource(ALuint buffer) {
ALuint source = GenSource();
if (source == 0) return source;
alSourcei(source, AL_BUFFER, buffer);
return source;
}

int main(int argc, char *argv[]) {
ALint state; // The state of the sound source
ALuint bufferID; // The OpenAL sound buffer ID
ALuint sourceID; // The OpenAL sound source
ALenum format; // The sound data format
ALsizei freq; // The frequency of the sound data
vector<char> bufferData; // The sound buffer data from file
ALuint *buffer = new ALuint[BUFFER_SIZE];
if (argc < 2) {
cerr << "Syntax: " << argv[0] << " OGGFile" << endl;
return -1;
}
alutInit(&argc, argv);
alGenBuffers(1, &bufferID);
alGenSources(1, &sourceID);
alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f);
sourceID = GenSource ();
alSource3f(sourceID, AL_POSITION, 0.0f, 0.0f, 0.0f);
string strTmp = argv[1];
if (!LoadOgg(strTmp.c_str (), buffer)) {
cerr << "Couldn't load file." << endl;
return -1;
}
alSourcei(sourceID, AL_BUFFER, bufferID);
alSourcePlay(sourceID);
do alGetSourcei(sourceID, AL_SOURCE_STATE, &state); //Goes in #inf loop
while (state != AL_STOPPED);
alDeleteBuffers(1, &bufferID);
alDeleteSources(1, &sourceID);
alutExit();
return 0;
}
// end of main

Share this post


Link to post
Share on other sites
Well, I'm not sure. I found a thread where a guy with an NVIDIA sound chipset was having very similar issues getting the correct state using that method. He just downloaded the latest audio drivers and it fixed it (see this post). I'm not sure if you have an NVIDIA card though...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this