I post again here becouse i can't sync video and audio. I really don't understand very well those links.
Im using a DirectSound class that has a method called CanWrite() that returns the number of bytes free in the DSound buffer. What i did before was:
Code:
While (CanWrite() < samples << 1)
Sleep(1);
where "samples" were the samples generated in one frame comming from the APU, usually 735 APU samples and then shifted left by 1 since im using 16 bit samples. I did that every frame and i synced to sound.
I post in GameDev my problem and as i suspected they told me to create a separate thread for sound.
I created a thread that looks like:
Code:
for (;;) //loops forever for the thread
{
if (can_stream)
{
While (CanWrite() < samples << 1); //waits samples free for the frame
WriteToDirectSoundBuffer(samples << 1); //Actually write to DSound buffer
can_stream = 0; //reset can_stream flag
}
}
Where "can_stream" is set to "true" every ending frame.
Anyway, i have problems. Sometimes the sound sync, sometimes don't. It's ramdomly, i load a rom and if syncs sound is not disturbed, but if i load other rom it's not synced.
All this waiting for PC VSync before writing any samples to the buffer at the beginning of the frame.
Im waiting for PC VSync to then render graphics using a:
Code:
While( ! WaitForVSync()) //if its "true" PC is in vsync
Sleep(0); //let other trheads do their tasks
At the beginning of the frame.
Any help is preciated.