DevMaster.net Forums
[[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]]

Go Back   DevMaster.net Forums > Programming & Development > Sound and Music Programming
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
Old 03-16-2006, 05:33 AM   #1
Cherish_He
New Member
 
Join Date: Mar 2006
Posts: 16
Default An ogg decode problem about the function ov_read()

I am using vorbis to decode an ogg file. My code is:

OggVorbis_File vorbisFile;
char* tempbuffer;

FILE *file = fopen("oggtest.ogg","rb");
ov_open(file,&vorbisFile,NULL,0);
int samples = ov_pcm_total(&vorbisFile,-1);
long totalsize = 2 * wfmtx.nChannels * samples;
tempbuffer = new char[totalsize];
vorbis_info *vorbisInfo = ov_info(&vorbisFile,-1);

long ret;
int currentSection;
char pcmout[4096];
long currentsize = 0;
int cnt = 0;
while(true){
ret = ov_read(&vorbisFile,pcmout,4096,0,2,1,&currentSect ion);
if(ret == 0) break;
memcpy(tempbuffer+currentsize,pcmout,ret);
currentsize += ret;
cnt++;
}

An exception happened when it run to this line:

ret = ov_read(&vorbisFile,pcmout,4096,0,2,1,&currentSect ion);

At that time, the value of the variable cnt is 1.
Why the function ov_read can be executed at the first time, but can not be executed at the second time? When I comment the following line:

memcpy(tempbuffer+currentsize,pcmout,ret);

No exception happened. Is there any thing wrong with the memcpy???
Thanks a lot.
Cherish_He is offline   Reply With Quote
Old 03-16-2006, 08:07 AM   #2
bignobody
Valued Member
 
bignobody's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 155
Default Re: An ogg decode problem about the function ov_read()

Quote:
Originally Posted by Cherish_He
Is there any thing wrong with the memcpy???


Looking at the code (and from what you describe), that seems the most likely culprit. Are you sure the buffer is big enough?

