Jump to content
  • Advertisement
Sign in to follow this  
PumpkinPieman

Vorbis Ogg & Callbacks.

This topic is 4881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Right now I've implemented Vorbis Ogg with file streams, but what I want to do now is cache certain files if they are small enough and have the sound piece decode from a chunk of memory. The only way I can possibly do this is to create callbacks which read in the data to the Vorbis decoder, these callbacks work just like a standard file stream except this way I'm just reading the data straight from memory. Now one of the big problems that I'm having is that the call backs are standard C declarations, so I can't necessarily pass any important variables when using classes; so to get around this the first 8 bytes of the character array are two pointers to important variables. Now, for some reason every time I run this it always seems to fail. I've been trying to find some code on how to use callbacks with Vorbis Ogg, but the only real examples they have don't use callbacks and their post archives don't seem to have much on the topic. Here's my code. Creation
	bool cSoundLoaderOGG::Create(cSound* pSound, LPCSTR pFilename)
	{
		LPVOID	tempbufpointers[2];

		if((sound = pSound) == NULL || (pFilename == NULL))
			return false;

		Free();

		if((m_filepointer = fopen(pFilename, "rb")) == NULL)
			return false;

		// Get the file size
		fseek(m_filepointer, 0, SEEK_END);
		m_filebufsize = ftell(m_filepointer);

		if(g_MaxBufferBytes >= m_filebufsize){
			// Create the buffer
			m_filebuffer = new char[m_filebufsize + (sizeof(LPVOID) * 2)];

			// Fill the buffer
			tempbufpointers[0] = (LPVOID)&buf_seek_offset;
			tempbufpointers[1] = (LPVOID)&m_filebufsize;
			memcpy(m_filebuffer, tempbufpointers, (sizeof(LPVOID) * 2));
			fread(m_filebuffer + (sizeof(LPVOID)*2), m_filebufsize, 1, m_filepointer);

			// Close the file
			fclose(m_filepointer);
			m_filepointer = NULL;

			// Set the callbacks
			ov_callbacks tempcallback;
			tempcallback.read_func = buf_read_func;
			tempcallback.seek_func = buf_seek_func;
			tempcallback.close_func = buf_close_func;
			tempcallback.tell_func = buf_tell_func;


// Fails Here
			if(ov_open_callbacks(m_filebuffer, &m_vorbisfile, NULL, -1, tempcallback) != 0){
				Free();
				return false;
			}
		}
		else{
			fseek(m_filepointer, 0, SEEK_SET);
			if(ov_open(m_filepointer, &m_vorbisfile, NULL, 0) != 0){
				Free();
				return false;
			}
		}



		vorbis_info *vi = ov_info(&m_vorbisfile,-1);

		ZeroMemory(&m_wavformat, sizeof(WAVEFORMATEX)); 
		m_wavformat.wFormatTag      = WAVE_FORMAT_PCM; 
		m_wavformat.nChannels       = (WORD)vi->channels;
		m_wavformat.nSamplesPerSec  = vi->rate;
		m_wavformat.wBitsPerSample  = (WORD)16;
		m_wavformat.nBlockAlign     = (m_wavformat.wBitsPerSample / 8) * m_wavformat.nChannels;
		m_wavformat.nAvgBytesPerSec = m_wavformat.nSamplesPerSec * m_wavformat.nBlockAlign;

		m_filestartpos = 0;
		m_filesize = (DWORD)(m_wavformat.nBlockAlign * ov_pcm_total(&m_vorbisfile, -1));

		m_wavformatlong = wavformatlong(m_wavformat.nSamplesPerSec, m_wavformat.wBitsPerSample, m_wavformat.nChannels);
		m_isloaded = true;

		m_bufferlist = new cSoundBufferManager(false);
		return true;
	};




Callbacks
	size_t buf_read_func  (void *ptr, size_t size, size_t nmemb, void *datasource)
	{
		LPVOID data[2];
		memcpy(data, datasource, sizeof(LPVOID)*2);

		DWORD* buf_seek_offset = (DWORD*)data[0];


		memcpy(ptr, ((char*)datasource + (sizeof(LPVOID)*2) + (*buf_seek_offset)), size * nmemb);
		return size * nmemb;
	};

	int buf_seek_func  (void *datasource, ogg_int64_t offset, int whence)
	{
		LPVOID data[2];
		memcpy(data, datasource, sizeof(LPVOID)*2);

		DWORD* buf_seek_offset = (DWORD*)data[0];
		DWORD* buf_filebufsize = (DWORD*)data[1];

		switch(whence){
			case SEEK_SET:
                (*buf_seek_offset) = (DWORD)offset;
				break;
			case SEEK_END:
				(*buf_seek_offset) = ((*buf_filebufsize)) - (DWORD)offset;
				break;
			case SEEK_CUR:
				(*buf_seek_offset) += (DWORD)offset;
				break;
		}
		return 0;
	};

	int	buf_close_func (void *datasource)
	{
		return 0;
	};

	long buf_tell_func  (void *datasource)
	{
		LPVOID data[2];
		memcpy(data, datasource, sizeof(LPVOID)*2);

		DWORD* buf_seek_offset = (DWORD*)data[0];

		return (*buf_seek_offset);
	};





Hopefully some of you have dealt with callbacks and could provide maybe an example or two that could help me out. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!