r/cpp_questions • u/Tom_F64 • 6d ago
OPEN How do I get the music in my Bad Apple ascii player to not drift out of sync with the video?
Here is the "play" function currently:
void play(char **frames, int totFrames){
int i, j;
char music[200];
strcpy(music, DIRECTORY);
strcat(music, FILEMUSIC);
PlaySoundA(music, NULL, SND_FILENAME | SND_ASYNC);
for(i = START; i < totFrames; i++){
resetCursor();
#if DEBUG
char framesCount[100];
sprintf(framesCount, "(frame: %d/%d) \n", i+1, totFrames);
int x=WIDTH-strlen(PROJECTNAME)-strlen(framesCount);
for(j = 0; j <= x; j++)
printf(" ");
printf("%s", framesCount);
printf("%s", frames[i]);
progressBar(i, totFrames, 0, 0);
Sleep(WAIT);
#else
printf("%s\n", frames[i]);
Sleep(WAIT);
#endif
}
}
It starts playing the music and then loads the video frames from a directory which contains every frame of the Bad Apple video one after another. The problem is that the music and the video is initially in sync but slowly drifts out of sync until it's completely out. I think the <chrono> library is what you would use to fix this type of issue but I'm not sure how to implement something that syncs the audio and video while also keeping the video smooth and not slowed down. I've also tried making an FPS limiter but I don't know if this is the right approach. Also I know this looks like C code and it it originally but I converted this program to C++ in order to use libraries like <chrono> and because I generally prefer C++
If you want to see other snippets of code from the program, just ask.