Sign in to follow this  
spuckfunkel

DirectSound Sound-Synthesis, Streaming, best way?

Recommended Posts

hello, i have a question with streaming sounds by directsound. i'm coding an sound game and it is almost finish. you can have a look here: http://de.youtube.com/watch?v=4HE3gZtO734 http://de.youtube.com/watch?v=FsmhVlTpTsc&feature=related http://de.youtube.com/watch?v=YxPORaU8bMU&feature=related all the sounds are generated in realtime (FM-Synthesis) and streamed via directSound. my problem/question is that my streamingbuffer is round about 100ms big and because there are so many oscillators to calculate, every 100ms the framerate of my game is breaking. that happens because i'm checking in every frame if the buffer is ready to fill. when the buffer is ready all the soundcalculations for the next 100ms are done and so this single frame will take much more time than the others. so the calculation of the framerate is going to be strange (i use the framerate for framerateundependent programming of the movements of all the graphical objects). usally the framrate is 60 (the rate of my monitor) but every half second the framerate is rising, which is strange because this rising of the framerate depends on the soundcalculations (when i disable the soundcalculations the framerate is stable). i think the framerate should fall when a frame is taken more time because of the soundcalculations. i wonder where my mistake is. when i use an external framecounter (fraps) it shows allways stable 60 frames even when the sound is on. so there must be an error in my code. but i cant find it.

void TicTimer(void) {
	double static FPS, tmpFPS;
	double static AvarageFPSStartTime;
	LONGLONG static lastFrame;
	LONGLONG CurrentFrame;
	double static AvarageFPSCounter;

	if (freq > 0) {
		QueryPerformanceCounter((LARGE_INTEGER*)&CurrentFrame);
		elapsed = CurrentFrame - lastFrame;
		if(elapsed > 0) FPS = (double)((double)freq / (double)elapsed);
		else FPS = 1;
	} else {
		timeBeginPeriod(1);
		CurrentFrame = timeGetTime();
		timeEndPeriod(1);
		elapsed = CurrentFrame - lastFrame;
		if(elapsed > 0) FPS = 1000.0/elapsed;
		else FPS = 1;
	}
	
	if ((AvarageFPSStartTime + 500)>=(double)GetTickCount()) {
		AvarageFPSCounter++;
		tmpFPS+=FPS;
	} else {
		if(AvarageFPSCounter > 0.0) AverageFPS=tmpFPS/AvarageFPSCounter;
		else AverageFPS = 60.0;

		fFrameRate = AverageFPS;
		AvarageFPSCounter=0.0;
		tmpFPS=0.0;
		AvarageFPSStartTime=GetTickCount();
		fFrameFactor = 60.0f/fFrameRate;
	}

	lastFrame = CurrentFrame;
}
i hope this was comprehensible. and i would be very pleased if anybode could help me because the release of the game will be in two weeks. greets, andi general question: what is the best way for realtime sound synthesis. should i put the stremingbuffer-ready check and soundcalculations in a seperate thread?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this