![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
New Member
Join Date: Mar 2006
Posts: 16
|
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,¤tSect 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,¤tSect 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. |
|
|
|
|
|
#2 | |
|
Valued Member
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 155
|
Quote:
Looking at the code (and from what you describe), that seems the most likely culprit. Are you sure the buffer is big enough? Code:
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 |
|
New Member
Join Date: Mar 2006
Posts: 16
|
Thanks a lot. I have found the problem is that the value of the variable totalsize had not been calculated correctlly.
|
|
|
|
|
|
#4 | |
|
New Member
Join Date: Mar 2006
Posts: 16
|
Quote:
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 |
|
Valued Member
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 155
|
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, |
|
|
|
|
|
#6 | |
|
New Member
Join Date: Mar 2006
Posts: 16
|
Quote:
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 |
|
New Member
Join Date: Mar 2006
Posts: 16
|
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 |
|
Valued Member
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 155
|
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, |
|
|
|
|
|
#9 |
|
DevMaster Staff
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
|
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. |
|
|
|
|
|
#10 |
|
Valued Member
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 155
|
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, |
|
|
|
|
|
#11 | |
|
New Member
Join Date: Mar 2006
Posts: 16
|
Quote:
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. |
|
|
|
|
|
|
#12 |
|
Valued Member
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 155
|
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, |
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|