Archived

This topic is now archived and is closed to further replies.

stefu

Ogg & OpenAL & seg fault

Recommended Posts

Hi! I wrote ogg file loader to use with OpenALfor sound effects. The problem is I get segmentation fault. Sometimes it starts playing and I get seg fault while playing or when I exit.
  
#include "vorbis/codec.h"
#include "vorbis/vorbisfile.h"


bool decode(const string& ogg_path,vector<char>& buffer,int& channels, int& rate)
{
	
	char pcmout[4096];
	OggVorbis_File vf;
	int eof=0;
	int current_section;

	buffer.clear();
	buffer.reserve(10000);
	
	FILE *ogg_file = fopen(ogg_path.c_str(),"rb");
	if( !ogg_file ) {
		printf("Cannot open %s\n",ogg_path.c_str());
		return false;
	}
	
	if(ov_open(ogg_file, &vf, NULL, 0) < 0) {
		printf("Input does not appear to be an Ogg bitstream.\n");
		fclose(ogg_file);
		return false;
	}
	
	char **ptr = ov_comment(&vf,-1)->user_comments;
	vorbis_info *vi = ov_info(&vf,-1);
	while(*ptr) {
		printf("%s\n",*ptr);
		++ptr;
	}
	channels = vi->channels;
	rate = vi->rate;
	
	printf("\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
	printf("\nDecoded length: %ld samples\n", (long)ov_pcm_total(&vf,-1));
	printf("Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);

	// http://www.xiph.org/ogg/vorbis/doc/vorbisfile/ov_read.html

	
	while(!eof) {
		
		long ret = ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,¤t_section);
		if (ret == 0) {
			/* EOF */
			eof=1;
			//cout << "EOF." << endl;
		} else if (ret < 0) {
			/* error in the stream.  Not a problem, just reporting it in
			case we (the app) cares.  In this case, we don''t. */
			//cout << "Error in stream." << endl;
		} else {
			/* we don''t bother dealing with sample rate changes, etc, but
			you''ll have to*/
			//fwrite(pcmout,1,ret,stdout);

			//cout << "Read " << ret << " bytes." << endl;

			for(int i=0; i<ret; i++)
				buffer.push_back( pcmout[i] );
		}
	}
	
	cout << "Buffersize=" << buffer.size() << endl;
	
	ov_clear(&vf);
	fclose(ogg_file);
	return true;
}

  

  

ALuint buffer=0;

alGenBuffers( 1, &buffer);
	
if( channels==1 )
	alBufferData( buffer, AL_FORMAT_MONO16, &buff[0], buff.size(), rate );
else // what about channels==3??? 

	alBufferData( buffer, AL_FORMAT_STEREO16, &buff[0], buff.size(), rate );
	
if( alGetError() != AL_NO_ERROR ) {
	printf("Could not BufferData\n");
	return;
}

  
If anyone familiar with ogg or openal can see there anything wrong please help. Atleast this is here for someone who is interested playing ogg-files with OpenAL to get the idea Thanks. ----------- RacingTreme - for fun multiplayer racing: http://users.utu.fi/stkibr

Share this post


Link to post
Share on other sites
I''m familiar with both the Vorbis File library and OpenAL (and using them together). I don''t immediately see anything wrong with that, but it''s 4 AM here so I could be missing something . You might want to look for problems elsewhere in your code.

As a side note: you might want to look into OpenAL''s queueing mechanism to actually stream the file from disk instead of loading it all at once.

Share this post


Link to post
Share on other sites
quote:
Original post by Null and Void
You might want to look for problems elsewhere in your code.


I hoped that I don''t need but it seems I have to
quote:

As a side note: you might want to look into OpenAL''s queueing mechanism to actually stream the file from disk instead of loading it all at once.


Yeah, but this part is only for sound effects that are played frequently (engine, crash, boom boom, you know)
For the backgroud music I thought to program separately as you adviced


-----------
RacingTreme - for fun multiplayer racing:
http://users.utu.fi/stkibr

Share this post


Link to post
Share on other sites