Advertisement

Ogg - DirectSound

Started by July 19, 2004 03:47 AM
2 comments, last by Ratterbox 20 years, 4 months ago
Hi, I have recently been working on a DirectSound and DirectMusic engine for one of my gaming projects. After discovering that to write a decoder for MP3 costs a licensing fee I decided to use the OggVorbis format. I have downloaded the SDK and added the include and lib directories to MS Visual C++ .NET My problem is that I get an Access Violation exception error when I use the function ov_open(). I was wondering if anybody knows whats wrong and if they know how to fix it. Ill provide my Ogg opening code as well: s_oggvorbis.h

// Global header files
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>

// Local header files
#include "s_soundbuffer.h"

// Definitions
#define						S_OGGVORBIS_LITTLEENDIAN				0
#define						S_OGGVORBIS_BIGENDIAN					1

// Include library(s)
#pragma						comment(lib, "ogg.lib")
#pragma						comment(lib, "vorbis.lib")
#pragma						comment(lib, "vorbisfile.lib")


// Prototypes
class OggVorbis;

// Classes

class OggVorbis : public SoundBuffer {
public:
	BOOL					LoadOGG(LPCSTR pFilename);

	BOOL					Decode(VOID);
protected:
	vorbis_info*			m_pFileInfo;
	OggVorbis_File			m_oggFile;
};

and s_oggvorbis.cpp:

#include "s_oggvorbis.h"

using namespace std;
BOOL					OggVorbis::LoadOGG(LPCSTR pFilename) {
	BOOL				bEndian=FALSE;
	FILE*				fp=NULL;

	// Open the Ogg file for reading
	fp = fopen(/* pFilename */ "Test.ogg", "rb");
	if(!fp)
		return FALSE; // Unable to open file for reading in binary mode, failed

//	ZeroMemory(&m_oggFile, sizeof(OggVorbis_File));

	// Open the Ogg file for reading within the Ogg Vorbis SDK Library
	ov_open(fp, &m_oggFile, NULL, 0);

	// Get Ogg file information
	m_pFileInfo = ov_info(&m_oggFile, -1);

	// Set the PCM format of the Ogg File
	SetFormat(m_pFileInfo->rate, m_pFileInfo->channels, /* TODO: Find proper bit rate here */ 16);

	// Create an empty buffer
	Create(0);

	// Read and Decode the Ogg File
	Decode();

	// Close the Ogg file in the SDK
	ov_clear(&m_oggFile);

	// Close the Ogg file
	fp=NULL;

	return TRUE;
}

//
//
//
BOOL					OggVorbis::Decode(VOID) {
	LONG				lBytes=0;
	CHAR				szBuffer[32768];
	int					nBitstream;

	do {
		// Read a buffers worth of decoded sound data
		lBytes = ov_read(&m_oggFile, szBuffer, 32768, S_OGGVORBIS_LITTLEENDIAN, 2, 1, (int*)&nBitstream);
		// Append the sound data to the buffer
		Append((BYTE*)szBuffer, lBytes);
	} while(lBytes > 0);

	return TRUE;
}


Its ok now, I have found the problem.
Advertisement
Quote: Original post by Ratterbox
Its ok now, I have found the problem.


What was it? Just in case someone else has the same problem..
When searching the internet I found a bug to do with the OggVorbis fseek wrapper (for 64-bit compatibility). This bug causes an access violation when fseek is called.

I fixed it by making my own wrapper like so

My Fseek Wrapper:
static int            _MyFseekWrapper(LPVOID pPtr, ogg_int64_t nOffset, int nPos) {    return fseek((FILE*)pPtr, (LONG)nOffset, nPos);}


and instead of calling ov_open()

I make my own callbacks struct and call ov_open_callbacks() like so:

ov_callbacks		oggCallbacks = {	(size_t (*)(void *, size_t, size_t, void *))   fread,	(int (*)(void *, ogg_int64_t, int))            _MyFseekWrapper,	(int (*)(void *))                              fclose,	(long (*)(void *))                             ftell};// Then calling ov_open_callbacksov_open_callbacks((LPVOID)fp, &m_oggFile, NULL, 0, oggCallbacks);


And that fixed my problem.
Now I am able to play Ogg files in my Sound engine.

:D

This topic is closed to new replies.

Advertisement