Code:
if (currentsize + ret > totalsize) { // buffer overflow } else { memcpy(tempbuffer+currentsize,pcmout,ret); currentsize += ret; cnt++; }

BTW, if you plan on using this to play music, you don't need to decode the whole thing all at once. You just need to keep a little ahead of the current playing position (I only use a half-second buffer).

Hope this helps!
___________________________________________
-bignobody
notsoftgames.com - Creator of Shlongg!
bignobody is offline   Reply With Quote
Old 03-17-2006, 01:19 AM   #3
Cherish_He
New Member
 
Join Date: Mar 2006
Posts: 16
Default Re: An ogg decode problem about the function ov_read()

Thanks a lot. I have found the problem is that the value of the variable totalsize had not been calculated correctlly.
Cherish_He is offline   Reply With Quote
Old 03-17-2006, 01:28 AM   #4
Cherish_He
New Member
 
Join Date: Mar 2006
Posts: 16
Default Re: An ogg decode problem about the function ov_read()

Quote:
Originally Posted by bignobody
BTW, if you plan on using this to play music, you don't need to decode the whole thing all at once. You just need to keep a little ahead of the current playing position (I only use a half-second buffer).

BTW, you said that I should not decode the whole song all at once. But how could I do this? I am using the direct sound interface to play the song. I have tried to use several direct sound buffer to store the pcm data. But there seems to be some noise between one buffer and the next buffer. How can I solve the problem? Thank you very much. ^_^
Cherish_He is offline   Reply With Quote
Old 03-17-2006, 07:34 AM   #5
bignobody
Valued Member
 
bignobody's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 155
Default Re: An ogg decode problem about the function ov_read()

You only need one buffer. I'll refer you to this FlipCode Code Of The Day article where I leared how to do this. The Update method shows how this is done.

http://www.flipcode.com/cgi-bin/fcar...cgi?show=64067

Let me know if you're still having trouble getting it to work after reading it.

Regards,
___________________________________________
-bignobody
notsoftgames.com - Creator of Shlongg!
bignobody is offline   Reply With Quote
Old 03-18-2006, 05:57 AM   #6
Cherish_He
New Member
 
Join Date: Mar 2006
Posts: 16
Default Re: An ogg decode problem about the function ov_read()

Quote:
Originally Posted by bignobody
You only need one buffer. I'll refer you to this FlipCode Code Of The Day article where I leared how to do this. The Update method shows how this is done.

http://www.flipcode.com/cgi-bin/fcar...cgi?show=64067

Let me know if you're still having trouble getting it to work after reading it.

Regards,

Thank you very much. The codes in the url you gave me are very useful to my programm. I learn much from these code.
Cherish_He is offline   Reply With Quote
Old 03-18-2006, 07:32 AM   #7
Cherish_He
New Member
 
Join Date: Mar 2006
Posts: 16
Default Re: An ogg decode problem about the function ov_read()

But I still have one question. It just told me that the function Update() should be called from time to time, but how often we should call this function? I call it every (BUFSIZE*500/wfm.nAvgBytesPerSec) ms, does it right?
Cherish_He is offline   Reply With Quote
Old 03-18-2006, 12:41 PM   #8
bignobody
Valued Member
 
bignobody's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 155
Default Re: An ogg decode problem about the function ov_read()

Hmmm. Actually I've never given it much thought. I currently just call update in my main game loop before I do all the game logic. This seems to work fine for me. What you suggest seems like a good idea, and you can always try reducing how often you Update until you hear problems

Let me know how it goes

Regards,
___________________________________________
-bignobody
notsoftgames.com - Creator of Shlongg!
bignobody is offline   Reply With Quote
Old 03-18-2006, 12:49 PM   #9
Reedbeta
DevMaster Staff
 
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
Default Re: An ogg decode problem about the function ov_read()

Presumably, if your buffer is 0.5 seconds long you would only need to call Update() every 0.5 seconds, although it's probably a good idea to call it more frequently, at least every 0.25 seconds, just to be sure that small timing errors don't lead to a skip in the sound.
___________________________________________
Currently working at Sucker Punch
reedbeta.com - OpenGL demos and other projects
Luabridge - a lightweight, dependency-free C++/Lua binding library.
CD Lite - an unobtrusive, minimal CD player application for Windows.
Reedbeta is offline   Reply With Quote
Old 03-18-2006, 09:26 PM   #10
bignobody
Valued Member
 
bignobody's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 155
Default Re: An ogg decode problem about the function ov_read()

Seems logical enough. It's a part of my project that hasn't been touched in ages, so I just had another look. Seems my thinking at the time was to call Update frequently (every frame) but to only decode a little bit at a time (512 bytes per call). I think the Ogg Vorbis docs say 4096 bytes at a time is more typical. So actually my half second buffer is probably much more than I actually need... But, I've filed this one under "good enough for now"

Regards,
___________________________________________
-bignobody
notsoftgames.com - Creator of Shlongg!
bignobody is offline   Reply With Quote
Old 03-19-2006, 01:00 AM   #11
Cherish_He
New Member
 
Join Date: Mar 2006
Posts: 16
Default Re: An ogg decode problem about the function ov_read()

Quote:
Originally Posted by bignobody
Hmmm. Actually I've never given it much thought. I currently just call update in my main game loop before I do all the game logic. This seems to work fine for me. What you suggest seems like a good idea, and you can always try reducing how often you Update until you hear problems

Let me know how it goes

Regards,

Now I set the BUFSIZE (half size of the sound buffer) to be 16KB, and I call the Update() every one quarter buffer time. It seems to be all right. But if I set the BUFSIZE to be 8KB, there are some noise in the song when being played.

BTW, do you know any libraries which could decode mp3 to be PCM format just like the vorbis decodes the ogg files into PCM format? Thanks a lots. ^_^

Regards

Last edited by Cherish_He : 03-19-2006 at 01:33 AM.
Cherish_He is offline   Reply With Quote
Old 03-19-2006, 09:41 AM   #12
bignobody
Valued Member
 
bignobody's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 155
Default Re: An ogg decode problem about the function ov_read()

Sorry, I've never bothered to look into it. I use OGG so I don't have to use mp3. Looks like you've got some good suggestions in your thread specific to this issue ( http://www.devmaster.net/forums/showthread.php?t=5492 ).

Regards,
___________________________________________
-bignobody
notsoftgames.com - Creator of Shlongg!
bignobody is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump


All times are GMT -7. The time now is 07:37 AM.


Powered by vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.