ov_open_callbacks issues (no sound) |
![]() ![]() |
ov_open_callbacks issues (no sound) |
Jul 14 2008, 06:14
Post
#1
|
|
|
Group: Members Posts: 1 Joined: 6-August 07 Member No.: 45941 |
For the past 4 weeks i have been working on a wrapper around openal and ogg vorbis(cAudio). everything went great on linux.....i decided it was time to actually test it on windows. so i find out i have to change ov_open to ov_open_callbacks. This is fine till it compiles loads the file yet there is no sound playing
CODE #include "../Headers/cAudio.h"
#include "../Headers/cUtils.h" #include "mikmod.h" namespace cAudio{ std::vector<cAudio*> cAudio::thevector; cAudio::cAudio(){ thevector.push_back(this); oggMemoryFile.dataPtr = NULL; streaming = false; modfile = false; playaudio = false; pauseaudio = false; } size_t VorbisRead(void *ptr, size_t byteSize,size_t sizeToRead, void *datasource){ size_t spaceToEOF; size_t actualSizeToRead; SOggFile* vorbisData; vorbisData = (SOggFile*)datasource; spaceToEOF = vorbisData->dataSize - vorbisData->dataRead; if((sizeToRead*byteSize)<spaceToEOF){ actualSizeToRead = (sizeToRead*byteSize); }else{ actualSizeToRead = spaceToEOF; } if (actualSizeToRead){ memcpy(ptr,(char*)vorbisData->dataPtr + vorbisData->dataRead, actualSizeToRead); vorbisData->dataRead += (actualSizeToRead); } return actualSizeToRead; } int VorbisSeek(void *datasource,ogg_int64_t offset,int whence){ size_t spaceToEOF; ogg_int64_t actualOffset; SOggFile* vorbisData; vorbisData = (SOggFile*)datasource; switch(whence){ case SEEK_SET: if(vorbisData->dataSize >= offset){ actualOffset = offset; }else{ actualOffset = vorbisData->dataSize; } vorbisData->dataRead = (int)actualOffset; break; case SEEK_CUR: spaceToEOF = vorbisData->dataSize - vorbisData->dataRead; if(offset < spaceToEOF){ actualOffset = (offset); }else{ actualOffset = spaceToEOF; } vorbisData->dataRead += actualOffset; break; case SEEK_END: vorbisData->dataRead = vorbisData->dataSize+1; break; default: std::cout<<"errror cant seek"; }; return 0; } int VorbisClose(void *datasource){ return 1; } long VorbisTell(void *datasource){ SOggFile* vorbisData; vorbisData = (SOggFile*)datasource; return vorbisData->dataRead; } void cAudio::open(const std::string& path){ FILE* tempOggFile; int sizeOfFile; char tempChar; int tempArray; if(path.length() == 0){ }else{ if(!(tempOggFile = fopen(path.c_str(),"rb"))){ std::cout<<"cant load file"; }else{ extension = getExt(path); if(extension == "ogg"){ sizeOfFile = 0; while(!feof(tempOggFile)){ tempChar = getc(tempOggFile); sizeOfFile++; } oggMemoryFile.dataPtr = new char[sizeOfFile]; rewind(tempOggFile); tempArray = 0; while(!feof(tempOggFile)){ oggMemoryFile.dataPtr[tempArray] = getc(tempOggFile); tempArray++; } fclose(tempOggFile); oggMemoryFile.dataRead = 0; oggMemoryFile.dataSize = sizeOfFile; vorbisCallbacks.read_func = VorbisRead; vorbisCallbacks.close_func = VorbisClose; vorbisCallbacks.seek_func = VorbisSeek; vorbisCallbacks.tell_func = VorbisTell; if(ov_open_callbacks(&oggMemoryFile,&oggStream,NULL,0,vorbisCallbacks) != 0) std::cout<<"cant load ogg to memory"; vorbisInfo = ov_info(&oggStream, -1); vorbisComment = ov_comment(&oggStream, -1); if(vorbisInfo->channels == 1) format = AL_FORMAT_MONO16; else format = AL_FORMAT_STEREO16; alGenBuffers(3, buffers); alGenSources(1, &source); }else{ if(extension == "mod" || extension == "xm" || extension == "s3d" || extension == "it"){ if((modStream = Player_LoadFP(modFile, 255, 0)) == NULL) { fclose(modFile); throw std::string("Could not open Mod stream.The ") + MikMod_strerror(MikMod_errno); } /* Activate the module */ Player_Start(modStream); alGenBuffers(2, buffers); check(); alGenSources(1, &source); check(); } } } } } void cAudio::openstream(const std::string& path){ int result; if(path.length() == 0){ }else{ if(!(oggFile = fopen(path.c_str(), "rb"))){ std::cout<<"Could not open Ogg file."; streaming = false; }else{ extension = getExt(path); if(extension == "ogg"){ //This is were all the troubles occur one line. if((result = ov_open_callbacks(oggFile, &oggStream,NULL,0,OV_CALLBACKS_DEFAULT)) < 0) // if((result = ov_open(oggFile, &oggStream,NULL,0)) < 0) { std::cout<<"shit"; fclose(oggFile); } streaming = true; vorbisInfo = ov_info(&oggStream, -1); vorbisComment = ov_comment(&oggStream, -1); if(vorbisInfo->channels == 1) format = AL_FORMAT_MONO16; else format = AL_FORMAT_STEREO16; alGenBuffers(3, buffers); alGenSources(1, &source); }else{ if(extension == "mod" || extension == "xm" || extension == "s3d" || extension == "it"){ if((modStream = Player_LoadFP(modFile, 255, 0)) == NULL) { fclose(modFile); throw std::string("Could not open Mod stream.The ") + MikMod_strerror(MikMod_errno); } /* Activate the module */ Player_Start(modStream); alGenBuffers(2, buffers); check(); alGenSources(1, &source); check(); } } } } } void cAudio::release(){ alSourceStop(source); empty(); alDeleteSources(1, &source); alDeleteBuffers(1, buffers); ov_clear(&oggStream); delete[] oggMemoryFile.dataPtr; oggMemoryFile.dataPtr = NULL; if(modfile == true){ Player_Free(modStream); } } bool cAudio::playback(){ if(playing()) return true; if(!stream(buffers[0])) return false; if(!stream(buffers[1])) return false;void cAudio::play2d(bool loop){ alSourcei (source, AL_SOURCE_RELATIVE, true); alSourcei (source,AL_LOOPING,loop); playaudio = true; } if(!stream(buffers[2])) return false; alSourceQueueBuffers(source, 3, buffers); if(playaudio == true){ alSourcePlay(source);} if(playaudio == false){ alSourceStop(source);} if(playaudio == true && pauseaudio == true){ alSourcePause(source);} return true; } bool cAudio::playing(){ ALenum state; alGetSourcei(source, AL_SOURCE_STATE, &state); return (state == AL_PLAYING); } bool cAudio::update(){ int processed; bool active = true; alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed); while(processed--) { ALuint buffer; alSourceUnqueueBuffers(source, 1, &buffer); active = stream(buffer); alSourceQueueBuffers(source, 1, &buffer); } return active; } bool cAudio::stream(ALuint buffer){ SBYTE pcm2[BUFFER_SIZE]; char pcm[BUFFER_SIZE]; int size = 0; int section; int result; if(modfile == false){ while(size < BUFFER_SIZE) { result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, §ion); if(result > 0) size += result; else if(result < 0) break; else break; } if(size == 0) return false; alBufferData(buffer, format, pcm, size, vorbisInfo->rate); return true; }else{ /* Write bytes en pcm */ VC_WriteBytes(pcm2, BUFFER_SIZE); /* If there aren't bytes that we could write in pcm, break */ if (!Player_Active()) return false; alBufferData(buffer, AL_FORMAT_STEREO16,(char*) pcm, BUFFER_SIZE, md_mixfreq); check(); return true; } } void cAudio::empty(){ int queued; alGetSourcei(source, AL_BUFFERS_QUEUED, &queued); while(queued--) { ALuint buffer; alSourceUnqueueBuffers(source, 1, &buffer); } } void cAudio::check(){ int error = alGetError(); if(error != AL_NO_ERROR){ //throw string("OpenAL error was Raised."); } } bool cAudio::isvalid(){ if(oggMemoryFile.dataPtr == NULL){ if(streaming == false){ return false; } }else{ return true; } } void cAudio::play2d(bool loop){ alSourcei (source, AL_SOURCE_RELATIVE, true); alSourcei (source,AL_LOOPING,loop); playaudio = true; } |
|
|
|
Jul 25 2008, 13:57
Post
#2
|
|
|
Group: Members Posts: 7 Joined: 21-June 08 Member No.: 54656 |
I'm going to assume that you're sure that it's not your OpenAL code that's causing problems. So when you look at the data coming out of the Ogg decoder you're getting silence.
I would first null out your seek and tell functions. This will eliminate them as a source of bugs. It will also make your ogg files open 10x faster, and I didn't see any seeking in your library anyway. vorbisCallbacks.read_func = VorbisRead; vorbisCallbacks.close_func = VorbisClose; vorbisCallbacks.seek_func = NULL; vorbisCallbacks.tell_func = NULL; Then I'd put some break points in your read method and make sure that the data is getting read. p |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 20th May 2013 - 23:31 |