An ogg decode problem about the function ov_read()
Started by Cherish_He, Mar 16 2006 12:33 PM
11 replies to this topic
#1
Posted 16 March 2006 - 12:33 PM
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,¤tSection);
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,¤tSection);
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.
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,¤tSection);
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,¤tSection);
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.
#2
Posted 16 March 2006 - 03:07 PM
Cherish_He said:
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?
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!
#3
Posted 17 March 2006 - 08:19 AM
Thanks a lot. I have found the problem is that the value of the variable totalsize had not been calculated correctlly.
#4
Posted 17 March 2006 - 08:28 AM
bignobody said:
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. ^_^
#5
Posted 17 March 2006 - 02:34 PM
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.....cgi?show=64067
Let me know if you're still having trouble getting it to work after reading it.
Regards,
http://www.flipcode.....cgi?show=64067
Let me know if you're still having trouble getting it to work after reading it.
Regards,
#6
Posted 18 March 2006 - 12:57 PM
bignobody said:
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.....cgi?show=64067
Let me know if you're still having trouble getting it to work after reading it.
Regards,
http://www.flipcode.....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.
#7
Posted 18 March 2006 - 02:32 PM
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?
#8
Posted 18 March 2006 - 07:41 PM
Hmmm. Actually I've never given it much thought. :blush: 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 :unsure:
Let me know how it goes ;)
Regards,
Let me know how it goes ;)
Regards,
#9
Posted 18 March 2006 - 07:49 PM
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.
reedbeta.com - developer blog, OpenGL demos, and other projects
#10
Posted 19 March 2006 - 04:26 AM
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,
Regards,
#11
Posted 19 March 2006 - 08:00 AM
bignobody said:
Hmmm. Actually I've never given it much thought. :blush: 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 :unsure:
Let me know how it goes ;)
Regards,
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
#12
Posted 19 March 2006 - 04:41 PM
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...read.php?t=5492 ).
Regards,
Regards,
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












